CRTTimingValidation_module.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 // Class: CRTTimingValidation
3 // Plugin Type: analyzer (art v2_11_02)
4 // File: CRTTimingValidation_module.cc
5 //
6 // Written by: Richard Diurba
7 ////////////////////////////////////////////////////////////////////////
8 
9 //Framework includes
18 #include "fhiclcpp/ParameterSet.h"
21 #include "canvas/Persistency/Common/FindManyP.h"
22 #include "art_root_io/TFileService.h"
23 
24 //LArSoft includes
25 
29 #include "nug4/ParticleNavigation/ParticleList.h"
30 
35 
38 
41 
42 
43 //Local includes
45 
46 //ROOT includes
47 #include "TH1.h"
48 #include "TH2.h"
49 #include "TCanvas.h"
50 #include "TImage.h"
51 #include "TTree.h"
52 #include "TH1D.h"
53 #include "TStyle.h"
54 #include "TString.h"
55 
56 //c++ includes
57 #include <numeric> //std::accumulate was moved from <algorithm> to <numeric> in c++14
58 #include <iostream>
59 #include <cmath>
60 using namespace std; // Namespaces established to make life easier
61 using namespace ROOT::Math;
62 namespace CRT {
63  class CRTTimingValidation;
64 
65 }
66 
67 
68 
70  public: // Setup functions and variables
72  const & p);
73  // The compiler-generated destructor is fine for non-base
74  // classes without bare pointers or other resource use.
75  // Plugins should not be copied or assigned.
77  const & ) = delete;
80  const & ) = delete;
81  CRTTimingValidation & operator = (CRTTimingValidation && ) = delete;
82  void analyze(art::Event
83  const & e) override;
84 // Declare functions and variables for validation
85  bool moduleCheckX(int module);
86  int moduletoCTB(int module2, int module1);
87  void beginJob() override;
88  void endJob() override;
89 
90  //Parameters for reading in CRT::Triggers and associated AuxDetSimChannels.
91  art::InputTag fCRTLabel; //Label for the module that produced
93 
94  TTree * fCRTTree;
95  TTree * fCRTTreeF;
96  TTree * fCRTTreeB;
97  bool fMCCSwitch;
100  int nEvents=0;
101  int matchedCTBtoCRT=0;
102  int ctb_F, ctb_B;
103 
104  double CRT_TOF;
107  double T_F, T_B;
108  int moduleX_F, moduleX_B, moduleY_F, moduleY_B;
109  int stripX_F, stripX_B, stripY_F, stripY_B;
110  double RDminDeltaT_F, RDminDeltaT_B;
111 
112  typedef struct // Structures for arrays to move hits from raw to reco to validation
113  {
114 
115  int module;
116  int64_t triggerTime;
117  }
118  tempTrigger;
119 
120  typedef struct // Structures for arrays to move hits from raw to reco to validation
121  {
122 
123  int moduleX;
124  int moduleY;
127  int RDDeltaT;
128  }
129  tempHits;
130 
131  typedef struct // Structures for arrays to move hits from raw to reco to validation
132  {
133 
134  int module1;
135  int module2;
137  }
138  ctbHits;
139 
140 
141  std::vector < tempTrigger > trigger_F_X;
142  std::vector < tempTrigger > trigger_F_Y;
143  std::vector < tempTrigger > trigger_B_X;
144  std::vector < tempTrigger > trigger_B_Y;
145 
146  std::vector < tempHits > hits_F;
147  std::vector < tempHits > hits_B;
148  std::vector <ctbHits> ctbTriggers;
149 
150 
151 
152 };
153 
155  const & p):
156  EDAnalyzer(p), fCRTLabel(p.get < art::InputTag > ("CRTLabel")), fCTBLabel(p.get<art::InputTag>("CTBLabel")) {
157  consumes < std::vector < CRT::Trigger >> (fCRTLabel);
158  consumes < std::vector < art::Assns < sim::AuxDetSimChannel, CRT::Trigger >>> (fCRTLabel); // CRT art consumables
159  fMCCSwitch=(p.get<bool>("MCC"));
160 
161  }
162 
163 
164 
165 
167 if (module==4 || module==5 || module==6 || module==7 || module==8 || module==9 || module==10 || module==11 || module==20 || module==21 || module==22 || module==23 || module==24 || module==25 || module==26 || module==27) return 1;
168 else return 0;
169 }
170 
171 
172 int CRT::CRTTimingValidation::moduletoCTB(int module2, int module1){
173  if (module1 == 13 && module2 == 6 ) return 15;
174  else if (module1 == 13 && module2 == 7) return 10;
175  else if (module1 == 1 && module2 == 6) return 8;
176  else if (module1 == 1 && module2 == 7) return 9;
177  else if (module1 == 16 && module2 == 20) return 4;
178  else if (module1 == 16 && module2 == 21) return 13;
179  else if (module1 == 28 && module2 == 20) return 3;
180  else if (module1 == 28 && module2 == 21) return 2;
181  else if (module1 == 29 && module2 == 22) return 1;
182  else if (module1 == 29 && module2 == 23) return 0;
183  else if (module1 == 17 && module2 == 22) return 12;
184  else if (module1 == 17 && module2 == 23) return 11;
185  else if (module1 == 0 && module2 == 5) return 7;
186  else if (module1 == 0 && module2 == 4) return 6;
187  else if (module1 == 12 && module2 == 5) return 14;
188  else if (module1 == 12 && module2 == 4) return 5;
189  else if (module1 == 3 && module2 == 8) return 25;
190  else if (module1 == 3 && module2 == 9) return 24;
191  else if (module1 == 15 && module2 == 8) return 26;
192  else if (module1 == 15 && module2 == 9) return 31;
193  else if (module1 == 18 && module2 == 26) return 27;
194  else if (module1 == 18 && module2 == 27) return 28;
195  else if (module1 == 30 && module2 == 26) return 16;
196  else if (module1 == 30 && module2 == 27) return 17;
197  else if (module1 == 31 && module2 == 24) return 18;
198  else if (module1 == 31 && module2 == 25) return 19;
199  else if (module1 == 19 && module2 == 24) return 29;
200  else if (module1 == 19 && module2 == 25) return 20;
201  else if (module1 == 14 && module2 == 10) return 30;
202  else if (module1 == 14 && module2 == 11) return 21;
203  else if (module1 == 2 && module2 == 10) return 23;
204  else if (module1 == 2 && module2 == 11) return 22;
205  else return -1;
206 }
207 
208 
209 
210 
211 
213  const & event) // Analysis module
214 {
215 //cout<<"Setting everything up"<<endl;
216  art::ValidHandle<std::vector<raw::RDTimeStamp>> timingHandle = event.getValidHandle<std::vector<raw::RDTimeStamp>>("timingrawdecoder:daq");
217 
218  using timestamp_t = int64_t;
219 
220 
221  const raw::RDTimeStamp& timeStamp = timingHandle->at(0);
222  if(timeStamp.GetFlags()!= 13) return;
223 
224  if (fMCCSwitch){
225  fModuleSwitch=1;
226  fADCThreshold=800;
227 
228 }
229  else {
230  fModuleSwitch=0;
231  fADCThreshold=50;
232 
233 
234 }
235 
236 
237 
238 
239  hits_F.clear();
240  hits_B.clear();
241  trigger_F_X.clear();
242 
243  trigger_F_Y.clear();
244 
245  trigger_B_Y.clear();
246 
247  trigger_B_X.clear();
248  ctbTriggers.clear();
249  // Arrays to compile hits and move them through
250 
251  //allTracksPair.clear();
252 
253  //Get triggers
254  //cout << "Getting triggers" << endl;
255  const auto & triggers = event.getValidHandle < std::vector < CRT::Trigger >> (fCRTLabel);
256 
257  art::FindManyP < sim::AuxDetSimChannel > trigToSim(triggers, event, fCRTLabel);
258 
259  //Get a handle to the Geometry service to look up AuxDetGeos from module numbers
261 
262  //Mapping from channel to trigger
263  std::unordered_map < size_t, double > prevTimes;
264  int hitID = 0;
265  //cout << "Looking for hits in Triggers" << endl;
266 
267  for (const auto & trigger: * triggers) {
268 
269  tempTrigger tTrigger;
270 
271  tTrigger.module = trigger.Channel(); // Values to add to array
272  tTrigger.triggerTime=trigger.Timestamp();
273 
274  //cout<<ctbPixels[0]<<endl;
275 
276  const auto & trigGeo = geom -> AuxDet(trigger.Channel()); // Get geo
277  const auto & csens = trigGeo.SensitiveVolume(0);
278  const auto center = csens.GetCenter();
279  if (center.Z() < 100 and moduleCheckX(trigger.Channel())==1) trigger_F_X.push_back(tTrigger);
280  else if (moduleCheckX(trigger.Channel())==1) trigger_B_X.push_back(tTrigger);
281  else if (center.Z() < 100 and moduleCheckX(trigger.Channel())!=1) trigger_F_Y.push_back(tTrigger);
282  else trigger_B_Y.push_back(tTrigger);
283  hitID++;
284  }
285 for (unsigned int i=0; i < trigger_F_X.size(); i++){
286 for (unsigned int j=0; j < trigger_F_Y.size(); j++){
287 T0Offset_F=trigger_F_X[i].triggerTime-trigger_F_Y[j].triggerTime;
288 moduleX_F=trigger_F_X[i].module;
289 moduleY_F=trigger_F_Y[j].module;
290 
291 
292 
293  tempHits tHits;
294 
295  tHits.moduleX = trigger_F_X[i].module; // Values to add to array
296  tHits.moduleY=trigger_F_Y[j].module;
297  tHits.triggerDiff=T0Offset_F;
298  tHits.triggerTimeAvg=(trigger_F_X[i].triggerTime+trigger_F_Y[j].triggerTime)/2.;
299  int minDeltaT=9999;
300  for(const auto& time: *timingHandle)
301  {
302 
303  const timestamp_t& rawTime = time.GetTimeStamp();
304  const auto deltaT = rawTime - (trigger_F_X[i].triggerTime);
305  //cout<<rawTime<<','<<deltaT<<endl;
306  if(fabs(deltaT) < fabs(minDeltaT)) minDeltaT = deltaT;
307  }
308  tHits.RDDeltaT=minDeltaT;
309  RDminDeltaT_F=minDeltaT;
310 if (fabs(T0Offset_F)<5) {fCRTTreeF->Fill(); hits_F.push_back(tHits);}
311 
312 }
313 }
314 
315 for (unsigned int i=0; i<trigger_B_X.size(); i++){
316 for (unsigned int j=0; j<trigger_B_Y.size(); j++){
317 T0Offset_B=trigger_B_X[i].triggerTime-trigger_B_Y[j].triggerTime;
318 moduleX_B=trigger_B_X[i].module;
319 moduleY_B=trigger_B_Y[j].module;
320 
321 
322 
323  tempHits tHits;
324 
325  tHits.moduleX = trigger_B_X[i].module; // Values to add to array
326  tHits.moduleY=trigger_B_Y[j].module;
327  tHits.triggerDiff=T0Offset_B;
328  tHits.triggerTimeAvg=(trigger_B_X[i].triggerTime+trigger_B_Y[j].triggerTime)/2.;
329  int minDeltaT=9999;
330  //cout<<"Timing offset:"<<trigger_B_X[i].triggerTime-trigger_F_X[i].triggerTime<<endl;
331  for(const auto& time: *timingHandle)
332  {
333  const timestamp_t& rawTime = time.GetTimeStamp();
334  const auto deltaT = rawTime - (trigger_B_X[i].triggerTime);
335  if(fabs(deltaT) < fabs(minDeltaT)) minDeltaT=deltaT;
336  }
337 
338  tHits.RDDeltaT=minDeltaT;
339  RDminDeltaT_B=minDeltaT;
340 if (fabs(T0Offset_B)<5) {fCRTTreeB->Fill(); hits_B.push_back(tHits);}
341 }
342 }
343 
344  const auto& pdspctbs = *event.getValidHandle<std::vector<raw::ctb::pdspctb>>(fCTBLabel);
345  std::vector<int> uS, dS;
346 
347 //cout<<pdspctbs.size()<<endl;
348 
349 
350 
351 
352  const size_t npdspctbs = pdspctbs.size();
353  for(size_t j=0;j<npdspctbs;++j)
354  {
355  const std::vector<raw::ctb::Trigger> HLTriggers = pdspctbs[j].GetHLTriggers();
356  const std::vector<raw::ctb::ChStatus> chs = pdspctbs[j].GetChStatusAfterHLTs();
357 for (size_t k=0; k<HLTriggers.size(); ++k)
358  {
359 
360 
361 
362 
363 
364 
365  dS.clear(); uS.clear();
366  //cout<<chs[k].timestamp<<endl;
367  int num = chs[k].crt;
368  //cout<<num<<endl;
369 
370  const std::string binary = std::bitset<32>(num).to_string();
371 
372  //std::vector<CRT::Trigger> inWindow(triggers->size());
373 
374  //const auto crtMask = fGeom->toCTBMask(inWindow);
375  //cout<<crtMask<<','<<chs[k].crt<<endl;
376  //constexpr size_t nBits = 32;
377  //std::bitset<nBits> diff(crtMask ^ status.crt), crtOnly(crtMask & (~status.crt)), ctbOnly(status.crt & (~crtMask));
378  const auto crtmask=chs[k].crt;
379  int pixel0 = -1;
380  int pixel1 = -1;
381  //cout<<crtmask<<endl;
382  for (int i = 0; i<32; ++i){
383  if (crtmask & (1<<i)){
384  if (i<16){
385  pixel0 = i;
386  }
387  else {
388  pixel1 = i;
389  }
390  }
391  }
392  if (pixel0!=-1 && pixel1!=1) {
393 
394 
395 
396  cout<<nEvents<<" TJYang Pixels: "<<pixel0<<","<<pixel1<<endl;
397  ctb_F=pixel0;
398  ctb_B=pixel1;
399 
400 
401  }
402  else return;
403 
404 
405 
406 
407 
408  }
409  }
410 int loopTimer=0;
411 for (unsigned int i=0; i<hits_F.size(); i++){
412 for (unsigned int j=0; j<hits_B.size(); j++){
413 moduleX_B=hits_B[j].moduleX;
414 moduleY_B=hits_B[j].moduleY;
415 
416 moduleX_F=hits_F[i].moduleX;
417 moduleY_F=hits_F[i].moduleY;
418 T_F=hits_F[i].triggerTimeAvg;
419 T_B=hits_B[j].triggerTimeAvg;
420 CRT_TOF=hits_B[j].triggerTimeAvg-hits_F[i].triggerTimeAvg;
421 
422 
423 if (fabs(CRT_TOF)<5 && ctb_F==moduletoCTB(hits_F[i].moduleX,hits_F[i].moduleY) && ctb_B==moduletoCTB(hits_B[j].moduleX,hits_B[j].moduleY)) {cout<<nEvents<<" CRT to CTB Front: "<<hits_F[i].moduleX<<','<<hits_F[i].moduleY<<','<<moduletoCTB(hits_F[i].moduleX,hits_F[i].moduleY)<<endl;
424 cout<<nEvents<<" CRT to CTB Back: "<<hits_B[j].moduleX<<','<<hits_B[j].moduleY<<','<<moduletoCTB(hits_B[j].moduleX,hits_B[j].moduleY)<<endl; fCRTTree->Fill();
426 
427 cout<<matchedCTBtoCRT<<endl;
428 }
429 loopTimer++;
430 }
431 }
432 
433 nEvents++;
434  }
435 
436 
437 // Setup CRT
439  art::ServiceHandle<art::TFileService> fileServiceHandle;
440  fCRTTreeF = fileServiceHandle->make<TTree>("T_F", "event by event info");
441  fCRTTreeB = fileServiceHandle->make<TTree>("T_B", "event by event info");
442 
443  fCRTTree = fileServiceHandle->make<TTree>("Matching TOF", "event by event info");
444 
445 
446  fCRTTreeF->Branch("nEvents", &nEvents, "nEvents/I");
447  fCRTTreeB->Branch("nEvents", &nEvents, "nEvents/I");
448  fCRTTree->Branch("nEvents", &nEvents, "nEvents/I");
449  fCRTTreeF->Branch("RDminDeltaT_F", &RDminDeltaT_F, "RDminDeltaT_F/D");
450  fCRTTreeB->Branch("minDeltaT_B", &RDminDeltaT_B, "RDminDeltaT_B/D");
451 
452  fCRTTreeF->Branch("T0Offset_F", &T0Offset_F, "T0Offset_F/I");
453  fCRTTreeB->Branch("T0Offset_B", &T0Offset_B, "T0Offset_B/I");
454 
455 
456  fCRTTreeF->Branch("hmoduleX_F", &moduleX_F, "moduleX_F/I");
457  fCRTTreeB->Branch("hmoduleX_B", &moduleX_B, "moduleX_B/I");
458 
459  fCRTTreeF->Branch("hmoduleY_F", &moduleY_F, "moduleY_F/I");
460  fCRTTreeB->Branch("hmoduleY_B", &moduleY_B, "moduleY_B/I");
461 
462  fCRTTree->Branch("CRT_TOF", &CRT_TOF, "CRT_TOF/D");
463 fCRTTree->Branch("T_B", &T_B, "T_B/D");
464 fCRTTree->Branch("T_F", &T_F, "T_F/D");
465 
466 
467  fCRTTree->Branch("hmoduleX_F", &moduleX_F, "moduleX_F/I");
468  fCRTTree->Branch("hmoduleX_B", &moduleX_B, "moduleX_B/I");
469  fCRTTree->Branch("hmoduleY_F", &moduleY_F, "moduleY_F/I");
470  fCRTTree->Branch("hmoduleY_B", &moduleY_B, "moduleY_B/I");
471 
472 
473 }
474 // Endjob actions
476 {
477 
478 cout<<matchedCTBtoCRT<<endl;
479 
480 }
481 
482 
483 
484 
485 
486 
487 
488 
489 
490 
491 
492 
493 
494 
def analyze(root, level, gtrees, gbranches, doprint)
Definition: rootstat.py:69
int moduletoCTB(int module2, int module1)
uint16_t GetFlags() const
Definition: RDTimeStamp.h:46
std::vector< tempTrigger > trigger_B_X
std::vector< tempTrigger > trigger_F_X
std::string string
Definition: nybbler.cc:12
STL namespace.
std::vector< tempTrigger > trigger_B_Y
Particle class.
art framework interface to geometry description
const double e
#define DEFINE_ART_MODULE(klass)
Definition: ModuleMacros.h:67
void beginJob()
Definition: Breakpoints.cc:14
T get(std::string const &key) const
Definition: ParameterSet.h:271
p
Definition: test.py:223
struct ptb::content::word::timestamp_t timestamp_t
CRTTimingValidation(fhicl::ParameterSet const &p)
Declaration of signal hit object.
def center(depos, point)
Definition: depos.py:117
Provides recob::Track data product.
auto const & get(AssnsNode< L, R, D > const &r)
Definition: AssnsNode.h:115
std::vector< tempTrigger > trigger_F_Y
std::string to_string(ModuleType const mt)
Definition: ModuleType.h:34
void analyze(art::Event const &e) override
QTextStream & endl(QTextStream &s)
Event finding and building.