Hall-D Software  alpha
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DBCALPoint_factory.cc
Go to the documentation of this file.
1 //most of the code was orginally written by Matt Shepherd in
2 //DBCALCluster_factory.cc
3 
4 #include <vector>
5 using namespace std;
6 
7 #include <JANA/JApplication.h>
8 using namespace jana;
9 
11 #include "BCAL/DBCALHit.h"
12 #include "BCAL/DBCALGeometry.h"
13 
14 #include "DANA/DApplication.h"
15 
16 #include "units.h"
17 
18 
19 //----------------
20 // brun
21 //----------------
22 jerror_t DBCALPoint_factory::brun(JEventLoop *loop, int32_t runnumber) {
23  // Only print messages for one thread whenever run number changes
24  static pthread_mutex_t print_mutex = PTHREAD_MUTEX_INITIALIZER;
25  static set<int> runs_announced;
26  pthread_mutex_lock(&print_mutex);
27  bool print_messages = false;
28  if(runs_announced.find(runnumber) == runs_announced.end()){
29  print_messages = true;
30  runs_announced.insert(runnumber);
31  }
32  pthread_mutex_unlock(&print_mutex);
33 
34  DApplication* app = dynamic_cast<DApplication*>(loop->GetJApplication());
35  DGeometry* geom = app->GetDGeometry(runnumber);
36  geom->GetTargetZ(m_z_target_center);
37 
38  // load BCAL geometry
39  vector<const DBCALGeometry *> BCALGeomVec;
40  loop->Get(BCALGeomVec);
41  if(BCALGeomVec.size() == 0)
42  throw JException("Could not load DBCALGeometry object!");
43  m_BCALGeom = BCALGeomVec[0];
44 
45  if(print_messages) jout << "in DBCALPoint_factory, loading constants ..." << endl;
46 
47  // load attenuation correction parameters
48  attenuation_parameters.clear();
49 
50  vector< vector<double> > attenuation_parameters_temp;
51  loop->GetCalib("/BCAL/attenuation_parameters", attenuation_parameters_temp);
52 
53  // avoid potential crash ...
54  for (unsigned int i = 0; i < attenuation_parameters_temp.size(); i++){
55  attenuation_parameters.push_back(attenuation_parameters_temp.at(i));
56  }
57 
58  if (PRINTCALIBRATION) {
59  int channel = 0;
60  for (int module=1; module<=BCAL_NUM_MODULES; module++) {
61  for (int layer=1; layer<=BCAL_NUM_LAYERS; layer++) {
62  for (int sector=1; sector<=BCAL_NUM_SECTORS; sector++) {
63  printf("%2i %2i %2i %12.4f %12.4f %12.4f\n",
64  module,layer,sector,
65  attenuation_parameters[channel][0],
66  attenuation_parameters[channel][1],
67  attenuation_parameters[channel][2]);
68  }
69  channel++;
70  }
71  }
72  }
73 
74 
75  // load effective velocities
76  effective_velocities.clear();
77 
78  // Passing in the member vector directly was sometimes causing a crash...
79  vector <double> effective_velocities_temp;
80  loop->GetCalib("/BCAL/effective_velocities", effective_velocities_temp);
81 
82  for (unsigned int i = 0; i < effective_velocities_temp.size(); i++){
83  effective_velocities.push_back(effective_velocities_temp.at(i));
84  }
85 
86  // load track parameters (the parameters of the quadratic fit in histograms of z_track = f(tUp - tDown) )
87  track_parameters.clear();
88 
89  vector< vector<double> > track_parameters_temp;
90  loop->GetCalib("/BCAL/z_track_parms", track_parameters_temp);
91 
92  // Passing in the member vector directly was sometimes causing a crash...
93  for (unsigned int i = 0; i < track_parameters_temp.size(); i++){
94  track_parameters.push_back(track_parameters_temp.at(i));
95  }
96 
97  return NOERROR;
98 }
99 
100 //----------------
101 // evnt
102 //----------------
103 jerror_t DBCALPoint_factory::evnt(JEventLoop *loop, uint64_t eventnumber) {
104 
105  vector<const DBCALUnifiedHit*> hits;
106  loop->Get(hits);
107  if (hits.size() <= 0) return NOERROR;
108 
109  // first arrange the list of hits so they are grouped by cell
110  map< int, cellHits > cellHitMap;
111  for( vector< const DBCALUnifiedHit* >::const_iterator hitPtr = hits.begin();
112  hitPtr != hits.end();
113  ++hitPtr ){
114 
115  const DBCALUnifiedHit& hit = (**hitPtr);
116 
117  int id = m_BCALGeom->cellId( hit.module, hit.layer, hit.sector );
118 
119  // Add hit to appropriate list for this cell
120  if(hit.end == m_BCALGeom->kUpstream){
121  cellHitMap[id].uphits.push_back( *hitPtr );
122  }else{
123  cellHitMap[id].dnhits.push_back( *hitPtr );
124  }
125  }
126 
127  // now go through this list and group hits into BCAL points
128  // this combines information from both ends
129  for( map< int, cellHits >::const_iterator mapItr = cellHitMap.begin();
130  mapItr != cellHitMap.end();
131  ++mapItr ){
132 
133  const vector<const DBCALUnifiedHit*> &uphits = mapItr->second.uphits;
134  const vector<const DBCALUnifiedHit*> &dnhits = mapItr->second.dnhits;
135 
136  //require double-ended hits
137  if(uphits.size()==0 || dnhits.size()==0) continue;
138 
139  // Each SiPM sum can have multiple hits, some caused purely by
140  // dark hits. A more sophisticated algorithm may be needed here
141  // to decipher the multi-hit events. For now, we just take the
142  // most energetic hit from each end. (Single ended hits are
143  // ignored.
144 
145  const DBCALUnifiedHit *uphit=uphits[0];
146  const DBCALUnifiedHit *dnhit=dnhits[0];
147 
148  for(unsigned int i=1; i<uphits.size(); i++){
149  if(uphits[i]->E > uphit->E) uphit = uphits[i];
150  }
151 
152  for(unsigned int i=1; i<dnhits.size(); i++){
153  if(dnhits[i]->E > dnhit->E) dnhit = dnhits[i];
154  }
155 
156  // first check that the hits don't have absurd timing information
157 
158  //int id = m_BCALGeom->cellId( uphit->module, uphit->layer, uphit->sector ); // key the cell identification off of the upstream cell
159  int table_id = GetCalibIndex( uphit->module, uphit->layer, uphit->sector ); // key the cell identification off of the upstream cell
160 
161  // float fibLen = m_BCALGeom->GetBCAL_length();
162  //float cEff = m_BCALGeom->C_EFFECTIVE;
163  float cEff = GetEffectiveVelocity(table_id);
164 
165  // get the position with respect to the center of the module -- positive
166  // z in the downstream direction
167  // double zLocal = 0.5 * cEff * ( uphit->t - dnhit->t );
168 
169  // if the timing information indicates that the z position is more than 60 cm outside the BCAL, likely the hit is contamined by noise or entirely noise, skip this cell
170  // double tol = 60*k_cm;
171 
172  // comment out this section and move any checks on z position (i.e. timing) to clusterizer. Elton 5/3/2017
173  // if (zLocal > (0.5*fibLen + tol) || zLocal < (-0.5*fibLen - tol)) continue;
174 
175  // pass attenuation length parameters to the DBCALPoint constructor, since
176  // many of the calculations are implemented there
177  double attenuation_length = m_BCALGeom->GetBCAL_attenutation_length();
178  double attenuation_L1=-1., attenuation_L2=-1.; // these parameters are ignored for now
179  GetAttenuationParameters(table_id, attenuation_length, attenuation_L1, attenuation_L2);
180  // if (GetAttenuationParameters(id, attenuation_length, attenuation_L1, attenuation_L2)) {
181  // printf("got new att length %f\n",attenuation_length);
182  // } else {
183  // printf("default att length %f\n",attenuation_length);
184  // }
185 
186  // pass track parameters to the DBCALPoint constructor, since
187  // many of the calculations are implemented there. This should change promptly
188  double track_p0 = -1.0; // will be updated from GetTrackParameters (dimensions: cm)
189  double track_p1 = -1.0; // will be updated from GetTrackParameters (dimensions: cm/ns)
190  double track_p2 = -100.0; // will be updated from GetTrackParameters (dimensions: cm/ns^2)
191  GetTrackParameters(table_id, track_p0, track_p1, track_p2);
192 
193  DBCALPoint *point = new DBCALPoint(*uphit,*dnhit,m_z_target_center,attenuation_length,cEff,track_p0,track_p1,track_p2,m_BCALGeom);
194 
195  point->AddAssociatedObject(uphit);
196  point->AddAssociatedObject(dnhit);
197 
198  _data.push_back(point);
199  }
200 
201  //Possibly we should also construct points from single-ended hits here.
202  //The code for this is currently (commented out) in
203  //DBCALCluster_factory.cc
204 
205  return NOERROR;
206 }
207 
208 bool DBCALPoint_factory::GetAttenuationParameters(int id, double &attenuation_length,
209  double &attenuation_L1, double &attenuation_L2)
210 {
211  vector<double> &parms = attenuation_parameters.at(id);
212 
213  attenuation_length = parms[0];
214  attenuation_L1 = parms[1];
215  attenuation_L2 = parms[2];
216 
217  return true;
218 }
219 
221 {
222  return effective_velocities.at(id);
223 }
224 
225 bool DBCALPoint_factory::GetTrackParameters(int id, double &track_p0,
226  double &track_p1, double &track_p2)
227 {
228  vector<double> &z_parms = track_parameters.at(id);
229 
230  if(!z_parms.empty()){
231  track_p0 = z_parms[0];
232  track_p1 = z_parms[1];
233  track_p2 = z_parms[2];
234  return true;
235  }
236  else{
237  jerr<<"Failed to retrieve the z_track parameters from CCDB!!!" << endl;
238  exit(-1);
239  }
240 }
Int_t layer
bool GetAttenuationParameters(int id, double &attenuation_length, double &attenuation_L1, double &attenuation_L2)
DGeometry * GetDGeometry(unsigned int run_number)
DBCALGeometry::End end
double GetEffectiveVelocity(int id)
bool GetTrackParameters(int id, double &track_p0, double &track_p1, double &track_p2)
printf("string=%s", string)
jerror_t brun(JEventLoop *loop, int32_t runnumber)
bool GetTargetZ(double &z_target) const
z-location of center of target
Definition: DGeometry.cc:1933
jerror_t evnt(JEventLoop *loop, uint64_t eventnumber)