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 ) );
239 int vecsize = v_Q.size();
241 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) );
245 graflong ->Fit(functlong.get() ,
"MRQ");
247 pixLongParams =
getParams(functlong.get() ,3 );
262 auto itr = map_info.find( modID );
264 if (itr != map_info.end()) {
265 (itr->second).at(FE)->set_NormalParams( pixNormalParams);
266 (itr->second).at(FE)->set_LongParams ( pixLongParams );
267 (itr->second).at(FE)->set_SigParams ( pixSigParams );
269 (itr->second).at(FE)->set_times_fitted ( n_fit );
271 (itr->second).at(FE)->set_NormalParamsQuality( pixNormalParamsQuality);
272 (itr->second).at(FE)->set_LongParamsQuality ( pixLongParamsQuality );
273 (itr->second).at(FE)->set_SigParamsQuality ( pixSigParamsQuality );
276 printf(
"Error - Module not found in fitting step... Skipping.\n");
285 for(
unsigned int FE = 0; FE <
m_nFE; FE++) {
287 histogramsTOT.at(FE).at(
pixel).reset();
288 histogramsTOTsig.at(FE).at(
pixel).reset();
298 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){
300 float vecFit_size =
q.size()+1;
304 if(vecFit_size < stopFit) {
311 printf(
"reFit_normalPix: Refitting skipped. Not enough points to fit.\n");
316 float parAI0 =
params.at(0);
317 float parEI0 =
params.at(1);
318 float parCI0 =
params.at(2);
320 std::vector<float> v_discrepancy;
322 for(
unsigned int i = 0;
i <
q.size();
i++){
323 float discrepancy = std::abs( 1 - ( (parAI0 * parEI0 - parCI0 * tot.at(
i)) / (tot.at(
i) - parAI0) ) /
q.at(
i) );
328 v_discrepancy.push_back(discrepancy);
331 auto itr_max = std::max_element(v_discrepancy.begin(),v_discrepancy.end());
335 size_t n_max =
std::distance(v_discrepancy.begin(), itr_max);
336 printf(
"FE %02u Refitted, removing charge %5.0f with chi_error %7.5f\n", fe ,
q.at(n_max),*itr_max);
337 q.erase(
q.begin()+n_max);
338 qerr.erase(qerr.begin()+n_max);
339 tot.erase(tot.begin()+n_max);
340 toterr.erase(toterr.begin()+n_max);
341 sig.erase(
sig.begin()+n_max);
342 sigerr.erase(sigerr.begin()+n_max);
354 if (inTimFile.empty())
return false;
356 TFile timFile(inTimFile.c_str(),
"READ");
357 if (not timFile.IsOpen()) {
358 printf(
"Error - File %s could not be opened.\n",inTimFile.c_str());
361 printf(
"File %s opened.\n",inTimFile.c_str());
362 printf(
"Running timming calibration...\n");
366 std::vector< std::vector< std::unique_ptr<TH1F> > > histogramsTIM;
367 for(
unsigned int FE = 0; FE <
m_nFE; FE++){
369 histogramsTIM.push_back( std::vector< std::unique_ptr<TH1F> >() );
376 histogramsTIM.at(FE).push_back(std::move(
h));
383 while ((rodKey=
static_cast<TKey*
>(rodItr()))) {
384 const TString rodName(rodKey->GetName());
385 TDirectoryFile* rodDir =
static_cast<TDirectoryFile*
>(rodKey->ReadObj());
390 while ((modKey=
static_cast<TKey*
>(modItr()))) {
391 TString
modName(modKey->GetName());
397 printf(
"Error - Module %s not found in the PixelMapping tool\n",
modName.Data());
406 h2dTim->SetDirectory(0);
408 for (
unsigned int ieta = 0; ieta <
m_etaBins; ieta++) {
409 for (
unsigned int iphi = 0; iphi <
m_phiBins; iphi++) {
411 float tim = h2dTim->GetBinContent(ieta + 1, iphi + 1);
417 int FE =
chipId(iphi, ieta);
424 histogramsTIM.at(FE).at(
pixel)->Fill(tim);
432 auto itr = map_info.find( modID );
433 if (itr == map_info.end()) {
434 printf(
"Calib::fillTiming: ERROR - Mod ID= %16s not found. Creating it -----> Inform Pixel Offline Software Experts... \n",std::string(
modName).c_str());
436 map_info[modID] = std::vector<std::unique_ptr<CalibFrontEndInfo>> ();
438 for(
unsigned int FE = 0; FE <
m_nFE; FE++){
440 map_info[modID].push_back( std::unique_ptr<CalibFrontEndInfo>() );
441 std::unique_ptr<CalibFrontEndInfo>
p = std::make_unique<CalibFrontEndInfo>(modID,FE,std::string(
modName),std::string(rodKey->GetName()));
442 map_info[modID].at(FE) = std::move(
p);
447 int tim_mean = histogramsTIM.at(FE).at(
pixel)->GetMean();
450 histogramsTIM.at(FE).at(
pixel)->Reset(
"ICESM");
453 map_info[modID].at(FE)->set_NormalIntime(tim_mean);
456 map_info[modID].at(FE)->set_LongIntime(tim_mean);
459 map_info[modID].at(FE)->set_GangedIntime(tim_mean);
462 printf(
"Error - Bad pixel in Calib::fillTiming\n");
470 for(
unsigned int FE = 0; FE <
m_nFE; FE++){
475 int tim_mean = histogramsTIM.at(FE).at(
pixel)->GetMean();
486 histogramsTIM.at(FE).at(
pixel)->SetTitle(
"Intime;Pixel intime;Counts");
488 std::string
type =
"";
493 histogramsTIM.at(FE).at(
pixel)->Write(TString(
"intime_"+
type).
Data(), TObject::kWriteDelete);
497 histogramsTIM.at(FE).at(
pixel)->Reset(
"ICESM");
500 (itr->second).at(FE)->set_NormalIntime(tim_mean);
503 (itr->second).at(FE)->set_LongIntime(tim_mean);
506 (itr->second).at(FE)->set_GangedIntime(tim_mean);
509 printf(
"Error - Bad pixel in Calib::fillTiming\n");
518 for(
unsigned int FE = 0; FE <
m_nFE; FE++) {
520 histogramsTIM.at(FE).at(
pixel).reset();
526 printf(
"DONE with threshold calibration.\n");
534 if (inThrFile.empty())
return false;
536 TFile riThrFile(inThrFile.c_str(),
"READ");
537 if (not riThrFile.IsOpen()) {
538 printf(
"Error - File %s could not be opened.\n",inThrFile.c_str());
541 printf(
"File %s opened.\n",inThrFile.c_str());
542 printf(
"Running threshold calibration...\n");
546 std::vector< std::vector< std::unique_ptr<TH1F> > > histogramsTHR;
547 std::vector< std::vector< std::unique_ptr<TH1F> > > histogramsSIG;
548 for(
unsigned int FE = 0; FE <
m_nFE; FE++){
550 histogramsTHR.push_back( std::vector< std::unique_ptr<TH1F> >() );
551 histogramsSIG.push_back( std::vector< std::unique_ptr<TH1F> >() );
556 hthr->SetDirectory(0);
558 hsig->SetDirectory(0);
560 histogramsTHR.at(FE).push_back(std::move(hthr));
562 histogramsSIG.at(FE).push_back(std::move(hsig));
569 while ((rodKey=
static_cast<TKey*
>(rodItr()))) {
570 const TString rodName(rodKey->GetName());
571 TDirectoryFile* rodDir = (TDirectoryFile*)rodKey->ReadObj();
576 while ((modKey=
static_cast<TKey*
>(modItr()))) {
577 TString
modName(modKey->GetName());
583 printf(
"Error - Module %s not found in the PixelMapping tool\n",
modName.Data());
593 h2dThr->SetDirectory(0);
597 h2dSig->SetDirectory(0);
599 for (
unsigned int ieta = 0; ieta <
m_etaBins; ieta++) {
600 for (
unsigned int iphi = 0; iphi <
m_phiBins; iphi++) {
602 float thr = h2dThr->GetBinContent(ieta + 1, iphi + 1);
603 float sig = h2dSig->GetBinContent(ieta + 1, iphi + 1);
605 if (thr == 0 || thr > 10000 ||
sig == 0 ||
sig > 1000) {
609 int FE =
chipId(iphi, ieta);
616 histogramsTHR.at(FE).at(
pixel)->Fill(thr);
617 histogramsSIG.at(FE).at(
pixel)->Fill(
sig);
627 auto itr = map_info.find( modID );
630 if (itr == map_info.end()) {
632 map_info[modID] = std::vector<std::unique_ptr<CalibFrontEndInfo>> ();
634 for(
unsigned int FE = 0; FE <
m_nFE; FE++){
636 map_info[modID].push_back( std::unique_ptr<CalibFrontEndInfo>() );
637 std::unique_ptr<CalibFrontEndInfo>
p = std::make_unique<CalibFrontEndInfo>(modID,FE,std::string(
modName),std::string(rodKey->GetName()));
638 map_info[modID].at(FE) = std::move(
p);
643 int thr_mean = histogramsTHR.at(FE).at(
pixel)->GetMean();
644 int thr_rms = histogramsTHR.at(FE).at(
pixel)->GetRMS();
645 int sig_mean = histogramsSIG.at(FE).at(
pixel)->GetMean();
656 histogramsTHR.at(FE).at(
pixel)->SetTitle(
"Threshold;Pixel threshold;Counts");
657 histogramsSIG.at(FE).at(
pixel)->SetTitle(
"Sigma;Pixel sigma;Counts");
659 std::string
type =
"";
664 histogramsTHR.at(FE).at(
pixel)->Write(TString(
"thres_"+
type).
Data(), TObject::kWriteDelete);
665 histogramsSIG.at(FE).at(
pixel)->Write(TString(
"sigma_"+
type).
Data(), TObject::kWriteDelete);
669 histogramsTHR.at(FE).at(
pixel)->Reset(
"ICESM");
670 histogramsSIG.at(FE).at(
pixel)->Reset(
"ICESM");
673 map_info[modID].at(FE)->set_NormalThreshold(thr_mean);
674 map_info[modID].at(FE)->set_NormalRms(thr_rms);
675 map_info[modID].at(FE)->set_NormalNoise(sig_mean);
678 map_info[modID].at(FE)->set_LongThreshold(thr_mean);
679 map_info[modID].at(FE)->set_LongRms(thr_rms);
680 map_info[modID].at(FE)->set_LongNoise(sig_mean);
683 map_info[modID].at(FE)->set_GangedThreshold(thr_mean);
684 map_info[modID].at(FE)->set_GangedRms(thr_rms);
685 map_info[modID].at(FE)->set_GangedNoise(sig_mean);
688 printf(
"Calib::fillThresholds: ERROR - Bad pixel in Calib::fillThresholds\n");
696 printf(
"Calib::fillThresholds: ERROR - REPEATED MOD ID: %s! Contact Offline team\n",std::string(
modName).c_str());
702 for(
unsigned int FE = 0; FE <
m_nFE; FE++) {
704 histogramsTHR.at(FE).at(
pixel).reset();
705 histogramsSIG.at(FE).at(
pixel).reset();
711 printf(
"DONE with threshold calibration.\n");
719 circ = (
int)(ieta / 18);
721 circ = 15 - (
int)(ieta / 18);
725 printf(
"Error - FE id error: %d, setting it to -1",circ);
738 if (ieta % 18 == 0 || ieta % 18 == 17) {
742 if (iphi > 152 && iphi < 160 && iphi % 2 == 1) {
745 if (iphi > 159 && iphi < 167 && iphi % 2 == 0) {
749 if(isForTOT and pixtype == 2 ) pixtype=1;
754 TDirectoryFile*
scanDir =
static_cast<TDirectoryFile*
>((
static_cast<TKey*
>(
inputFile.GetListOfKeys()->First()))->ReadObj());
755 TList* rodKeyList =
static_cast<TList*
>(
scanDir->GetListOfKeys());
756 return TIter(rodKeyList);
760 TList* modKeyList =
static_cast<TList*
>(rodDir->GetListOfKeys());
761 return TIter(modKeyList);
766 TString fullHistoPath = moduleName +
"/" +
histName +
"/A0/B0" +
suffix;
767 TDirectoryFile *histDir =
static_cast<TDirectoryFile *
>(rodDir->GetDirectory(fullHistoPath));
770 printf(
"Error - Directory \"%s\" not found. Exiting..\n",fullHistoPath.Data());
773 TH2F *pTH2 =
static_cast<TH2F*
>((
static_cast<TKey*
>(histDir->GetListOfKeys()->First()))->ReadObj());
774 pTH2->SetDirectory(0);
788 std::vector<float>
v;
790 v.push_back(
f->GetParameter(
i));
797 std::vector<float>
v;
799 v.push_back(
f->GetChisquare());
800 v.push_back(
f->GetNDF());
806 graph->SetTitle(TString(
name)+
";Charge;"+TString(Yname));
807 graph->SetMarkerStyle(20);