FastMMStudy_module.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 // Class: FastMMStudy
3 // Module Type: analyzer
4 // File: FastMMStudy_module.cc
5 //
6 // Created at Nov 22 2015 by Enhao Song
7 ////////////////////////////////////////////////////////////////////////
8 
14 
15 #include "MCCheater/BackTracker.h"
16 #include "Calibrator/Calibrator.h"
17 #include "Simulation/Particle.h"
21 #include "Utilities/func/MathUtil.h"
22 #include "fhiclcpp/ParameterSet.h"
24 
25 #include "Geometry/Geometry.h"
27 #include "GeometryObjects/Geo.h"
29 
30 #include "NovaDAQConventions/DAQConventions.h"
31 #include "RecoBase/CellHit.h"
32 #include "RecoBase/RecoHit.h"
33 #include "RecoBase/Cluster.h"
34 #include "RecoBase/Track.h"
35 
36 
37 #include <algorithm>
38 #include <utility>
39 #include <cmath>
40 #include <iostream>
41 #include <string>
42 #include <vector>
43 #include <numeric>
44 #include <map>
45 
46 #include "TF1.h"
47 #include "TH2F.h"
48 #include "TMath.h"
49 #include "TProfile.h"
50 #include "TGraph.h"
51 #include "TPad.h"
52 #include "TCanvas.h"
53 #include "TROOT.h"
54 #include "TTree.h"
55 #include "TVector3.h"
56 #include "TRandom3.h"
57 
58 namespace zcl {
59  class FastMMStudy;
60 }
62  public:
63  explicit FastMMStudy(fhicl::ParameterSet const & p);
64  FastMMStudy(FastMMStudy const &) = delete;
65  FastMMStudy(FastMMStudy &&) = delete;
66  FastMMStudy & operator = (FastMMStudy const &) = delete;
67  FastMMStudy & operator = (FastMMStudy &&) = delete;
68  void analyze(art::Event const & e) override;
69  void beginJob() override;
70 
71  private:
74  //MC info:
76  double ti_MC, tf_MC;
77  double beta_MC;
78 
79  int _xMin;
80  int _xMax;
81  int _yMin;
82  int _yMax;
83  int _zMin;
84  int _zMax;
85  int _deltx;
86  int _delty;
87  int _deltz;
88  int _sigmaP;
89  int _trueHits;
90  int _nSatHits;
93  double _MaxOffCenter;
96  long long TX_max, TY_max, TX_min, TY_min;
98  TTree *tree_;
99  TTree *tree2_;
100  std::map<std::string, double> t_;
101  std::map<std::string, double> t2_;
103 
104  void tset(std::string const& branch_name, double const& value);
105  void tset2(std::string const& branch_name, double const& value);
106  void StdevCellsPerPlane(rb::Cluster const &hits, double &stdev_cells_per_plane_xview, double &stdev_cells_per_plane_yview ) const;
107  void NumberOfCellsPerLength(rb::Cluster const &hits,double tracklength_of_xview, double tracklength_of_yview, double &number_of_cells_per_length_xview,double &number_of_cells_per_length_yview) const;
108  float Distance(unsigned const &xcell1, unsigned const &ycell1, unsigned const &plane1,unsigned const &xcell2, unsigned const &ycell2 , unsigned const &plane2) const;
109  bool SurfAssign(rb::CellHit const & hit) const;
110  int GetSurfaceId(rb::CellHit const & hit) const;
111  double WeightedCenterCut(rb::Cluster const &hits,float PX_min,float PX_max,float PY_min,float PY_max,float CX_min, float CX_max, float CY_min, float CY_max, double &weighted_off_center_xx,double &weighted_off_center_xz,double &weighted_off_center_yy,double &weighted_off_center_yz) const;
112  double NumberOfHitsInOverlapPlanesCut(rb::Cluster const &hits,float PX_min,float PX_max,float PY_min,float PY_max ) const;
113  int NumberOfSurfaceHits(rb::Cluster const &hits) const;
114  void LinFit(const std::vector<double>& x, const std::vector<double>& y, double *fitpar);
115 };
117  :
118  EDAnalyzer(p),
119  _slicelabel(p.get<std::string>("slice_label")),
120  _sliceinstance(p.get<std::string>("slice_instance")),
121  _xMin(p.get<int>("x_min")),
122  _xMax(p.get<int>("x_max")),
123  _yMin(p.get<int>("y_min")),
124  _yMax(p.get<int>("y_max")),
125  _zMin(p.get<int>("z_min")),
126  _zMax(p.get<int>("z_max")),
127  _deltx(p.get<int>("x_delt")),
128  _delty(p.get<int>("y_delt")),
129  _deltz(p.get<int>("z_delt")),
130  _sigmaP(p.get<int>("sigma_P")),
131  _stdevcellsperplane(p.get<double>("stdev_cells_per_plane")),
132  _MaxHitsPerLength(p.get<double>("max_hits_per_length")),
133  _MaxOffCenter(p.get<double>("max_off_center")),
134  _MinNumberSurfaceHits(p.get<int>("min_number_surface_hits"))
135 {
136 }
137 
138 
140  // std::unique_ptr<std::vector<novaddt::TriggerDecision>>
141  // trigger_decisions(new std::vector<novaddt::TriggerDecision>);
145  e.getByLabel(_slicelabel, slices);
146  //e.getByLabel(_slicelabel, _sliceinstance, slices);
147 
148  //Get MC info first:
150  const sim::ParticleNavigator& pnav = bt->ParticleNavigator();
151  for (sim::ParticleNavigator::const_iterator i = pnav.begin(); i != pnav.end(); ++i) {
152  nxhits_MC = 0;
153  nyhits_MC = 0;
154  const sim::Particle *p = (*i).second;
155  if (p->PdgCode()!=42 ) continue;
156  // std::cout<<"Found monopole mctruth"<<std::endl;
157  const std::vector<sim::FLSHit>& flshits = bt->ParticleToFLSHit(p->TrackId());
158  unsigned ntruehits_Tot = flshits.size();
159  if ( ntruehits_Tot==0 ) continue;
160  // std::cout<<"Monopole mctruth hits more than 0!"<<std::endl;
161  double momentum2 = (p->P())*(p->P());
162  double mass2 = (p->Mass())*(p->Mass());
163  beta_MC = sqrt(momentum2 / (momentum2+mass2) );
164  ti_MC = flshits[0].GetEntryT();
165  tf_MC = flshits[0].GetExitT();
166  size_t planeID_i = flshits[0].GetPlaneID();
167  if (planeID_i%2) ++nxhits_MC;
168  else ++nyhits_MC;
169  for (unsigned h =1; h!= flshits.size(); ++h) {
170  double ti = flshits[h].GetEntryT();
171  double tf = flshits[h].GetExitT();
172  if (ti < ti_MC) {
173  ti_MC = ti;
174  }
175  if (tf > tf_MC) {
176  tf_MC = tf;
177  }
178  if (flshits[h].GetPlaneID() % 2) ++nxhits_MC;
179  else ++nyhits_MC;
180  }
181  tset2("betaMC",beta_MC);
182  tset2("run_number", e.run());
183  tset2("subrun_number", e.subRun());
184  tset2("event_number", e.event());
185  tree2_->Fill();
186  //1 monopole per event, so you can break out from the loop:
187  break;
188  }
189 
190  ///I want to remove isolated hits from beginning
191  // std::cout<<"number of slices you are getting: "<<slices->size()<<std::endl;
192  // std::cout<<"event start time: "<<event_time*15.625<<std::endl;
193  // std::cout<<"TDCT0: "<<TDCT0*15.625<<std::endl;
194  // std::cout<<"ti MC and tf MC: "<<ti_MC<<" "<<tf_MC<<std::endl;
195  for (unsigned i = 0; i!= slices->size(); ++i) {
196  const rb::Cluster& hits = (*slices)[i];
197  PX_max = 0;//P is for plane .enhao
198  CX_max = 0;
199  CY_max = 0;
200  PY_max = 0;
201  PX_min = 99999;
202  CX_min = 99999;
203  CY_min = 99999;
204  PY_min = 99999;
205  TX_max = hits.XCell(0)->TDC();
206  TY_max = TX_max;
207  TX_min = hits.XCell(0)->TDC();
208  TY_min = TX_min;
209  _trueHits = 0;
210  _nSatHits = 0;
211 
212  //int nb_hits = hits.NCell();
213  // std::cout<<"slice number = "<<i+1<<" number of hits: "<<nb_hits<<std::endl;
214  int entry_id = 0;
215  bool PassThrough = false;
216  bool Intrusion = false;
217 
218  int nx = 0;
219  int ny = 0;
220  double sumADC=0;
221  double stdevADC=0;
222  double sq_sum_ADC=0;
223 
224  std::vector<double> x_hit;
225  std::vector<double> y_hit;
226  std::vector<double> zy_hit;
227  std::vector<double> zx_hit;
228  //const double peakToBaselineOffset = 96;
229  for (auto const hit_ptr: hits.AllCells()) {
230  const rb::CellHit hit = *hit_ptr;
231  sumADC+=hit.ADC();
232  sq_sum_ADC+=hit.ADC()*hit.ADC();
233  if (hit.ADC()>3100) ++ _nSatHits;
234 
235  //X view hits
236  if (hit.View()== geo::kX) {
237  //std::cout<<"X hits"<<std::endl;
238  ++nx;
239  x_hit.push_back(hit.Cell());
240  zx_hit.push_back(hit.Plane());
241  if (PX_max < hit.Plane()) PX_max = hit.Plane();
242  if (PX_min > hit.Plane()) PX_min = hit.Plane();
243  if (CX_max < hit.Cell()) CX_max = hit.Cell();
244  if (CX_min > hit.Cell()) CX_min = hit.Cell();
245  if (TX_max < hit.TDC()) TX_max = hit.TDC();
246  if (TX_min > hit.TDC()) TX_min = hit.TDC();
247 
248  if (!Intrusion) {
249  if (hit.Cell() < _xMin+_deltx) {
250  entry_id = 1;
251  Intrusion = true;
252  }
253  else if (hit.Cell()> _xMax-_deltx) {
254  entry_id = 2;
255  Intrusion = true;
256  }
257  }
258  else if (!PassThrough) {
259  if (hit.Cell() < _xMin+_deltx && entry_id != 1) PassThrough = true;
260  else if (hit.Cell()> _xMax-_deltx && entry_id != 2) PassThrough = true;
261  }
262  }
263  //Y view hits
264  else {
265  //std::cout<<"Y hits"<<std::endl;
266  ++ny;
267  y_hit.push_back(hit.Cell());
268  zy_hit.push_back(hit.Plane());
269  if (PY_max < hit.Plane()) PY_max = hit.Plane();
270  if (PY_min > hit.Plane()) PY_min = hit.Plane();
271  if (CY_max < hit.Cell()) CY_max = hit.Cell();
272  if (CY_min > hit.Cell()) CY_min = hit.Cell();
273  if (TY_max < hit.TDC() ) TY_max = hit.TDC();
274  if (TY_min > hit.TDC() ) TY_min = hit.TDC();
275 
276  if (!Intrusion) {
277  if (hit.Cell() < _yMin+_delty) {
278  entry_id = 3;
279  Intrusion = true;
280  }
281  else if (hit.Cell()> _yMax-_delty) {
282  entry_id = 4;
283  Intrusion = true;
284  }
285  }
286  else if (!PassThrough) {
287  if (hit.Cell() < _yMin+_delty && entry_id != 3) PassThrough = true;
288  else if (hit.Cell()> _yMax-_delty && entry_id != 4) PassThrough = true;
289  }
290  }
291  //No matter which view:
292  if (!Intrusion) {
293  if (hit.Plane() < _zMin+_deltz) {
294  entry_id = 5;
295  Intrusion = true;
296  }
297  else if (hit.Plane() > _zMax-_deltz) {
298  entry_id = 6;
299  Intrusion = true;
300  }
301  }
302  else if (!PassThrough) {
303  if (hit.Plane() < _zMin+_deltz && entry_id != 5) PassThrough=true;
304  else if (hit.Plane() > _zMax-_deltz && entry_id != 6) PassThrough=true;
305  }//I think there is a bug if this hit is at corner, it's cell value will set x intrusion true and entry id =1 and then it will pass through if it's plane val is 0 . by enhao
306  }
307 
308  //Min-hits on both view:
309  // std::cout<<"xhits number : "<<nx<<" yhits number : "<<ny<<std::endl;
310  double preselection=0;
311  if (_trueHits>1 || nx<3 || ny<3||!PassThrough||(std::min(PX_max,PY_max) < std::max(PX_min,PY_min)+_sigmaP)||(std::min(TX_max,TY_max) < std::max(TX_min,TY_min))) { //select cosmic I should add a handler in fcl file.
312  continue;
313  }
314  // if (_trueHits<1 || nx<3 || ny<3||!PassThrough||(std::min(PX_max,PY_max) < std::max(PX_min,PY_min)+_sigmaP)||(std::min(TX_max,TY_max) < std::max(TX_min,TY_min))) {//select MC
315  //tset("betaMC",beta_MC);
316  //tset("trueHits",_trueHits);
317  //tset("preselection",preselection);
318  // if(_trueHits>0) {
319  // tree_->Fill();
320  //std::cout<<"event id: "<<e.event()<<" trueHits: "<<_trueHits<<" nx: "<<nx <<" ny: "<< ny<<" Pass Through: "<<PassThrough<<" Pmax: "<<std::min(PX_max,PY_max)<<" Pmin: "<< std::max(PX_min,PY_min)<<" Tmax: "<<std::min(TX_max,TY_max) <<" Tmin:"<< std::max(TX_min,TY_min)<<std::endl;
321  // }
322  //continue;
323  //}
324 
325  //min number of surface hits test:
326  //cut if (NumberOfSurfaceHits(hits)<_MinNumberSurfaceHits) continue;
327  float tracklength =Distance(CX_max, CY_max, PX_max, CX_min, CY_min, PX_min);
328  double tracklength_of_xview=Distance(CX_max, 0, PX_max, CX_min, 0, PX_min);
329  double tracklength_of_yview=Distance(0, CY_max, PY_max, 0, CY_min, PY_min);
330  double number_of_cells_per_length_xview=0;
331  double number_of_cells_per_length_yview=0;
332  NumberOfCellsPerLength(hits,tracklength_of_xview,tracklength_of_yview,number_of_cells_per_length_xview,number_of_cells_per_length_yview);
333  double fitpar[3];
334  LinFit(x_hit, zx_hit, fitpar);
335  double r2x = fitpar[2];
336  LinFit(y_hit, zy_hit, fitpar);
337  double r2y = fitpar[2];
338  //std::cout<<"hits/length is "<<hits.size()/tracklength<<std::endl;
339  //cut if (hits.size()/tracklength>_MaxHitsPerLength) continue;
340  //I didn't add minhitsperlength because there might be noise hits make tracklength too long.
341 
342  //cut if (StdevCellsPerPlane(hits)==false) continue;
343  //cut if(WeightedCenterCut(hits, PX_min, PX_max, PY_min, PY_max, CX_min, CX_max, CY_min, CY_max )==false) continue;
344  // gROOT->SetBatch(kTRUE);
345  stdevADC = std::sqrt( sq_sum_ADC*1.0/(nx+ny) - sumADC*1.0/(nx+ny)*sumADC*1.0/(nx+ny));
346 
347  // Fill Tree
348  preselection=1;
349  for (auto & branch : t_)
350  branch.second = branch_default_value_;
351  double weighted_off_center_xx=0, weighted_off_center_xz=0, weighted_off_center_yy=0, weighted_off_center_yz=0;
352  double stdev_cells_per_plane_xview=0, stdev_cells_per_plane_yview=0;
353  tset("run_number", e.run());
354  tset("subrun_number", e.subRun());
355  tset("event_number", e.event());
356  tset("slice_number",i);
357  // tset("event_length", event_length);
358  tset("number_of_surface_hits",NumberOfSurfaceHits(hits));
359  tset("number_of_hits_per_length",hits.NCell()/tracklength);
360  tset("number_of_cells_per_length_xview",number_of_cells_per_length_xview);
361  tset("number_of_cells_per_length_yview",number_of_cells_per_length_yview);
362  tset("track_length",tracklength);
363  StdevCellsPerPlane(hits, stdev_cells_per_plane_xview, stdev_cells_per_plane_yview);
364  tset("stdev_cells_per_plane_xview",stdev_cells_per_plane_xview);
365  tset("stdev_cells_per_plane_yview",stdev_cells_per_plane_yview);
366  tset("weighted_off_center",WeightedCenterCut(hits, PX_min, PX_max, PY_min, PY_max, CX_min, CX_max, CY_min, CY_max, weighted_off_center_xx, weighted_off_center_xz, weighted_off_center_yy, weighted_off_center_yz ));
367  tset("weighted_off_center_xx",weighted_off_center_xx);
368  tset("weighted_off_center_xz",weighted_off_center_xz);
369  tset("weighted_off_center_yy",weighted_off_center_yy);
370  tset("weighted_off_center_yz",weighted_off_center_yz);
371  tset("meanADC",sumADC*1.0/(nx+ny));
372  tset("stdevADC",stdevADC);
373  tset("nxhits",nx);
374  tset("nyhits",ny);
375  tset("percent_of_hits_in_overlap_planes",NumberOfHitsInOverlapPlanesCut(hits,PX_min,PX_max,PY_min,PY_max));
376  tset("trueHits",_trueHits);
377  tset("betaMC",beta_MC);
378  tset("xhitsMC",nxhits_MC);
379  tset("yhitsMC",nyhits_MC);
380  tset("deltaTDC",std::min(TX_max,TY_max) - std::max(TX_min,TY_min));
382  tset("preselection",preselection);
383  tset("nSatHits",_nSatHits);
384  tset("r2x",r2x);
385  tset("r2y",r2y);
386  tset("dPX",PX_max-PX_min+1);
387  tset("dPY",PY_max-PY_min+1);
388  tset("dCX",CX_max-CX_min+1);
389  tset("dCY",CY_max-CY_min+1);
390  // std::cout<<"event id: "<<e.event()<<" trueHits: "<<_trueHits<<" nx: "<<nx <<" ny: "<< ny<<" Pass Through: "<<PassThrough<<" Pmax: "<<std::min(PX_max,PY_max)<<" Pmin: "<< std::max(PX_min,PY_min)<<" Tmax: "<<std::min(TX_max,TY_max) <<" Tmin:"<< std::max(TX_min,TY_min)<<std::endl;
391  tree_->Fill();
392  //if(_trueHits>0) {
393  // tree_->Fill();
394  //}
395  }
396 }
397 
399 {
400  tree_ = tfs_->make<TTree>("Event", "Monopole Event Tree");
401  tree2_ = tfs_->make<TTree>("MC", "Monopole MC Tree");
402  std::vector<std::string> branch_names =
403  { "run_number", "subrun_number", "event_number", "slice_number","number_of_hits_per_length","number_of_cells_per_length_xview","number_of_cells_per_length_yview","track_length","stdev_cells_per_plane_xview","stdev_cells_per_plane_yview", "weighted_off_center", "weighted_off_center_xx", "weighted_off_center_xz", "weighted_off_center_yy", "weighted_off_center_yz", "percent_of_hits_in_overlap_planes","number_of_surface_hits","betaMC","xhitsMC","yhitsMC","trueHits","preselection","meanADC","stdevADC","nxhits","nyhits","deltaTDC","deltaP","nSatHits","r2x","r2y","dPX","dPY","dCX","dCY"};
404  for (auto const& name : branch_names)
405  tree_->Branch(name.c_str(), &t_[name], (name + "/D").c_str());
406 
407  std::vector<std::string> branch_names2 =
408  { "run_number", "subrun_number", "event_number", "betaMC"};
409  for (auto const& name : branch_names2)
410  tree2_->Branch(name.c_str(), &t2_[name], (name + "/D").c_str());
411 }
412 
413 void zcl::FastMMStudy::tset(std::string const& branch_name, double const& value)
414 {
415  auto it = t_.find(branch_name);
416  if (it == t_.end())
417  {
418  std::cerr << "\n\t Branch " << branch_name << " does not exist! \n"
419  << std::endl;
420  assert(false);
421  }
422 
423  it->second = value;
424 }
425 
426 void zcl::FastMMStudy::tset2(std::string const& branch_name, double const& value)
427 {
428  auto it = t2_.find(branch_name);
429  if (it == t2_.end())
430  {
431  std::cerr << "\n\t Branch " << branch_name << " does not exist! \n"
432  << std::endl;
433  assert(false);
434  }
435 
436  it->second = value;
437 }
438 void zcl::FastMMStudy::NumberOfCellsPerLength(rb::Cluster const &hits,double tracklength_of_xview, double tracklength_of_yview, double &number_of_cells_per_length_xview,double &number_of_cells_per_length_yview) const{
439  //Maybe use hits instead of cells?
440  std::vector<std::vector<int>> x_hits_plane(PX_max-PX_min+1,std::vector<int>(0));
441  std::vector<std::vector<int>> y_hits_plane(PY_max-PY_min+1,std::vector<int>(0));
442  for (auto const hit_ptr: hits.AllCells()) {
443  const rb::CellHit hit = *hit_ptr;
444  if (hit.View() == geo::kX) {
445  //std::cout<<"X hits"<<std::endl;
446  x_hits_plane[hit.Plane()-PX_min].push_back(hit.Cell());
447  }
448  if (hit.View() == geo::kY) {
449  //std::cout<<"Y hits"<<std::endl;
450  y_hits_plane[hit.Plane()-PY_min].push_back(hit.Cell());
451  }
452  }
453  //following is to get unique number of cells hit per plane
454  std::vector<int> x_cells_plane(PX_max-PX_min+1,0);
455  std::vector<int> y_cells_plane(PY_max-PY_min+1,0);
456  for (unsigned i=0;i!=x_hits_plane.size();i++){
457  std::sort(x_hits_plane[i].begin(),x_hits_plane[i].end());
458  std::vector<int>::iterator it;
459  it = std::unique (x_hits_plane[i].begin(),x_hits_plane[i].end());
460  x_hits_plane[i].resize(std::distance(x_hits_plane[i].begin(),it));
461  x_cells_plane[i]=x_hits_plane[i].size();
462  }
463  for (unsigned i=0;i!=y_hits_plane.size();i++){
464  std::sort(y_hits_plane[i].begin(),y_hits_plane[i].end());
465  std::vector<int>::iterator it;
466  it = std::unique (y_hits_plane[i].begin(),y_hits_plane[i].end());
467  y_hits_plane[i].resize(std::distance(y_hits_plane[i].begin(),it));
468  y_cells_plane[i]=y_hits_plane[i].size();
469  }
470  //following is to get number of cells per track length in x view and y view
471  int sum_of_cells=0;
472  for (unsigned i=0;i!=x_cells_plane.size();i++){
473  sum_of_cells+=x_cells_plane[i];
474  }
475  number_of_cells_per_length_xview=sum_of_cells/tracklength_of_xview;
476  sum_of_cells=0;
477  for (unsigned i=0;i!=y_cells_plane.size();i++){
478  sum_of_cells+=y_cells_plane[i];
479  }
480  number_of_cells_per_length_yview=sum_of_cells/tracklength_of_yview;
481 
482 }
483 void zcl::FastMMStudy::StdevCellsPerPlane(rb::Cluster const &hits, double &stdev_cells_per_plane_xview, double &stdev_cells_per_plane_yview ) const{
484  //fluctuation of number of cells of each plane check by Enhao
485  //I need to use cells not hits, because there might be many hits in same cell, and the cosmic shower has many different cells hits
486  //Maybe use hits instead of cells?
487  std::vector<std::vector<int>> x_hits_plane(PX_max-PX_min+1,std::vector<int>(0));
488  std::vector<std::vector<int>> y_hits_plane(PY_max-PY_min+1,std::vector<int>(0));
489  for (auto const hit_ptr: hits.AllCells()) {
490  const rb::CellHit hit = *hit_ptr;
491  if (hit.View() == geo::kX) {
492  //std::cout<<"X hits"<<std::endl;
493  x_hits_plane[hit.Plane()-PX_min].push_back(hit.Cell());
494  }
495  if (hit.View() == geo::kY) {
496  //std::cout<<"Y hits"<<std::endl;
497  y_hits_plane[hit.Plane()-PY_min].push_back(hit.Cell());
498  }
499  }
500  double sum =0;
501  double mean = 0;
502  double sq_sum = 0;
503  double stdev = 0;
504  //following is to get unique number of cells hit per plane
505  std::vector<int> x_cells_plane(PX_max-PX_min+1,0);
506  std::vector<int> y_cells_plane(PY_max-PY_min+1,0);
507  for (unsigned i=0;i!=x_hits_plane.size();i++){
508  std::sort(x_hits_plane[i].begin(),x_hits_plane[i].end());
509  std::vector<int>::iterator it;
510  it = std::unique (x_hits_plane[i].begin(),x_hits_plane[i].end());
511  x_hits_plane[i].resize(std::distance(x_hits_plane[i].begin(),it));
512  x_cells_plane[i]=x_hits_plane[i].size();
513  }
514  for (unsigned i=0;i!=y_hits_plane.size();i++){
515  std::sort(y_hits_plane[i].begin(),y_hits_plane[i].end());
516  std::vector<int>::iterator it;
517  it = std::unique (y_hits_plane[i].begin(),y_hits_plane[i].end());
518  y_hits_plane[i].resize(std::distance(y_hits_plane[i].begin(),it));
519  y_cells_plane[i]=y_hits_plane[i].size();
520  }
521  sum = std::accumulate(x_cells_plane.begin(),x_cells_plane.end(),0.0);
522  mean = sum / x_cells_plane.size();
523  sq_sum = std::inner_product(x_cells_plane.begin(),x_cells_plane.end(),x_cells_plane.begin(),0.0);
524  stdev = std::sqrt(sq_sum / x_cells_plane.size()-mean * mean);
525  // std::cout<<"x stdev is "<<stdev<<" "<<stdev/mean<<std::endl;
526  //if (stdev/mean > _stdevcellsperplane) return false;
527  stdev_cells_per_plane_xview=stdev/mean;
528  sum = std::accumulate(y_cells_plane.begin(),y_cells_plane.end(),0.0);
529  mean = sum / y_cells_plane.size();
530  sq_sum = std::inner_product(y_cells_plane.begin(),y_cells_plane.end(),y_cells_plane.begin(),0.0);
531  stdev = std::sqrt(sq_sum / y_cells_plane.size()-mean * mean);
532  stdev_cells_per_plane_yview=stdev/mean;
533  // std::cout<<"y stdev is "<<stdev<<" "<<stdev/mean<<std::endl;
534  // std::cout<<"sum of x and y stdev is "<<sum_stdev<<std::endl;
535  //if (stdev/mean > _stdevcellsperplane) return false;
536 }
537 float zcl::FastMMStudy::Distance( unsigned const &xcell1, unsigned const &ycell1, unsigned const &plane1,unsigned const &xcell2, unsigned const &ycell2 , unsigned const &plane2) const {
538  return std::sqrt((plane1-plane2)*(plane1-plane2)*2.82072+(xcell1-xcell2)*(xcell1-xcell2)+(ycell1-ycell2)*(ycell1-ycell2));
539  //This part I should use Martin's smt::Constant::cell width, plane width
540 }
541 
543  //Check if this is close to surface
544  if ( hit.Cell() > _xMax - _deltx ) return true;
545  else if ( hit.Cell() < _xMin+_deltx ) return true;
546  else if ( hit.Plane() < _zMin+_deltz ) return true;
547  else if ( hit.Plane() > _zMax - _deltz ) return true;
548 
549  return false;
550 }
551 
552 
554  int entry_id=0;
555  if (hit.View() == geo::kX) {
556  if (hit.Cell() < _xMin+_deltx) {
557  entry_id = 1;
558  }
559  else if (hit.Cell()> _xMax-_deltx) {
560  entry_id = 2;
561  }
562  }
563  //Y view hits
564  else {
565  if (hit.Cell() < _yMin+_delty) {
566  entry_id = 3;
567  }
568  else if (hit.Cell()> _yMax-_delty) {
569  entry_id = 4;
570  }
571  }
572  //No matter which view:
573  if (hit.Plane() < _zMin+_deltz) {
574  entry_id = 5;
575  }
576  else if (hit.Plane() > _zMax-_deltz) {
577  entry_id = 6;
578  }
579  return entry_id;
580 
581 }
582 
584  int number_of_surface_hits=0;
585  for(unsigned i=0;i!=hits.NCell();++i){
586  if(SurfAssign(*(hits.Cell(i)))==true) number_of_surface_hits++;
587  }
588  return number_of_surface_hits;
589 }
590 double zcl::FastMMStudy::WeightedCenterCut(rb::Cluster const &hits,float PX_min,float PX_max,float PY_min,float PY_max,float CX_min, float CX_max, float CY_min, float CY_max, double &weighted_off_center_xx,double &weighted_off_center_xz,double &weighted_off_center_yy,double &weighted_off_center_yz ) const{
591  float Weighted_PX_Center=0.5,Weighted_CX_Center=0.5,Weighted_PY_Center=0.5,Weighted_CY_Center=0.5,SumOfXHitsADC=0,SumOfYHitsADC=0;
592  for (auto const hit_ptr: hits.AllCells()) {
593  const rb::CellHit hit = *hit_ptr;
594  if (hit.View() == geo::kX) {
595  SumOfXHitsADC+=hit.ADC();
596  Weighted_PX_Center+=hit.ADC()*(hit.Plane()-PX_min);
597  Weighted_CX_Center+=hit.ADC()*(hit.Cell()-CX_min);
598  }
599  if (hit.View() == geo::kY) {
600  SumOfYHitsADC+=hit.ADC();
601  Weighted_PY_Center+=hit.ADC()*(hit.Plane()-PY_min);
602  Weighted_CY_Center+=hit.ADC()*(hit.Cell()-CY_min);
603  }
604  }
605  if(PX_max!=PX_min) Weighted_PX_Center=Weighted_PX_Center/SumOfXHitsADC/(PX_max-PX_min);
606  if(PY_max!=PY_min) Weighted_PY_Center=Weighted_PY_Center/SumOfYHitsADC/(PY_max-PY_min);
607  if(CX_max!=CX_min) Weighted_CX_Center=Weighted_CX_Center/SumOfXHitsADC/(CX_max-CX_min);
608  if(CY_max!=CY_min) Weighted_CY_Center=Weighted_CY_Center/SumOfYHitsADC/(CY_max-CY_min);
609  // std::cout<<"weighted px, py, cx, cy center is "<<Weighted_PX_Center<<" "<<Weighted_PY_Center<<" "<<Weighted_CX_Center<<" "<<Weighted_CY_Center<<std::endl;
610  // if(std::abs(Weighted_PX_Center-0.5)>_MaxOffCenter||std::abs(Weighted_PY_Center-0.5)>_MaxOffCenter||std::abs(Weighted_CX_Center-0.5)>_MaxOffCenter||std::abs(Weighted_CY_Center-0.5)>_MaxOffCenter) return false;
611  //else return true;
612  weighted_off_center_xx=std::abs(Weighted_CX_Center-0.5);
613  weighted_off_center_xz=std::abs(Weighted_PX_Center-0.5);
614  weighted_off_center_yy=std::abs(Weighted_CY_Center-0.5);
615  weighted_off_center_yz=std::abs(Weighted_PY_Center-0.5);
616  return std::max(std::max(std::abs(Weighted_PX_Center-0.5),std::abs(Weighted_PY_Center-0.5)),std::max(std::abs(Weighted_CX_Center-0.5),std::abs(Weighted_CY_Center-0.5)));
617 }
618 
620  int max=std::min(PX_max,PY_max);
621  int min=std::max(PX_min,PY_min);
622  double number_hits_in_overlap_planes=0;
623  for (auto const hit_ptr: hits.AllCells()) {
624  const rb::CellHit hit = *hit_ptr;
625  if(hit.Plane()<=max && hit.Plane()>=min) number_hits_in_overlap_planes++;
626  }
627 
628  return number_hits_in_overlap_planes/hits.NCell();
629 
630 }
631 
632 void zcl::FastMMStudy::LinFit(const std::vector<double>& x_val, const std::vector<double>& y_val, double *fitpar){
633  // http://en.wikipedia.org/wiki/Simple_linear_regression
634  const auto n = x_val.size();
635  const auto x = (std::accumulate(x_val.begin(), x_val.end(), 0.0))/n; // <x>
636  const auto y = (std::accumulate(y_val.begin(), y_val.end(), 0.0))/n; // <y>
637  const auto xx = (std::inner_product(x_val.begin(), x_val.end(), x_val.begin(), 0.0))/n; // <xx>
638  const auto yy = (std::inner_product(y_val.begin(), y_val.end(), y_val.begin(), 0.0))/n; // <yy>
639  const auto xy = (std::inner_product(x_val.begin(), x_val.end(), y_val.begin(), 0.0))/n; // <xy>
640 
641  const auto b = (xy - x*y)/(xx - x*x); // slope
642  const auto a = y - b*x; // intercept
643  const auto r = (xy - x*y)/sqrt((xx - x*x)*(yy - y*y)); // Rxy - coeffcient of determination
644  fitpar[0] = a;
645  fitpar[1] = b;
646  fitpar[2] = r*r;
647 
648 }
649 
std::map< std::string, double > t_
void StdevCellsPerPlane(rb::Cluster const &hits, double &stdev_cells_per_plane_xview, double &stdev_cells_per_plane_yview) const
T max(const caf::Proxy< T > &a, T b)
const XML_Char * name
Definition: expat.h:151
SubRunNumber_t subRun() const
Definition: Event.h:72
back track the reconstruction to the simulation
int PdgCode() const
Definition: MCParticle.h:211
def stdev(lst)
Definition: HandyFuncs.py:286
unsigned int NCell(geo::View_t view) const
Number of cells in view view.
Definition: Cluster.cxx:134
Double_t xx
Definition: macro.C:12
set< int >::iterator it
art::Ptr< rb::CellHit > XCell(unsigned int xIdx) const
Get the ith cell in the x-view.
Definition: Cluster.cxx:157
int32_t TDC() const
The time of the last baseline sample.
Definition: RawDigit.h:94
void analyze(art::Event const &e) override
FastMMStudy & operator=(FastMMStudy const &)=delete
const sim::ParticleNavigator & ParticleNavigator() const
Get a reference to the ParticleNavigator.
Definition: BackTracker.h:744
std::string _sliceinstance
void LinFit(const std::vector< double > &x, const std::vector< double > &y, double *fitpar)
unsigned short Plane() const
Definition: CellHit.h:39
geo::View_t View() const
Definition: CellHit.h:41
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
Vertical planes which measure X.
Definition: PlaneGeo.h:28
void tset(std::string const &branch_name, double const &value)
A collection of associated CellHits.
Definition: Cluster.h:47
OStream cerr
Definition: OStream.cxx:7
double WeightedCenterCut(rb::Cluster const &hits, float PX_min, float PX_max, float PY_min, float PY_max, float CX_min, float CX_max, float CY_min, float CY_max, double &weighted_off_center_xx, double &weighted_off_center_xz, double &weighted_off_center_yy, double &weighted_off_center_yz) const
double NumberOfHitsInOverlapPlanesCut(rb::Cluster const &hits, float PX_min, float PX_max, float PY_min, float PY_max) const
DEFINE_ART_MODULE(TestTMapFile)
Timing fit.
unsigned distance(const T &t1, const T &t2)
float abs(float number)
Definition: d0nt_math.hpp:39
Horizontal planes which measure Y.
Definition: PlaneGeo.h:29
art::PtrVector< rb::CellHit > AllCells() const
Get all cells from both views.
Definition: Cluster.cxx:180
int TrackId() const
Definition: MCParticle.h:209
bool SurfAssign(rb::CellHit const &hit) const
unsigned short Cell() const
Definition: CellHit.h:40
CDPStorage service.
void hits()
Definition: readHits.C:15
art::ServiceHandle< art::TFileService > tfs_
const XML_Char int const XML_Char * value
Definition: expat.h:331
const double a
double P(const int i=0) const
Definition: MCParticle.h:233
Collect Geo headers and supply basic geometry functions.
EventNumber_t event() const
Definition: Event.h:67
EDAnalyzer(Table< Config > const &config)
Definition: EDAnalyzer.h:100
static float min(const float a, const float b, const float c)
Definition: absgeo.cxx:45
void NumberOfCellsPerLength(rb::Cluster const &hits, double tracklength_of_xview, double tracklength_of_yview, double &number_of_cells_per_length_xview, double &number_of_cells_per_length_yview) const
art::Ptr< rb::CellHit > Cell(geo::View_t view, unsigned int viewIdx) const
Get the ith cell from view view.
Definition: Cluster.cxx:145
::xsd::cxx::tree::string< char, simple_type > string
Definition: Database.h:154
int NumberOfSurfaceHits(rb::Cluster const &hits) const
A rawdata::RawDigit with channel information decoded.
Definition: CellHit.h:27
T * make(ARGS...args) const
int16_t ADC(uint32_t i) const
Definition: RawDigit.cxx:58
void tset2(std::string const &branch_name, double const &value)
bool getByLabel(std::string const &label, std::string const &productInstanceName, Handle< PROD > &result) const
Definition: DataViewImpl.h:344
Definition: structs.h:12
void geom(int which=0)
Definition: geom.C:163
const hit & b
Definition: hits.cxx:21
int GetSurfaceId(rb::CellHit const &hit) const
assert(nhit_max >=nhit_nbins)
TRandom3 r(0)
void beginJob() override
T min(const caf::Proxy< T > &a, T b)
float Distance(unsigned const &xcell1, unsigned const &ycell1, unsigned const &plane1, unsigned const &xcell2, unsigned const &ycell2, unsigned const &plane2) const
Double_t sum
Definition: plot.C:31
Float_t e
Definition: plot.C:35
RunNumber_t run() const
Definition: Event.h:77
std::map< std::string, double > t2_
T max(sqlite3 *const db, std::string const &table_name, std::string const &column_name)
Definition: statistics.h:68
FastMMStudy(fhicl::ParameterSet const &p)
Encapsulate the geometry of one entire detector (near, far, ndos)