FmmTriggerEvd_module.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 // Class: FmmTriggerEvd
3 // Module Type: producer
4 // File: FmmTriggerEvd_module.cc
5 //
6 // Generated at 21/01/2016 by Enhao Song
7 // this works like fast monopole trigger and filter then produce the
8 // slice that triggered
9 ////////////////////////////////////////////////////////////////////////
15 
16 #include "MCCheater/BackTracker.h"
17 //#include "Calibrator/Calibrator.h"
18 #include "Simulation/Particle.h"
22 #include "Utilities/func/MathUtil.h"
23 #include "fhiclcpp/ParameterSet.h"
25 
26 #include "Geometry/Geometry.h"
28 #include "GeometryObjects/Geo.h"
30 
31 #include "NovaDAQConventions/DAQConventions.h"
32 #include "RecoBase/CellHit.h"
33 #include "RecoBase/RecoHit.h"
34 #include "RecoBase/Cluster.h"
35 #include "RecoBase/Track.h"
36 
37 #include "TVector3.h"
38 #include "TTree.h"
39 #include "TProfile.h"
40 
41 #include <algorithm>
42 #include <utility>
43 #include <cmath>
44 #include <iostream>
45 
46 namespace zcl {
47  class FmmTriggerEvd;
48 }
49 
51  public:
52  explicit FmmTriggerEvd(fhicl::ParameterSet const & pset);
53  virtual ~FmmTriggerEvd();
54 
55  void produce(art::Event & evt);
56 
57  private:
58  std::string fClusterInput; ///< Input folder from cluster reco
59  //Each entry is corresponding to 1 reconstructed slice
60 
62 
63  int _Nslices;
67  double _dpx, _dpy, _dcx, _dcy,_dp;
68  double _rmsADC, _meanADC;
69  double _cx, _cy, _cz;
70  double _cxW, _cyW, _czW;
71 
72  //added by Enhao
78  double _cxz,_cyz;
79  double _cxzW,_cyzW;
81 
82 
83  double _tmin, _tmax; //The time window of the slice
84 
85  const int _xMax = 383;
86  const int _yMax = 383;
87  const int _zMax = 895;
88  const int _xdelt = 35;
89  const int _ydelt = 35;
90  const int _zdelt = 15;
91 
92  double RMS(std::vector<double> & v, double & mean);
93  bool IsSurfaceHit(int plane, int cell) const;
94  void NumberOfCellsPerLength(rb::Cluster const &slice,double tracklength_of_xview, double tracklength_of_yview, double &number_of_cells_per_length_xview,double &number_of_cells_per_length_yview)const;
95  void StdevCellsPerPlane(rb::Cluster const &slice, double &stdev_cells_per_plane_xview, double &stdev_cells_per_plane_yview ) const;
96  double NumberOfHitsInOverlapPlanesCut(rb::Cluster const &slice,float PX_min,float PX_max,float PY_min,float PY_max ) const;
97 };
98 
100  fClusterInput (pset.get< std::string > ("ClusterInput") )
101 {
102  produces< std::vector<rb::Cluster> >();
103 }
104 
106 {
107  // Clean up dynamic memory and other resources here.
108 }
109 
111 {
112  evtID = evt.id().event();
113  subRunID = evt.subRun();
114  RunID = evt.run();
115 
117  // art::ServiceHandle<calib::Calibrator> calib;
118 
119  //=====================================================================================
120  //Now, start pseudo trigger:
121 
122  double TX_max, TY_max, TX_min, TY_min;
123 
125  evt.getByLabel(fClusterInput, slices);
126 
127  std::unique_ptr< std::vector<rb::Cluster> > clustercol(new std::vector<rb::Cluster>);
128 
129  _Nslices = slices->size();
130  std::cout<<"number of slices: "<<_Nslices<<std::endl;
131 
132  //Instead of splitting the hits into 2 views, just directly take the entire 3D slice:
133  for (unsigned sliceIdx = 0; sliceIdx != slices->size(); ++sliceIdx) {
134  std::cout<<"slice ID: "<<sliceIdx<<std::endl;
135  const rb::Cluster& slice = (*slices)[sliceIdx];
136  std::vector<art::Ptr<rb::CellHit> > Xhitlist, Yhitlist;
137  CX_min = 400;
138  CX_max = 0;
139  CY_min = 400;
140  CY_max = 0;
141  PX_max = 0;
142  PY_max = 0;
143  PX_min = 999;
144  PY_min = 999;
145  TX_max = -9e7;
146  TY_max = -9e7;
147  TX_min = 9e9;
148  TY_min = 9e9;
149 
150  _penetrated = false;
151  _nxHits = 0;
152  _nyHits = 0;
153  _meanADC = 0;
154  _rmsADC = 0;
155  _cx = 0;
156  _cy = 0;
157  _cz = 0;
158  _cxW = 0;
159  _cyW = 0;
160  _czW = 0;
161 
162  //added by enhao
173  _cxz=0;_cyz=0;
174  _cxzW=0;_cyzW=0;
176 
177  bool Intrusion = false;
178  int entry_id = -1;
179  std::vector<double> adcList;
180  double ADCx = 0.;
181  double ADCy = 0.;
182 
183  for (unsigned i = 0; i != slice.NCell(); ++i) {
184  const art::Ptr<rb::CellHit> chit = slice.Cell(i);
185 
186  double tns = chit->TNS();
187  int c = chit->Cell();
188  int p = chit->Plane();
189  adcList.emplace_back(chit->ADC());
190 
191  if(IsSurfaceHit(p,c)==true) _number_of_surface_hits++;
192  _meanADC += chit->ADC();
193  _cz += p;
194  _czW += p * (chit->ADC());
195 
196  if (chit->View()==geo::kX) { //X view
197  Xhitlist.emplace_back(chit);
198  ADCx += chit->ADC();
199  ++_nxHits;
200  _cx += c;
201  _cxW += c*(chit->ADC());
202  _cxz+=p;
203  _cxzW+=p*(chit->ADC());
204 
205  if (PX_max < p) PX_max = p;
206  if (PX_min > p) PX_min = p;
207  if (CX_min > c) CX_min = c;
208  if (CX_max < c) CX_max = c;
209  if (TX_max < tns) TX_max = tns;
210  if (TX_min > tns) TX_min = tns;
211 
212  if (!Intrusion) {
213  if (c < _xdelt){
214  entry_id = 1;
215  Intrusion = true;
216  }
217  else if (c > _xMax - _xdelt){
218  entry_id = 2;
219  Intrusion = true;
220  }
221  }
222  else if (!_penetrated) {
223  if (c < _xdelt && entry_id!=1) _penetrated = true;
224  else if (c > _xMax-_xdelt && entry_id!=2) _penetrated = true;
225  }
226  }
227 
228  else { //Y view
229  Yhitlist.emplace_back(chit);
230  ADCy += chit->ADC();
231  ++_nyHits;
232  _cy += c;
233  _cyW += c*(chit->ADC());
234  _cyz+=p;
235  _cyzW+=p*(chit->ADC());
236 
237  if (PY_max < p) PY_max = p;
238  if (PY_min > p) PY_min = p;
239  if (CY_min > c) CY_min = c;
240  if (CY_max < c) CY_max = c;
241  if (TY_max < tns) TY_max = tns;
242  if (TY_min > tns) TY_min = tns;
243 
244  if (!Intrusion) {
245  if (chit->Cell() < _ydelt) {
246  entry_id = 3;
247  Intrusion = true;
248  }
249  else if (chit->Cell() > _yMax - _ydelt) {
250  entry_id = 4;
251  Intrusion = true;
252  }
253  }
254 
255  else if (!_penetrated) {
256  if (chit->Cell() < _ydelt && entry_id!=3) _penetrated = true;
257  else if (chit->Cell() > _yMax-_ydelt && entry_id!=4) _penetrated = true;
258  }
259  }
260 
261  if (!Intrusion) {
262  if (chit->Plane() < _zdelt) {
263  entry_id = 5;
264  Intrusion = true;
265  }
266  else if (chit->Plane() > _zMax - _zdelt) {
267  entry_id = 6;
268  Intrusion = true;
269  }
270  }
271  else if (!_penetrated) { //intrusion happened but not through yet
272  if (chit->Plane() < _zdelt && entry_id!=5) _penetrated=true;
273  else if (chit->Plane() > _zMax-_zdelt && entry_id!=6) _penetrated=true;
274  }
275  }
276  //print out slice information:
277  std::cout<<"X view Plane range: "<<PX_min<<" "<<PX_max<<std::endl;
278  std::cout<<"X view Cell range: "<<CX_min<<" "<<CX_max<<std::endl;
279  std::cout<<"X view time range: "<<TX_min<<" "<<TX_max<<std::endl;
280 
281  std::cout<<"Y view Plane range: "<<PY_min<<" "<<PY_max<<std::endl;
282  std::cout<<"Y view Cell range: "<<CY_min<<" "<<CY_max<<std::endl;
283  std::cout<<"Y view time range: "<<TY_min<<" "<<TY_max<<std::endl;
284 
285 
286  //Record the info
287  double nhits = _nxHits+_nyHits+0.;
288  _cx /= (_nxHits+0.);
289  _cxW /= ADCx;
290  _cy /= (_nyHits+0.);
291  _cyW /= ADCy;
292  _cz /= nhits;
293  _czW /= _meanADC;
294 
295 
296  // _meanADC = _meanADC/nhits;
297  _rmsADC = RMS(adcList, _meanADC);
298 
299  _dpx = PX_max-PX_min;
300  _dpy = PY_max-PY_min;
301  _dcx = CX_max-CX_min;
302  _dcy = CY_max-CY_min;
303  _dp =std::max(_dpx,_dpy);
304  _tmin = std::min(TX_min,TY_min);
305  _tmax = std::max(TX_max,TY_max);
306 
307  //preselection: if failed these, don't record.
308  std::cout<<"nxHits: "<<_nxHits<<" nyHits: "<<_nyHits<<" penetration: "<<_penetrated<<std::endl;
309  if(_nxHits<3||_nyHits<3||(!_penetrated)||(std::min(PX_max,PY_max) < std::max(PX_min,PY_min)+1)||(std::min(TX_max,TY_max)+62 < std::max(TX_min,TY_min) )) continue;
310 
311 
312  //added by enhao
313  _cxz /= (_nxHits+0.0);
314  _cxzW/=ADCx;
315  _cyz /= (_nyHits+0.0);
316  _cyzW/=ADCy;
317  std::cout<<"number of surface hits: "<<_number_of_surface_hits<<std::endl;
318  if(_number_of_surface_hits<22) continue;
319  double _diagonal_length=std::sqrt(2.82072025*_dp*_dp+_dcx*_dcx+_dcy*_dcy);
320  std::cout<<"slice diagonal length: "<<_diagonal_length<<std::endl;
321  if(_diagonal_length<79) continue;
322  _number_of_hits_per_length=1.0*(_nxHits+_nyHits)/std::sqrt(2.82072025*_dp*_dp+_dcx*_dcx+_dcy*_dcy);
323  std::cout<<"number of hits divided by slice diagonal length"<<_number_of_hits_per_length<<std::endl;
324  if(_number_of_hits_per_length<0.43) continue;
325  double tracklength_of_xview=std::sqrt(2.82072025*_dpx*_dpx+_dcx*_dcx);
326  double tracklength_of_yview=std::sqrt(2.82072025*_dpy*_dpy+_dcy*_dcy);
327  NumberOfCellsPerLength(slice,tracklength_of_xview,tracklength_of_yview,_number_of_cells_per_length_xview,_number_of_cells_per_length_yview);
328  std::cout<<"number of cells per length xview: "<<_number_of_cells_per_length_xview<<" yview: "<<_number_of_cells_per_length_yview<<std::endl;
331  std::cout<<"stdev cells per plane xview: "<<_stdev_cells_per_plane_xview<<" yview: "<<_stdev_cells_per_plane_yview<<std::endl;
333  _weighted_off_center_xx=std::abs((_cxW-0.5*(CX_max+CX_min))/_dcx);
335  _weighted_off_center_yy=std::abs((_cyW-0.5*(CY_max+CY_min))/_dcy);
338 
339 
340  std::cout<<"percent of hits in overlap planes: "<<_percent_of_hits_in_overlap_planes<<std::endl;
341  std::cout<<"weighted off center xz: "<<_weighted_off_center_xz<<" yz: "<<_weighted_off_center_yz<<std::endl;
343  std::cout<<"trigger!!!!!!!!!!!!!"<<std::endl;
344  std::cout<<"trigger slice diagonal length: "<<std::sqrt(2.82072025*_dp*_dp+_dcx*_dcx+_dcy*_dcy)<<std::endl;
345  std::cout<<"evt id: "<<evtID<<std::endl;
346  clustercol->push_back(slice);
347  }
348  }
349  evt.put(std::move(clustercol));
350 
351 } //end analyzing
352 
353 double zcl::FmmTriggerEvd::RMS(std::vector<double> & v, double & mean) {
354  double rms = 0;
355  mean = 0;
356 
357  for (unsigned i = 0; i!= v.size(); ++i) {
358  mean += v[i];
359  }
360  mean /= double(v.size());
361  for (unsigned i = 0; i!= v.size(); ++i) {
362  rms += (v[i]-mean)*(v[i]-mean);
363  }
364  rms /= double(v.size());
365  rms = sqrt(rms);
366  return rms;
367 }
368 
370  if (plane<_zdelt) return true;
371  if (plane>_zMax-_zdelt)return true;
372  if (cell<_xdelt) return true;
373  if (cell>_xMax-_xdelt) return true;
374  return false;
375 }
376 
377 
378 void zcl::FmmTriggerEvd::NumberOfCellsPerLength(rb::Cluster const &slice,double tracklength_of_xview, double tracklength_of_yview, double &number_of_cells_per_length_xview,double &number_of_cells_per_length_yview) const{
379  std::vector<std::vector<int>> x_hits_plane(PX_max-PX_min+1,std::vector<int>(0));
380  std::vector<std::vector<int>> y_hits_plane(PY_max-PY_min+1,std::vector<int>(0));
381  for (unsigned i = 0; i != slice.NCell(); ++i) {
382  const art::Ptr<rb::CellHit> chit = slice.Cell(i);
383  int c = chit->Cell();
384  int p = chit->Plane();
385  if (chit->View()==geo::kX) {
386  x_hits_plane[p-PX_min].push_back(c);
387  }
388  else {
389  y_hits_plane[p-PY_min].push_back(c);
390  }
391  }
392  //following is to get unique number of cells hit per plane
393  std::vector<int> x_cells_plane(PX_max-PX_min+1,0);
394  std::vector<int> y_cells_plane(PY_max-PY_min+1,0);
395  for (size_t i=0;i!=x_hits_plane.size();i++){
396  std::sort(x_hits_plane[i].begin(),x_hits_plane[i].end());
397  std::vector<int>::iterator it;
398  it = std::unique (x_hits_plane[i].begin(),x_hits_plane[i].end());
399  x_hits_plane[i].resize(std::distance(x_hits_plane[i].begin(),it));
400  x_cells_plane[i]=x_hits_plane[i].size();
401  }
402  for (size_t i=0;i!=y_hits_plane.size();i++){
403  std::sort(y_hits_plane[i].begin(),y_hits_plane[i].end());
404  std::vector<int>::iterator it;
405  it = std::unique (y_hits_plane[i].begin(),y_hits_plane[i].end());
406  y_hits_plane[i].resize(std::distance(y_hits_plane[i].begin(),it));
407  y_cells_plane[i]=y_hits_plane[i].size();
408  }
409  //following is to get number of cells per track length in x view and y view
410  int sum_of_cells=0;
411  for (size_t i=0;i!=x_cells_plane.size();i++){
412  sum_of_cells+=x_cells_plane[i];
413  }
414  number_of_cells_per_length_xview=sum_of_cells/tracklength_of_xview;
415  sum_of_cells=0;
416  for (size_t i=0;i!=y_cells_plane.size();i++){
417  sum_of_cells+=y_cells_plane[i];
418  }
419  number_of_cells_per_length_yview=sum_of_cells/tracklength_of_yview;
420 }
421 void zcl::FmmTriggerEvd::StdevCellsPerPlane(rb::Cluster const &slice, double &stdev_cells_per_plane_xview, double &stdev_cells_per_plane_yview ) const{
422  //fluctuation of number of cells of each plane check by Enhao
423  //I need to use cells not hits, because there might be many hits in same cell, for example sag effect, and the cosmic shower has many different cells hits
424  std::vector<std::vector<int>> x_hits_plane(PX_max-PX_min+1,std::vector<int>(0));
425  std::vector<std::vector<int>> y_hits_plane(PY_max-PY_min+1,std::vector<int>(0));
426  for (unsigned i = 0; i != slice.NCell(); ++i) {
427  const art::Ptr<rb::CellHit> chit = slice.Cell(i);
428  int c = chit->Cell();
429  int p = chit->Plane();
430  if (chit->View()==geo::kX) {
431  x_hits_plane[p-PX_min].push_back(c);
432  }
433  else {
434  y_hits_plane[p-PY_min].push_back(c);
435  }
436  }
437  double sum =0;
438  double mean = 0;
439  double sq_sum = 0;
440  double stdev = 0;
441  //following is to get unique number of cells hit per plane
442  std::vector<int> x_cells_plane(PX_max-PX_min+1,0);
443  std::vector<int> y_cells_plane(PY_max-PY_min+1,0);
444  for (size_t i=0;i!=x_hits_plane.size();i++){
445  std::sort(x_hits_plane[i].begin(),x_hits_plane[i].end());
446  std::vector<int>::iterator it;
447  it = std::unique (x_hits_plane[i].begin(),x_hits_plane[i].end());
448  x_hits_plane[i].resize(std::distance(x_hits_plane[i].begin(),it));
449  x_cells_plane[i]=x_hits_plane[i].size();
450  }
451  for (size_t i=0;i!=y_hits_plane.size();i++){
452  std::sort(y_hits_plane[i].begin(),y_hits_plane[i].end());
453  std::vector<int>::iterator it;
454  it = std::unique (y_hits_plane[i].begin(),y_hits_plane[i].end());
455  y_hits_plane[i].resize(std::distance(y_hits_plane[i].begin(),it));
456  y_cells_plane[i]=y_hits_plane[i].size();
457  }
458  sum = std::accumulate(x_cells_plane.begin(),x_cells_plane.end(),0.0);
459  mean = sum / x_cells_plane.size();
460  sq_sum = std::inner_product(x_cells_plane.begin(),x_cells_plane.end(),x_cells_plane.begin(),0.0);
461  stdev = std::sqrt(sq_sum / x_cells_plane.size()-mean * mean);
462  stdev_cells_per_plane_xview=stdev/mean;
463  sum = std::accumulate(y_cells_plane.begin(),y_cells_plane.end(),0.0);
464  mean = sum / y_cells_plane.size();
465  sq_sum = std::inner_product(y_cells_plane.begin(),y_cells_plane.end(),y_cells_plane.begin(),0.0);
466  stdev = std::sqrt(sq_sum / y_cells_plane.size()-mean * mean);
467  stdev_cells_per_plane_yview=stdev/mean;
468 }
469 double zcl::FmmTriggerEvd::NumberOfHitsInOverlapPlanesCut(rb::Cluster const &slice,float PX_min,float PX_max,float PY_min,float PY_max ) const{
470  int max=std::min(PX_max,PY_max);
471  int min=std::max(PX_min,PY_min);
472  double number_hits_in_overlap_planes=0;
473  for (unsigned i = 0; i != slice.NCell(); ++i) {
474  const art::Ptr<rb::CellHit> chit = slice.Cell(i);
475  if(chit->Plane()<=max && chit->Plane()>=min) number_hits_in_overlap_planes++;
476  }
477  return number_hits_in_overlap_planes/slice.NCell();
478 }
float TNS() const
Definition: CellHit.h:46
T max(const caf::Proxy< T > &a, T b)
double NumberOfHitsInOverlapPlanesCut(rb::Cluster const &slice, float PX_min, float PX_max, float PY_min, float PY_max) const
SubRunNumber_t subRun() const
Definition: Event.h:72
back track the reconstruction to the simulation
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
set< int >::iterator it
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
Vertical planes which measure X.
Definition: PlaneGeo.h:28
A collection of associated CellHits.
Definition: Cluster.h:47
DEFINE_ART_MODULE(TestTMapFile)
unsigned distance(const T &t1, const T &t2)
float abs(float number)
Definition: d0nt_math.hpp:39
ProductID put(std::unique_ptr< PROD > &&product)
Definition: Event.h:102
unsigned short Cell() const
Definition: CellHit.h:40
void StdevCellsPerPlane(rb::Cluster const &slice, double &stdev_cells_per_plane_xview, double &stdev_cells_per_plane_yview) const
int evt
void produce(art::Event &evt)
Collect Geo headers and supply basic geometry functions.
void NumberOfCellsPerLength(rb::Cluster const &slice, double tracklength_of_xview, double tracklength_of_yview, double &number_of_cells_per_length_xview, double &number_of_cells_per_length_yview) const
double RMS(std::vector< double > &v, double &mean)
OStream cout
Definition: OStream.cxx:6
bool IsSurfaceHit(int plane, int cell) const
static float min(const float a, const float b, const float c)
Definition: absgeo.cxx:45
EventNumber_t event() const
Definition: EventID.h:116
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
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
void geom(int which=0)
Definition: geom.C:163
T min(const caf::Proxy< T > &a, T b)
Double_t sum
Definition: plot.C:31
RunNumber_t run() const
Definition: Event.h:77
std::string fClusterInput
Input folder from cluster reco.
T max(sqlite3 *const db, std::string const &table_name, std::string const &column_name)
Definition: statistics.h:68
EventID id() const
Definition: Event.h:56
Encapsulate the geometry of one entire detector (near, far, ndos)
FmmTriggerEvd(fhicl::ParameterSet const &pset)