BeamlineTriggerAna_module.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////
2 /// \file BeamlineTriggerAna_module.cc
3 /// \module analyzer
4 /// \brief Basic analyzer module to look at the raw data within a file.
5 /// \author Mike Wallbank (University of Cincinnati) <wallbank@fnal.gov>
6 /// \date January 2020
7 ////////////////////////////////////////////////////////////////////////////
8 
9 // framework
13 #include "fhiclcpp/ParameterSet.h"
21 
22 // services
23 #include "IFBeam_service.h"
24 
25 // nova
26 #include "NovaTimingUtilities/TimingUtilities.h"
27 #include "RawData/RawBeamline.h"
29 #include "BeamlineRecoBase/ToF.h"
31 
32 // stl
33 #include <iostream>
34 #include <bitset>
35 
36 // root
37 #include "TStyle.h"
38 #include "TFile.h"
39 #include "TH2I.h"
40 #include "TProfile.h"
41 #include "TLine.h"
42 #include "TText.h"
43 #include "TCanvas.h"
44 
45 // -----------------------------------------------------------------------
46 namespace tbana {
47 
49 
50  public:
51 
53 
54  void reconfigure(const fhicl::ParameterSet& pset);
55  void analyze(const art::Event& evt);
56  void beginJob();
57  void beginRun(const art::Run& r);
58  void beginSubRun(const art::SubRun& sr);
59  void endJob();
60  void endSubRun(const art::SubRun& sr);
61 
62  private:
63 
64  // config
74  double fSpillLength;
75 
76  // services
77  std::unique_ptr<BeamFolder> fFolder;
78 
79  // analysis
80  unsigned int fTriggersPerSpill;
81  unsigned int fGoodTriggersPerSpill;
82  uint64_t fLastTDUTime;
83  unsigned int fLastSpill;
84  unsigned int fTotSpills;
85  unsigned int fTotTriggers;
86  unsigned int fTotGoodTriggers;
87  double fTotMC6IC;
88  std::map<unsigned int, unsigned int> fNumTriggers;
89  std::map<unsigned int, unsigned int> fNumGoodTriggers;
90  std::map<unsigned int, unsigned int> fNumPotentialTriggers;
91  std::vector<unsigned int> fNumAllPlanesHit;
92  std::vector<unsigned int> fNumAllPlanesExceptHit;
93  std::vector<unsigned int> fNumAllPaddlesHit;
94  std::vector<unsigned int> fNumAllPaddlesExceptHit;
95 
96  // output
97  TFile* fOutFile;
98 
99  // hists
104  TProfile* hEventRecoInfo;
106  TProfile* hTriggerBitsFrac;
107  TH1I* hPaddlesHit;
110 
111  };
112 
113 }
114 
115 // -----------------------------------------------------------------------
117  this->reconfigure(pset);
118 
120  fFolder = ifbeam->getBeamFolder(pset.get<std::string>("ifBundle"),
121  pset.get<std::string>("ifURL"),
122  pset.get<double>("ifDBTimeWindow"));
123 
124  // Look this far back in time for a spill, in seconds
125  fFolder->set_epsilon(pset.get<double>("ifEpsilon"));
126 }
127 
128 // -----------------------------------------------------------------------
130  fRawTriggerLabel = pset.get<std::string>("RawTriggerLabel");
131  fRawToFDigitLabel = pset.get<std::string>("RawToFDigitLabel");
132  fRecoToFDigitLabel = pset.get<std::string>("RecoToFDigitLabel");
133  fRecoToFLabel = pset.get<std::string>("RecoToFLabel");
134  fRawWCLabel = pset.get<std::string>("RawWCLabel");
135  fRawWCDigitLabel = pset.get<std::string>("RawWCDigitLabel");
136  fRecoWCTrackLabel = pset.get<std::string>("RecoWCTrackLabel");
137  fRawPaddleDigitLabel = pset.get<std::string>("RawPaddleDigitLabel");
138  fRecoPaddleDigitLabel = pset.get<std::string>("RecoPaddleDigitLabel");
139  fSpillLength = pset.get<double> ("SpillLength");
140 }
141 
142 // -----------------------------------------------------------------------
144  gStyle->SetOptStat(0);
145  fOutFile = new TFile("BeamlineTriggerAna.root", "RECREATE");
146  fTotSpills = 0;
147  fTotTriggers = 0;
148  fTotGoodTriggers = 0;
149  fTotMC6IC = 0.;
150  fNumAllPlanesHit.reserve(8);
151  fNumAllPlanesExceptHit.reserve(8);
152  for (unsigned int plane = 0; plane < 8; ++plane) {
153  fNumAllPlanesHit[plane] = 0;
155  }
156  fNumAllPaddlesHit.reserve(4);
157  fNumAllPaddlesExceptHit.reserve(4);
158  for (unsigned int paddle = 0; paddle < 4; ++paddle) {
159  fNumAllPaddlesHit[paddle] = 0;
160  fNumAllPaddlesExceptHit[paddle] = 0;
161  }
162  hTriggersPerSpill = new TH1I("TriggersPerSpill", ";Number of Triggers / Spill;", 60, 0, 60);
163  hTriggersPerSpillNorm = new TH1F("TriggersPerSpillNorm", ";Number of Triggers / Spill / 1e9ppp;", 100, 0, 10);
164  hGoodTriggersPerSpill = new TH1I("GoodTriggersPerSpill", ";Number of Good Triggers / Spill", 60, 0, 60);
165  hGoodTriggersPerSpillNorm = new TH1F("GoodTriggersPerSpillNorm", ";Number of Good Triggers / Spill / 1e9ppp;", 100, 0, 10);
166  hEventRecoInfo = new TProfile("EventRecoInfo", ";;Fraction of Events;", 3, 0, 3);
167  hEventRecoInfo->GetXaxis()->SetBinLabel(1, "ToF Reco");
168  hEventRecoInfo->GetXaxis()->SetBinLabel(2, "WCTrack Reco");
169  hEventRecoInfo->GetXaxis()->SetBinLabel(3, "Both");
170  hEventRecoInfo->GetYaxis()->SetRangeUser(0, 1);
171  hTriggerBits = new TH1I("TriggerBits", ";Trigger Bit;", 16, 0, 16);
172  hTriggerBits->GetXaxis()->SetNdivisions(16);
173  hTriggerBits->GetXaxis()->CenterLabels();
174  hTriggerBits->SetMinimum(0);
175  hTriggerBitsFrac = new TProfile("TriggerBitsFrac", ";Trigger Bit;", 16, 0, 16);
176  hTriggerBitsFrac->GetXaxis()->SetNdivisions(16);
177  hTriggerBitsFrac->GetXaxis()->CenterLabels();
178  hTriggerBitsFrac->GetYaxis()->SetRangeUser(0, 1);
179  hPaddlesHit = new TH1I("PaddlesHit", ";Paddle;", 4, 0, 4);
180  hPaddlesHit->GetXaxis()->SetNdivisions(4);
181  hPaddlesHit->GetXaxis()->CenterLabels();
182  hPaddlesHit->SetMinimum(0);
183  hWCPlaneEfficiency = new TH1F("WCPlaneEfficiency", ";Plane;", 8, 0, 8);
184  hWCPlaneEfficiency->GetXaxis()->SetNdivisions(8);
185  hWCPlaneEfficiency->GetXaxis()->CenterLabels();
186  hWCPlaneEfficiency->GetYaxis()->SetRangeUser(0, 1);
187  hPaddleEfficiency = new TH1F("PaddleEfficiency", ";Paddle;", 4, 0, 4);
188  hPaddleEfficiency->GetXaxis()->SetNdivisions(4);
189  hPaddleEfficiency->GetXaxis()->CenterLabels();
190  hPaddleEfficiency->GetYaxis()->SetRangeUser(0, 1);
191 }
192 
193 // -----------------------------------------------------------------------
195  fLastSpill = 0;
196 }
197 
198 // -----------------------------------------------------------------------
200 
201  ++fTotSpills;
202 
203  // Take into account spills with no triggers
204  unsigned int spillsWithoutTriggers = sr.subRun()-fLastSpill-1;
205  fNumTriggers[0] += spillsWithoutTriggers;
206  fNumGoodTriggers[0] += spillsWithoutTriggers;
207  fTotSpills += spillsWithoutTriggers;
208 
209  fTriggersPerSpill = 0;
211 
212 }
213 
214 // -----------------------------------------------------------------------
216 
217  fOutFile->cd();
218  for (unsigned int plane = 0; plane < 8; ++plane)
221  for (unsigned int paddle = 0; paddle < 4; ++paddle)
222  hPaddleEfficiency->Fill(paddle,
223  (double)fNumAllPaddlesHit[paddle]/((double)fNumAllPaddlesHit[paddle]+fNumAllPaddlesExceptHit[paddle]));
224  hTriggersPerSpill->Write();
225  hTriggersPerSpillNorm->Write();
226  hGoodTriggersPerSpill->Write();
227  hGoodTriggersPerSpillNorm->Write();
228  hEventRecoInfo->Write();
229  hTriggerBits->Write();
230  hTriggerBitsFrac->Write();
231  hPaddlesHit->Write();
232  hWCPlaneEfficiency->Write();
233  hPaddleEfficiency->Write();
234  fOutFile->Close();
235  delete fOutFile;
236 
237  // Trigger information
238  std::cout << "Total spills: " << fTotSpills << std::endl;
239  std::cout << "Total triggers: " << fTotTriggers << std::endl;
240  std::cout << "Triggers/1e9ppp: " << (double)fTotTriggers/(double)(fTotMC6IC/1e9) << std::endl;
241  for (std::map<unsigned int, unsigned int>::const_iterator numTrigIt = fNumTriggers.begin();
242  numTrigIt != fNumTriggers.end(); ++numTrigIt)
243  std::cout << " Spill(s) with " << numTrigIt->first << " triggers: " << numTrigIt->second
244  << " (" << (double)numTrigIt->second*100/(double)fTotSpills << "%)" << std::endl;
245  std::cout << "Total good triggers: " << fTotGoodTriggers
246  << " (" << (double)fTotGoodTriggers*100/(double)fTotTriggers << "%)" << std::endl;
247  std::cout << "Good Triggers/1e9ppp: " << (double)fTotGoodTriggers/(double)(fTotMC6IC/1e9) << std::endl;
248  for (std::map<unsigned int, unsigned int>::const_iterator numTrigIt = fNumGoodTriggers.begin();
249  numTrigIt != fNumGoodTriggers.end(); ++numTrigIt)
250  std::cout << " Spill(s) with " << numTrigIt->first << " good triggers: " << numTrigIt->second
251  << " (" << (double)numTrigIt->second*100/(double)fTotSpills << "%)" << std::endl;
252 
253 }
254 
255 // -----------------------------------------------------------------------
257 
260 
261  // Get data from database
262 
263  // Intensity of spill
264  // Get trigger time in unix time
265  struct timespec unixTime;
267  const double eventTime = unixTime.tv_sec + 1e-9*unixTime.tv_nsec;
268 
269  // Query the database to get MC6IC end spill time.
270  // Since we are querying $36, have to add an offset to get the end spill time,
271  // rather than when the spill actually starts. The epsilon on the beam folder should be
272  // large enough to encapsulate the entire spill, plus any offset.
273  // Test Beam spills are ~4.2s long.
274 
275  double mc6ic = -999.;
276  double mc7sc1 = -999.;
277  double novtsc01 = -999.;
278  double time = -999.;
279  try{
280  fFolder->GetNamedData(eventTime+fSpillLength, "F:MC6IC@", &mc6ic, &time);
281  fFolder->GetNamedData(eventTime+fSpillLength, "F:MC7SC1@", &mc7sc1, &time);
282  //fFolder->GetNamedData(eventTime+fSpillLength, "Z:NOVTSC01@", &novtsc01, &time);
283  }
284  catch(std::exception& e) {
285  std::string err(e.what());
286  // If the variable can't be found, it means there was no spill within Epsilon of eventTime+fSpillLength
287  if (!err.find("variable not found")) {
288  mf::LogError("BeamlineTriggerAna") << "Exception encountered: " << e.what();
289  abort();
290  }
291  }
292 
293  std::cout << "End of spill, MC6IC " << mc6ic << ", MC7SC1 " << mc7sc1 << ", NOVTSC01 " << novtsc01 << std::endl;
294 
295  fTotMC6IC += mc6ic;
296 
297  float triggersPerSpillNorm = ((float)fTriggersPerSpill)/(mc6ic/1.e9);
298  float goodTriggersPerSpillNorm = ((float)fGoodTriggersPerSpill)/(mc6ic/1.e9);
299 
300  // std::cout << "Number of triggers " << fTriggersPerSubrun << ", and good " << fGoodTriggersPerSubrun << "; intensity " << mc6ic << std::endl;
301  // std::cout << "Normalized triggers per spill " << triggersPerSubrunNorm << ", and good " << goodTriggersPerSubrunNorm << std::endl;
302 
305  hTriggersPerSpillNorm->Fill(triggersPerSpillNorm);
306  hGoodTriggersPerSpillNorm->Fill(goodTriggersPerSpillNorm);
307 
308  fLastSpill = sr.subRun();
309 }
310 
311 // -----------------------------------------------------------------------
313 
314  // Get data from art::Event
315 
316  // Raw trigger objects
318  std::vector<art::Ptr<rawdata::RawBeamlineTrigger> > triggers;
319  if (evt.getByLabel(fRawTriggerLabel, triggerHandle))
320  art::fill_ptr_vector(triggers, triggerHandle);
321 
322  // Reco ToF digits
324  std::vector<art::Ptr<brb::BeamlineDigit> > tofDigits;
325  if (evt.getByLabel(fRecoToFDigitLabel, tofDigitHandle))
326  art::fill_ptr_vector(tofDigits, tofDigitHandle);
327 
328  // Reco ToF
330  std::vector<art::Ptr<brb::ToF> > tofs;
331  if (evt.getByLabel(fRecoToFLabel, tofHandle))
332  art::fill_ptr_vector(tofs, tofHandle);
333 
334  // Raw paddle digits
336  std::vector<art::Ptr<rawdata::RawBeamlineDigit> > rawPaddleDigits;
337  if (evt.getByLabel(fRawPaddleDigitLabel, rawPaddleDigitHandle))
338  art::fill_ptr_vector(rawPaddleDigits, rawPaddleDigitHandle);
339 
340  // Reco paddle digits
341  art::Handle<std::vector<brb::BeamlineDigit> > recoPaddleDigitHandle;
342  std::vector<art::Ptr<brb::BeamlineDigit> > recoPaddleDigits;
343  if (evt.getByLabel(fRecoPaddleDigitLabel, recoPaddleDigitHandle))
344  art::fill_ptr_vector(recoPaddleDigits, recoPaddleDigitHandle);
345 
346  // Raw wire chambers
348  std::vector<art::Ptr<rawdata::RawBeamlineWC> > wcs;
349  if (evt.getByLabel(fRawWCLabel, wcHandle))
350  art::fill_ptr_vector(wcs, wcHandle);
351 
352  // Reco WC tracks
354  std::vector<art::Ptr<brb::WCTrack> > wcTracks;
355  if (evt.getByLabel(fRecoWCTrackLabel, wcTrackHandle))
356  art::fill_ptr_vector(wcTracks, wcTrackHandle);
357 
358  // Trigger bits
359  fLastTDUTime = triggers[0]->TDUTime();
360  uint32_t triggerPattern = triggers[0]->Pattern();
361  for (unsigned int bit = 0; bit < 16; ++bit) {
362  hTriggerBits->Fill(bit, std::bitset<16>(triggerPattern)[bit]);
363  hTriggerBitsFrac->Fill(bit, std::bitset<16>(triggerPattern)[bit]);
364  }
365 
366  // Get paddle digits
367  std::vector<art::Ptr<brb::BeamlineDigit> > tcDigits, paddleDigits;
368  for (std::vector<art::Ptr<brb::BeamlineDigit> >::const_iterator digitIt = recoPaddleDigits.begin();
369  digitIt != recoPaddleDigits.end(); ++digitIt) {
370  if ((*digitIt)->ChannelID().Channel == 0)
371  tcDigits.push_back(*digitIt);
372  else
373  paddleDigits.push_back(*digitIt);
374  }
375 
376  // Get TC hit
377  bool particleOnTarget = tcDigits.size() == 1;
378  if (!particleOnTarget)
379  return;
380 
381  // ------------- Begin counting -------------
382 
383  ++fTotTriggers;
385 
386  // Event reco information
387  bool tofReco = tofs.size(), wcTrackReco = wcTracks.size();
388  hEventRecoInfo->Fill("ToF Reco", tofReco);
389  hEventRecoInfo->Fill("WCTrack Reco", wcTrackReco);
390  hEventRecoInfo->Fill("Both", tofReco and wcTrackReco);
391  if (tofReco and wcTrackReco) {
394  }
395 
396  // Paddles hit
397  std::vector<bool> paddleHit(4, false);
398  for (std::vector<art::Ptr<brb::BeamlineDigit> >::const_iterator paddleDigitIt = paddleDigits.begin();
399  paddleDigitIt != paddleDigits.end(); ++paddleDigitIt)
400  paddleHit[(*paddleDigitIt)->ChannelID().Channel-1] = true;
401 
402  for (unsigned int paddle = 0; paddle < 4; ++paddle)
403  if (paddleHit[paddle])
404  hPaddlesHit->Fill(paddle);
405 
406  std::vector<bool> wcHit(4, false);
407  std::vector<bool> wcPlaneHit(8, false);
408  for (unsigned int wcIt = 0; wcIt < wcs.size(); ++wcIt) {
409  std::vector<rawdata::RawBeamlineWC::WCPulse> x_pulses = wcs[wcIt]->XPulses();
410  std::vector<rawdata::RawBeamlineWC::WCPulse> y_pulses = wcs[wcIt]->YPulses();
411  if (x_pulses.size())
412  wcPlaneHit[2*wcIt] = true;
413  if (y_pulses.size())
414  wcPlaneHit[(2*wcIt)+1] = true;
415  if (x_pulses.size() and y_pulses.size())
416  wcHit[wcIt] = true;
417  }
418 
419  // Wire chamber plane efficiency
420  for (unsigned int plane = 0; plane < 8; ++plane) {
421  int allOtherPlanesHit = true;
422  for (unsigned int otherPlane = 0; otherPlane < 8; ++otherPlane)
423  if ((otherPlane != plane) and !wcPlaneHit[otherPlane])
424  allOtherPlanesHit = false;
425  if (!allOtherPlanesHit)
426  continue;
427  if (wcPlaneHit[plane])
429  else
431  }
432 
433  // Paddle efficiency
434  for (unsigned int paddle = 0; paddle < 4; ++paddle) {
435  int allOtherPaddlesHits = true;
436  for (unsigned int otherPaddle = 0; otherPaddle < 4; ++otherPaddle)
437  if ((otherPaddle != paddle) and !paddleHit[otherPaddle])
438  allOtherPaddlesHits = false;
439  if (!allOtherPaddlesHits)
440  continue;
441  if (paddleHit[paddle])
442  ++fNumAllPaddlesHit[paddle];
443  else
444  ++fNumAllPaddlesExceptHit[paddle];
445  }
446 
447  return;
448 
449 }
450 
SubRunNumber_t subRun() const
Definition: SubRun.h:44
std::map< unsigned int, unsigned int > fNumPotentialTriggers
::xsd::cxx::tree::exception< char > exception
Definition: Database.h:225
MaybeLogger_< ELseverityLevel::ELsev_error, false > LogError
DEFINE_ART_MODULE(TestTMapFile)
bool convertNovaTimeToUnixTime(uint64_t const &inputNovaTime, struct timespec &outputUnixTime)
Definition: Run.h:31
void beginSubRun(const art::SubRun &sr)
std::vector< unsigned int > fNumAllPlanesHit
std::vector< unsigned int > fNumAllPaddlesExceptHit
std::vector< unsigned int > fNumAllPaddlesHit
Encapsulation of reconstructed digitizer &#39;hits&#39;. Used for ToF PMTs and SiPMs, and Cherenkov and Muon ...
void reconfigure(const fhicl::ParameterSet &pset)
T get(std::string const &key) const
Definition: ParameterSet.h:231
Encapsulation of reconstructed Time-of-Flight (ToF) information. Part of beamline reconstruction for ...
std::map< unsigned int, unsigned int > fNumGoodTriggers
int evt
BeamlineTriggerAna(const fhicl::ParameterSet &pset)
Encapsulation of reconstructed Wire Chamber track. Part of beamline reconstruction for NOvA test beam...
caf::StandardRecord * sr
EDAnalyzer(Table< Config > const &config)
Definition: EDAnalyzer.h:100
std::vector< unsigned int > fNumAllPlanesExceptHit
OStream cout
Definition: OStream.cxx:6
std::map< unsigned int, unsigned int > fNumTriggers
bool getByLabel(std::string const &label, std::string const &productInstanceName, Handle< PROD > &result) const
Definition: DataViewImpl.h:344
std::unique_ptr< BeamFolder > getBeamFolder(std::string bundle_name, std::string url="", double time_width=1200.0)
TRandom3 r(0)
void endSubRun(const art::SubRun &sr)
Raw data definitions for beamline data used in NOvA test beam experiment.
void analyze(const art::Event &evt)
std::unique_ptr< BeamFolder > fFolder
void fill_ptr_vector(std::vector< Ptr< T >> &ptrs, H const &h)
Definition: Ptr.h:464
Float_t e
Definition: plot.C:35
Definition: fwd.h:28
enum BeamMode string