LArSoft  v06_85_00
Liquid Argon Software toolkit - http://larsoft.org/
OpFlashAna_module.cc
Go to the documentation of this file.
1 // -*- mode: c++; c-basic-offset: 2; -*-
2 // This analyzer writes out a TTree containing the properties of
3 // each reconstructed flash
4 //
5 
6 #ifndef OpFlashAna_H
7 #define OpFlashAna_H 1
8 
9 // ROOT includes
10 #include "TH1.h"
11 #include "TH2.h"
12 #include "TLorentzVector.h"
13 #include "TVector3.h"
14 #include "TTree.h"
15 
16 // C++ includes
17 #include <map>
18 #include <vector>
19 #include <iostream>
20 #include <cstring>
21 #include <sstream>
22 #include "math.h"
23 #include <climits>
24 
25 // LArSoft includes
31 //#include "OpticalDetector/OpDigiProperties.h"
32 
33 // ART includes.
36 #include "fhiclcpp/ParameterSet.h"
46 
47 namespace opdet {
48 
49  class OpFlashAna : public art::EDAnalyzer{
50  public:
51 
52  // Standard constructor and destructor for an ART module.
54  virtual ~OpFlashAna();
55 
56  // This method is called once, at the start of the job. In this
57  // example, it will define the histogram we'll write.
58  void beginJob();
59 
60  // The analyzer routine, called once per event.
61  void analyze (const art::Event&);
62 
63  private:
64 
65  // The stuff below is the part you'll most likely have to change to
66  // go from this custom example to your own task.
67 
68  // The parameters we'll read from the .fcl file.
69  std::string fOpFlashModuleLabel; // Input tag for OpFlash collection
70  std::string fOpHitModuleLabel; // Input tag for OpHit collection
71  float fSampleFreq; // in MHz
72  float fTimeBegin; // in us
73  float fTimeEnd; // in us
74 
75  float fYMin, fYMax, fZMin, fZMax;
76 
78 
82 
88 
90  TTree * fPerFlashTree;
91  TTree * fPerOpHitTree;
94 
95  Int_t fEventID;
96  Int_t fFlashID;
97  Int_t fHitID;
98  Float_t fFlashTime;
99  Float_t fAbsTime;
102  Float_t fTotalPE;
103  Int_t fFlashFrame;
104 
105  Float_t fNPe;
106  Float_t fYCenter;
107  Float_t fYWidth;
108  Float_t fZCenter;
109  Float_t fZWidth;
110 
111  Int_t fOpChannel;
112  Float_t fPeakTimeAbs;
113  Float_t fPeakTime;
114  Int_t fFrame;
115  Float_t fWidth;
116  Float_t fArea;
117  Float_t fAmplitude;
118  Float_t fPE;
119  Float_t fFastToTotal;
120 
122  std::vector< int > fFlashIDVector;
123  std::vector< float > fYCenterVector;
124  std::vector< float > fZCenterVector;
125  std::vector< float > fYWidthVector;
126  std::vector< float > fZWidthVector;
127  std::vector< float > fFlashTimeVector;
128  std::vector< float > fAbsTimeVector;
129  std::vector< int > fFlashFrameVector;
130  std::vector< bool > fInBeamFrameVector;
131  std::vector< int > fOnBeamTimeVector;
132  std::vector< float > fTotalPEVector;
134  std::vector< float > fPEsPerFlashPerChannelVector;
135  };
136 
137 }
138 
139 #endif // OpFlashAna_H
140 
141 namespace opdet {
142 
143  //-----------------------------------------------------------------------
144  // Constructor
146  : EDAnalyzer(pset)
147  {
148 
149  // Indicate that the Input Module comes from .fcl
150  fOpFlashModuleLabel = pset.get<std::string>("OpFlashModuleLabel");
151  fOpHitModuleLabel = pset.get<std::string>("OpHitModuleLabel");
152 
153 // art::ServiceHandle<OpDigiProperties> odp;
154 // fTimeBegin = odp->TimeBegin();
155 // fTimeEnd = odp->TimeEnd();
156 // fSampleFreq = odp->SampleFreq();
157 
158  auto const* timeService = lar::providerFrom<detinfo::DetectorClocksService>();
159  fTimeBegin = timeService->OpticalClock().Time();
160  fTimeEnd = timeService->OpticalClock().FramePeriod();
161  fSampleFreq = timeService->OpticalClock().Frequency();
162 
163  fYMin = pset.get<float>("YMin");
164  fYMax = pset.get<float>("YMax");
165  fZMin = pset.get<float>("ZMin");
166  fZMax = pset.get<float>("ZMax");
167 
168  fMakeFlashTimeHist = pset.get<bool>("MakeFlashTimeHist");
169  fMakeFlashPosHist = pset.get<bool>("MakeFlashPosHist");
170  fMakePerFlashHists = pset.get<bool>("MakePerFlashHists");
171 
172  fMakePerEventFlashTree = pset.get<bool>("MakePerEventFlashTree");
173  fMakePerFlashTree = pset.get<bool>("MakePerFlashTree");
174  fMakePerOpHitTree = pset.get<bool>("MakePerOpHitTree");
175  fMakeFlashBreakdownTree = pset.get<bool>("MakeFlashBreakdownTree");
176  fMakeFlashHitMatchTree = pset.get<bool>("MakeFlashHitMatchTree");
177 
178 
179  PosHistYRes = 100;
180  PosHistZRes = 100;
181 
183 
184  if(fMakeFlashBreakdownTree)
185  {
186  fFlashBreakdownTree = tfs->make<TTree>("FlashBreakdownTree","FlashBreakdownTree");
187  fFlashBreakdownTree->Branch("EventID", &fEventID, "EventID/I");
188  fFlashBreakdownTree->Branch("FlashID", &fFlashID, "FlashID/I");
189  fFlashBreakdownTree->Branch("OpChannel", &fOpChannel, "OpChannel/I");
190  fFlashBreakdownTree->Branch("FlashTime", &fFlashTime, "FlashTime/F");
191  fFlashBreakdownTree->Branch("NPe", &fNPe, "NPe/F");
192  fFlashBreakdownTree->Branch("AbsTime", &fAbsTime, "AbsTime/F");
193  fFlashBreakdownTree->Branch("FlashFrame", &fFlashFrame, "FlashFrame/I");
194  fFlashBreakdownTree->Branch("InBeamFrame",&fInBeamFrame, "InBeamFrame/B");
195  fFlashBreakdownTree->Branch("OnBeamTime", &fOnBeamTime, "OnBeamTime/I");
196  fFlashBreakdownTree->Branch("YCenter", &fYCenter, "YCenter/F");
197  fFlashBreakdownTree->Branch("ZCenter", &fZCenter, "ZCenter/F");
198  fFlashBreakdownTree->Branch("YWidth", &fYWidth, "YWidth/F");
199  fFlashBreakdownTree->Branch("ZWidth", &fZWidth, "ZWidth/F");
200  fFlashBreakdownTree->Branch("TotalPE", &fTotalPE, "TotalPE/F");
201  }
202 
203  if(fMakePerOpHitTree)
204  {
205  fPerOpHitTree = tfs->make<TTree>("PerOpHitTree","PerOpHitTree");
206  fPerOpHitTree->Branch("EventID", &fEventID, "EventID/I");
207  fPerOpHitTree->Branch("HitID", &fHitID, "HitID/I");
208  fPerOpHitTree->Branch("OpChannel", &fOpChannel, "OpChannel/I");
209  fPerOpHitTree->Branch("PeakTimeAbs", &fPeakTimeAbs, "PeakTimeAbs/F");
210  fPerOpHitTree->Branch("PeakTime", &fPeakTime, "PeakTime/F");
211  fPerOpHitTree->Branch("Frame", &fFrame, "Frame/I");
212  fPerOpHitTree->Branch("Width", &fWidth, "Width/F");
213  fPerOpHitTree->Branch("Area", &fArea, "Area/F");
214  fPerOpHitTree->Branch("Amplitude", &fAmplitude, "Amplitude/F");
215  fPerOpHitTree->Branch("PE", &fPE, "PE/F");
216  fPerOpHitTree->Branch("FastToTotal", &fFastToTotal, "FastToTotal/F");
217  }
218 
219  if(fMakePerFlashTree)
220  {
221  fPerFlashTree = tfs->make<TTree>("PerFlashTree","PerFlashTree");
222  fPerFlashTree->Branch("EventID", &fEventID, "EventID/I");
223  fPerFlashTree->Branch("FlashID", &fFlashID, "FlashID/I");
224  fPerFlashTree->Branch("YCenter", &fYCenter, "YCenter/F");
225  fPerFlashTree->Branch("ZCenter", &fZCenter, "ZCenter/F");
226  fPerFlashTree->Branch("YWidth", &fYWidth, "YWidth/F");
227  fPerFlashTree->Branch("ZWidth", &fZWidth, "ZWidth/F");
228  fPerFlashTree->Branch("FlashTime", &fFlashTime, "FlashTime/F");
229  fPerFlashTree->Branch("AbsTime", &fAbsTime, "AbsTime/F");
230  fPerFlashTree->Branch("FlashFrame", &fFlashFrame, "FlashFrame/I");
231  fPerFlashTree->Branch("InBeamFrame",&fInBeamFrame, "InBeamFrame/B");
232  fPerFlashTree->Branch("OnBeamTime", &fOnBeamTime, "OnBeamTime/I");
233  fPerFlashTree->Branch("TotalPE", &fTotalPE, "TotalPE/F");
234  }
235 
236  if(fMakePerEventFlashTree)
237  {
238  fPerEventFlashTree = tfs->make<TTree>("PerEventFlashTree","PerEventFlashTree");
239  fPerEventFlashTree->Branch("EventID", &fEventID, "EventID/I");
240  fPerEventFlashTree->Branch("NFlashes", &fNFlashes, "NFlashes/I");
241  fPerEventFlashTree->Branch("FlashIDVector", &fFlashIDVector);
242  fPerEventFlashTree->Branch("YCenterVector", &fYCenterVector);
243  fPerEventFlashTree->Branch("ZCenterVector", &fZCenterVector);
244  fPerEventFlashTree->Branch("YWidthVector", &fYWidthVector);
245  fPerEventFlashTree->Branch("ZWidthVector", &fZWidthVector);
246  fPerEventFlashTree->Branch("FlashTimeVector", &fFlashTimeVector);
247  fPerEventFlashTree->Branch("AbsTimeVector", &fAbsTimeVector);
248  fPerEventFlashTree->Branch("FlashFrameVector", &fFlashFrameVector);
249  fPerEventFlashTree->Branch("InBeamFrameVector", &fInBeamFrameVector);
250  fPerEventFlashTree->Branch("OnBeamTimeVector", &fOnBeamTimeVector);
251  fPerEventFlashTree->Branch("TotalPEVector", &fTotalPEVector);
252  fPerEventFlashTree->Branch("NChannels", &fNChannels, "NChannels/I");
253  // The only way I can think of to record a two-dimensional variable-size array in a TTree
254  // is by flattening it into a one-dimension variable-size array
255  fPerEventFlashTree->Branch("PEsPerFlashPerChannelVector", &fPEsPerFlashPerChannelVector);
256  }
257 
258  if(fMakeFlashHitMatchTree)
259  {
260  fFlashHitMatchTree = tfs->make<TTree>("FlashHitMatchTree","FlashHitMatchTree");
261  fFlashHitMatchTree->Branch("EventID", &fEventID, "EventID/I");
262  fFlashHitMatchTree->Branch("FlashID", &fFlashID, "FlashID/I");
263  fFlashHitMatchTree->Branch("HitID", &fHitID, "HitID/I");
264  fFlashHitMatchTree->Branch("OpChannel", &fOpChannel, "OpChannel/I");
265  fFlashHitMatchTree->Branch("HitPeakTimeAbs",&fPeakTimeAbs,"HitPeakTimeAbs/F");
266  fFlashHitMatchTree->Branch("HitPeakTime", &fPeakTime, "HitPeakTime/F");
267  fFlashHitMatchTree->Branch("HitPE", &fPE, "HitPE/F");
268  fFlashHitMatchTree->Branch("FlashPE", &fTotalPE, "FlashPE/F");
269  fFlashHitMatchTree->Branch("FlashTimeAbs", &fAbsTime, "FlashTimeAbs/F");
270  fFlashHitMatchTree->Branch("FlashTime", &fFlashTime, "FlashTime/F");
271  fFlashHitMatchTree->Branch("HitFrame", &fFrame, "HitFrame/I");
272  fFlashHitMatchTree->Branch("FlashFrame", &fFlashFrame, "FlashFrame/I");
273  }
274 
275  fFlashID = 0;
276  }
277 
278  //-----------------------------------------------------------------------
279  // Destructor
281  {}
282 
283  //-----------------------------------------------------------------------
285  {}
286 
287  //-----------------------------------------------------------------------
288  void OpFlashAna::analyze(const art::Event& evt)
289  {
290 
291  // Get flashes from event
293  evt.getByLabel(fOpFlashModuleLabel, FlashHandle);
294 
295  // Get assosciations between flashes and hits
296  art::FindManyP< recob::OpHit > Assns(FlashHandle, evt, fOpFlashModuleLabel);
297 
298  // Create string for histogram name
299  char HistName[50];
300 
301  fFlashID = 0;
302 
303  // Access ART's TFileService, which will handle creating and writing
304  // histograms for us.
306 
307  std::vector<TH1D*> FlashHist;
308 
309  fEventID = evt.id().event();
310 
311  sprintf(HistName, "Event %d Flash Times", evt.id().event());
312  TH1D * FlashTimes = nullptr;
314  {
315  FlashTimes = tfs->make<TH1D>(HistName, ";t (ns);",
316  int((fTimeEnd - fTimeBegin) * fSampleFreq),
317  fTimeBegin * 1000.,
318  fTimeEnd * 1000.);
319  }
320 
321  TH2D * FlashPositions = nullptr;
323  {
324  sprintf(HistName, "Event %d All Flashes YZ", evt.id().event());
325 
326  FlashPositions = tfs->make<TH2D>(HistName, ";y ;z ",
329  }
330 
332  unsigned int NOpChannels = geom->NOpChannels();
333 
335  {
336  fNFlashes = FlashHandle->size();
337  fNChannels = NOpChannels;
338  }
339 
340  // For every OpFlash in the vector
341  for(unsigned int i = 0; i < FlashHandle->size(); ++i)
342  {
343 
344  // Get OpFlash
345  art::Ptr< recob::OpFlash > TheFlashPtr(FlashHandle, i);
346  recob::OpFlash TheFlash = *TheFlashPtr;
347 
348  fFlashTime = TheFlash.Time();
349  fFlashID = i; //++;
350 
351  TH2D * ThisFlashPosition = nullptr;
353  {
354  sprintf(HistName, "Event %d t = %f", evt.id().event(), fFlashTime);
355  FlashHist.push_back(tfs->make<TH1D>(HistName, ";OpChannel;PE",
356  NOpChannels, 0, NOpChannels));
357 
358  sprintf(HistName, "Event %d Flash %f YZ", evt.id().event(), fFlashTime);
359 
360  ThisFlashPosition = tfs->make<TH2D>(HistName, ";y ;z ",
363  }
364  fYCenter = TheFlash.YCenter();
365  fZCenter = TheFlash.ZCenter();
366  fYWidth = TheFlash.YWidth();
367  fZWidth = TheFlash.ZWidth();
368  fInBeamFrame = TheFlash.InBeamFrame();
369  fOnBeamTime = TheFlash.OnBeamTime();
370  fAbsTime = TheFlash.AbsTime();
371  fFlashFrame = TheFlash.Frame();
372  fTotalPE = TheFlash.TotalPE();
373 
375  {
376  fFlashIDVector .emplace_back(i);
377  fYCenterVector .emplace_back(TheFlash.YCenter());
378  fZCenterVector .emplace_back(TheFlash.ZCenter());
379  fYWidthVector .emplace_back(TheFlash.YWidth());
380  fZWidthVector .emplace_back(TheFlash.ZWidth());
381  fFlashTimeVector .emplace_back(TheFlash.Time());
382  fAbsTimeVector .emplace_back(TheFlash.AbsTime());
383  fFlashFrameVector .emplace_back(TheFlash.Frame());
384  fInBeamFrameVector.emplace_back(TheFlash.InBeamFrame());
385  fOnBeamTimeVector .emplace_back(TheFlash.OnBeamTime());
386  fTotalPEVector .emplace_back(TheFlash.TotalPE());
387  }
388 
389  for(unsigned int j=0; j!=NOpChannels; ++j)
390  {
391  if(fMakePerFlashHists) FlashHist.at(FlashHist.size()-1)->Fill(j, TheFlash.PE(j));
392  fNPe = TheFlash.PE(j);
393  fOpChannel = j;
394 
396  fPEsPerFlashPerChannelVector.emplace_back(TheFlash.PE(j));
397 
399  }
400 
401  for(int y=0; y!=PosHistYRes; ++y)
402  for(int z=0; z!=PosHistZRes; ++z)
403  {
404  float ThisY = fYMin + (fYMax-fYMin)*float(y)/PosHistYRes + 0.0001;
405  float ThisZ = fZMin + (fZMax-fZMin)*float(z)/PosHistZRes + 0.0001;
406  if (fMakePerFlashHists) ThisFlashPosition->Fill(ThisY, ThisZ, fTotalPE * exp(-pow((ThisY-fYCenter)/fYWidth,2)/2.-pow((ThisZ-fZCenter)/fZWidth,2)/2.));
407  if (fMakeFlashPosHist) FlashPositions->Fill(ThisY, ThisZ, fTotalPE * exp(-pow((ThisY-fYCenter)/fYWidth,2)-pow((ThisZ-fZCenter)/fZWidth,2)));
408  }
409 
410  if(fMakeFlashTimeHist) FlashTimes->Fill(fFlashTime, fTotalPE);
411 
412  if(fMakePerFlashTree) fPerFlashTree->Fill();
413 
415  {
416  // Extract the assosciations
417  fHitID = 0;
418  std::vector< art::Ptr<recob::OpHit> > matchedHits = Assns.at(i);
419  for (auto ophit : matchedHits)
420  {
421  fOpChannel = ophit->OpChannel();
422  fPeakTimeAbs = ophit->PeakTimeAbs();
423  fPeakTime = ophit->PeakTime();
424  fFrame = ophit->Frame();
425  fWidth = ophit->Width();
426  fArea = ophit->Area();
427  fAmplitude = ophit->Amplitude();
428  fPE = ophit->PE();
429  fFastToTotal = ophit->FastToTotal();
430  fFlashHitMatchTree->Fill();
431  fHitID++;
432  }
433  }
434  }
435 
437  {
439  evt.getByLabel(fOpHitModuleLabel, OpHitHandle);
440 
441  for(size_t i=0; i!=OpHitHandle->size(); ++i)
442  {
443  fOpChannel = OpHitHandle->at(i).OpChannel();
444  fPeakTimeAbs = OpHitHandle->at(i).PeakTimeAbs();
445  fPeakTime = OpHitHandle->at(i).PeakTime();
446  fFrame = OpHitHandle->at(i).Frame();
447  fWidth = OpHitHandle->at(i).Width();
448  fArea = OpHitHandle->at(i).Area();
449  fAmplitude = OpHitHandle->at(i).Amplitude();
450  fPE = OpHitHandle->at(i).PE();
451  fFastToTotal = OpHitHandle->at(i).FastToTotal();
452  fHitID = i;
453  fPerOpHitTree->Fill();
454  }
455  }
456 
458  {
459  fPerEventFlashTree->Fill();
460  fFlashIDVector .clear();
461  fYCenterVector .clear();
462  fZCenterVector .clear();
463  fYWidthVector .clear();
464  fZWidthVector .clear();
465  fFlashTimeVector .clear();
466  fAbsTimeVector .clear();
467  fFlashFrameVector .clear();
468  fInBeamFrameVector .clear();
469  fOnBeamTimeVector .clear();
470  fTotalPEVector .clear();
472  }
473 
474  }
475 
476 } // namespace opdet
477 
478 namespace opdet {
480 }
std::vector< int > fOnBeamTimeVector
std::vector< float > fTotalPEVector
std::vector< int > fFlashIDVector
std::string fOpHitModuleLabel
OpFlashAna(const fhicl::ParameterSet &)
TNtupleSim Fill(f1, f2, f3, f4)
Float_t y
Definition: compare.C:6
Double_t z
Definition: plot.C:279
double PE(unsigned int i) const
Definition: OpFlash.h:87
unsigned int NOpChannels() const
Number of electronics channels for all the optical detectors.
std::vector< float > fYCenterVector
double ZCenter() const
Definition: OpFlash.h:91
double Time() const
Definition: OpFlash.h:83
std::vector< float > fYWidthVector
void analyze(const art::Event &)
#define DEFINE_ART_MODULE(klass)
Definition: ModuleMacros.h:42
int OnBeamTime() const
Definition: OpFlash.h:97
std::vector< float > fZCenterVector
unsigned int Frame() const
Definition: OpFlash.h:86
std::vector< int > fFlashFrameVector
T get(std::string const &key) const
Definition: ParameterSet.h:231
std::vector< float > fFlashTimeVector
EDAnalyzer(Table< Config > const &config)
Definition: EDAnalyzer.h:100
double YWidth() const
Definition: OpFlash.h:90
T * make(ARGS...args) const
bool getByLabel(std::string const &label, std::string const &productInstanceName, Handle< PROD > &result) const
Definition: DataViewImpl.h:344
std::vector< float > fZWidthVector
std::vector< float > fAbsTimeVector
EventNumber_t event() const
Definition: EventID.h:117
std::vector< float > fPEsPerFlashPerChannelVector
double TotalPE() const
Definition: OpFlash.cxx:56
std::vector< bool > fInBeamFrameVector
double YCenter() const
Definition: OpFlash.h:89
EventID id() const
Definition: Event.h:56
art framework interface to geometry description
double AbsTime() const
Definition: OpFlash.h:85
double ZWidth() const
Definition: OpFlash.h:92
bool InBeamFrame() const
Definition: OpFlash.h:96
std::string fOpFlashModuleLabel