17 if (inTotFile.empty())
return false;
19 TFile totFile(inTotFile.c_str(),
"READ");
20 if (not totFile.IsOpen()) {
21 printf(
"Error - File %s could not be opened.\n",inTotFile.c_str());
25 printf(
"File %s opened.\n",inTotFile.c_str());
26 printf(
"Running TOT calibration...\n");
30 std::vector< std::vector< std::unique_ptr<TH1F> > > histogramsTOT;
31 std::vector< std::vector< std::unique_ptr<TH1F> > > histogramsTOTsig;
32 for(
unsigned int FE = 0; FE <
m_nFE; FE++){
34 histogramsTOT.push_back( std::vector< std::unique_ptr<TH1F> >() );
35 histogramsTOTsig.push_back( std::vector< std::unique_ptr<TH1F> >() );
41 htot->SetDirectory(0);
43 htotsig->SetDirectory(0);
45 histogramsTOT.at(FE).push_back(std::move(htot));
47 histogramsTOTsig.at(FE).push_back(std::move(htotsig));
55 while ((rodKey=
static_cast<TKey*
>(rodItr()))) {
56 TDirectoryFile* rodDir =
static_cast<TDirectoryFile*
>(rodKey->ReadObj());
58 const TString rodName(rodKey->GetName());
59 printf(
"%s\n",rodName.Data());
61 while ((modKey=
static_cast<TKey*
>(modItr()))) {
62 const TString
modName(modKey->GetName());
71 printf(
" -> %s\n",
modName.Data());
74 std::array<std::array<float, m_ncharge>,
m_nFE> totArrI{};
75 std::array<std::array<float, m_ncharge>,
m_nFE> totErrArrI{};
76 std::array<std::array<float, m_ncharge>,
m_nFE> totSigArrI{};
77 std::array<std::array<float, m_ncharge>,
m_nFE> totSigErrArrI{};
78 std::array<std::array<float, m_ncharge>,
m_nFE> totLongArrI{};
79 std::array<std::array<float, m_ncharge>,
m_nFE> totErrLongArrI{};
87 if(!h2dTOTmean or !h2dTOTsig) {
90 h2dTOTmean->SetDirectory(0);
91 h2dTOTsig->SetDirectory(0);
93 for (
unsigned int ieta = 0; ieta <
m_etaBins; ieta++) {
94 for (
unsigned int iphi = 0; iphi <
m_phiBins; iphi++) {
95 float totmean = h2dTOTmean->GetBinContent(ieta + 1, iphi + 1);
96 float totsig = h2dTOTsig ->GetBinContent(ieta + 1, iphi + 1);
102 int FE =
chipId(iphi, ieta);
109 histogramsTOT.at(FE).at(
pixel)->Fill(totmean);
110 histogramsTOTsig.at(FE).at(
pixel)->Fill(totsig);
120 for(
unsigned int FE = 0; FE <
m_nFE; FE++){
124 totArrI.at(FE).at(
c) = histogramsTOT.at(FE).at(
pixel)->GetMean();
125 totErrArrI.at(FE).at(
c) = histogramsTOT.at(FE).at(
pixel)->GetMeanError();
126 totSigArrI.at(FE).at(
c) = std::sqrt(
std::pow(histogramsTOTsig.at(FE).at(
pixel)->GetMean() ,2)+
std::pow(histogramsTOT.at(FE).at(
pixel)->GetRMS() ,2));
127 totSigErrArrI.at(FE).at(
c) = std::sqrt(
std::pow(histogramsTOTsig.at(FE).at(
pixel)->GetMeanError(),2)+
std::pow(histogramsTOT.at(FE).at(
pixel)->GetRMSError(),2));
129 if(totSigErrArrI.at(FE).at(
c) > 1.0){
130 totArrI.at(FE).at(
c) = 0.0;
135 totLongArrI.at(FE).at(
c) = histogramsTOT.at(FE).at(
pixel)->GetMean();
136 totErrLongArrI.at(FE).at(
c) = histogramsTOT.at(FE).at(
pixel)->GetMeanError();
138 if(totErrLongArrI.at(FE).at(
c) > 1.0){
139 totLongArrI.at(FE).at(
c) = 0.0;
145 histogramsTOT.at(FE).at(
pixel)->Reset(
"ICESM");
146 histogramsTOTsig.at(FE).at(
pixel)->Reset(
"ICESM");
152 for(
unsigned int FE = 0; FE <
m_nFE; FE++) {
156 std::vector<float> v_Q;
157 std::vector<float> v_Qerr;
158 std::vector<float> v_TOT;
159 std::vector<float> v_TOTerr;
160 std::vector<float> v_TOTsig;
161 std::vector<float> v_TOTsigerr;
162 std::vector<float> v_TOTlong;
163 std::vector<float> v_TOTlongerr;
174 std::vector<float> pixNormalParams;
175 std::vector<float> pixNormalParamsQuality;
176 std::vector<float> pixSigParams;
177 std::vector<float> pixSigParamsQuality;
178 std::vector<float> pixLongParams;
179 std::vector<float> pixLongParamsQuality;
184 int vecsize = v_Q.size();
186 std::unique_ptr<TGraphErrors> graphnormal = std::make_unique<TGraphErrors>(vecsize, &v_Q.at(0), &v_TOT.at(0) , &v_Qerr.at(0), &v_TOTerr.at(0) );
187 std::unique_ptr<TGraphErrors> graphsig = std::make_unique<TGraphErrors>(vecsize, &v_Q.at(0), &v_TOTsig.at(0) , &v_Qerr.at(0), &v_TOTsigerr.at(0) );
192 graphnormal->Fit(functnormal.get() ,
"MRQ");
193 graphsig ->Fit(functnormalsig.get(),
"MRQ");
195 pixNormalParams =
getParams(functnormal.get() ,3 );
196 pixSigParams =
getParams(functnormalsig.get(),2 );
213 graphnormal->Write(TString(
"normal_fit_"+
std::to_string(n_fit)), TObject::kWriteDelete);
214 graphsig->Write(TString(
"smearing_fit_"+
std::to_string(n_fit)), TObject::kWriteDelete);
219 functnormalsig.reset();
224 }
while(
reFit_normalPix(pixNormalParams, v_Q, v_Qerr, v_TOT, v_TOTerr, v_TOTsig, v_TOTsigerr, FE ) );
240 int vecsize = v_Q.size();
242 std::unique_ptr<TGraphErrors> graflong = std::make_unique<TGraphErrors>(vecsize, &v_Q.at(0), &v_TOTlong.at(0), &v_Qerr.at(0), &v_TOTlongerr.at(0) );
246 graflong ->Fit(functlong.get() ,
"MRQ");
248 pixLongParams =
getParams(functlong.get() ,3 );
262 graflong->Write(TString(
"long_ganged_fit_"+
std::to_string(n_fit)), TObject::kWriteDelete);
278 auto itr = map_info.find( modID );
280 if (itr != map_info.end()) {
281 (itr->second).at(FE)->set_NormalParams( pixNormalParams);
282 (itr->second).at(FE)->set_LongParams ( pixLongParams );
283 (itr->second).at(FE)->set_SigParams ( pixSigParams );
285 (itr->second).at(FE)->set_times_fitted ( n_fit );
287 (itr->second).at(FE)->set_NormalParamsQuality( pixNormalParamsQuality);
288 (itr->second).at(FE)->set_LongParamsQuality ( pixLongParamsQuality );
289 (itr->second).at(FE)->set_SigParamsQuality ( pixSigParamsQuality );
292 printf(
"Error - Module not found in fitting step... Skipping.\n");
301 for(
unsigned int FE = 0; FE <
m_nFE; FE++) {
303 histogramsTOT.at(FE).at(
pixel).reset();
304 histogramsTOTsig.at(FE).at(
pixel).reset();
314 bool Calib::reFit_normalPix(std::vector<float> &
params, std::vector<float> &
q, std::vector<float> &qerr, std::vector<float> &
tot, std::vector<float> &toterr, std::vector<float> &
sig, std::vector<float> &sigerr,
const unsigned int fe){
316 float vecFit_size =
q.size()+1;
320 if(vecFit_size < stopFit) {
327 printf(
"reFit_normalPix: Refitting skipped. Not enough points to fit.\n");
332 float parAI0 =
params.at(0);
333 float parEI0 =
params.at(1);
334 float parCI0 =
params.at(2);
336 std::vector<float> v_discrepancy;
338 for(
unsigned int i = 0;
i <
q.size();
i++){
339 float discrepancy = std::abs( 1 - ( (parAI0 * parEI0 - parCI0 *
tot.at(
i)) / (
tot.at(
i) - parAI0) ) /
q.at(
i) );
344 v_discrepancy.push_back(discrepancy);
347 auto itr_max = std::max_element(v_discrepancy.begin(),v_discrepancy.end());
351 size_t n_max =
std::distance(v_discrepancy.begin(), itr_max);
352 printf(
"FE %02u Refitted, removing charge %5.0f with chi_error %7.5f\n", fe ,
q.at(n_max),*itr_max);
353 q.erase(
q.begin()+n_max);
354 qerr.erase(qerr.begin()+n_max);
355 tot.erase(
tot.begin()+n_max);
356 toterr.erase(toterr.begin()+n_max);
357 sig.erase(
sig.begin()+n_max);
358 sigerr.erase(sigerr.begin()+n_max);
370 if (inTimFile.empty())
return false;
372 TFile timFile(inTimFile.c_str(),
"READ");
373 if (not timFile.IsOpen()) {
374 printf(
"Error - File %s could not be opened.\n",inTimFile.c_str());
377 printf(
"File %s opened.\n",inTimFile.c_str());
378 printf(
"Running timming calibration...\n");
382 std::vector< std::vector< std::unique_ptr<TH1F> > > histogramsTIM;
383 for(
unsigned int FE = 0; FE <
m_nFE; FE++){
385 histogramsTIM.push_back( std::vector< std::unique_ptr<TH1F> >() );
392 histogramsTIM.at(FE).push_back(std::move(
h));
399 while ((rodKey=
static_cast<TKey*
>(rodItr()))) {
400 const TString rodName(rodKey->GetName());
401 TDirectoryFile* rodDir =
static_cast<TDirectoryFile*
>(rodKey->ReadObj());
406 while ((modKey=
static_cast<TKey*
>(modItr()))) {
407 TString
modName(modKey->GetName());
413 printf(
"Error - Module %s not found in the PixelMapping tool\n",
modName.Data());
422 h2dTim->SetDirectory(0);
424 for (
unsigned int ieta = 0; ieta <
m_etaBins; ieta++) {
425 for (
unsigned int iphi = 0; iphi <
m_phiBins; iphi++) {
427 float tim = h2dTim->GetBinContent(ieta + 1, iphi + 1);
433 int FE =
chipId(iphi, ieta);
440 histogramsTIM.at(FE).at(
pixel)->Fill(tim);
448 auto itr = map_info.find( modID );
449 if (itr == map_info.end()) {
450 printf(
"Calib::fillTiming: ERROR - Mod ID= %16s not found. Creating it -----> Inform Pixel Offline Software Experts... \n",std::string(
modName).c_str());
452 map_info[modID] = std::vector<std::unique_ptr<CalibFrontEndInfo>> ();
454 for(
unsigned int FE = 0; FE <
m_nFE; FE++){
456 map_info[modID].push_back( std::unique_ptr<CalibFrontEndInfo>() );
457 std::unique_ptr<CalibFrontEndInfo>
p = std::make_unique<CalibFrontEndInfo>(modID,FE,std::string(
modName),std::string(rodKey->GetName()));
458 map_info[modID].at(FE) = std::move(
p);
463 int tim_mean = histogramsTIM.at(FE).at(
pixel)->GetMean();
466 histogramsTIM.at(FE).at(
pixel)->Reset(
"ICESM");
469 map_info[modID].at(FE)->set_NormalIntime(tim_mean);
472 map_info[modID].at(FE)->set_LongIntime(tim_mean);
475 map_info[modID].at(FE)->set_GangedIntime(tim_mean);
478 printf(
"Error - Bad pixel in Calib::fillTiming\n");
486 for(
unsigned int FE = 0; FE <
m_nFE; FE++){
491 int tim_mean = histogramsTIM.at(FE).at(
pixel)->GetMean();
502 histogramsTIM.at(FE).at(
pixel)->SetTitle(
"Intime;Pixel intime;Counts");
504 std::string
type =
"";
509 histogramsTIM.at(FE).at(
pixel)->Write(TString(
"intime_"+
type).
Data(), TObject::kWriteDelete);
513 histogramsTIM.at(FE).at(
pixel)->Reset(
"ICESM");
516 (itr->second).at(FE)->set_NormalIntime(tim_mean);
519 (itr->second).at(FE)->set_LongIntime(tim_mean);
522 (itr->second).at(FE)->set_GangedIntime(tim_mean);
525 printf(
"Error - Bad pixel in Calib::fillTiming\n");
534 for(
unsigned int FE = 0; FE <
m_nFE; FE++) {
536 histogramsTIM.at(FE).at(
pixel).reset();
542 printf(
"DONE with threshold calibration.\n");
550 if (inThrFile.empty())
return false;
552 TFile riThrFile(inThrFile.c_str(),
"READ");
553 if (not riThrFile.IsOpen()) {
554 printf(
"Error - File %s could not be opened.\n",inThrFile.c_str());
557 printf(
"File %s opened.\n",inThrFile.c_str());
558 printf(
"Running threshold calibration...\n");
562 std::vector< std::vector< std::unique_ptr<TH1F> > > histogramsTHR;
563 std::vector< std::vector< std::unique_ptr<TH1F> > > histogramsSIG;
564 for(
unsigned int FE = 0; FE <
m_nFE; FE++){
566 histogramsTHR.push_back( std::vector< std::unique_ptr<TH1F> >() );
567 histogramsSIG.push_back( std::vector< std::unique_ptr<TH1F> >() );
572 hthr->SetDirectory(0);
574 hsig->SetDirectory(0);
576 histogramsTHR.at(FE).push_back(std::move(hthr));
578 histogramsSIG.at(FE).push_back(std::move(hsig));
585 while ((rodKey=
static_cast<TKey*
>(rodItr()))) {
586 const TString rodName(rodKey->GetName());
587 TDirectoryFile* rodDir = (TDirectoryFile*)rodKey->ReadObj();
592 while ((modKey=
static_cast<TKey*
>(modItr()))) {
593 TString
modName(modKey->GetName());
599 printf(
"Error - Module %s not found in the PixelMapping tool\n",
modName.Data());
609 h2dThr->SetDirectory(0);
613 h2dSig->SetDirectory(0);
615 for (
unsigned int ieta = 0; ieta <
m_etaBins; ieta++) {
616 for (
unsigned int iphi = 0; iphi <
m_phiBins; iphi++) {
618 float thr = h2dThr->GetBinContent(ieta + 1, iphi + 1);
619 float sig = h2dSig->GetBinContent(ieta + 1, iphi + 1);
621 if (
thr == 0 ||
thr > 10000 ||
sig == 0 ||
sig > 1000) {
625 int FE =
chipId(iphi, ieta);
632 histogramsTHR.at(FE).at(
pixel)->Fill(
thr);
633 histogramsSIG.at(FE).at(
pixel)->Fill(
sig);
643 auto itr = map_info.find( modID );
646 if (itr == map_info.end()) {
648 map_info[modID] = std::vector<std::unique_ptr<CalibFrontEndInfo>> ();
650 for(
unsigned int FE = 0; FE <
m_nFE; FE++){
652 map_info[modID].push_back( std::unique_ptr<CalibFrontEndInfo>() );
653 std::unique_ptr<CalibFrontEndInfo>
p = std::make_unique<CalibFrontEndInfo>(modID,FE,std::string(
modName),std::string(rodKey->GetName()));
654 map_info[modID].at(FE) = std::move(
p);
659 int thr_mean = histogramsTHR.at(FE).at(
pixel)->GetMean();
660 int thr_rms = histogramsTHR.at(FE).at(
pixel)->GetRMS();
661 int sig_mean = histogramsSIG.at(FE).at(
pixel)->GetMean();
672 histogramsTHR.at(FE).at(
pixel)->SetTitle(
"Threshold;Pixel threshold;Counts");
673 histogramsSIG.at(FE).at(
pixel)->SetTitle(
"Sigma;Pixel sigma;Counts");
675 std::string
type =
"";
680 histogramsTHR.at(FE).at(
pixel)->Write(TString(
"thres_"+
type).
Data(), TObject::kWriteDelete);
681 histogramsSIG.at(FE).at(
pixel)->Write(TString(
"sigma_"+
type).
Data(), TObject::kWriteDelete);
685 histogramsTHR.at(FE).at(
pixel)->Reset(
"ICESM");
686 histogramsSIG.at(FE).at(
pixel)->Reset(
"ICESM");
689 map_info[modID].at(FE)->set_NormalThreshold(thr_mean);
690 map_info[modID].at(FE)->set_NormalRms(thr_rms);
691 map_info[modID].at(FE)->set_NormalNoise(sig_mean);
694 map_info[modID].at(FE)->set_LongThreshold(thr_mean);
695 map_info[modID].at(FE)->set_LongRms(thr_rms);
696 map_info[modID].at(FE)->set_LongNoise(sig_mean);
699 map_info[modID].at(FE)->set_GangedThreshold(thr_mean);
700 map_info[modID].at(FE)->set_GangedRms(thr_rms);
701 map_info[modID].at(FE)->set_GangedNoise(sig_mean);
704 printf(
"Calib::fillThresholds: ERROR - Bad pixel in Calib::fillThresholds\n");
712 printf(
"Calib::fillThresholds: ERROR - REPEATED MOD ID: %s! Contact Offline team\n",std::string(
modName).c_str());
718 for(
unsigned int FE = 0; FE <
m_nFE; FE++) {
720 histogramsTHR.at(FE).at(
pixel).reset();
721 histogramsSIG.at(FE).at(
pixel).reset();
727 printf(
"DONE with threshold calibration.\n");
735 circ = (
int)(ieta / 18);
737 circ = 15 - (
int)(ieta / 18);
741 printf(
"Error - FE id error: %d, setting it to -1",circ);
754 if (ieta % 18 == 0 || ieta % 18 == 17) {
758 if (iphi > 152 && iphi < 160 && iphi % 2 == 1) {
761 if (iphi > 159 && iphi < 167 && iphi % 2 == 0) {
765 if(isForTOT and pixtype == 2 ) pixtype=1;
770 TDirectoryFile*
scanDir =
static_cast<TDirectoryFile*
>((
static_cast<TKey*
>(
inputFile.GetListOfKeys()->First()))->ReadObj());
771 TList* rodKeyList =
static_cast<TList*
>(
scanDir->GetListOfKeys());
772 return TIter(rodKeyList);
776 TList* modKeyList =
static_cast<TList*
>(rodDir->GetListOfKeys());
777 return TIter(modKeyList);
782 TString fullHistoPath = moduleName +
"/" +
histName +
"/A0/B0" +
suffix;
783 TDirectoryFile *histDir =
static_cast<TDirectoryFile *
>(rodDir->GetDirectory(fullHistoPath));
786 printf(
"Error - Directory \"%s\" not found. Exiting..\n",fullHistoPath.Data());
789 TH2F *pTH2 =
static_cast<TH2F*
>((
static_cast<TKey*
>(histDir->GetListOfKeys()->First()))->ReadObj());
790 pTH2->SetDirectory(0);
804 std::vector<float>
v;
806 v.push_back(
f->GetParameter(
i));
813 std::vector<float>
v;
815 v.push_back(
f->GetChisquare());
816 v.push_back(
f->GetNDF());
822 graph->SetTitle(TString(
name)+
";Charge;"+TString(Yname));
823 graph->SetMarkerStyle(20);