ProtoDUNEelectronWireAna_module.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 // Class: ProtoDUNEelectronWireAna
3 // Plugin Type: analyzer (art v3_03_01)
4 // File: ProtoDUNEelectronWireAna_module.cc
5 //
6 // Generated at Mon Dec 2 16:17:42 2019 by Aaron Higuera Pichardo using cetskelgen
7 // from cetlib version v3_08_00.
8 ////////////////////////////////////////////////////////////////////////
9 
17 #include "fhiclcpp/ParameterSet.h"
19 #include "art_root_io/TFileService.h"
20 
27 #include "canvas/Persistency/Common/FindManyP.h"
28 
34 
35 #include "TFile.h"
36 #include "TProfile.h"
37 #include "TH1.h"
38 #include "TTree.h"
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <fstream>
42 #include <string>
43 #include <sstream>
44 #include <cmath>
45 #include <algorithm>
46 #include <iostream>
47 #include <vector>
48 
49 const int MAXHits = 6000;
50 
52 
53 
55 public:
57  // The compiler-generated destructor is fine for non-base
58  // classes without bare pointers or other resource use.
59 
60  // Plugins should not be copied or assigned.
65 
66  // Required functions.
67  void analyze(art::Event const& evt) override;
68  void beginJob() override;
69 private:
70 
71  void Initialize();
72  // Declare member data here.
76 
78 
82 
83  TTree *fTree;
84  unsigned int frun;
85  unsigned int fsubrun;
86  unsigned int fevent;
87  std::vector<int> fprimary_Shower_wire_w;
88  std::vector<float> fprimary_Shower_wire_ch; //charge
89  std::vector<float>fprimary_Shower_wire_X;
90  std::vector<float>fprimary_Shower_wire_Z;
91  std::vector<float>fprimary_Shower_wire_Y;
92 
93  std::vector<float> fprimary_Shower_MCwire_E; //energy in MeV
94  std::vector<int> fprimary_Shower_MCwire_w;
96 };
97 
99 {
100 
102  fTree = tfs->make<TTree>("WireAna","Recob::Wire");
103  fTree->Branch("run",&frun,"run/I");
104  fTree->Branch("subrun",&fsubrun,"subrun/I");
105  fTree->Branch("event",&fevent,"event/I");
106 
107  fTree->Branch("primaryStartPosition", &fprimaryStartPosition, "primaryStartPosition[3]/f");
108  fTree->Branch("primary_Shower_wire_w",&fprimary_Shower_wire_w);
109  fTree->Branch("primary_Shower_wire_ch",&fprimary_Shower_wire_ch);
110  fTree->Branch("primary_Shower_wire_X",&fprimary_Shower_wire_X);
111  fTree->Branch("primary_Shower_wire_Z",&fprimary_Shower_wire_Z);
112  fTree->Branch("primary_Shower_wire_Y",&fprimary_Shower_wire_Y);
113  fTree->Branch("primary_Shower_MCwire_w",&fprimary_Shower_MCwire_w);
114  fTree->Branch("primary_Shower_MCwire_E",&fprimary_Shower_MCwire_E);
115 
116 }
117 
118 
120  : EDAnalyzer{p},
121  fShowerTag(p.get<std::string>("ShowerTag")),
122  fPFParticleTag(p.get<std::string>("PFParticleTag")),
123  fWireTag(p.get<std::string>("WireTag"))
124 {
125  // Call appropriate consumes<>() for any products to be retrieved by this module.
126 }
127 
129 {
130 
131  Initialize();
132  frun = evt.run();
133  fsubrun = evt.subRun();
134  fevent = evt.id().event();
135 
136  //check for reco pandora stuff
137  auto recoParticleHandle = evt.getHandle<std::vector<recob::PFParticle>>(fPFParticleTag);
138  if( !recoParticleHandle ) return;
139 
140  //std::vector<int> hit_w;
141  std::map<int,std::vector<double>> hit_w_and_t1;
142  std::map<int,std::vector<double>> hit_w_and_t2;
143  std::map<int,std::vector<double>> hit_w_and_y;
144  std::map<int,std::vector<double>> hit_w_and_x;
145  std::vector<const recob::PFParticle*> pfParticles = pfpUtil.GetPFParticlesFromBeamSlice(evt,fPFParticleTag);
146  const simb::MCParticle* mcparticle = NULL;
147  bool doAna = false;
148  auto const clockData = art::ServiceHandle<detinfo::DetectorClocksService>()->DataFor(evt);
149  auto const detProp = art::ServiceHandle<detinfo::DetectorPropertiesService>()->DataFor(evt, clockData);
150  for(const recob::PFParticle* particle : pfParticles){
151  const recob::Shower* thisShower = pfpUtil.GetPFParticleShower(*particle,evt,fPFParticleTag,fShowerTag);
152  if( thisShower == 0x0 ) continue;
153  if( !evt.isRealData() ){
154  mcparticle = truthUtil.GetMCParticleFromRecoShower(clockData, *thisShower, evt, "pandoraShower");
155  if( abs(mcparticle->PdgCode()) != 11 ) return;
156  }
157  fprimaryStartPosition[0] = thisShower->ShowerStart().X();
158  fprimaryStartPosition[1] = thisShower->ShowerStart().Y();
159  fprimaryStartPosition[2] = thisShower->ShowerStart().Z();
160  doAna = true;
161  const std::vector<const recob::Hit*> sh_hits = showerUtil.GetRecoShowerHits(*thisShower, evt, fShowerTag);
162  art::FindManyP<recob::Wire> wFromHits(sh_hits,evt,"hitpdune");
163  art::FindManyP<recob::SpacePoint> spFromShowerHits(sh_hits,evt,fPFParticleTag);
164  for( size_t j=0; j<sh_hits.size() && j<MAXHits; ++j){
165  if( sh_hits[j]->WireID().Plane != 2 ) continue;
166  std::vector<art::Ptr<recob::Wire>> wires = wFromHits.at(j);
167  //hit_w.push_back(wires[0]->Channel());
168  hit_w_and_t1[wires[0]->Channel()].push_back(sh_hits[j]->PeakTimeMinusRMS(5.0));
169  hit_w_and_t2[wires[0]->Channel()].push_back(sh_hits[j]->PeakTimePlusRMS(5.0));
170  //std::cout<<wires[0]->Channel()<<" "<<sh_hits[j]->PeakTime()<<" "<<sh_hits[j]->PeakTimePlusRMS(5.0)<<" "<<sh_hits[j]->PeakTimeMinusRMS(5.0)<<std::endl;
171  hit_w_and_x[wires[0]->Channel()].push_back(detProp.ConvertTicksToX(sh_hits[j]->PeakTime(),sh_hits[j]->WireID().Plane,sh_hits[j]->WireID().TPC,0));
172  std::vector<art::Ptr<recob::SpacePoint>> sp = spFromShowerHits.at(j);
173  if(!sp.empty()){
174  hit_w_and_y[wires[0]->Channel()].push_back(sp[0]->XYZ()[1]);
175  }
176  else hit_w_and_y[wires[0]->Channel()].push_back(fprimaryStartPosition[1]); //use vtx if no sp
177  }
178  break;
179  }
180 
181  //one wire can have various hits so lets remove duplicate wires
182  //std::sort(hit_w.begin(),hit_w.end());
183  //hit_w.erase(std::unique(hit_w.begin(),hit_w.end()), hit_w.end());
184 
185  if( doAna ){
186  auto const& wires = evt.getValidHandle<std::vector<recob::Wire> >(fWireTag);
187  //auto const& wires = evt.getValidHandle<std::vector<recob::Wire> >("wclsdatasp:gauss"); //new reco
188  auto w1 = hit_w_and_t1.begin();
189  auto w2 = hit_w_and_t2.begin();
190  auto x = hit_w_and_x.begin();
191  auto y = hit_w_and_y.begin();
192  while( w1 != hit_w_and_t1.end()){
193  int it_w = w1->first;
194  int n_hits = w1->second.size();
195  // std::cout<<it_w<<" "<<n_hits<<" "<<w1->second[n_hits-1]<<" "<<w2->second[n_hits-1]<<std::endl;
196  double t1 = w1->second[0]; //first hit
197  double t2 = w2->second[n_hits-1]; //last hit
198  double x_w, y_w;
199  if( x->second.size() < 1 ){
200  x_w = (x->second[0]-x->second[n_hits-1])/2.0;
201  y_w = (y->second[0]-y->second[n_hits-1])/2.0;
202  }
203  else {
204  x_w = x->second[0];
205  y_w = y->second[0];
206  }
207  for(auto & wire : * wires){
208  int channel_no = wire.Channel();
209  int plane = fGeometry->View(wire.Channel());
210  if( plane != 2 ) continue;
211  std::vector< geo::WireID > wireID= fGeometry->ChannelToWire(channel_no);
212  const geo::WireGeo* pwire = fGeometry->WirePtr(wireID.at(0)); //for collection plane there is one wire per channel
213  TVector3 xyzWire = pwire->GetCenter<TVector3>();
214  if( it_w == channel_no ){
215  double charge =0.0;
216  for(size_t i = 0; i < wire.Signal().size(); ++i){
217  if( i > t1 && i < t2 ) charge += wire.Signal()[i];
218  }
219  fprimary_Shower_wire_Z.push_back(xyzWire.Z());
220  fprimary_Shower_wire_Y.push_back(y_w);
221  fprimary_Shower_wire_X.push_back(x_w);
222  fprimary_Shower_wire_ch.push_back(charge);
223  fprimary_Shower_wire_w.push_back(channel_no);
224 
225  break;
226  }
227  }// all recob::wire
228  w1 ++; w2 ++;
229  x ++; y ++;
230  }//wire from shower
231 
232  //look at MC info if available
233  if(!evt.isRealData()){
234  auto mcTruths = evt.getValidHandle<std::vector<simb::MCTruth>>("generator");
235  const simb::MCParticle* geantGoodParticle = truthUtil.GetGeantGoodParticle((*mcTruths)[0],evt);
236  int trackid = mcparticle->TrackId();
237  if( mcparticle->TrackId() == geantGoodParticle->TrackId() ){
238  if( doAna ){ //we have a reco shower
239  auto simchannelHandle = evt.getHandle< std::vector<sim::SimChannel> >("largeant");
240  if (simchannelHandle){
241  for(auto const& simchannel : (*simchannelHandle)){
242  if(fGeometry->View(simchannel.Channel()) != 2) continue;
243  auto const& alltimeslices = simchannel.TDCIDEMap();
244  double EperCh=0;
245  for(auto const& tslice : alltimeslices){
246  auto const& simide = tslice.second;
247  // Loop over energy deposits
248  for(auto const& eDep : simide){
249  if(eDep.trackID == trackid || eDep.trackID == -trackid){
250  EperCh += eDep.energy;
251  }
252  }
253  }
254  if( EperCh== 0 ) continue;//save only channeles with ID
255  fprimary_Shower_MCwire_w.push_back(simchannel.Channel());
256  fprimary_Shower_MCwire_E.push_back(EperCh);
257  }
258  }
259  }//do analysis of MC
260  }//is MC same as beam?
261  }//is MC?
262  }//shower
263 
264 
265  fTree->Fill();
266 }
268  frun =-999;
269  fsubrun =-999;
270  fevent =-999;
271  fprimary_Shower_wire_w.clear();
272  fprimary_Shower_wire_Y.clear();
273  fprimary_Shower_wire_X.clear();
274  fprimary_Shower_wire_Z.clear();
275  fprimary_Shower_wire_ch.clear();
276  fprimary_Shower_MCwire_w.clear();
277  fprimary_Shower_MCwire_E.clear();
278  for(int k=0; k < 3; k++){
279  fprimaryStartPosition[k] = -999.0;
280  }
281 }
Geometry description of a TPC wireThe wire is a single straight segment on a wire plane...
Definition: WireGeo.h:65
const TVector3 & ShowerStart() const
Definition: Shower.h:192
const std::vector< const recob::Hit * > GetRecoShowerHits(const recob::Shower &shower, art::Event const &evt, const std::string showerModule) const
Get the hits from a given reco shower.
int PdgCode() const
Definition: MCParticle.h:212
const simb::MCParticle * GetGeantGoodParticle(const simb::MCTruth &genTruth, const art::Event &evt) const
const recob::Shower * GetPFParticleShower(const recob::PFParticle &particle, art::Event const &evt, const std::string particleLabel, const std::string showerLabel) const
Get the shower associated to this particle. Returns a null pointer if not found.
Handle< PROD > getHandle(SelectorBase const &) const
Definition: DataViewImpl.h:382
std::string string
Definition: nybbler.cc:12
std::vector< geo::WireID > ChannelToWire(raw::ChannelID_t const channel) const
Returns a list of wires connected to the specified TPC channel.
protoana::ProtoDUNETruthUtils truthUtil
EDAnalyzer(fhicl::ParameterSet const &pset)
Definition: EDAnalyzer.h:25
art framework interface to geometry description
int TrackId() const
Definition: MCParticle.h:210
bool isRealData() const
T abs(T value)
ProtoDUNEelectronWireAna & operator=(ProtoDUNEelectronWireAna const &)=delete
#define DEFINE_ART_MODULE(klass)
Definition: ModuleMacros.h:67
IDparameter< geo::WireID > WireID
Member type of validated geo::WireID parameter.
ValidHandle< PROD > getValidHandle(InputTag const &tag) const
Definition: DataViewImpl.h:441
p
Definition: test.py:223
SubRunNumber_t subRun() const
Definition: DataViewImpl.cc:78
const std::vector< const recob::PFParticle * > GetPFParticlesFromBeamSlice(art::Event const &evt, const std::string particleLabel) const
Return the pointers for the PFParticles in the beam slice. Returns an empty vector is no beam slice w...
RunNumber_t run() const
Definition: DataViewImpl.cc:71
Description of geometry of one entire detector.
View_t View(geo::PlaneID const &pid) const
Returns the view (wire orientation) on the channels of specified TPC plane.
Declaration of signal hit object.
Hierarchical representation of particle flow.
Definition: PFParticle.h:44
EventNumber_t event() const
Definition: EventID.h:116
protoana::ProtoDUNEPFParticleUtils pfpUtil
Declaration of basic channel signal object.
void analyze(art::Event const &evt) override
list x
Definition: train.py:276
void GetCenter(double *xyz, double localz=0.0) const
Fills the world coordinate of a point on the wire.
Definition: WireGeo.cxx:73
TCEvent evt
Definition: DataStructs.cxx:7
const simb::MCParticle * GetMCParticleFromRecoShower(detinfo::DetectorClocksData const &clockData, const recob::Shower &shower, art::Event const &evt, std::string showerModule) const
ProtoDUNEelectronWireAna(fhicl::ParameterSet const &p)
protoana::ProtoDUNEShowerUtils showerUtil
EventID id() const
Definition: Event.cc:34
WireGeo const * WirePtr(geo::WireID const &wireid) const
Returns the specified wire.