Hall-D Software  alpha
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DTAGHHit_factory.cc
Go to the documentation of this file.
1 // $Id$
2 //
3 // File: DTAGHHit_factory.cc
4 // Created: Sat Aug 2 12:23:43 EDT 2014
5 // Creator: jonesrt (on Linux gluex.phys.uconn.edu)
6 //
7 // nsparks moved original factory to DTAGHHit_factory_Calib.cc on Aug 3 2016.
8 
9 #include <iostream>
10 #include <iomanip>
11 #include <cmath>
12 using namespace std;
13 
14 #include "DTAGHHit_factory.h"
15 using namespace jana;
16 
17 inline bool DTAGHHit_SortByID(const DTAGHHit* h1, const DTAGHHit* h2)
18 {
19  if (h1->counter_id == h2->counter_id) return h1->t < h2->t;
20  return h1->counter_id < h2->counter_id;
21 }
22 
23 //------------------
24 // init
25 //------------------
26 jerror_t DTAGHHit_factory::init(void)
27 {
28  // Set default config. parameters
29  MERGE_DOUBLES = true; // Merge double hits?
30  gPARMS->SetDefaultParameter("TAGHHit:MERGE_DOUBLES", MERGE_DOUBLES,
31  "Merge double hits?");
32  DELTA_T_DOUBLES_MAX = 1.0; // ns
33  gPARMS->SetDefaultParameter("TAGHHit:DELTA_T_DOUBLES_MAX", DELTA_T_DOUBLES_MAX,
34  "Maximum time difference in ns between hits in adjacent counters"
35  " for them to be merged into a single hit");
36  DELTA_ID_DOUBLES_MAX = 1; // counters
37  gPARMS->SetDefaultParameter("TAGHHit:DELTA_ID_DOUBLES_MAX", DELTA_ID_DOUBLES_MAX,
38  "Maximum counter id difference of merged hits");
39  ID_DOUBLES_MAX = 274;
40  gPARMS->SetDefaultParameter("TAGHHit:ID_DOUBLES_MAX", ID_DOUBLES_MAX,
41  "Maximum counter id of a double hit");
42  USE_SIDEBAND_DOUBLES = false;
43  gPARMS->SetDefaultParameter("TAGHHit:USE_SIDEBAND_DOUBLES", USE_SIDEBAND_DOUBLES,
44  "Use sideband to estimate accidental coincidences between neighbors?");
45 
46  // Setting this flag makes it so that JANA does not delete the objects in _data.
47  SetFactoryFlag(NOT_OBJECT_OWNER);
48 
49  return NOERROR;
50 }
51 
52 //------------------
53 // brun
54 //------------------
55 jerror_t DTAGHHit_factory::brun(jana::JEventLoop *eventLoop, int32_t runnumber)
56 {
57  //RF Period
58  vector<double> locBeamPeriodVector;
59  eventLoop->GetCalib("PHOTON_BEAM/RF/beam_period", locBeamPeriodVector);
60  dBeamBunchPeriod = locBeamPeriodVector[0];
61  return NOERROR;
62 }
63 
64 //------------------
65 // evnt
66 //------------------
67 jerror_t DTAGHHit_factory::evnt(JEventLoop *loop, uint64_t eventnumber)
68 {
69  // A scattered (brems.) electron can hit multiple TAGH counters, due
70  // to multiple scattering and small geometric overlap between certain counters.
71  // These time-coincident hits should be merged to avoid double counting.
72  // This factory outputs hits after merging double hits (doubles).
73 
74  // Clear hit vector for next event
75  _data.clear();
76 
77  // Get (calibrated) TAGH hits
78  vector<const DTAGHHit*> hits;
79  loop->Get(hits, "Calib");
80 
81  // Sort TAGH hits by counter id
82  sort(hits.begin(),hits.end(),DTAGHHit_SortByID);
83 
84  for (size_t i = 0; i < hits.size(); i++) {
85  DTAGHHit* hit1 = const_cast<DTAGHHit*>(hits[i]);
86 
87  if (!hit1->has_fADC) continue;
88  if (!MERGE_DOUBLES || hit1->counter_id > ID_DOUBLES_MAX) {
89  _data.push_back(hit1);
90  continue;
91  }
92  if (hit1->is_double) continue;
93 
94  for (size_t j = i+1; j < hits.size(); j++) {
95  DTAGHHit* hit2 = const_cast<DTAGHHit*>(hits[j]);
96 
97  if (!hit2->has_fADC) continue;
98  size_t d = abs(hit2->counter_id-hit1->counter_id);
99  if (d == 0) continue;
100  if (d > DELTA_ID_DOUBLES_MAX) break;
101 
102  if (IsDoubleHit(hit2->t-hit1->t)) {
103  hit2->is_double = true;
104  hit1->is_double = true;
105  }
106  }
107  _data.push_back(hit1);
108  }
109 
110  return NOERROR;
111 }
112 
114 {
115  if (!USE_SIDEBAND_DOUBLES) {
116  return fabs(tdiff) < DELTA_T_DOUBLES_MAX;
117  } else {
118  return (tdiff > -DELTA_T_DOUBLES_MAX - dBeamBunchPeriod)
119  && (tdiff < DELTA_T_DOUBLES_MAX - dBeamBunchPeriod);
120  }
121 }
122 
123 //------------------
124 // erun
125 //------------------
127 {
128  return NOERROR;
129 }
130 
131 //------------------
132 // fini
133 //------------------
135 {
136  return NOERROR;
137 }
double t
Definition: DTAGHHit.h:19
bool is_double
Definition: DTAGHHit.h:26
jerror_t fini(void)
Called after last event of last event source has been processed.
bool IsDoubleHit(double tdiff)
jerror_t erun(void)
Called everytime run number changes, if brun has been called.
jerror_t evnt(jana::JEventLoop *eventLoop, uint64_t eventnumber)
Called every event.
int counter_id
Definition: DTAGHHit.h:20
bool DTAGHHit_SortByID(const DTAGHHit *h1, const DTAGHHit *h2)
jerror_t brun(jana::JEventLoop *eventLoop, int32_t runnumber)
Called everytime a new run number is detected.
bool has_fADC
Definition: DTAGHHit.h:26
jerror_t init(void)
Called once at program start.