13 #include <JANA/JEventLoop.h>
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);
49 pthread_mutex_unlock(&print_mutex);
63 if(print_messages) jout <<
"In DBCALTDCHit_factory, loading constants..." << endl;
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"];
72 jerr <<
"Unable to get BCAL_TDC_SCALE from /BCAL/digi_scales !" << endl;
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"];
82 jerr <<
"Unable to get BCAL_TDC_BASE_TIME_OFFSET from /BCAL/base_time_offset !" << endl;
84 vector<double> raw_time_offsets;
85 vector<double> raw_channel_global_offset;
86 vector<double> raw_tdiff_u_d;
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;
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);
114 loop->GetSingle(locTTabUtilities);
116 vector<const DBCALTDCDigiHit*> digihits;
118 for(
unsigned int i=0; i<digihits.size(); i++){
132 digihit->GetSingle(F1TDCHit);
133 double end_sign = digihit->
end ? -1.0 : 1.0;
134 if (F1TDCHit != NULL) {
139 T = T_raw - GetConstant(time_offsets,digihit)
140 - GetConstant(channel_global_offset,digihit)
141 - (0.5 * end_sign) * GetConstant(tdiff_u_d,digihit);
150 hit->AddAssociatedObject(digihit);
152 _data.push_back(hit);
179 const vector<double> &raw_table)
187 for(
int module=1; module<=BCAL_NUM_MODULES; module++) {
189 for(
int sector=1; sector<=BCAL_NUM_SECTORS; sector++) {
190 if( (channel > BCAL_MAX_TDC_CHANNELS) || (channel+1 > BCAL_MAX_TDC_CHANNELS) ) {
191 sprintf(str,
"Too many channels for BCAL table! channel=%d (should be %d)",
192 channel, BCAL_MAX_TDC_CHANNELS);
194 throw JException(str);
197 table.push_back(
cell_calib_t(raw_table[channel],raw_table[channel+1]) );
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);
209 throw JException(str);
217 const vector<double> &raw_table)
225 for(
int module=1; module<=BCAL_NUM_MODULES; module++) {
227 for(
int sector=1; sector<=BCAL_NUM_SECTORS; sector++) {
232 if( channel > BCAL_MAX_CHANNELS/2 ) {
233 sprintf(str,
"Too many channels for BCAL table! channel=%d (should be %d)",
234 channel, BCAL_MAX_CHANNELS/2);
236 throw JException(str);
239 table.push_back(
cell_calib_t(raw_table[channel],raw_table[channel]) );
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);
251 throw JException(str);
260 const int in_module,
const int in_layer,
261 const int in_sector,
const int in_end)
const
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);
268 throw JException(str);
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);
273 throw JException(str);
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);
278 throw JException(str);
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);
283 throw JException(str);
286 sprintf(str,
"Bad end # requested in DBCALTDCHit_factory::GetConstant()! requested=%d , should be 0-1", in_end);
288 throw JException(str);
291 const int the_cell = GetCalibIndex( in_module, in_layer, in_sector);
295 return the_table.at(the_cell).first;
298 return the_table.at(the_cell).second;
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);
311 throw JException(str);
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);
316 throw JException(str);
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);
321 throw JException(str);
324 sprintf(str,
"Bad end # requested in DBCALTDCHit_factory::GetConstant()! requested=%d , should be 0-1", in_hit->
end);
326 throw JException(str);
329 const int the_cell = GetCalibIndex( in_hit->
module, in_hit->
layer, in_hit->
sector);
333 return the_table.at(the_cell).first;
337 return the_table.at(the_cell).second;
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);
351 throw JException(str);
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);
356 throw JException(str);
358 if( (in_digihit->
sector <= 0) || (in_digihit->
sector >static_cast<unsigned int>( BCAL_NUM_SECTORS))) {
359 throw JException(str);
362 const int the_cell = GetCalibIndex( in_digihit->
module, in_digihit->
layer, in_digihit->
sector);
366 return the_table.at(the_cell).first;
369 return the_table.at(the_cell).second;
double Convert_DigiTimeToNs_F1TDC(const JObject *locTDCDigiHit) const
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
float t_raw
Uncalibrated time in ns.
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
vector< cell_calib_t > bcal_digi_constants_t
jerror_t fini(void)
Called after last event of last event source has been processed.