Hall-D Software  alpha
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DBCALTDCHit_factory.cc
Go to the documentation of this file.
1 // $Id$
2 //
3 // File: DBCALTDCHit_factory.cc
4 // Created: Tue Aug 6 11:04:11 EDT 2013
5 // Creator: davidl (on Darwin harriet.jlab.org 11.4.2 i386)
6 //
7 
8 
9 #include <iostream>
10 #include <iomanip>
11 using namespace std;
12 
13 #include <JANA/JEventLoop.h>
14 #include <BCAL/DBCALTDCDigiHit.h>
16 #include <DAQ/DF1TDCHit.h>
17 
18 using namespace jana;
19 
20 
21 //------------------
22 // init
23 //------------------
25 {
26  /// set the base conversion scale
27  t_scale = 0.058; // 60 ps/count
28  t_base = 0.; // ns
29  t_rollover = 65250;
30  //t_offset = 0;
31 
32  return NOERROR;
33 }
34 
35 //------------------
36 // brun
37 //------------------
38 jerror_t DBCALTDCHit_factory::brun(jana::JEventLoop *eventLoop, int32_t runnumber)
39 {
40  // Only print messages for one thread whenever run number changes
41  static pthread_mutex_t print_mutex = PTHREAD_MUTEX_INITIALIZER;
42  static set<int> runs_announced;
43  pthread_mutex_lock(&print_mutex);
44  bool print_messages = false;
45  if(runs_announced.find(runnumber) == runs_announced.end()){
46  print_messages = true;
47  runs_announced.insert(runnumber);
48  }
49  pthread_mutex_unlock(&print_mutex);
50 
51  /* Rollover now handled by TTABUtilities
52  t_rollover = 65250;
53  if (runnumber>1776){
54  t_rollover = 64678;
55  }
56  if (runnumber>2010){
57  t_rollover = 64466;
58  }
59  */
60 
61  /// Read in calibration constants
62 
63  if(print_messages) jout << "In DBCALTDCHit_factory, loading constants..." << endl;
64 
65  // load scale factors
66  map<string,double> scale_factors;
67  if(eventLoop->GetCalib("/BCAL/digi_scales", scale_factors))
68  jout << "Error loading /BCAL/digi_scales !" << endl;
69  if( scale_factors.find("BCAL_TDC_SCALE") != scale_factors.end() ) {
70  t_scale = scale_factors["BCAL_TDC_SCALE"];
71  } else {
72  jerr << "Unable to get BCAL_TDC_SCALE from /BCAL/digi_scales !" << endl;
73  }
74 
75  // load base time offset
76  map<string,double> base_time_offset;
77  if (eventLoop->GetCalib("/BCAL/base_time_offset",base_time_offset))
78  jout << "Error loading /BCAL/base_time_offset !" << endl;
79  if (base_time_offset.find("BCAL_TDC_BASE_TIME_OFFSET") != base_time_offset.end())
80  t_base = base_time_offset["BCAL_TDC_BASE_TIME_OFFSET"];
81  else
82  jerr << "Unable to get BCAL_TDC_BASE_TIME_OFFSET from /BCAL/base_time_offset !" << endl;
83 
84  vector<double> raw_time_offsets;
85  vector<double> raw_channel_global_offset;
86  vector<double> raw_tdiff_u_d;
87 
88  if(eventLoop->GetCalib("/BCAL/TDC_offsets", raw_time_offsets))
89  jout << "Error loading /BCAL/TDC_offsets !" << endl;
90  if(eventLoop->GetCalib("/BCAL/channel_global_offset", raw_channel_global_offset))
91  jout << "Error loading /BCAL/channel_global_offset !" << endl;
92  if(eventLoop->GetCalib("/BCAL/tdiff_u_d", raw_tdiff_u_d))
93  jout << "Error loading /BCAL/tdiff_u_d !" << endl;
94 
95  FillCalibTable(time_offsets, raw_time_offsets);
96  FillCalibTableShort(channel_global_offset, raw_channel_global_offset);
97  FillCalibTableShort(tdiff_u_d, raw_tdiff_u_d);
98 
99  return NOERROR;
100 }
101 
102 //------------------
103 // evnt
104 //------------------
105 jerror_t DBCALTDCHit_factory::evnt(JEventLoop *loop, uint64_t eventnumber)
106 {
107  /// Generate DBCALTDCHit object for each DBCALTDCDigiHit object.
108  /// This is where the first set of calibration constants
109  /// is applied to convert from digitzed units into natural
110  /// units.
111 
112  // Get the TTabUtilities object
113  const DTTabUtilities* locTTabUtilities = NULL;
114  loop->GetSingle(locTTabUtilities);
115 
116  vector<const DBCALTDCDigiHit*> digihits;
117  loop->Get(digihits);
118  for(unsigned int i=0; i<digihits.size(); i++){
119  const DBCALTDCDigiHit *digihit = digihits[i];
120 
121  DBCALTDCHit *hit = new DBCALTDCHit;
122  hit->module = digihit->module;
123  hit->layer = digihit->layer;
124  hit->sector = digihit->sector;
125  hit->end = digihit->end;
126 
127  // Apply calibration constants here
128  double T, T_raw;
129 
130  //See if the input object is an DF1TDCHit. If so, it is real data. If not, it is simulated data.
131  const DF1TDCHit* F1TDCHit = NULL;
132  digihit->GetSingle(F1TDCHit);
133  double end_sign = digihit->end ? -1.0 : 1.0; // Upstream = 0 -> Positive
134  if (F1TDCHit != NULL) { // This is real data.
135  T_raw = locTTabUtilities->Convert_DigiTimeToNs_F1TDC(digihit) + t_base;
136  } else { // This is simulated data. Use a simplified time conversion.
137  T_raw = digihit->time * t_scale + t_base;
138  }
139  T = T_raw - GetConstant(time_offsets,digihit)
140  - GetConstant(channel_global_offset,digihit)
141  - (0.5 * end_sign) * GetConstant(tdiff_u_d,digihit);
142  hit->t_raw = T_raw;
143  hit->t = T;
144 
145  /*
146  cout << "BCAL TDC Hit: ( " << digihit->module << ", " << digihit->layer << ", "
147  << digihit->sector << ", " << digihit->end << " ) -> "
148  << T << " " << GetConstant(time_offsets,digihit) << " " << hit->t << endl;
149  */
150  hit->AddAssociatedObject(digihit);
151 
152  _data.push_back(hit);
153  }
154 
155  return NOERROR;
156 }
157 
158 //------------------
159 // erun
160 //------------------
162 {
163  return NOERROR;
164 }
165 
166 //------------------
167 // fini
168 //------------------
170 {
171  return NOERROR;
172 }
173 
174 
175 //------------------
176 // FillCalibTable
177 //------------------
179  const vector<double> &raw_table)
180 {
181  char str[256];
182  int channel = 0;
183 
184  // reset the table before filling it
185  table.clear();
186 
187  for(int module=1; module<=BCAL_NUM_MODULES; module++) {
188  for(int layer=1; layer<=BCAL_NUM_TDC_LAYERS; layer++) {
189  for(int sector=1; sector<=BCAL_NUM_SECTORS; sector++) {
190  if( (channel > BCAL_MAX_TDC_CHANNELS) || (channel+1 > BCAL_MAX_TDC_CHANNELS) ) { // sanity check
191  sprintf(str, "Too many channels for BCAL table! channel=%d (should be %d)",
192  channel, BCAL_MAX_TDC_CHANNELS);
193  cerr << str << endl;
194  throw JException(str);
195  }
196 
197  table.push_back( cell_calib_t(raw_table[channel],raw_table[channel+1]) );
198 
199  channel += 2;
200  }
201  }
202  }
203 
204  // check to make sure that we loaded enough channels
205  if(channel != BCAL_MAX_TDC_CHANNELS) {
206  sprintf(str, "Not enough channels for BCAL table! channel=%d (should be %d)",
207  channel, BCAL_MAX_TDC_CHANNELS);
208  cerr << str << endl;
209  throw JException(str);
210  }
211 }
212 
213 //------------------
214 // FillCalibTableShort
215 //------------------
217  const vector<double> &raw_table)
218 {
219  char str[256];
220  int channel = 0;
221 
222  // reset the table before filling it
223  table.clear();
224  // These short tables have 768 values
225  for(int module=1; module<=BCAL_NUM_MODULES; module++) {
226  for(int layer=1; layer<=BCAL_NUM_LAYERS; layer++) {
227  for(int sector=1; sector<=BCAL_NUM_SECTORS; sector++) {
228  if (layer == 4) {
229  channel += 1;
230  continue;
231  }
232  if( channel > BCAL_MAX_CHANNELS/2 ) { // sanity check
233  sprintf(str, "Too many channels for BCAL table! channel=%d (should be %d)",
234  channel, BCAL_MAX_CHANNELS/2);
235  cerr << str << endl;
236  throw JException(str);
237  }
238 
239  table.push_back( cell_calib_t(raw_table[channel],raw_table[channel]) );
240 
241  channel += 1;
242  }
243  }
244  }
245 
246  // check to make sure that we loaded enough channels
247  if(channel != BCAL_MAX_CHANNELS/2) {
248  sprintf(str, "Not enough channels for BCAL table! channel=%d (should be %d)",
249  channel, BCAL_MAX_CHANNELS/2);
250  cerr << str << endl;
251  throw JException(str);
252  }
253 }
254 
255 //------------------------------------
256 // GetConstant
257 // Allow a few different interfaces
258 //------------------------------------
260  const int in_module, const int in_layer,
261  const int in_sector, const int in_end) const
262 {
263  char str[256];
264 
265  if( (in_module <= 0) || (in_module > BCAL_NUM_MODULES)) {
266  sprintf(str, "Bad module # requested in DBCALTDCHit_factory::GetConstant()! requested=%d , should be 1-%d", in_module, BCAL_NUM_MODULES);
267  cerr << str << endl;
268  throw JException(str);
269  }
270  if( (in_layer <= 0) || (in_layer > BCAL_NUM_TDC_LAYERS)) {
271  sprintf(str, "Bad layer # requested in DBCALTDCHit_factory::GetConstant()! requested=%d , should be 1-%d", in_layer, BCAL_NUM_TDC_LAYERS);
272  cerr << str << endl;
273  throw JException(str);
274  }
275  if( (in_sector <= 0) || (in_sector > BCAL_NUM_SECTORS)) {
276  sprintf(str, "Bad sector # requested in DBCALTDCHit_factory::GetConstant()! requested=%d , should be 1-%d", in_sector, BCAL_NUM_SECTORS);
277  cerr << str << endl;
278  throw JException(str);
279  }
280  if( (in_sector <= 0) || (in_sector > BCAL_NUM_SECTORS)) {
281  sprintf(str, "Bad sector # requested in DBCALTDCHit_factory::GetConstant()! requested=%d , should be 1-%d", in_sector, BCAL_NUM_SECTORS);
282  cerr << str << endl;
283  throw JException(str);
284  }
285  if( (in_end != DBCALGeometry::kUpstream) && (in_end != DBCALGeometry::kDownstream) ) {
286  sprintf(str, "Bad end # requested in DBCALTDCHit_factory::GetConstant()! requested=%d , should be 0-1", in_end);
287  cerr << str << endl;
288  throw JException(str);
289  }
290 
291  const int the_cell = GetCalibIndex( in_module, in_layer, in_sector);
292 
293  if(in_end == DBCALGeometry::kUpstream) {
294  // handle the upstream end
295  return the_table.at(the_cell).first;
296  } else {
297  // handle the downstream end
298  return the_table.at(the_cell).second;
299  }
300 
301 }
302 
304  const DBCALTDCHit *in_hit) const
305 {
306  char str[256];
307 
308  if( (in_hit->module <= 0) || (in_hit->module > BCAL_NUM_MODULES)) {
309  sprintf(str, "Bad module # requested in DBCALTDCHit_factory::GetConstant()! requested=%d , should be 1-%d", in_hit->module, BCAL_NUM_MODULES);
310  cerr << str << endl;
311  throw JException(str);
312  }
313  if( (in_hit->layer <= 0) || (in_hit->layer > BCAL_NUM_TDC_LAYERS)) {
314  sprintf(str, "Bad layer # requested in DBCALTDCHit_factory::GetConstant()! requested=%d , should be 1-%d", in_hit->layer, BCAL_NUM_TDC_LAYERS);
315  cerr << str << endl;
316  throw JException(str);
317  }
318  if( (in_hit->sector <= 0) || (in_hit->sector > BCAL_NUM_SECTORS)) {
319  sprintf(str, "Bad sector # requested in DBCALTDCHit_factory::GetConstant()! requested=%d , should be 1-%d", in_hit->sector, BCAL_NUM_SECTORS);
320  cerr << str << endl;
321  throw JException(str);
322  }
323  if( (in_hit->end != DBCALGeometry::kUpstream) && (in_hit->end != DBCALGeometry::kDownstream) ) {
324  sprintf(str, "Bad end # requested in DBCALTDCHit_factory::GetConstant()! requested=%d , should be 0-1", in_hit->end);
325  cerr << str << endl;
326  throw JException(str);
327  }
328 
329  const int the_cell = GetCalibIndex( in_hit->module, in_hit->layer, in_hit->sector);
330 
331  if(in_hit->end == DBCALGeometry::kUpstream) {
332  // handle the upstream end
333  return the_table.at(the_cell).first;
334  //return the_table[the_cell].first;
335  } else {
336  // handle the downstream end
337  return the_table.at(the_cell).second;
338  //return the_table[the_cell].second;
339  }
340 
341 }
342 
344  const DBCALTDCDigiHit *in_digihit) const
345 {
346  char str[256];
347 
348  if( (in_digihit->module <= 0) || (in_digihit->module > static_cast<unsigned int>(BCAL_NUM_MODULES))) {
349  sprintf(str, "Bad module # requested in DBCALTDCHit_factory::GetConstant()! requested=%d , should be 1-%d", in_digihit->module, BCAL_NUM_MODULES);
350  cerr << str << endl;
351  throw JException(str);
352  }
353  if( (in_digihit->layer <= 0) || (in_digihit->layer > static_cast<unsigned int>(BCAL_NUM_TDC_LAYERS))) {
354  sprintf(str, "Bad layer # requested in DBCALTDCHit_factory::GetConstant()! requested=%d , should be 1-%d", in_digihit->layer, BCAL_NUM_TDC_LAYERS);
355  cerr << str << endl;
356  throw JException(str);
357  }
358  if( (in_digihit->sector <= 0) || (in_digihit->sector >static_cast<unsigned int>( BCAL_NUM_SECTORS))) {
359  throw JException(str);
360  }
361 
362  const int the_cell = GetCalibIndex( in_digihit->module, in_digihit->layer, in_digihit->sector);
363 
364  if(in_digihit->end == DBCALGeometry::kUpstream) {
365  // handle the upstream end
366  return the_table.at(the_cell).first;
367  } else {
368  // handle the downstream end
369  return the_table.at(the_cell).second;
370  }
371 
372 }
373 /*
374  const double DBCALTDCHit_factory::GetConstant( const bcal_digi_constants_t &the_table,
375  const DTranslationTable *ttab,
376  const int in_rocid, const int in_slot, const int in_channel) const {
377 
378  char str[256];
379 
380  DTranslationTable::csc_t daq_index = { in_rocid, in_slot, in_channel };
381  DTranslationTable::DChannelInfo channel_info = ttab->GetDetectorIndex(daq_index);
382 
383  if( (channel_info.bcal.module <= 0)
384  || (channel_info.bcal.module > static_cast<unsigned int>(BCAL_NUM_MODULES))) {
385  sprintf(str, "Bad module # requested in DBCALTDCHit_factory::GetConstant()! requested=%d , should be 1-%d", channel_info.bcal.module, BCAL_NUM_MODULES);
386  cerr << str << endl;
387  throw JException(str);
388  }
389  if( (channel_info.bcal.layer <= 0)
390  || (channel_info.bcal.layer > static_cast<unsigned int>(BCAL_NUM_TDC_LAYERS))) {
391  sprintf(str, "Bad layer # requested in DBCALTDCHit_factory::GetConstant()! requested=%d , should be 1-%d", channel_info.bcal.layer, BCAL_NUM_TDC_LAYERS);
392  cerr << str << endl;
393  throw JException(str);
394  }
395  if( (channel_info.bcal.sector <= 0)
396  || (channel_info.bcal.sector > static_cast<unsigned int>(BCAL_NUM_SECTORS))) {
397  sprintf(str, "Bad sector # requested in DBCALTDCHit_factory::GetConstant()! requested=%d , should be 1-%d", channel_info.bcal.sector, BCAL_NUM_SECTORS);
398  cerr << str << endl;
399  throw JException(str);
400  }
401  if( (channel_info.bcal.end != DBCALGeometry::kUpstream)
402  && (channel_info.bcal.end != DBCALGeometry::kDownstream) ) {
403  sprintf(str, "Bad end # requested in DBCALTDCHit_factory::GetConstant()! requested=%d , should be 0-1", channel_info.bcal.end);
404  cerr << str << endl;
405  throw JException(str);
406  }
407 
408  int the_cell = DBCALGeometry::cellId(channel_info.bcal.module,
409  channel_info.bcal.layer,
410  channel_info.bcal.sector);
411 
412  if(channel_info.bcal.end == DBCALGeometry::kUpstream) {
413 // handle the upstream end
414 return the_table.at(the_cell).first;
415 } else {
416 // handle the downstream end
417 return the_table.at(the_cell).second;
418 }
419 }
420 */
double Convert_DigiTimeToNs_F1TDC(const JObject *locTDCDigiHit) const
char str[256]
Int_t layer
sprintf(text,"Post KinFit Cut")
jerror_t init(void)
Called once at program start.
void FillCalibTable(bcal_digi_constants_t &table, const vector< double > &raw_table)
void FillCalibTableShort(bcal_digi_constants_t &table, const vector< double > &raw_table)
jerror_t erun(void)
Called everytime run number changes, provided brun has been called.
jerror_t brun(jana::JEventLoop *eventLoop, int32_t runnumber)
Called everytime a new run number is detected.
jerror_t evnt(jana::JEventLoop *eventLoop, uint64_t eventnumber)
Called every event.
pair< double, double > cell_calib_t
DBCALGeometry::End end
float t_raw
Uncalibrated time in ns.
Definition: DBCALTDCHit.h:27
const double GetConstant(const bcal_digi_constants_t &the_table, const int in_module, const int in_layer, const int in_sector, const int in_end) const
DBCALGeometry::End end
Definition: DBCALTDCHit.h:25
vector< cell_calib_t > bcal_digi_constants_t
jerror_t fini(void)
Called after last event of last event source has been processed.