38 gPARMS->SetDefaultParameter(
"TOF:DEBUG_TOF_HITS",
TOF_DEBUG,
39 "Generate DEBUG output");
41 USE_NEWAMP_4WALKCORR = 1;
42 gPARMS->SetDefaultParameter(
"TOF:USE_NEWAMP_4WALKCORR", USE_NEWAMP_4WALKCORR,
43 "Use Signal Amplitude for NEW walk correction with two fit functions");
44 USE_AMP_4WALKCORR = 0;
45 gPARMS->SetDefaultParameter(
"TOF:USE_AMP_4WALKCORR", USE_AMP_4WALKCORR,
46 "Use Signal Amplitude for walk correction rather than Integral");
48 USE_NEW_4WALKCORR = 0;
49 gPARMS->SetDefaultParameter(
"TOF:USE_NEW_4WALKCORR", USE_NEW_4WALKCORR,
50 "Use NEW walk correction function with 4 parameters");
52 DELTA_T_ADC_TDC_MAX = 20.0;
54 gPARMS->SetDefaultParameter(
"TOF:DELTA_T_ADC_TDC_MAX", DELTA_T_ADC_TDC_MAX,
55 "Maximum difference in ns between a (calibrated) fADC time and F1TDC time for them to be matched in a single hit");
57 int analyze_cosmic_data = 0;
58 gPARMS->SetDefaultParameter(
"TOF:COSMIC_DATA", analyze_cosmic_data,
59 "Special settings for analysing cosmic data");
60 if(analyze_cosmic_data > 0)
63 CHECK_FADC_ERRORS =
true;
64 gPARMS->SetDefaultParameter(
"TOF:CHECK_FADC_ERRORS", CHECK_FADC_ERRORS,
"Set to 1 to reject hits with fADC250 errors, ser to 0 to keep these hits");
75 tdc_adc_time_offset = 110.;
77 tdc_adc_time_offset = 0.;
92 static pthread_mutex_t print_mutex = PTHREAD_MUTEX_INITIALIZER;
93 static set<int> runs_announced;
94 pthread_mutex_lock(&print_mutex);
95 bool print_messages =
false;
96 if(runs_announced.find(runnumber) == runs_announced.end()){
97 print_messages =
true;
98 runs_announced.insert(runnumber);
100 pthread_mutex_unlock(&print_mutex);
103 vector<const DTOFGeometry*> tofGeomVect;
104 eventLoop->Get( tofGeomVect );
105 if(tofGeomVect.size()<1)
return OBJECT_NOT_AVAILABLE;
112 vector<double> raw_adc_pedestals;
113 vector<double> raw_adc_gains;
114 vector<double> raw_adc_offsets;
115 vector<double> raw_tdc_offsets;
116 vector<double> raw_adc2E;
118 if(print_messages) jout <<
"In DTOFHit_factory, loading constants..." << endl;
121 vector<double> time_cut_values;
122 if(eventLoop->GetCalib(
"/TOF/HitTimeCut", time_cut_values)){
123 jout <<
"Error loading /TOF/HitTimeCut SET DEFUALT to 0 and 100!" << endl;
127 double loli = time_cut_values[0];
128 double hili = time_cut_values[1];
129 TimeCenterCut = hili - (hili-loli)/2.;
130 TimeWidthCut = (hili-loli)/2.;
135 map<string,double> scale_factors;
136 if(eventLoop->GetCalib(
"/TOF/digi_scales", scale_factors))
137 jout <<
"Error loading /TOF/digi_scales !" << endl;
138 if( scale_factors.find(
"TOF_ADC_ASCALE") != scale_factors.end() ) {
141 jerr <<
"Unable to get TOF_ADC_ASCALE from /TOF/digi_scales !" << endl;
143 if( scale_factors.find(
"TOF_ADC_TSCALE") != scale_factors.end() ) {
146 jerr <<
"Unable to get TOF_ADC_TSCALE from /TOF/digi_scales !" << endl;
150 map<string,double> base_time_offset;
151 if (eventLoop->GetCalib(
"/TOF/base_time_offset",base_time_offset))
152 jout <<
"Error loading /TOF/base_time_offset !" << endl;
153 if (base_time_offset.find(
"TOF_BASE_TIME_OFFSET") != base_time_offset.end())
154 t_base = base_time_offset[
"TOF_BASE_TIME_OFFSET"];
156 jerr <<
"Unable to get TOF_BASE_TIME_OFFSET from /TOF/base_time_offset !" << endl;
158 if (base_time_offset.find(
"TOF_TDC_BASE_TIME_OFFSET") != base_time_offset.end())
159 t_base_tdc = base_time_offset[
"TOF_TDC_BASE_TIME_OFFSET"];
161 jerr <<
"Unable to get TOF_TDC_BASE_TIME_OFFSET from /TOF/base_time_offset !" << endl;
164 if(eventLoop->GetCalib(
"TOF/pedestals", raw_adc_pedestals))
165 jout <<
"Error loading /TOF/pedestals !" << endl;
166 if(eventLoop->GetCalib(
"TOF/gains", raw_adc_gains))
167 jout <<
"Error loading /TOF/gains !" << endl;
168 if(eventLoop->GetCalib(
"TOF/adc_timing_offsets", raw_adc_offsets))
169 jout <<
"Error loading /TOF/adc_timing_offsets !" << endl;
171 if (USE_NEWAMP_4WALKCORR){
173 if(eventLoop->GetCalib(
"TOF/timing_offsets_NEWAMP", raw_tdc_offsets)){
176 jout<<
"Error loading /TOF/timing_offsets_NEWAMP !\033[0m" << endl;
178 USE_NEWAMP_4WALKCORR = 0;
179 jout <<
"\033[1;31m";
180 jout <<
"Try to resort back to old calibration table /TOF/timing_offsets !\033[0m" << endl;
181 if(eventLoop->GetCalib(
"TOF/timing_offsets", raw_tdc_offsets)){
182 jout <<
"Error loading /TOF/timing_offsets !" << endl;
184 jout<<
"OK Found!"<<endl;
186 if(eventLoop->GetCalib(
"TOF/timewalk_parms", timewalk_parameters))
187 jout <<
"Error loading /TOF/timewalk_parms !" << endl;
191 if(eventLoop->GetCalib(
"TOF/timewalk_parms_NEWAMP", timewalk_parameters_NEWAMP))
192 jout <<
"Error loading /TOF/timewalk_parms_NEWAMP !" << endl;
197 }
else if (USE_AMP_4WALKCORR){
198 if(eventLoop->GetCalib(
"TOF/timewalk_parms_AMP", timewalk_parameters_AMP))
199 jout <<
"Error loading /TOF/timewalk_parms_AMP !" << endl;
200 if(eventLoop->GetCalib(
"TOF/timing_offsets", raw_tdc_offsets))
201 jout <<
"Error loading /TOF/timing_offsets !" << endl;
202 }
else if (USE_NEW_4WALKCORR){
203 if(eventLoop->GetCalib(
"TOF/timewalk_parms_NEW", timewalk_parameters_NEW))
204 jout <<
"Error loading /TOF/timewalk_parms_NEW !" << endl;
205 if(eventLoop->GetCalib(
"TOF/timing_offsets", raw_tdc_offsets))
206 jout <<
"Error loading /TOF/timing_offsets !" << endl;
208 if(eventLoop->GetCalib(
"TOF/timewalk_parms", timewalk_parameters))
209 jout <<
"Error loading /TOF/timewalk_parms !" << endl;
210 if(eventLoop->GetCalib(
"TOF/timing_offsets", raw_tdc_offsets))
211 jout <<
"Error loading /TOF/timing_offsets !" << endl;
215 FillCalibTable(adc_pedestals, raw_adc_pedestals, tofGeom);
216 FillCalibTable(adc_gains, raw_adc_gains, tofGeom);
221 if(eventLoop->GetCalib(
"TOF/adc2E", raw_adc2E))
222 jout <<
"Error loading /TOF/adc2E !" << endl;
225 adc2E.resize(TOF_NUM_PLANES*TOF_NUM_BARS*2);
226 for (
unsigned int n=0; n<raw_adc2E.size(); n++){
227 adc2E[n] = raw_adc2E[n];
255 loop->GetSingle(locTTabUtilities);
258 vector<const DTOFDigiHit*> digihits;
260 for(
unsigned int i=0; i<digihits.size(); i++){
269 digihit->GetSingle(PPobj);
281 vector <const Df250PulseData *> pulses;
282 digihit->Get(pulses);
285 cout<<
"1: "<<eventnumber<<
" P/B/E "<<digihit->
plane<<
"/"<<digihit->
bar<<
"/"<<digihit->
end
287 <<
" QF: 0x"<<hex<<digihit->
QF<<dec
298 double pedestal = GetConstant(adc_pedestals, digihit);
303 if(nsamples_pedestal == 0) {
304 jerr <<
"DTOFDigiHit with nsamples_pedestal == 0 ! Event = " << eventnumber << endl;
311 pedestal = digihit->
pedestal * (double)(nsamples_integral)/(double)(nsamples_pedestal);
315 vector <const Df250PulseData *> pulses;
316 digihit->Get(pulses);
319 cout<<
"2: "<<eventnumber<<
" P/B/E "<<digihit->
plane<<
"/"<<digihit->
bar<<
"/"<<digihit->
end
321 <<
" QF: 0x"<<hex<<digihit->
QF<<dec
327 pedestal *= (double)(nsamples_integral);
328 pedestal4Amp *= (double)nsamples_pedestal;
333 if ((digihit->
pulse_peak == 0) && (!AlreadyDone)){
334 pedestal = pedestal4Amp * (double)(nsamples_integral);
335 pedestal4Amp *= (double)nsamples_pedestal;
348 vector <const Df250PulseData *> pulses;
349 digihit->Get(pulses);
352 cout<<
"3: "<<eventnumber<<
" "<<dA<<
" "<<digihit->
plane<<
" "<<digihit->
bar<<
" "<<digihit->
end
362 if (TMath::Abs(T-TimeCenterCut)> TimeWidthCut )
continue;
369 hit->
Amp = (float)digihit->
pulse_peak - pedestal4Amp/(
float)nsamples_pedestal;
378 hit->
t_TDC=numeric_limits<double>::quiet_NaN();
392 hit->AddAssociatedObject(digihit);
394 _data.push_back(hit);
398 vector<const DTOFTDCDigiHit*> tdcdigihits;
399 loop->Get(tdcdigihits);
405 for(
unsigned int i=0; i<tdcdigihits.size(); i++)
411 T += t_base_tdc - GetConstant(
tdc_time_offsets, digihit) + tdc_adc_time_offset;
414 if (TMath::Abs(T-TimeCenterCut)> TimeWidthCut )
continue;
444 newhit->
dE = hit->
dE;
448 newhit->
t_TDC=numeric_limits<double>::quiet_NaN();
451 newhit->AddAssociatedObject(digihit);
452 _data.push_back(newhit);
463 if (USE_AMP_4WALKCORR) {
465 tcorr = CalcWalkCorrAmplitude(hit);
467 }
else if (USE_NEW_4WALKCORR) {
469 tcorr = CalcWalkCorrNEW(hit);
471 }
else if (USE_NEWAMP_4WALKCORR) {
473 tcorr = CalcWalkCorrNEWAMP(hit);
477 tcorr = CalcWalkCorrIntegral(hit);
485 hit->AddAssociatedObject(digihit);
489 for (
unsigned int i=0;i<_data.size();i++){
490 int id=2*TOF_NUM_BARS*_data[i]->
plane + TOF_NUM_BARS*_data[i]->end + _data[i]->bar-1;
491 _data[i]->dE *= adc2E[id];
507 for(
unsigned int i=0; i<_data.size(); i++){
510 if(!isfinite(hit->
t_fADC))
continue;
511 if(hit->
plane != plane)
continue;
512 if(hit->
bar != bar)
continue;
513 if(hit->
end != end)
continue;
516 double delta_T = fabs(T - hit->
t);
517 if(delta_T > DELTA_T_ADC_TDC_MAX)
continue;
520 if(best_match != NULL) {
521 if(delta_T < fabs(best_match->
t - T))
561 for(
int plane=0; plane<tofGeom.
Get_NPlanes(); plane++) {
562 int plane_index=2*tofGeom.
Get_NBars()*plane;
563 table.push_back( vector< pair<double,double> >(tofGeom.
Get_NBars()) );
564 for(
int bar=0; bar<tofGeom.
Get_NBars(); bar++) {
566 = pair<double,double>(raw_table[plane_index+bar],
567 raw_table[plane_index+tofGeom.
Get_NBars()+bar]);
573 if(channel != TOF_MAX_CHANNELS) {
574 sprintf(str,
"Not enough channels for TOF table! channel=%d (should be %d)",
575 channel, TOF_MAX_CHANNELS);
577 throw JException(str);
594 const int in_plane,
const int in_bar,
const int in_end)
const
598 if( (in_plane < 0) || (in_plane > TOF_NUM_PLANES)) {
599 sprintf(str,
"Bad module # requested in DTOFHit_factory::GetConstant()! requested=%d , should be 1-%d", in_plane, TOF_NUM_PLANES);
601 throw JException(str);
603 if( (in_bar <= 0) || (in_bar > TOF_NUM_BARS)) {
604 sprintf(str,
"Bad layer # requested in DTOFHit_factory::GetConstant()! requested=%d , should be 1-%d", in_bar, TOF_NUM_BARS);
606 throw JException(str);
608 if( (in_end != 0) && (in_end != 1) ) {
609 sprintf(str,
"Bad end # requested in DTOFHit_factory::GetConstant()! requested=%d , should be 0-1", in_end);
611 throw JException(str);
617 return the_table[in_plane][in_bar].first;
619 return the_table[in_plane][in_bar].second;
628 if( (in_hit->
plane < 0) || (in_hit->
plane > TOF_NUM_PLANES)) {
629 sprintf(str,
"Bad module # requested in DTOFHit_factory::GetConstant()! requested=%d , should be 1-%d", in_hit->
plane, TOF_NUM_PLANES);
631 throw JException(str);
633 if( (in_hit->
bar <= 0) || (in_hit->
bar > TOF_NUM_BARS)) {
634 sprintf(str,
"Bad layer # requested in DTOFHit_factory::GetConstant()! requested=%d , should be 1-%d", in_hit->
bar, TOF_NUM_BARS);
636 throw JException(str);
638 if( (in_hit->
end != 0) && (in_hit->
end != 1) ) {
639 sprintf(str,
"Bad end # requested in DTOFHit_factory::GetConstant()! requested=%d , should be 0-1", in_hit->
end);
641 throw JException(str);
646 if(in_hit->
end == 0) {
647 return the_table[in_hit->
plane][in_hit->
bar-1].first;
649 return the_table[in_hit->
plane][in_hit->
bar-1].second;
658 if( (in_digihit->
plane < 0) || (in_digihit->
plane > TOF_NUM_PLANES)) {
659 sprintf(str,
"Bad module # requested in DTOFHit_factory::GetConstant()! requested=%d , should be 1-%d", in_digihit->
plane, TOF_NUM_PLANES);
661 throw JException(str);
663 if( (in_digihit->
bar <= 0) || (in_digihit->
bar > TOF_NUM_BARS)) {
664 sprintf(str,
"Bad layer # requested in DTOFHit_factory::GetConstant()! requested=%d , should be 1-%d", in_digihit->
bar, TOF_NUM_BARS);
666 throw JException(str);
668 if( (in_digihit->
end != 0) && (in_digihit->
end != 1) ) {
669 sprintf(str,
"Bad end # requested in DTOFHit_factory::GetConstant()! requested=%d , should be 0-1", in_digihit->
end);
671 throw JException(str);
676 if(in_digihit->
end == 0) {
677 return the_table[in_digihit->
plane][in_digihit->
bar-1].first;
679 return the_table[in_digihit->
plane][in_digihit->
bar-1].second;
688 if( (in_digihit->
plane < 0) || (in_digihit->
plane > TOF_NUM_PLANES)) {
689 sprintf(str,
"Bad module # requested in DTOFHit_factory::GetConstant()! requested=%d , should be 1-%d", in_digihit->
plane, TOF_NUM_PLANES);
691 throw JException(str);
693 if( (in_digihit->
bar <= 0) || (in_digihit->
bar > TOF_NUM_BARS)) {
694 sprintf(str,
"Bad layer # requested in DTOFHit_factory::GetConstant()! requested=%d , should be 1-%d", in_digihit->
bar, TOF_NUM_BARS);
696 throw JException(str);
698 if( (in_digihit->
end != 0) && (in_digihit->
end != 1) ) {
699 sprintf(str,
"Bad end # requested in DTOFHit_factory::GetConstant()! requested=%d , should be 0-1", in_digihit->
end);
701 throw JException(str);
706 if(in_digihit->
end == 0) {
707 return the_table[in_digihit->
plane][in_digihit->
bar-1].first;
709 return the_table[in_digihit->
plane][in_digihit->
bar-1].second;
755 int id=2*TOF_NUM_BARS*hit->
plane+TOF_NUM_BARS*hit->
end+hit->
bar-1;
757 double C0=timewalk_parameters[id][1];
758 double C1=timewalk_parameters[id][1];
759 double C2=timewalk_parameters[id][2];
760 double A0=timewalk_parameters[id][3];
762 double a1 = C0 + C1*pow(A,C2);
763 double a2 = C0 + C1*pow(A0,C2);
765 float corr = a1 - a2;
777 int id=2*TOF_NUM_BARS*hit->
plane+TOF_NUM_BARS*hit->
end+hit->
bar-1;
779 double C0 = timewalk_parameters_AMP[id][0];
780 double C1 = timewalk_parameters_AMP[id][1];
781 double C2 = timewalk_parameters_AMP[id][2];
782 double C3 = timewalk_parameters_AMP[id][3];
784 double hookx = timewalk_parameters_AMP[id][4];
785 double refx = timewalk_parameters_AMP[id][5];
786 double val_at_ref = C0 + C1*pow(refx,C2);
787 double val_at_hook = C0 + C1*pow(hookx,C2);
788 double slope = (val_at_hook - C3)/hookx;
790 val_at_ref = slope * refx + C3;
792 double val_at_A = C0 + C1*pow(A,C2);
794 val_at_A = slope * A + C3;
797 float corr = val_at_A - val_at_ref;
808 int id=2*TOF_NUM_BARS*hit->
plane+TOF_NUM_BARS*hit->
end+hit->
bar-1;
815 double A = timewalk_parameters_NEW[id][0];
816 double B = timewalk_parameters_NEW[id][1];
817 double C = timewalk_parameters_NEW[id][2];
818 double D = timewalk_parameters_NEW[id][3];
819 double ADCREF = timewalk_parameters_NEW[id][4];
824 double a1 = A + B*pow(ADC,-0.5) + C*pow(ADC,-0.33) + D*pow(ADC,-0.2);
825 double a2 = A + B*pow(ADCREF,-0.5) + C*pow(ADCREF,-0.33) + D*pow(ADCREF,-0.2);
828 float corr = a1 - a2;
838 int id=2*TOF_NUM_BARS*hit->
plane+TOF_NUM_BARS*hit->
end+hit->
bar-1;
843 double loc = timewalk_parameters_NEWAMP[id][8];
848 double A = timewalk_parameters_NEWAMP[id][0+offset];
849 double B = timewalk_parameters_NEWAMP[id][1+offset];
850 double C = timewalk_parameters_NEWAMP[id][2+offset];
851 double D = timewalk_parameters_NEWAMP[id][3+offset];
853 double ADCREF = timewalk_parameters_NEWAMP[id][9];
854 double A2 = timewalk_parameters_NEWAMP[id][4];
855 double B2 = timewalk_parameters_NEWAMP[id][5];
856 double C2 = timewalk_parameters_NEWAMP[id][6];
857 double D2 = timewalk_parameters_NEWAMP[id][7];
859 double a1 = A + B*pow(ADC,-0.5) + C*pow(ADC,-0.33) + D*pow(ADC,-0.2);
860 double a2 = A2 + B2*pow(ADCREF,-0.5) + C2*pow(ADCREF,-0.33) + D2*pow(ADCREF,-0.2);
866 float corr = a1 - a2;
jerror_t evnt(jana::JEventLoop *eventLoop, uint64_t eventnumber)
vector< vector< pair< double, double > > > tof_digi_constants_t
bool CheckFADC250_NoErrors(uint32_t QF) const
DTOFHit * FindMatch(int plane, int bar, int end, double T)
uint32_t pedestal
pedestal info used by FPGA (if any)
sprintf(text,"Post KinFit Cut")
double Convert_DigiTimeToNs_CAEN1290TDC(const JObject *locTDCDigiHit) const
uint32_t nsamples_integral
number of samples used in integral
uint32_t pulse_peak
from Pulse Pedestal Data word
double CalcWalkCorrAmplitude(DTOFHit *hit)
void FillCalibTable(tof_digi_constants_t &table, vector< double > &raw_table, const DTOFGeometry &tofGeom)
uint32_t nsamples_pedestal
number of samples used in pedestal
bool CheckFADC250_PedestalOK(uint32_t QF) const
vector< double > tdc_time_offsets
int end
left/right 0/1 or North/South 0/1
static TH1I * pedestal[nChan]
double CalcWalkCorrNEWAMP(DTOFHit *hit)
uint32_t pulse_time
identified pulse time as returned by FPGA algorithm
uint32_t pedestal
from Pulse Pedestal Data word
File: DTOFHit.h Created: Tue Jan 18 16:15:26 EST 2011 Creator: B. Zihlmann Purpose: Container class t...
uint32_t pulse_peak
maximum sample in pulse
uint32_t QF
Quality Factor from FPGA algorithms.
int plane
plane (0: vertical, 1: horizontal)
double CalcWalkCorrNEW(DTOFHit *hit)
vector< double > adc_time_offsets
const double GetConstant(const tof_digi_constants_t &the_table, const int in_plane, const int in_bar, const int in_end) const
jerror_t brun(jana::JEventLoop *eventLoop, int32_t runnumber)
uint32_t datasource
0=window raw data, 1=old(pre-Fall16) firmware, 2=Df250PulseData
double CalcWalkCorrIntegral(DTOFHit *hit)
uint32_t pulse_integral
identified pulse integral as returned by FPGA algorithm