30 CHECK_FADC_ERRORS =
true;
31 gPARMS->SetDefaultParameter(
"BCAL:CHECK_FADC_ERRORS", CHECK_FADC_ERRORS,
"Set to 1 to reject hits with fADC250 errors, ser to 0 to keep these hits");
32 CORRECT_FADC_SATURATION =
true;
33 gPARMS->SetDefaultParameter(
"BCAL:CORRECT_FADC_SATURATION", CORRECT_FADC_SATURATION,
"Set to 1 to correct pulse integral for fADC saturation, set to 0 to not correct pulse integral. (default = 1)");
34 CORRECT_SIPM_SATURATION =
true;
35 gPARMS->SetDefaultParameter(
"BCAL:CORRECT_SIPM_SATURATION", CORRECT_SIPM_SATURATION,
"Set to 1 to correct for SiPM saturation, set to 0 to not correct pulse integral or peak. (default = 1)");
48 static pthread_mutex_t print_mutex = PTHREAD_MUTEX_INITIALIZER;
49 static set<int> runs_announced;
50 pthread_mutex_lock(&print_mutex);
51 bool print_messages =
false;
52 if(runs_announced.find(runnumber) == runs_announced.end()){
53 print_messages =
true;
54 runs_announced.insert(runnumber);
56 pthread_mutex_unlock(&print_mutex);
59 vector<double> raw_gains;
60 vector<double> raw_pedestals;
61 vector<double> raw_ADC_timing_offsets;
62 vector<double> raw_channel_global_offset;
63 vector<double> raw_tdiff_u_d;
65 if(print_messages) jout <<
"In DBCALHit_factory, loading constants..." << endl;
68 map<string,double> scale_factors;
69 if (eventLoop->GetCalib(
"/BCAL/digi_scales", scale_factors))
70 jout <<
"Error loading /BCAL/digi_scales !" << endl;
71 if (scale_factors.find(
"BCAL_ADC_ASCALE") != scale_factors.end())
72 a_scale = scale_factors[
"BCAL_ADC_ASCALE"];
74 jerr <<
"Unable to get BCAL_ADC_ASCALE from /BCAL/digi_scales !" << endl;
75 if (scale_factors.find(
"BCAL_ADC_TSCALE") != scale_factors.end()) {
76 t_scale = scale_factors[
"BCAL_ADC_TSCALE"];
77 if (PRINTCALIBRATION) {
78 jout <<
"DBCALHit_factory >>BCAL_ADC_TSCALE = " <<
t_scale << endl;
82 jerr <<
"Unable to get BCAL_ADC_TSCALE from /BCAL/digi_scales !" << endl;
85 map<string,double> base_time_offset;
86 if (eventLoop->GetCalib(
"/BCAL/base_time_offset",base_time_offset))
87 jout <<
"Error loading /BCAL/base_time_offset !" << endl;
88 if (base_time_offset.find(
"BCAL_BASE_TIME_OFFSET") != base_time_offset.end()) {
89 t_base = base_time_offset[
"BCAL_BASE_TIME_OFFSET"];
90 if (PRINTCALIBRATION) {
91 jout <<
"DBCALHit_factory >>BCAL_BASE_TIME_OFFSET = " <<
t_base << endl;
95 jerr <<
"Unable to get BCAL_BASE_TIME_OFFSET from /BCAL/base_time_offset !" << endl;
98 if (eventLoop->GetCalib(
"/BCAL/ADC_gains", raw_gains))
99 jout <<
"Error loading /BCAL/ADC_gains !" << endl;
100 if (eventLoop->GetCalib(
"/BCAL/ADC_pedestals", raw_pedestals))
101 jout <<
"Error loading /BCAL/ADC_pedestals !" << endl;
102 if (eventLoop->GetCalib(
"/BCAL/ADC_timing_offsets", raw_ADC_timing_offsets))
103 jout <<
"Error loading /BCAL/ADC_timing_offsets !" << endl;
104 if(eventLoop->GetCalib(
"/BCAL/channel_global_offset", raw_channel_global_offset))
105 jout <<
"Error loading /BCAL/channel_global_offset !" << endl;
106 if(eventLoop->GetCalib(
"/BCAL/tdiff_u_d", raw_tdiff_u_d))
107 jout <<
"Error loading /BCAL/tdiff_u_d !" << endl;
109 if (PRINTCALIBRATION) jout <<
"DBCALHit_factory >> raw_gains" << endl;
110 FillCalibTable(gains, raw_gains);
111 if (PRINTCALIBRATION) jout <<
"DBCALHit_factory >> raw_pedestals" << endl;
112 FillCalibTable(pedestals, raw_pedestals);
113 if (PRINTCALIBRATION) jout <<
"DBCALHit_factory >> raw_ADC_timing_offsets" << endl;
114 FillCalibTable(ADC_timing_offsets, raw_ADC_timing_offsets);
115 if (PRINTCALIBRATION) jout <<
"DBCALHit_factory >> raw_channel_global_offset" << endl;
116 FillCalibTableShort(channel_global_offset, raw_channel_global_offset);
117 if (PRINTCALIBRATION) jout <<
"DBCALHit_factory >> raw_tdiff_u_d" << endl;
118 FillCalibTableShort(tdiff_u_d, raw_tdiff_u_d);
120 std::vector<std::map<string,double> > saturation_ADC_pars;
121 if(eventLoop->GetCalib(
"/BCAL/ADC_saturation", saturation_ADC_pars))
122 jout <<
"Error loading /BCAL/ADC_saturation !" << endl;
123 for (
unsigned int i=0; i < saturation_ADC_pars.size(); i++) {
124 int end = (saturation_ADC_pars[i])[
"end"];
125 int layer = (saturation_ADC_pars[i])[
"layer"] - 1;
126 fADC_MinIntegral_Saturation[end][
layer] = (saturation_ADC_pars[i])[
"par0"];
127 fADC_Saturation_Linear[end][
layer] = (saturation_ADC_pars[i])[
"par1"];
128 fADC_Saturation_Quadratic[end][
layer] = (saturation_ADC_pars[i])[
"par2"];
132 std::vector<std::map<string,double> > saturation_SiPM_pars;
133 if(eventLoop->GetCalib(
"/BCAL/SiPM_saturation", saturation_SiPM_pars))
134 jout <<
"Error loading /SiPM/SiPM_saturation !" << endl;
135 for (
unsigned int i=0; i < saturation_SiPM_pars.size(); i++) {
136 int end = (saturation_SiPM_pars[i])[
"END"];
137 int layer = (saturation_SiPM_pars[i])[
"LAYER"] - 1;
138 integral_to_peak[end][
layer] = (saturation_SiPM_pars[i])[
"INTEGRAL_TO_PEAK"];
139 sipm_npixels[end][
layer] = (saturation_SiPM_pars[i])[
"SIPM_NPIXELS"];
140 pixel_per_count[end][
layer] = (saturation_SiPM_pars[i])[
"PIXEL_PER_COUNT"];
163 loop->GetSingle(locTTabUtilities);
165 vector<const DBCALDigiHit*> digihits;
167 for(
unsigned int i=0; i<digihits.size(); i++){
176 digihit->GetSingle(PPobj);
186 double integral,
pedestal, nsamples_integral, nsamples_pedestal;
190 digihit->GetSingle(PIobj);
192 if (PIobj == NULL && digihit->
pedestal == 1 && digihit->
QF == 1){
195 nsamples_integral = 0.;
196 nsamples_pedestal = 1.;
209 pedestal = (double)digihit->
pedestal;
219 if(nsamples_pedestal == 0) {
221 if(
VERBOSE>0)jerr <<
"DBCALDigiHit with nsamples_pedestal == 0 ! Event = " << eventnumber << endl;
226 double single_sample_ped = pedestal/nsamples_pedestal;
227 double totalpedestal = nsamples_integral * single_sample_ped;
229 double gain = GetConstant(gains,digihit);
235 double integral_pedsub =0;
236 if ( integral > 0 ) {
238 integral_pedsub = integral - totalpedestal;
239 if(CORRECT_FADC_SATURATION && integral_pedsub > fADC_MinIntegral_Saturation[digihit->
end][digihit->
layer-1]) {
241 double locSaturatedIntegral = integral_pedsub - fADC_MinIntegral_Saturation[digihit->
end][digihit->
layer-1];
242 double locScaleFactor = 1. + fADC_Saturation_Linear[digihit->
end][digihit->
layer-1]*locSaturatedIntegral + fADC_Saturation_Quadratic[digihit->
end][digihit->
layer-1]*locSaturatedIntegral*locSaturatedIntegral;
243 integral_pedsub *= 1./locScaleFactor;
248 if (CORRECT_SIPM_SATURATION) {
249 double integral_pedsub_measured = integral_pedsub;
250 double Npixels_measured = pixel_per_count[digihit->
end][digihit->
layer-1]*integral_pedsub_measured;
251 double Mpixels = sipm_npixels[digihit->
end][digihit->
layer-1];
252 double Npixels_true = Npixels_measured < Mpixels? -Mpixels*log(1 - Npixels_measured/Mpixels) : Mpixels;
253 integral_pedsub = Npixels_true/pixel_per_count[digihit->
end][digihit->
layer-1];
257 hit_E = gain * integral_pedsub;
260 if (
VERBOSE>2)
printf(
"%5lu digihit %2i of %2lu, type %i time %4u, peak %3u, int %4.0f %.0f, ped %3.0f %.0f %5.1f %6.1f, gain %.1e, E=%5.0f MeV\n",
261 eventnumber,i,digihits.size(),digihit->
datasource,
263 pedestal,nsamples_pedestal,single_sample_ped,totalpedestal,gain,hit_E*1000);
264 if ( hit_E <= 0 )
continue;
268 int pulse_peak_pedsub=0;
269 if (CORRECT_SIPM_SATURATION) {
270 double pulse_peak_pedsub_measured = (int)digihit->
pulse_peak - (
int)single_sample_ped;
271 double Npixels_measured = pixel_per_count[digihit->
end][digihit->
layer-1]*pulse_peak_pedsub_measured*integral_to_peak[digihit->
end][digihit->
layer-1];
272 double Mpixels = sipm_npixels[digihit->
end][digihit->
layer-1];
273 double Npixels_true = Npixels_measured < Mpixels? -Mpixels*log(1 - Npixels_measured/Mpixels) : Mpixels;
274 pulse_peak_pedsub = round(Npixels_true/pixel_per_count[digihit->
end][digihit->
layer-1]/integral_to_peak[digihit->
end][digihit->
layer-1]);
280 pulse_peak_pedsub = (int)digihit->
pulse_peak - (
int)single_sample_ped;
284 double pulse_time = (double)digihit->
pulse_time;
285 double end_sign = digihit->
end ? -1.0 : 1.0;
288 + GetConstant(ADC_timing_offsets,digihit)
289 - GetConstant(channel_global_offset,digihit)
290 - (0.5 * end_sign) * GetConstant(tdiff_u_d,digihit);
291 if (
VERBOSE>2)
printf(
" %2i %i %i %i , t: %4.0f %.4f %7.3f traw=%7.3f %7.3f %7.3f %7.3f t=%7.3f\n",
294 GetConstant(ADC_timing_offsets,digihit),
295 GetConstant(channel_global_offset,digihit),
296 (0.5 * end_sign * GetConstant(tdiff_u_d,digihit)),hit_t);
306 hit->
t_raw = hit_t_raw;
308 hit->AddAssociatedObject(digihit);
310 _data.push_back(hit);
337 const vector<double> &raw_table)
345 for (
int module=1; module<=BCAL_NUM_MODULES; module++) {
347 for (
int sector=1; sector<=BCAL_NUM_SECTORS; sector++) {
348 if ((channel > BCAL_MAX_CHANNELS) || (channel+1 > BCAL_MAX_CHANNELS)) {
349 sprintf(str,
"Too many channels for BCAL table!"
350 " channel=%d (should be %d)",
351 channel, BCAL_MAX_CHANNELS);
353 throw JException(str);
356 table.push_back(
cell_calib_t(raw_table[channel],raw_table[channel+1]) );
358 if (PRINTCALIBRATION) {
359 printf(
"%2i %2i %2i %.10f %.10f\n",module,
layer,sector,raw_table[channel],raw_table[channel+1]);
368 if (channel != BCAL_MAX_CHANNELS) {
369 sprintf(str,
"Not enough channels for BCAL table!"
370 " channel=%d (should be %d)",
371 channel, BCAL_MAX_CHANNELS);
373 throw JException(str);
381 const vector<double> &raw_table)
389 for (
int module=1; module<=BCAL_NUM_MODULES; module++) {
391 for (
int sector=1; sector<=BCAL_NUM_SECTORS; sector++) {
392 if (channel > BCAL_MAX_CHANNELS/2) {
393 sprintf(str,
"Too many channels for BCAL table!"
394 " channel=%d (should be %d)",
395 channel, BCAL_MAX_CHANNELS/2);
397 throw JException(str);
400 table.push_back(
cell_calib_t(raw_table[channel],raw_table[channel]) );
402 if (PRINTCALIBRATION) {
403 printf(
"%2i %2i %2i %.10f\n",module,
layer,sector,raw_table[channel]);
412 if (channel != BCAL_MAX_CHANNELS/2) {
413 sprintf(str,
"Not enough channels for BCAL table!"
414 " channel=%d (should be %d)",
415 channel, BCAL_MAX_CHANNELS/2);
417 throw JException(str);
429 const int in_end)
const
433 if ( (in_module <= 0) || (in_module > BCAL_NUM_MODULES)) {
434 sprintf(str,
"Bad module # requested in DBCALHit_factory::GetConstant()!"
435 " requested=%d , should be 1-%d", in_module, BCAL_NUM_MODULES);
437 throw JException(str);
439 if ( (in_layer <= 0) || (in_layer > BCAL_NUM_LAYERS)) {
440 sprintf(str,
"Bad layer # requested in DBCALHit_factory::GetConstant()!"
441 " requested=%d , should be 1-%d", in_layer, BCAL_NUM_LAYERS);
443 throw JException(str);
445 if ( (in_sector <= 0) || (in_sector > BCAL_NUM_SECTORS)) {
446 sprintf(str,
"Bad sector # requested in DBCALHit_factory::GetConstant()!"
447 " requested=%d , should be 1-%d", in_sector, BCAL_NUM_SECTORS);
449 throw JException(str);
452 sprintf(str,
"Bad end # requested in DBCALHit_factory::GetConstant()!"
453 " requested=%d , should be 0-1", in_end);
455 throw JException(str);
458 const int the_cell = GetCalibIndex( in_module, in_layer, in_sector);
462 return the_table.at(the_cell).first;
465 return the_table.at(the_cell).second;
475 if ( (in_hit->
module <= 0) || (in_hit->
module > BCAL_NUM_MODULES)) {
476 sprintf(str,
"Bad module # requested in DBCALHit_factory::GetConstant()!"
477 " requested=%d , should be 1-%d", in_hit->
module, BCAL_NUM_MODULES);
479 throw JException(str);
481 if ( (in_hit->
layer <= 0) || (in_hit->
layer > BCAL_NUM_LAYERS)) {
482 sprintf(str,
"Bad layer # requested in DBCALHit_factory::GetConstant()!"
483 " requested=%d , should be 1-%d", in_hit->
layer, BCAL_NUM_LAYERS);
485 throw JException(str);
487 if ( (in_hit->
sector <= 0) || (in_hit->
sector > BCAL_NUM_SECTORS)) {
488 sprintf(str,
"Bad sector # requested in DBCALHit_factory::GetConstant()!"
489 " requested=%d , should be 1-%d", in_hit->
sector, BCAL_NUM_SECTORS);
491 throw JException(str);
496 sprintf(str,
"Bad end # requested in DBCALHit_factory::GetConstant()!"
497 " requested=%d , should be 0-1", in_hit->
end);
499 throw JException(str);
502 const int the_cell = GetCalibIndex( in_hit->
module, in_hit->
layer, in_hit->
sector );
506 return the_table.at(the_cell).first;
510 return the_table.at(the_cell).second;
521 if ( (in_digihit->
module <= 0) || (in_digihit->
module > BCAL_NUM_MODULES)) {
522 sprintf(str,
"Bad module # requested in DBCALHit_factory::GetConstant()!"
523 " requested=%d , should be 1-%d",
524 in_digihit->
module, BCAL_NUM_MODULES);
526 throw JException(str);
528 if ( (in_digihit->
layer <= 0) || (in_digihit->
layer > BCAL_NUM_LAYERS)) {
529 sprintf(str,
"Bad layer # requested in DBCALHit_factory::GetConstant()!"
530 " requested=%d , should be 1-%d",
531 in_digihit->
layer, BCAL_NUM_LAYERS);
533 throw JException(str);
535 if ( (in_digihit->
sector <= 0) || (in_digihit->
sector > BCAL_NUM_SECTORS)) {
536 sprintf(str,
"Bad sector # requested in DBCALHit_factory::GetConstant()!"
537 " requested=%d , should be 1-%d",
538 in_digihit->
sector, BCAL_NUM_SECTORS);
540 throw JException(str);
545 sprintf(str,
"Bad end # requested in DBCALHit_factory::GetConstant()!"
546 " requested=%d , should be 0-1", in_digihit->
end);
548 throw JException(str);
551 const int the_cell = GetCalibIndex( in_digihit->
module, in_digihit->
layer, in_digihit->
sector );
555 return the_table.at(the_cell).first;
558 return the_table.at(the_cell).second;
bool CheckFADC250_NoErrors(uint32_t QF) const
void FillCalibTable(bcal_digi_constants_t &table, const vector< double > &raw_table)
sprintf(text,"Post KinFit Cut")
jerror_t init(void)
Called once at program start.
void FillCalibTableShort(bcal_digi_constants_t &table, const vector< double > &raw_table)
jerror_t evnt(jana::JEventLoop *eventLoop, uint64_t eventnumber)
Called every event.
uint32_t nsamples_integral
number of samples used in integral
uint32_t nsamples_integral
number of samples used in integral
uint32_t pulse_peak
from Pulse Pedestal Data word
jerror_t brun(jana::JEventLoop *eventLoop, int32_t runnumber)
Called everytime a new run number is detected.
jerror_t fini(void)
Called after last event of last event source has been processed.
uint32_t datasource
0=window raw data, 1=old(pre-Fall16) firmware, 2=Df250PulseData, 3=MC
uint32_t pulse_peak
identified pulse height as returned by FPGA algorithm
uint32_t pulse_integral
identified pulse integral as returned by FPGA algorithm
uint32_t nsamples_pedestal
number of samples used in pedestal
pair< double, double > cell_calib_t
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
uint32_t QF
Quality Factor from FPGA algorithms.
jerror_t erun(void)
Called everytime run number changes, provided brun has been called.
bool CheckFADC250_PedestalOK(uint32_t QF) const
uint32_t pedestal
pedestal info used by FPGA (if any)
uint32_t integral
from Pulse Integral Data word
uint32_t pedestal
from Pulse Integral Data word (future)
static TH1I * pedestal[nChan]
uint32_t pulse_time
identified pulse time as returned by FPGA algorithm
uint32_t pedestal
from Pulse Pedestal Data word
vector< cell_calib_t > bcal_digi_constants_t
uint32_t nsamples_pedestal
number of samples used in pedestal
printf("string=%s", string)
float t_raw
Uncalibrated time in ns.