driftvel_module.cc
Go to the documentation of this file.
1 // Class: driftvel
2 ////////////////////////////////////////////////////////////////////////
3 // Plugin Type: analyzer (art v2_09_06)
4 // File: driftvel_module.cc
5 //
6 // Generated at Wed Mar 7 09:17:09 2018 by Ajib Paudel using cetskelgen
7 // from cetlib version v3_01_03.
8 ////////////////////////////////////////////////////////////////////////
9 
14 #include "art_root_io/TFileService.h"
15 #include "art_root_io/TFileDirectory.h"
17 #include "canvas/Persistency/Common/FindManyP.h"
20 #include "fhiclcpp/ParameterSet.h"
22 #include "dune/Protodune/singlephase/DataUtils/ProtoDUNEDataUtils.h"
24 
34 #include "lardataobj/RawData/raw.h"
58 
59 #include "TFile.h"
60 #include "TTree.h"
61 #include "TDirectory.h"
62 #include "TH1.h"
63 #include "TH2.h"
64 #include "TF1.h"
65 #include "TProfile.h"
66 #include "TROOT.h"
67 #include "TStyle.h"
68 #include "TMath.h"
69 #include "TGraphErrors.h"
70 #include "TMinuit.h"
71 #include "TString.h"
72 #include "TTimeStamp.h"
73 #include "TVectorD.h"
74 #include "TCanvas.h"
75 #include "TFrame.h"
76 #include "TLine.h"
77 #include "TAxis.h"
78 #include "TTimeStamp.h"
79 
80 #include <vector>
81 #include <fstream>
82 #include "TPaveStats.h"
83 #include <iostream>
84 #include <string>
85 #include "math.h"
86 #include "stdio.h"
87 #include <iterator>
88 
89 
90 const int kMaxTracks = 300;
91 using namespace std;
92 
93 namespace protoana{
94  class driftvel : public art::EDAnalyzer {
95  public:
96 
97  explicit driftvel(fhicl::ParameterSet const& pset);
98  virtual ~driftvel();
99 
100  void beginJob();
101  void endJob();
102  void beginRun(const art::Run& run);
103  void analyze(const art::Event& evt);
104  void reset();
105 
106  private:
108  TTree* fEventTree;
109  Int_t run;
110  Int_t subrun;
111  Int_t event;
112  Double_t evttime;
113  Int_t all_trks;
114  int fNactivefembs[6];
115  Float_t trackthetaxz[kMaxTracks];
116  Float_t trackthetayz[kMaxTracks];
117  Float_t trkstartx[kMaxTracks];
118  Float_t trkstarty[kMaxTracks];
119  Float_t trkstartz[kMaxTracks];
120  Float_t trkendx[kMaxTracks];
121  Float_t trkendy[kMaxTracks];
122  Float_t trkendz[kMaxTracks];
123  Float_t trklen[kMaxTracks];
124  Int_t TrkID[kMaxTracks];
125  Float_t xprojectedlen[kMaxTracks];
126  Int_t ntrkhits[kMaxTracks][3];
127  Float_t hit_peakT[kMaxTracks][3][3000];
128  Int_t hit_tpc[kMaxTracks][3][3000];
129  Int_t hit_wire[kMaxTracks][3][3000];
130  Int_t hit_channel[kMaxTracks][3][3000];
131  Float_t trkhitx[kMaxTracks][3][3000];
132  Float_t trkhity[kMaxTracks][3][3000];
133  Float_t trkhitz[kMaxTracks][3][3000];
134  Float_t trkdq_amp[kMaxTracks][3][3000];
135  Float_t trkdq_int[kMaxTracks][3][3000];
140  // bool fSaveHitInfo;
142  };
143 
144  //========================================================================
145  driftvel::driftvel(fhicl::ParameterSet const& pset) :
146  EDAnalyzer(pset),
147  fDataUtils (pset.get<fhicl::ParameterSet>("DataUtils")),
148  fHitsModuleLabel (pset.get< std::string >("HitsModuleLabel","") ),
149  fTrackModuleLabel (pset.get< std::string >("TrackModuleLabel","") ),
150  fCalorimetryModuleLabel (pset.get< std::string >("CalorimetryModuleLabel","") ),
151  fSaveTrackInfo (pset.get< bool>("SaveTrackInfo",false)),
152  fSaveCaloInfo (pset.get< bool>("SaveCaloInfo",false))
153 
154  // fSaveHitInfo (pset.get< bool>("SaveHitInfo",false))
155 
156  {
157  if (fSaveTrackInfo == false) fSaveCaloInfo = false;
158  }
159 
160  //========================================================================
162  }
163  //========================================================================
164 
165  //========================================================================
167  std::cout<<"job begin..."<<std::endl;
169  fEventTree = tfs->make<TTree>("Event", "Event Tree from Reco");
170  fEventTree->Branch("event", &event,"event/I");
171  fEventTree->Branch("evttime",&evttime,"evttime/D");
172  fEventTree->Branch("run", &run,"run/I");
173  fEventTree->Branch("Nactivefembs",&fNactivefembs,"Nactivefembs[5]/I");
174  fEventTree->Branch("subrun", &subrun,"surbrun/I");
175  fEventTree->Branch("all_trks",&all_trks,"all_trks/I");
176  fEventTree->Branch("ntrkhits",ntrkhits,"ntrkhits[all_trks][3]/I");
177  fEventTree->Branch("xprojectedlen",xprojectedlen,"xprojectedlen[all_trks]/F");
178  fEventTree->Branch("trackthetaxz",trackthetaxz,"trackthetaxz[all_trks]/F");
179  fEventTree->Branch("trackthetayz",trackthetayz,"trackthetayz[all_trks]/F");
180  fEventTree->Branch("trkstartx",trkstartx,"trkstartx[all_trks]/F");
181  fEventTree->Branch("trkstarty",trkstarty,"trkstarty[all_trks]/F");
182  fEventTree->Branch("trkstartz",trkstartz,"trkstartz[all_trks]/F");
183  fEventTree->Branch("trkendx",trkendx,"trkendx[all_trks]/F");
184  fEventTree->Branch("trkendy",trkendy,"trkendy[all_trks]/F");
185  fEventTree->Branch("trkendz",trkendz,"trkendz[all_trks]/F");
186  fEventTree->Branch("trklen",trklen,"trklen[all_trks]/F");
187  fEventTree->Branch("TrkID",TrkID,"TrkID[all_trks]/I");
188  fEventTree->Branch("hit_peakT",hit_peakT,"hit_peakT[all_trks][3][3000]/F");
189  fEventTree->Branch("hit_tpc",hit_tpc,"hit_tpc[all_trks][3][3000]/I");
190  fEventTree->Branch("hit_wire",hit_wire,"hit_wire[all_trks][3][3000]/I");
191  fEventTree->Branch("hit_channel",hit_channel,"hit_channel[all_trks][3][3000]/I");
192  fEventTree->Branch("trkhitx",trkhitx,"trkhitx[all_trks][3][3000]/F");
193  fEventTree->Branch("trkhity",trkhity,"trkhity[all_trks][3][3000]/F");
194  fEventTree->Branch("trkhitz",trkhitz,"trkhitz[all_trks][3][3000]/F");
195  fEventTree->Branch("trkdq_int",trkdq_int,"trkdq_int[all_trks][3][3000]/F");
196  fEventTree->Branch("trkdq_amp",trkdq_amp,"trkdq_amp[all_trks][3][3000]/F");
197  }
198 
199  //========================================================================
201 
202  }
203 
204  //========================================================================
206  mf::LogInfo("driftvel")<<"begin run..."<<std::endl;
207  }
208  //========================================================================
209 
210  //========================================================================
211 
212  //========================================================================
213 
214  void driftvel::analyze( const art::Event& evt){//analyze
215  reset();
216 
217  std::vector<art::Ptr<recob::Track> > tracklist;
218  std::vector<art::Ptr<recob::PFParticle> > pfplist;
219 
220  auto trackListHandle = evt.getHandle< std::vector<recob::Track> >(fTrackModuleLabel);
221  if (trackListHandle) art::fill_ptr_vector(tracklist, trackListHandle);
222 
223  auto PFPListHandle = evt.getHandle< std::vector<recob::PFParticle> >("pandora");
224  if (PFPListHandle) art::fill_ptr_vector(pfplist, PFPListHandle);
225 
226  std::vector<art::Ptr<recob::Hit>> hitlist;
227  auto hitListHandle = evt.getHandle< std::vector<recob::Hit> >(fHitsModuleLabel); // to get information about the hits
228  if (hitListHandle) art::fill_ptr_vector(hitlist, hitListHandle);
229 
230  // art::FindManyP<recob::Hit> fmtht(trackListHandle, evt, fTrackModuleLabel); // to associate tracks and hits
231  art::FindManyP<recob::Hit, recob::TrackHitMeta> fmthm(trackListHandle, evt, fTrackModuleLabel); // to associate tracks and hits
232 
233  art::FindManyP<anab::T0> trk_t0_assn_v(PFPListHandle, evt ,"pandora");
234  art::FindManyP<recob::PFParticle> pfp_trk_assn(trackListHandle,evt,"pandoraTrack");
235  art::FindManyP<anab::T0> fmT0(trackListHandle, evt ,"pmtrack");
236 
237 
238  run = evt.run();
239  subrun = evt.subRun();
240  event = evt.id().event();
241  art::Timestamp ts = evt.time();
242  TTimeStamp tts(ts.timeHigh(), ts.timeLow());
243  evttime=tts.AsDouble();
244 
245 
246 
247  // Get number of active fembs
248  if(!evt.isRealData()){
249  for(int k=0; k < 6; k++)
250  fNactivefembs[k] = 20;
251  }
252  else{
253  for(int k=0; k < 6; k++)
255  }
256 
257 
258 
259  all_trks=0;
260  size_t NTracks = tracklist.size();
261  for(size_t i=0; i<NTracks;++i){
262  art::Ptr<recob::Track> ptrack(trackListHandle, i);
263  /* if(fTrackModuleLabel=="pandoraTrack"){
264  std::vector<art::Ptr<recob::PFParticle>> pfps=pfp_trk_assn.at(i);
265  if(!pfps.size()) continue;
266  std::vector<art::Ptr<anab::T0>> t0s=trk_t0_assn_v.at(pfps[0].key());
267  if(!t0s.size()) continue;
268  //auto t0 = t0s.at(0);
269  // double t_zero=t0->Time();
270  }
271 
272  if(fTrackModuleLabel=="pmtrack"){
273  std::vector<art::Ptr<anab::T0>> T0s=fmT0.at(i);
274  if(T0s.size()==0)
275  continue;
276  }
277  */
278  all_trks++;
279  const recob::Track& track = *ptrack;
280  if(track.Length()<300) continue;
281  // TVector3 pos, dir_start, dir_end, end;
282  auto pos = track.Vertex();
283  auto dir_start = track.VertexDirection();
284  //auto dir_end = track.EndDirection();
285  auto end = track.End();
286  if(TMath::Abs(end.X()-pos.X())<330 ||TMath::Abs(end.X()-pos.X())>1500 ) continue;
287  double theta_xz = std::atan2(dir_start.X(), dir_start.Z());
288  double theta_yz = std::atan2(dir_start.Y(), dir_start.Z());
289  xprojectedlen[all_trks-1]=TMath::Abs(end.X()-pos.X());
290  trackthetaxz[all_trks-1]=theta_xz;
291  trackthetayz[all_trks-1]=theta_yz;
292  trkstartx[all_trks-1]=pos.X();
293  trkstarty[all_trks-1]=pos.Y();
294  trkstartz[all_trks-1]=pos.Z();
295  trkendx[all_trks-1]=end.X();
296  trkendy[all_trks-1]=end.Y();
297  trkendz[all_trks-1]=end.Z();
298  trklen[all_trks-1]=track.Length();
299  TrkID[all_trks-1]=track.ID();
300  int planenum=999;
301  int nhits_0=0;
302  int nhits_1=0;
303  int nhits_2=0;
304  float xpos=-9999;
305  float ypos=-9999;
306  float zpos=-9999;
307  // auto vhit=fmtht.at(i);
308  cout<<"fmthm is valid "<<fmthm.isValid()<<endl;
309  if(fmthm.isValid()){
310  auto vhit=fmthm.at(i);
311  auto vmeta=fmthm.data(i);
312  cout<<"vhit vmetadata"<<endl;
313  for (size_t ii = 0; ii<vhit.size(); ++ii){ //loop over all meta data hit
314  bool fBadhit = false;
315  if (vmeta[ii]->Index() == static_cast<unsigned int>(std::numeric_limits<int>::max())){
316  fBadhit = true;
317  cout<<"fBadHit"<<fBadhit<<endl;
318  continue;
319  }
320  cout<<"first ii"<<ii<<endl;
321  if (vmeta[ii]->Index()>=tracklist[i]->NumberTrajectoryPoints()){
322  throw cet::exception("Calorimetry_module.cc") << "Requested track trajectory index "<<vmeta[ii]->Index()<<" exceeds the total number of trajectory points "<<tracklist[i]->NumberTrajectoryPoints()<<" for track index "<<i<<". Something is wrong with the track reconstruction. Please contact tjyang@fnal.gov!!";
323  }
324  cout<<"second ii"<<ii<<endl;
325  if (!tracklist[i]->HasValidPoint(vmeta[ii]->Index())){
326  fBadhit = true;
327  cout<<"had valid point "<<fBadhit<<endl;
328  continue;
329  }
330  cout<<"3rd ii"<<ii<<endl;
331  // TVector3 loc = tracklist[i]->LocationAtPoint(vmeta[ii]->Index());
332  auto loc = tracklist[i]->LocationAtPoint(vmeta[ii]->Index());
333  xpos=loc.X();
334 
335  ypos=loc.Y();
336  zpos=loc.Z();
337  cout<<"x, y, z "<<xpos<<" "<<ypos<<" "<<zpos<<endl;
338  cout<<"BadHit"<<fBadhit<<endl;
339  if (fBadhit) continue; //HY::If BAD hit, skip this hit and go next
340  if (zpos<-100) continue; //hit not on track
341  planenum=vhit[ii]->WireID().Plane;
342  cout<<"plane num "<<planenum;
343  if(planenum==0){
344  nhits_0++;
345  cout<<"pl 0"<<endl;
346  hit_peakT[all_trks-1][planenum][nhits_0-1]=vhit[ii]->PeakTime();
347  int k=vhit[ii]->PeakTime();
348  cout<<"int k"<<k<<endl;
349  cout<<"peakT"<<vhit[ii]->PeakTime();
350  hit_peakT[all_trks-1][planenum][nhits_0-1]=vhit[ii]->PeakTime();
351  hit_tpc[all_trks-1][planenum][nhits_0-1]=vhit[ii] ->WireID().TPC;
352  hit_wire[all_trks-1][planenum][nhits_0-1]=vhit[ii] ->WireID().Wire;
353  trkdq_int[all_trks-1][planenum][nhits_0-1]=vhit[ii] ->Integral();
354  trkdq_amp[all_trks-1][planenum][nhits_0-1]=vhit[ii] ->PeakAmplitude();
355  hit_channel[all_trks-1][planenum][nhits_0-1]=vhit[ii]->Channel();
356  cout<<vhit[ii]->WireID().Wire;
357  cout<<"peakT"<<vhit[ii]->PeakTime();
358  trkhitx[all_trks-1][planenum][nhits_0-1]=xpos;
359  trkhity[all_trks-1][planenum][nhits_0-1]=ypos;
360  trkhitz[all_trks-1][planenum][nhits_0-1]=zpos;
361  }//planenum 0
362  if(planenum==1){
363  nhits_1++;
364  cout<<"pl 1"<<endl;
365  hit_peakT[all_trks-1][planenum][nhits_1-1]=vhit[ii]->PeakTime();
366  hit_tpc[all_trks-1][planenum][nhits_1-1]=vhit[ii] ->WireID().TPC;
367  hit_wire[all_trks-1][planenum][nhits_1-1]=vhit[ii] ->WireID().Wire;
368  trkdq_int[all_trks-1][planenum][nhits_1-1]=vhit[ii] ->Integral();
369  trkdq_amp[all_trks-1][planenum][nhits_1-1]=vhit[ii] ->PeakAmplitude();
370  hit_channel[all_trks-1][planenum][nhits_1-1]=vhit[ii]->Channel();
371  trkhitx[all_trks-1][planenum][nhits_1-1]=xpos;
372  trkhity[all_trks-1][planenum][nhits_1-1]=ypos;
373  trkhitz[all_trks-1][planenum][nhits_1-1]=zpos;
374  }//planenum 1
375  if(planenum==2){
376  nhits_2++;
377  cout<<"pl 2"<<endl;
378  hit_peakT[all_trks-1][planenum][nhits_2-1]=vhit[ii]->PeakTime();
379  hit_tpc[all_trks-1][planenum][nhits_2-1]=vhit[ii] ->WireID().TPC;
380  hit_wire[all_trks-1][planenum][nhits_2-1]=vhit[ii] ->WireID().Wire;
381  trkdq_int[all_trks-1][planenum][nhits_2-1]=vhit[ii] ->Integral();
382  trkdq_amp[all_trks-1][planenum][nhits_2-1]=vhit[ii] ->PeakAmplitude();
383  hit_channel[all_trks-1][planenum][nhits_2-1]=vhit[ii]->Channel();
384  trkhitx[all_trks-1][planenum][nhits_2-1]=xpos;
385  trkhity[all_trks-1][planenum][nhits_2-1]=ypos;
386 
387  trkhitz[all_trks-1][planenum][nhits_2-1]=zpos;
388  }//planenum 2
389  }//loop over vhit
390  }//fmthm valid
391  ntrkhits[all_trks-1][0]=nhits_0;
392  ntrkhits[all_trks-1][1]=nhits_1;
393  ntrkhits[all_trks-1][2]=nhits_2;
394  } // loop over trks...
395 
396  fEventTree->Fill();
397  } // end of analyze function
398 
399  /////////////////// Defintion of reset function ///////////
401  run = -9999;
402  subrun = -9999;
403  event = -9999;
404  evttime = -9999;
405  all_trks = -9999;
406  for(int k=0; k < 6; k++)
407  fNactivefembs[k] = -999;
408  for(int i=0; i<kMaxTracks; i++){
409  trackthetaxz[i]=-9999;
410  trackthetayz[i]=-9999;
411  trkstartx[i]=-9999;
412  trkstarty[i]=-9999;
413  trkstartz[i]=-9999;
414  trkendx[i]=-9999;
415  trkendy[i]=-9999;
416  trkendz[i]=-9999;
417  trklen[i]=-9999;
418  TrkID[i]=-9999;
419  xprojectedlen[i]=-9999;
420  ntrkhits[i][0]=-9999;
421  ntrkhits[i][1]=-9999;
422  ntrkhits[i][2]=-9999;
423  for(int j=0; j<3000; j++){
424  hit_peakT[i][0][j]=-9999;
425  hit_peakT[i][1][j]=-9999;
426  hit_peakT[i][2][j]=-9999;
427  hit_tpc[i][0][j]=-9999;
428  hit_tpc[i][1][j]=-9999;
429  hit_tpc[i][2][j]=-9999;
430  hit_wire[i][0][j]=-9999;
431  hit_wire[i][1][j]=-9999;
432  hit_wire[i][2][j]=-9999;
433  trkhitx[i][0][j]=-9999;
434  trkhitx[i][1][j]=-9999;
435  trkhitx[i][2][j]=-9999;
436  trkhity[i][0][j]=-9999;
437  trkhity[i][1][j]=-9999;
438  trkhity[i][2][j]=-9999;
439  trkhitz[i][0][j]=-9999;
440  trkhitz[i][1][j]=-9999;
441  trkhitz[i][2][j]=-9999;
442  hit_channel[i][0][j]=-9999;
443  hit_channel[i][1][j]=-9999;
444  hit_channel[i][2][j]=-9999;
445  trkdq_int[i][0][j]=-9999;
446  trkdq_int[i][1][j]=-9999;
447  trkdq_int[i][2][j]=-9999;
448  trkdq_amp[i][0][j]=-9999;
449  trkdq_amp[i][1][j]=-9999;
450  trkdq_amp[i][2][j]=-9999;
451  }
452  }
453  }
454  //////////////////////// End of definition ///////////////
455 
457 }
458 
459 
def analyze(root, level, gtrees, gbranches, doprint)
Definition: rootstat.py:69
end
while True: pbar.update(maxval-len(onlies[E][S])) #print iS, "/", len(onlies[E][S]) found = False for...
Int_t hit_channel[kMaxTracks][3][3000]
constexpr std::uint32_t timeLow() const
Definition: Timestamp.h:29
TH3F * xpos
Definition: doAna.cpp:29
Handle< PROD > getHandle(SelectorBase const &) const
Definition: DataViewImpl.h:382
std::string string
Definition: nybbler.cc:12
MaybeLogger_< ELseverityLevel::ELsev_info, false > LogInfo
constexpr std::uint32_t timeHigh() const
Definition: Timestamp.h:34
ProtoDUNEDataUtils fDataUtils
const int kMaxTracks
Class to keep data related to recob::Hit associated with recob::Track.
Vector_t VertexDirection() const
Definition: Track.h:132
void beginRun(const art::Run &run)
STL namespace.
unsigned int Index
void analyze(const art::Event &evt)
Particle class.
std::string fTrackModuleLabel
Float_t trackthetaxz[kMaxTracks]
Float_t trkstartz[kMaxTracks]
Float_t trkhity[kMaxTracks][3][3000]
Float_t trkdq_int[kMaxTracks][3][3000]
object containing MC flux information
art framework interface to geometry description
Definition: Run.h:17
TH3F * ypos
Definition: doAna.cpp:30
Float_t trkendz[kMaxTracks]
Float_t trklen[kMaxTracks]
Int_t ntrkhits[kMaxTracks][3]
bool isRealData() const
QTextStream & reset(QTextStream &s)
Float_t trackthetayz[kMaxTracks]
TH3F * zpos
Definition: doAna.cpp:31
Float_t trkendx[kMaxTracks]
double Length(size_t p=0) const
Access to various track properties.
Definition: Track.h:167
Timestamp time() const
#define DEFINE_ART_MODULE(klass)
Definition: ModuleMacros.h:67
void beginJob()
Definition: Breakpoints.cc:14
Float_t hit_peakT[kMaxTracks][3][3000]
Point_t const & Vertex() const
Definition: Track.h:124
Float_t trkhitz[kMaxTracks][3][3000]
SubRunNumber_t subRun() const
Definition: DataViewImpl.cc:78
Float_t trkhitx[kMaxTracks][3][3000]
RunNumber_t run() const
Definition: DataViewImpl.cc:71
static int max(int a, int b)
Float_t trkendy[kMaxTracks]
Definition of data types for geometry description.
std::string fHitsModuleLabel
Float_t trkstartx[kMaxTracks]
int ID() const
Definition: Track.h:198
Declaration of signal hit object.
Float_t xprojectedlen[kMaxTracks]
int GetNActiveFembsForAPA(art::Event const &evt, int apa) const
Get number of active fembs in an APA.
Float_t trkdq_amp[kMaxTracks][3][3000]
Provides recob::Track data product.
EventNumber_t event() const
Definition: EventID.h:116
Point_t const & End() const
Definition: Track.h:125
Int_t hit_tpc[kMaxTracks][3][3000]
Declaration of basic channel signal object.
std::string fCalorimetryModuleLabel
TCEvent evt
Definition: DataStructs.cxx:7
Int_t hit_wire[kMaxTracks][3][3000]
Float_t trkstarty[kMaxTracks]
auto const & get(AssnsNode< L, R, D > const &r)
Definition: AssnsNode.h:115
void fill_ptr_vector(std::vector< Ptr< T >> &ptrs, H const &h)
Definition: Ptr.h:297
int bool
Definition: qglobal.h:345
Int_t TrkID[kMaxTracks]
EventID id() const
Definition: Event.cc:34
Track from a non-cascading particle.A recob::Track consists of a recob::TrackTrajectory, plus additional members relevant for a "fitted" track:
Definition: Track.h:49
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
QTextStream & endl(QTextStream &s)
Event finding and building.