47 for(
unsigned int i = 0; i < input_file_names.size(); i++ ) {
67 input_file =
new TFile( input_file_name.data(),
"read" );
70 std::cout <<
"Cannot open " << input_file_name << std::endl;
113 output_file =
new TFile( output_file_name.data(),
"recreate" );
119 std::string cal_file_name = output_file_name.substr( 0, output_file_name.find_last_of(
".") );
120 cal_file_name +=
".cal";
121 output_cal.open( cal_file_name.data(), std::ios::trunc );
138 std::vector<unsigned int>().swap(
cd_Q_list);
139 std::vector<unsigned long long>().swap(
cd_ts_list);
151 if (n == 0)
return 1;
166 std::string hname, htitle;
175 double maxEn =
set->GetCDCalibratorMaxEnergy();
177 double maxRawEn = ( maxEn -
cal->FebexOffset(1,0,
ptag) ) /
cal->FebexGain(1,0,
ptag);
180 unsigned int Qbins = 8192;
182 if(
set->GetNumberOfCaenAdcModules() > 0 ) {
187 else if(
set->GetNumberOfFebexSfps() > 1 &&
188 set->GetNumberOfFebexBoards() > 0 &&
189 set->GetNumberOfFebexChannels() > 0 ) {
191 if(
cal->FebexType( 1, 0, 0 ) ==
"Qshort" ) {
196 for(
unsigned int i = 0; i <
set->GetNumberOfCDDetectors(); ++i ) {
201 for(
unsigned int j = 0; j <
set->GetNumberOfCDSectors(); ++j ) {
203 cd_nQ_pQ[i][j].resize(
set->GetNumberOfCDPStrips() );
205 for(
unsigned int k = 0; k <
set->GetNumberOfCDPStrips(); ++k ) {
207 hname =
"cd_" + std::to_string(i) +
"_" + std::to_string(j);
208 hname +=
"_nQ_" + std::to_string(
ntag) +
"_pQ_" + std::to_string(k);
209 htitle =
"CD n-side raw charge vs p-side raw charge for detector " + std::to_string(i);
210 htitle +=
", sector " + std::to_string(j) +
", pid " + std::to_string(k);
211 htitle +=
", nid " + std::to_string(
ntag);
212 htitle +=
";n-side raw charge (ADC units);p-side raw charge (ADC units);Counts";
213 cd_nQ_pQ[i][j][k] =
new TH2F( hname.data(), htitle.data(),
214 Qbins, 0, maxQ, Qbins, 0, maxQ );
221 for(
unsigned int k = 0; k <
set->GetNumberOfCDNStrips(); ++k ) {
223 hname =
"cd_" + std::to_string(i) +
"_" + std::to_string(j);
224 hname +=
"_pen_" + std::to_string(
ptag) +
"_nQ_" + std::to_string(k);
225 htitle =
"CD p-side energy vs n-side raw charge for detector " + std::to_string(i);
226 htitle +=
", sector " + std::to_string(j) +
", pid " + std::to_string(
ptag);
227 htitle +=
", nid " + std::to_string(k);
228 htitle +=
";p-side energy (keV);n-side raw charge (ADC units);Counts";
229 cd_pen_nQ[i][j][k] =
new TH2F( hname.data(), htitle.data(),
230 4000, 0, maxEn, Qbins, 0, maxQ );
251 TIter next(
histlist->MakeIterator() );
252 while( TObject *obj = next() ) {
254 if( obj->InheritsFrom(
"TH2" ) )
255 ( (TH2*)obj )->Reset(
"ICESM");
256 else if( obj->InheritsFrom(
"TH1" ) )
257 ( (TH1*)obj )->Reset(
"ICESM");
268 for(
unsigned int m = 0; m <
set->GetNumberOfCaenAdcModules(); ++m ) {
271 for(
unsigned int c = 0; c <
set->GetNumberOfCaenAdcChannels(); ++c ) {
274 if( !
set->IsCD(m,c) )
continue;
277 if(
set->GetCDDetector(m,c) != det )
continue;
280 if(
set->GetCDSector(m,c) != sec )
continue;
283 if(
set->GetCDSide(m,c) != side )
continue;
286 if(
set->GetCDStrip(m,c) != strip )
continue;
297 std::cerr <<
"CD strip not found, det=" << det <<
", sec=" << sec;
298 std::cerr <<
", side=" << side <<
", strip=" << strip << std::endl;
306 for(
unsigned int s = 0; s <
set->GetNumberOfFebexSfps(); ++s ) {
309 for(
unsigned int m = 0; m <
set->GetNumberOfFebexBoards(); ++m ) {
312 for(
unsigned int c = 0; c <
set->GetNumberOfFebexChannels(); ++c ) {
315 if( !
set->IsCD(s,m,c) )
continue;
318 if(
set->GetCDDetector(s,m,c) != det )
continue;
321 if(
set->GetCDSector(s,m,c) != sec )
continue;
324 if(
set->GetCDSide(s,m,c) != side )
continue;
327 if(
set->GetCDStrip(s,m,c) != strip )
continue;
341 std::cerr <<
"CD strip not found, det=" << det <<
", sec=" << sec;
342 std::cerr <<
", side=" << side <<
", strip=" << strip << std::endl;
351 if(
set->GetNumberOfCaenAdcModules() > 0 )
355 auto pfit = std::make_unique<TF1>(
"pfit",
"[0]+[1]*x", 0, 1e9 );
358 gErrorIgnoreLevel = kError;
359 std::vector<std::vector<std::unique_ptr<TCanvas>>> canv;
360 canv.resize(
set->GetNumberOfCDDetectors() );
363 for(
unsigned int i = 0; i <
set->GetNumberOfCDDetectors(); ++i ) {
365 canv[i].resize(
set->GetNumberOfCDSectors() );
368 for(
unsigned int j = 0; j <
set->GetNumberOfCDSectors(); ++j ) {
370 std::string cname =
"cdcal_p_" + std::to_string(i) +
"_" + std::to_string(j);
371 canv[i][j] = std::make_unique<TCanvas>( cname.data(), cname.data(), 800, 1000 );
374 for(
unsigned int k = 0; k <
set->GetNumberOfCDPStrips(); ++k ) {
377 auto res =
cd_nQ_pQ[i][j][k]->Fit( pfit.get(),
"QWL" );
378 if( res != 0 )
continue;
379 double fit_gain =
ngain / pfit->GetParameter(1);
380 double fit_offset =
noffset - pfit->GetParameter(0) * fit_gain;
384 std::cout <<
"!! This is the p-side tag channel, cross-check check the parameters below !!" << std::endl;
390 std::string cal_base;
391 std::string modchstr;
399 modchstr = std::to_string(fmod) +
"_" + std::to_string(fch);
409 modchstr = std::to_string(fsfp) +
"_" + std::to_string(fmod);
410 modchstr +=
"_" + std::to_string(fch);
415 std::string gainstr = cal_base + modchstr +
".Gain: " + std::to_string( fit_gain );
416 std::string offsetstr = cal_base + modchstr +
".Offset: " + std::to_string( fit_offset );
419 std::cout << gainstr << std::endl;
420 std::cout << offsetstr << std::endl;
425 std::string pdfname = cname +
".pdf";
426 if( k == 0 &&
set->GetNumberOfCDPStrips() != 1 )
428 else if( k > 0 && k ==
set->GetNumberOfCDPStrips() - 1 )
430 canv[i][j]->Print( pdfname.data(),
"pdf" );
439 gErrorIgnoreLevel = kInfo;
449 if(
set->GetNumberOfCaenAdcModules() > 0 )
453 auto nfit = std::make_unique<TF1>(
"nfit",
"[0]+[1]*x", 0, 1e9 );
456 gErrorIgnoreLevel = kError;
457 std::vector<std::vector<std::unique_ptr<TCanvas>>> canv;
458 canv.resize(
set->GetNumberOfCDDetectors() );
461 for(
unsigned int i = 0; i <
set->GetNumberOfCDDetectors(); ++i ) {
463 canv[i].resize(
set->GetNumberOfCDSectors() );
466 for(
unsigned int j = 0; j <
set->GetNumberOfCDSectors(); ++j ) {
468 std::string cname =
"cdcal_n_" + std::to_string(i) +
"_" + std::to_string(j);
469 canv[i][j] = std::make_unique<TCanvas>( cname.data(), cname.data(), 800, 1000 );
472 for(
unsigned int k = 0; k <
set->GetNumberOfCDNStrips(); ++k ) {
475 auto res =
cd_pen_nQ[i][j][k]->Fit( nfit.get(),
"QWL" );
476 if( res != 0 )
continue;
477 double fit_gain = 1.0 / nfit->GetParameter(1);
478 double fit_offset = -1.0 * nfit->GetParameter(0) * fit_gain;
489 std::string cal_base;
490 std::string modchstr;
498 modchstr = std::to_string(fmod) +
"_" + std::to_string(fch);
508 modchstr = std::to_string(fsfp) +
"_" + std::to_string(fmod);
509 modchstr +=
"_" + std::to_string(fch);
514 std::string gainstr = cal_base + modchstr +
".Gain: " + std::to_string( fit_gain );
515 std::string offsetstr = cal_base + modchstr +
".Offset: " + std::to_string( fit_offset );
518 std::cout << gainstr << std::endl;
519 std::cout << offsetstr << std::endl;
524 std::string pdfname = cname +
".pdf";
525 if( k == 0 &&
set->GetNumberOfCDNStrips() != 1 )
527 else if( k > 0 && k ==
set->GetNumberOfCDNStrips() - 1 )
529 canv[i][j]->Print( pdfname.data(),
"pdf" );
538 gErrorIgnoreLevel = kInfo;
547 std::vector<unsigned char> pindex;
548 std::vector<unsigned char> nindex;
551 for(
unsigned int i = 0; i <
set->GetNumberOfCDDetectors(); ++i ){
553 for(
unsigned int j = 0; j <
set->GetNumberOfCDSectors(); ++j ){
558 std::vector<unsigned char>().swap(pindex);
559 std::vector<unsigned char>().swap(nindex);
562 for(
unsigned int k = 0; k <
cd_en_list.size(); ++k ){
578 if( pindex.size() != 1 || nindex.size() != 1 )
590 if( nQ / pQ > 1.5 || pQ / nQ > 1.5 )
continue;
602 cd_nQ_pQ[i][j][pid]->Fill( nQ, pQ );
619 std::cout <<
" CD Calibrator: nothing to do" << std::endl;
629 std::cout <<
" CD Calibrator: number of entries in input tree = ";
632 std::cout <<
"\tnumber of MBS Events/triggers in input tree = ";
638 for(
unsigned long i = 0; i <
n_entries; ++i ) {
662 std::cerr <<
"Didn't find matching MBS Event IDs at start of the file: ";
675 if(
set->GetMbsEventMode() ) {
687 std::cout <<
"Out of order event in ";
688 std::cout <<
input_tree->GetName() << std::endl;
695 std::cout <<
"Out of order MBS event " <<
myeventid;
720 unsigned int adc_tmp_value;
770 unsigned int adc_tmp_value =
adc_data->GetQshort();
840 std::cerr <<
"MBS Event " <<
myeventid <<
" not found by index, looking up manually" << std::endl;
853 std::cerr <<
"Didn't find matching MBS Event IDs at start of the file: ";
867 if(
set->GetMbsEventMode() ) {
904 bool update_progress =
false;
906 update_progress =
true;
908 update_progress =
true;
910 if( update_progress ) {
913 float percent = (float)(i+1)*100.0/(float)
n_entries;
918 prog->SetPosition( percent );
919 gSystem->ProcessEvents();
924 std::cout <<
" " << std::setw(6) << std::setprecision(4);
925 std::cout << percent <<
"% \r";
937 std::cout <<
"\n\nUsing p-side strip " << (int)
ptag <<
" as reference for calibrating n-sides" << std::endl;
939 std::cout <<
"\n\nUsing n-side strip " << (int)
ntag <<
" as reference for calibrating p-sides" << std::endl;
946 std::cout <<
"\n MiniballCDCalibrator finished..." << std::endl;
int nextPowerOf2(unsigned int n)
unsigned long long int GetEventID()
unsigned long long myeventid
MBS event id.
unsigned long long mytime
absolute timestamp
std::shared_ptr< TGProgressBar > prog
unsigned long long time_max
void SetOutput(std::string output_file_name, bool cWrite=false)
unsigned char myboard
febex board number
bool mythres
above threshold?
unsigned char mysfp
sfp number
float myenergy
calibrated energy
unsigned long long preveventid
previous MBS event id
std::vector< unsigned int > cd_Q_list
list of CD uncalibrated energies for ParticleFinder
long myhittime
hit time with respect to event time
unsigned long long time_first
std::vector< std::vector< std::vector< TH2F * > > > cd_pen_nQ
MBSInfoPackets * mbs_info
unsigned char myadc
ADC module number.
std::shared_ptr< FebexData > febex_data
bool FindCDChannels(int det, int sec, int side, int strip, int &adc, int &ch)
std::vector< unsigned char > cd_side_list
list of CD side IDs; 0 = p, 1 = n
unsigned long long time_prev
bool mypileup
pileup flag?
MiniballCDCalibrator(std::shared_ptr< MiniballSettings > myset)
unsigned long FillHists()
unsigned char mych
channel number
void Initialise()
called for every event
std::vector< std::vector< std::vector< TH2F * > > > cd_nQ_pQ
std::shared_ptr< MiniballCalibration > cal
std::vector< unsigned char > cd_det_list
list of CD detector IDs
void SetInputFile(std::vector< std::string > input_file_names)
unsigned long n_mbs_entries
std::vector< float > cd_en_list
list of CD energies for ParticleFinder
std::vector< unsigned char > cd_strip_list
list of CD strip IDs
std::vector< unsigned char > cd_sec_list
list of CD sector IDs
void SetInputTree(TTree *user_tree)
bool myclipped
clipped flag?
void StartFile()
called for every file
unsigned long long myeventtime
MBS event time.
std::vector< unsigned long long > cd_ts_list
list of CD timestamps for ParticleFinder
std::shared_ptr< MiniballSettings > set
MiniballDataPackets * in_data
void SetMBSInfoTree(TTree *user_tree)
std::shared_ptr< AdcData > adc_data
unsigned long long time_min
TFile * output_file
Outputs.
TFile * input_file
Input tree.
bool flag_close_event
length of build window in ns
unsigned long long int GetEventID() const
std::shared_ptr< FebexData > GetFebexData() const
std::shared_ptr< AdcData > GetAdcData() const
long long int GetTime() const
std::shared_ptr< MiniballSettings > myset