SMMTriggerAna_module.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 // Class: SMMTriggerAna
3 // Module Type: producer
4 // File: SMMTriggerAna_module.cc
5 //
6 // Generated at Wed Dec 18 15:41:32 2013 by Zukai Wang using artmod
7 // from cetpkgsupport v1_04_02.
8 ////////////////////////////////////////////////////////////////////////
16 #include "fhiclcpp/ParameterSet.h"
18 
19 #include "MCCheater/BackTracker.h"
20 #include "Geometry/Geometry.h"
23 
24 #include "NovaDAQConventions/DAQConventions.h"
25 #include "RecoBase/CellHit.h"
26 #include "RecoBase/Cluster.h"
27 
28 #include "TH3.h"
29 #include "TVector3.h"
30 #include "TTree.h"
31 
32 #include <algorithm>
33 #include <utility>
34 #include <cmath>
35 
36 namespace zcl {
37  class SMMTriggerAna;
38 }
39 
41 public:
42  explicit SMMTriggerAna(fhicl::ParameterSet const & pset);
43  virtual ~SMMTriggerAna();
44 
45  void analyze(const art::Event & evt);
46  void beginJob();
47  void endJob();
48 
49 private:
50  bool SurfAssign(art::Ptr<rb::CellHit> const & hptr);
51  bool ContaineAssign(art::Ptr<rb::CellHit> const & hptr);
52 
53  //Return the indices of the earliest and latest matching elements in the other view:
54  bool ViewCheck(art::Ptr<rb::CellHit> const &,
55  art::Ptr<rb::CellHit> const &);
56 
57  float Dist(art::Ptr<rb::CellHit> const &,
58  art::Ptr<rb::CellHit> const &);
59 
60  //This function return the index of the hit which earlier than and closest to the time you assigned
61  unsigned IndexLocator(std::vector<art::Ptr<rb::CellHit> > & hit_ptrs,
62  float tns);
63 
64 
65 
66  // FHICL Parameters:
68  float _hf_min; //The threshold of hits/distance
69  //One should consider the speed of MM going to the next plane in the other view,
70  //so the timing cut should be looser in accomodating slower monopoles
71  unsigned _sigmaT; //Time resolution (ns) for single hit: same view parameter
72  unsigned _deltaT; //for checking mate hits on the other view
73  unsigned _minHits; //precut on hits number limit
74 
75  float _deltaP; //This is used for surface determination and also for view matching determination
76  float _deltaC; //For surface determination
77  float _sigmaL; // Triangle cut for the max additional ratio allowed
78  int _ADCmin;
79  int _ADCmax;
80  float _Xmax;
81  float _Ymax;
82  float _DistMin; //Min distance requirement
83  float _vproMin; //Max beta: 0.01
84  float _vproMax; //Min beta: 4e-5
85  float _df; //Maximum fraction of (plane gap)/(plane range) allowed
86 
87  //Tree record info:
88  TTree * RC_Tree;
90  double beta_MC;
91  double Edep_MC;
92  double xi_MC, xf_MC;
93  double yi_MC, yf_MC;
94  double zi_MC, zf_MC;
95  double ti_MC, tf_MC;
98 
100  bool trigger;
101  double ti, tf;
102  double dist;
104 };
105 
107  EDAnalyzer(p),
108  fCellHitInput (p.get< std::string >("CellHitInput") ),
109  _hf_min (p.get< float >("hf_min")), //Min hit number density in unit distance(in cell unit)
110  _sigmaT (1000), //64TDC
111  _deltaT (2000), //128TDC
112  _minHits (20),
113  _deltaP (2),
114  _deltaC (6),
115  _sigmaL (0.02),
116  _ADCmin (p.get<int>("ADCmin")),
117  _ADCmax (p.get<int>("ADCmax")),
118  _Xmax (p.get<float>("Xmax")),
119  _Ymax (p.get<float>("Ymax")),
120  _DistMin (p.get<float>("DistMin")),
121  _vproMin (p.get<float>("vproMin")),
122  _vproMax (p.get<float>("vproMax")),
123  _df (0.1)
124 {
125 }
126 
128 {
129  // Clean up dynamic memory and other resources here.
130 }
131 
133 {
135 
136  RC_Tree = tfs->make<TTree>("RC_Tree","Information from RC track info");
137  RC_Tree -> Branch("event", &evtID, "event/I");
138  RC_Tree -> Branch("subRun", &subRunID, "subRun/I");
139  RC_Tree -> Branch("Run", &RunID, "Run/I");
140 
141  //Cheater info ntuple part
142  RC_Tree -> Branch("betaMC", &beta_MC, "betaMC/D");
143  RC_Tree -> Branch("edepMC", &Edep_MC, "edepMC/D");
144  RC_Tree -> Branch("tiMC", &ti_MC, "tiMC/D");
145  RC_Tree -> Branch("xiMC", &xi_MC, "xiMC/D");
146  RC_Tree -> Branch("yiMC", &yi_MC, "yiMC/D");
147  RC_Tree -> Branch("ziMC", &zi_MC, "ziMC/D");
148  RC_Tree -> Branch("tfMC", &tf_MC, "tfMC/D");
149  RC_Tree -> Branch("xfMC", &xf_MC, "xfMC/D");
150  RC_Tree -> Branch("yfMC", &yf_MC, "yfMC/D");
151  RC_Tree -> Branch("zfMC", &zf_MC, "zfMC/D");
152  RC_Tree -> Branch("xhitsMC", &nxhits_MC, "xhitsMC/I");
153  RC_Tree -> Branch("yhitsMC", &nyhits_MC, "yhitsMC/I");
154  RC_Tree -> Branch("plane_entry", &plane_entry, "plane_entry/I");
155  RC_Tree -> Branch("cell_entry", &cell_entry, "cell_entry/I");
156  RC_Tree -> Branch("plane_exit", &plane_exit, "plane_exit/I");
157  RC_Tree -> Branch("cell_exit", &cell_exit, "cell_exit/I");
158 
159  //Trigger info
160  RC_Tree -> Branch("trigger", &trigger, "trigger/O");
161  RC_Tree -> Branch("ti", &ti, "ti/D");
162  RC_Tree -> Branch("tf", &tf, "tf/D");
163  RC_Tree -> Branch("dist", &dist, "dist/D");
164  RC_Tree -> Branch("hitsAttach", &hitsAttach, "hitsAttach/I");
165 }
166 
168 {
169  evtID = evt.id().event();
170  subRunID = evt.subRun();
171  RunID = evt.run();
172 
173  trigger = false;
174  ti = -10000;
175  tf = -10000;
176  dist = 0;
177  hitsAttach = 0;
178 
179  //First record the MC truth Info:
182  const sim::ParticleNavigator& pnav = bt->ParticleNavigator();
183  for (sim::ParticleNavigator::const_iterator i = pnav.begin(); i != pnav.end(); ++i) {
184  nxhits_MC = 0;
185  nyhits_MC = 0;
186  const sim::Particle *p = (*i).second;
187  if (p->PdgCode()!=42 ) continue;
188  const std::vector<sim::FLSHit>& flshits = bt->ParticleToFLSHit(p->TrackId());
189  unsigned ntruehits_Tot = flshits.size();
190 
191  if ( ntruehits_Tot==0 ) continue;
192  double momentum2 = (p->P())*(p->P());
193  double mass2 = (p->Mass())*(p->Mass());
194  beta_MC = sqrt(momentum2 / (momentum2+mass2) );
195 
196  ti_MC = flshits[0].GetEntryT();
197  xi_MC = flshits[0].GetEntryX();
198  yi_MC = flshits[0].GetEntryY();
199  zi_MC = flshits[0].GetEntryZ();
200  tf_MC = flshits[0].GetExitT();
201  xf_MC = flshits[0].GetExitX();
202  yf_MC = flshits[0].GetExitY();
203  zf_MC = flshits[0].GetExitZ();
204  Edep_MC = flshits[0].GetEdep();
205 
206  size_t planeID_i = flshits[0].GetPlaneID();
207  size_t cellID_i = flshits[0].GetCellID();
208  size_t planeID_f = planeID_i;
209  size_t cellID_f = cellID_i;
210 
211  if (planeID_i%2) ++nxhits_MC;
212  else ++nyhits_MC;
213 
214 
215  for (size_t h =1; h!= flshits.size(); ++h) {
216  double ti_tmp = flshits[h].GetEntryT();
217  double tf_tmp = flshits[h].GetExitT();
218 
219  if (ti_tmp < ti_MC) {
220  ti_MC = ti_tmp;
221  xi_MC = flshits[h].GetEntryX();
222  yi_MC = flshits[h].GetEntryY();
223  zi_MC = flshits[h].GetEntryZ();
224  planeID_i = flshits[h].GetPlaneID();
225  cellID_i = flshits[h].GetCellID();
226  }
227  if (tf_tmp > tf_MC) {
228  tf_MC = tf_tmp;
229  xf_MC = flshits[h].GetExitX();
230  yf_MC = flshits[h].GetExitY();
231  zf_MC = flshits[h].GetExitZ();
232  planeID_f = flshits[h].GetPlaneID();
233  cellID_f = flshits[h].GetCellID();
234  }
235 
236  if (flshits[h].GetPlaneID() % 2) ++nxhits_MC;
237  else ++nyhits_MC;
238 
239  Edep_MC += flshits[h].GetEdep();
240  }
241 
242  //By now, you have got the local coordinate, you have to translate it to world:
243  const geo::CellGeo* startCell = geom->Plane(planeID_i)->Cell(cellID_i);
244  double local[3], world[3];
245  local[0] = xi_MC;
246  local[1] = yi_MC;
247  local[2] = zi_MC;
248  startCell->LocalToWorld(local,world);
249  xi_MC = world[0];
250  yi_MC = world[1];
251  zi_MC = world[2];
252  plane_entry = planeID_i;
253  cell_entry = cellID_i;
254 
255  const geo::CellGeo* stopCell = geom->Plane(planeID_f)->Cell(cellID_f);
256  local[0] = xf_MC;
257  local[1] = yf_MC;
258  local[2] = zf_MC;
259  stopCell->LocalToWorld(local,world);
260  xf_MC = world[0];
261  yf_MC = world[1];
262  zf_MC = world[2];
263  plane_exit = planeID_f;
264  cell_exit = cellID_f;
265 
266 
267  //1 monopole per event, so you can break out from the loop:
268  break;
269  }
270 
271  // std::cout<<"Truth info obtained!"<<std::endl;
272 
273  //Now, pseudo trigger process:
275  evt.getByLabel(fCellHitInput, hitcol);
276  // Load the cell hits into a vector for easy use
277  std::vector<art::Ptr<rb::CellHit > > hitlist[2];
278 
279  //This process imitated the ADC filtering process
280  for(unsigned int i = 0; i < hitcol->size(); ++i){
281  art::Ptr<rb::CellHit> hit_ptr(hitcol, i);
282  // if (bt->IsNoise(hit_ptr) ) continue;
283 
284  if (hit_ptr->ADC()>_ADCmax || hit_ptr->ADC()<_ADCmin) continue;
285  unsigned v = 0;
286  if (hit_ptr->View()==geo::kY ) v = 1;
287  hitlist[v].emplace_back(hit_ptr);
288  }
289 
290  if (hitlist[0].empty() || hitlist[1].empty()) {
291  RC_Tree -> Fill();
292  return;
293  }
294 
295 
296  //Sort the hits by time:
297  rb::SortByTime(hitlist[0]);
298  rb::SortByTime(hitlist[1]);
299 
300  //X, Y, T (plane, cell, tns)
301  std::vector<art::Ptr<rb::CellHit> > c_PS; //They will contain all Valid non surface hits on X view
302  std::vector<art::Ptr<rb::CellHit> > s_PS; //They will contain all Valid surface and mate hits on X view
303 
304 
305  //View Check and doing surface hits collecion
306  for (unsigned i = 0; i!= hitlist[0].size(); ++i ) {
307  art::Ptr<rb::CellHit> const& hit = hitlist[0][i];
308  float t = hit->TNS();
309  unsigned start_id = IndexLocator(hitlist[1], t-_deltaT);
310  unsigned stop_id = IndexLocator(hitlist[1], t+_deltaT);
311  bool met = false;
312  for (unsigned j = start_id; j<stop_id; ++j ) {
313  art::Ptr<rb::CellHit> const& buddy = hitlist[1][j];
314  if (ViewCheck(hit, buddy)) {
315  if (SurfAssign(hit) || SurfAssign(buddy)) {
316  s_PS.emplace_back(hit);
317  break;
318  }
319  else if (!met ) c_PS.emplace_back(hit);
320  met=true;
321  }
322  }
323  }
324  // std::cout<<"contained hits: "<<c_PS.size()<<" surf hits: "<<s_PS.size()<<std::endl;
325 
326  if (c_PS.size()<_minHits) {
327  RC_Tree->Fill();
328  return;
329  }
330  if (s_PS.size()<2) {
331  RC_Tree->Fill();
332  return;
333  }
334 
335 
336  //Try to find entry hit on valid surface hits containers
337  for (unsigned i=0; i<s_PS.size()-1; ++i) {
338  // bool found = false;
339  art::Ptr<rb::CellHit> hit1 = s_PS[i];
340  ti = hit1->TNS();
341  int pi = hit1->Plane();
342  int ci = hit1->Cell();
343  // std::cout<<"ci: "<<ci <<" pi: "<<pi<<std::endl;
344 
345  unsigned ini = IndexLocator(c_PS, ti) + 1;
346 
347  if (ini+_minHits >= c_PS.size() ) break;
348 
349  for (unsigned j=s_PS.size()-1; j>i && ini+_minHits+1 < c_PS.size(); --j) {
350  art::Ptr<rb::CellHit> hit2 = s_PS[j];
351  tf = hit2->TNS();
352  int pf = hit2->Plane();
353  int cf = hit2->Cell();
354 
355  // std::cout<<"cf: "<<cf<<" pf: "<<pf<<std::endl;
356 
357  if (tf+ _deltaT < c_PS[ini+_minHits]->TNS()) break;
358  else if (tf > c_PS[c_PS.size()-1]->TNS() + _deltaT ) continue;
359 
360 
361  int p_max = std::max(pi,pf);
362  int p_min = std::min(pi,pf);
363  int c_max = std::max(ci,cf);
364  int c_min = std::min(ci,cf);
365 
366  if (p_max < p_min+_deltaP) continue;
367 
368  unsigned fin = IndexLocator(c_PS, tf+_deltaT);
369  dist = Dist(hit1, hit2);
370 
371  if (dist < _DistMin) continue;
372  if (fin<ini+dist*_hf_min) continue;
373 
374  float vpro = (tf-ti)/dist;
375  // std::cout<<"vpro: "<<vpro<<" Dist: "<<dist<<std::endl;
376  if (tf > ti+dist*_vproMax+_sigmaT) continue;
377 
378  else if (tf+_sigmaT < ti+dist*_vproMin) continue;
379 
380  //Here we implemente the CCB(continuity-check and break) mechanisim
381  //First, calculate the time window for checking the previous hit on track:
382  int _dP = (p_max-p_min)*_df;
383  float dt = (tf-ti)*_df;
384 
385  if (_dP<2) _dP = 2;
386  else if (_dP>4) _dP = 4;
387 
388  if (dt>256) dt = 256;
389 
390  //Fish is coming!
391  int hitsOnline = 0;
392  double disCut = dist*_sigmaL;
393 
394  int h_cut = dist*_hf_min;
395 
396  int last_C = ci;
397  int last_P = pi;
398  float last_T = ti;
399  for (unsigned k=ini; k!=fin; ++k) {
400  art::Ptr<rb::CellHit> Mid = c_PS[k];
401  int pk = Mid->Plane();
402  int ck = Mid->Cell();
403  float tk = Mid->TNS();
404 
405  if (pk > p_max || pk < p_min) continue;
406  if (ck > c_max || ck < c_min) continue;
407 
408  //Now check if it is forward going:
409  if (pf>pi) {
410  if (pk < last_P || pk> last_P+_dP ) {
411  if (tk>last_T+dt) break;
412  else continue;
413  }
414  if (cf > ci && ck < last_C) continue;
415  if (cf < ci && ck > last_C) continue;
416  }
417 
418  else {
419  if ( pk+_dP< last_P || pk>last_P ) {
420  if (tk>last_T+dt) break;
421  else continue;
422  }
423  if (pk > last_P || pk+_dP < last_P) continue;
424  if (cf > ci && ck < last_C) continue;
425  if (cf < ci && ck > last_C) continue;
426  }
427 
428 
429 
430  float t1 = tk-ti;
431  float t2 = tf-tk;
432  float d1 = Dist(hit1, Mid);
433  float d2 = Dist(hit2, Mid);
434 
435  if (d1+d2 > dist+disCut) continue;
436 
437  if (d1>d2) {
438  if (t1 < d1*vpro + _sigmaT && t1 > d1*vpro - _sigmaT) {
439  ++hitsAttach;
440  last_C= ck;
441  last_P= pk;
442  last_T= tk;
443  }
444  }
445  else {
446  if (t2 > d2*vpro - _sigmaT && t2 < d2*vpro + _sigmaT) {
447  ++hitsOnline;
448  last_C= ck;
449  last_P= pk;
450  last_T= tk;
451  }
452  }
453  }
454 
455  if (hitsOnline > h_cut ) {
456  trigger = true;
457  if (hitsOnline > hitsAttach)
458  hitsAttach = hitsOnline;
459  }
460  }
461  } // X view finished
462 
463 
464  RC_Tree->Fill();
465  /*
466  if (beta_MC < 0.005 && beta_MC > 0.0001 && nxhits_MC > 20 && !trigger) {
467  std::cout<<"beta: "<<beta_MC<<" xhitsMC: "<<nxhits_MC<<" yhitsMC: "<<nyhits_MC<<"\n"
468  <<" Plane entry: "<<plane_entry<<" Cell entry: "<<cell_entry<<"\n"
469  <<" Plane exit: "<<plane_exit<<" Cell exit: "<<cell_exit<<"\n"
470  //<<" tiMC: "<<ti_MC
471  <<" xiMC: "<<xi_MC<<" yiMC: "<<yi_MC<<" ziMC: "<<zi_MC<<"\n"
472  //<<" tf_MC: "<<tf_MC
473  <<" xfMC: "<<xf_MC<<" yfMC: "<<yf_MC<<" zfMC: "<<zf_MC<<"\n"
474  <<" edep: "<<Edep_MC
475  <<" ti: "<<ti<<" tf: "<<tf<<" hitsAttach: "<<hitsAttach<<std::endl;
476 
477  }
478  */
479 } //end production function
480 
482 
483 }
484 
486 {
487  if ( hptr->Cell() > _Ymax - _deltaC ) return true;
488  else if ( hptr->Cell() < _deltaC ) return true;
489  else if ( hptr->Plane() < _deltaP ) return true;
490  else if ( hptr->Plane() > _Xmax - _deltaP ) return true;
491 
492  return false;
493 
494 }
495 
496 
497 //Note: Ts < Tm (Looking forwards). Time check is already done in the main loop.
499  art::Ptr<rb::CellHit> const & hptr2)
500 {
501  if (hptr1->TNS() > hptr2->TNS()+_deltaT) return false;
502  if (hptr2->TNS() > hptr1->TNS()+_deltaT) return false;
503  if (hptr1->Plane() > hptr2->Plane() ) {
504  if (hptr1->Plane() - hptr2->Plane() < _deltaP ) return true;
505  }
506  else if (hptr2->Plane() - hptr1->Plane() < _deltaP ) return true;
507  return false;
508 }
509 
511  art::Ptr<rb::CellHit> const & hptr2)
512 {
513  float dx = ((hptr1->Plane() )- (hptr2->Plane()) )*1.6795;
514  float dy = (hptr1->Cell() ) - (hptr2->Cell());
515  return sqrt(dx*dx+dy*dy);
516 }
517 
518 //It should return the index before T
519 unsigned zcl::SMMTriggerAna::IndexLocator(std::vector<art::Ptr<rb::CellHit> > & hit_ptrs,
520  float tns)
521 {
522  unsigned i = 0;
523  unsigned f = hit_ptrs.size()-1;
524 
525  if (tns < (hit_ptrs[0])->TNS()) return i;
526  if (tns > (hit_ptrs[f])->TNS()) return f;
527 
528  while (f>i+1) {
529  unsigned m = (f+i)/2;
530  if (tns >= (hit_ptrs[m])->TNS() ) i = m;
531  else f = m;
532  }
533  return i;
534 }
535 
float TNS() const
Definition: CellHit.h:46
T max(const caf::Proxy< T > &a, T b)
TString fin
Definition: Style.C:24
SubRunNumber_t subRun() const
Definition: Event.h:72
back track the reconstruction to the simulation
bool SurfAssign(art::Ptr< rb::CellHit > const &hptr)
int PdgCode() const
Definition: MCParticle.h:211
SMMTriggerAna(fhicl::ParameterSet const &pset)
const sim::ParticleNavigator & ParticleNavigator() const
Get a reference to the ParticleNavigator.
Definition: BackTracker.h:744
void LocalToWorld(const double *local, double *world) const
Definition: CellGeo.cxx:80
unsigned short Plane() const
Definition: CellHit.h:39
bool ViewCheck(art::Ptr< rb::CellHit > const &, art::Ptr< rb::CellHit > const &)
geo::View_t View() const
Definition: CellHit.h:41
const CellGeo * Cell(int icell) const
Definition: PlaneGeo.h:48
const char * p
Definition: xmltok.h:285
T sqrt(T number)
Definition: d0nt_math.hpp:156
std::vector< sim::FLSHit > ParticleToFLSHit(const int &trackID) const
All the FLSHits that were created by the track id trackID, sorted from most to least light...
double Mass() const
Definition: MCParticle.h:238
list_type::const_iterator const_iterator
void SortByTime(std::vector< art::Ptr< rb::CellHit > > &c)
Sort c in time order (earliest to latest).
Definition: CellHit.cxx:134
const PlaneGeo * Plane(unsigned int i) const
DEFINE_ART_MODULE(TestTMapFile)
Timing fit.
void analyze(const art::Event &evt)
Horizontal planes which measure Y.
Definition: PlaneGeo.h:29
int TrackId() const
Definition: MCParticle.h:209
unsigned short Cell() const
Definition: CellHit.h:40
Definition: Cand.cxx:23
float Dist(art::Ptr< rb::CellHit > const &, art::Ptr< rb::CellHit > const &)
double dy[NP][NC]
double dx[NP][NC]
#define local
Definition: gzguts.h:107
correl_yv Fill(-(dy[iP-1][iC-1]), hyv->GetBinContent(iP, iC))
double P(const int i=0) const
Definition: MCParticle.h:233
const double j
Definition: BetheBloch.cxx:29
EDAnalyzer(Table< Config > const &config)
Definition: EDAnalyzer.h:100
double t2
EventNumber_t event() const
Definition: EventID.h:116
::xsd::cxx::tree::string< char, simple_type > string
Definition: Database.h:154
T * make(ARGS...args) const
int16_t ADC(uint32_t i) const
Definition: RawDigit.cxx:58
bool getByLabel(std::string const &label, std::string const &productInstanceName, Handle< PROD > &result) const
Definition: DataViewImpl.h:344
Definition: event.h:1
void geom(int which=0)
Definition: geom.C:163
unsigned IndexLocator(std::vector< art::Ptr< rb::CellHit > > &hit_ptrs, float tns)
T min(const caf::Proxy< T > &a, T b)
RunNumber_t run() const
Definition: Event.h:77
Encapsulate the cell geometry.
Definition: CellGeo.h:25
bool ContaineAssign(art::Ptr< rb::CellHit > const &hptr)
EventID id() const
Definition: Event.h:56
Encapsulate the geometry of one entire detector (near, far, ndos)