9 #include "calibFmsShow.C"
11 void calibFms(
const int iIter = 0,
const char* outPath =
"./Iterations",
bool PRINT =
true)
17 TFile* F = TFile::Open(Form(
"./fmsCalib_%i.root", iIter));
18 TFile* G =
new TFile(Form(
"out_fmsCalib_%i.root", iIter),
"RECREATE");
21 for (
int a=0; a<4; a++) H2_mass[a] = (TH2F*)F->Get(Form(
"mass_d%i", a+8));
24 map<int, int> iToCh[4];
25 map<int, int> chToBS[4];
26 map<int, int> chToCellStat[4];
27 map<int, float> chToGainCorr[4];
28 map<int, st_pos> chToPos[4];
29 GetMapIndexToCh (
"./FmsMapBase.txt", iToCh);
30 GetMapChToBS (
"./FmsMapBitShift.txt", chToBS);
31 GetMapChToCellStat(
"./FmsCellStat.txt", chToCellStat);
32 GetMapChToGainCorr(
"./FmsGainCorr.txt", chToGainCorr);
33 GetMapChToPos (
"./FmsMapBase.txt", chToPos);
39 map<int, st_fitR> chToFitR[4];
40 map<int, float> chToGainCorrNext[4];
43 map<int, int> chToSkew[4];
44 map<int, st_fitR> chToFitRPrev[4];
45 if (iIter > 0) GetMapChToSkew(Form(
"%s/out_fmsCalibSkew_%i.txt", outPath, iIter-1), chToSkew);
46 if (iIter > 0) GetMapChToFitR(Form(
"%s/out_fmsCalibFit_%i.txt", outPath, iIter-1), chToFitRPrev);
49 map<int, float> manGainCorr[4];
50 GetMapManualGainCorr(Form(
"FmsGainCorrManual_%i.txt", iIter), manGainCorr);
53 vector<int> markBadF[4];
54 vector<int> markCold[4];
55 vector<int> markLoHi[4];
56 vector<int> markSkew[4];
59 for (
int a=0; a<4; a++)
62 FitMass(iIter, H2_mass[a], iToCh[a], chToBS[a], chToCellStat[a], chToGainCorr[a], chToPos[a], chToFitR[a]);
65 for (
unsigned int b=0; b<iToCh[a].size(); b++)
67 const int ch = iToCh[a][b];
68 const float gainCorr = chToGainCorr[a][ch];
69 const float massFitR = chToFitR[a][ch].mass;
70 const float massBook = 0.1349766;
71 int nSkewed = (iIter==0)?0:chToSkew[a][ch];
73 bool doUpdate =
false;
74 if (chToCellStat[a][ch]==GOOD)
77 if (massFitR != 0.) doUpdate =
true;
78 else markCold[a].push_back(ch);
81 const char* infoQA =
"";
82 float gainCorrNext = gainCorr;
85 gainCorrNext = (massBook/massFitR) * gainCorr;
88 if (gainCorr<0.3 || gainCorr>3.0) markLoHi[a].push_back(ch);
91 if (fabs(massFitR - massBook) > 0.03 ||
92 chToFitR[a][ch].nRefit == 10 ||
93 chToFitR[a][ch].chi2 > 25. ||
94 chToFitR[a][ch].chi2 < 0.5)
96 const float upRatio = (gainCorrNext - gainCorr) / gainCorr;
97 if (fabs(upRatio)>0.2 && upRatio>0) gainCorrNext = gainCorr * 1.2;
98 if (fabs(upRatio)>0.2 && upRatio<0) gainCorrNext = gainCorr * 0.8;
100 markBadF[a].push_back(ch);
104 if (chToCellStat[a][ch]!=CONVERGED && iIter>0 && a>1 && massFitR<0.12)
106 const float dGainCorr = gainCorrNext - gainCorr;
107 const float dMassFitR = massFitR - chToFitRPrev[a][ch].mass;
108 if (dGainCorr>=0 && dMassFitR<0)
110 gainCorrNext = gainCorr;
111 infoQA = Form(
"%sSKEWED ", infoQA);
112 markSkew[a].push_back(ch);
117 infoQA = Form(
"%sRESET! ", infoQA);
118 gainCorrNext = 0.750;
121 map<int, int>::iterator chNewSkew = chToSkew[a].find(ch);
122 if (chNewSkew != chToSkew[a].end()) chNewSkew->second = nSkewed;
128 if (manGainCorr[a].size()!=0 && manGainCorr[a][ch]!=0.)
130 infoQA = Form(
"%sMANUAL", infoQA);
131 gainCorrNext = manGainCorr[a][ch];
135 chToGainCorrNext[a].insert(pair<int, float>(ch, gainCorrNext));
139 const char* infoUp = (gainCorrNext != gainCorr)?Form(
"-> %4.3f", gainCorrNext):Form(
"%8s",
"");
140 cout <<Form(
"%2i %3i | %4.3f %s %s", a+8, ch, gainCorr, infoUp, infoQA) <<endl;
152 cout <<
"Generating in-situ QA plots..." <<endl;
153 DrawMap(iToCh, chToPos, Form(
"GainCorrL_i%i", iIter),
false,
false,
true, chToGainCorr, chToCellStat);
154 DrawMap(iToCh, chToPos, Form(
"GainCorrS_i%i", iIter),
true,
false,
true, chToGainCorr, chToCellStat);
160 for (
int a=0; a<4; a++) {
if (markBadF[a].size() > 0) { QaBadF =
true;
break; } }
161 for (
int a=0; a<4; a++) {
if (markCold[a].size() > 0) { QaCold =
true;
break; } }
162 for (
int a=0; a<4; a++) {
if (markLoHi[a].size() > 0) { QaLoHi =
true;
break; } }
163 for (
int a=0; a<4; a++) {
if (markSkew[a].size() > 0) { QaSkew =
true;
break; } }
164 if (QaBadF) DrawMap(iToCh, chToPos,
"QABadF",
false,
false,
true, DumMapF, chToCellStat, markBadF);
165 if (QaCold) DrawMap(iToCh, chToPos,
"QACold",
false,
false,
true, DumMapF, chToCellStat, markCold);
166 if (QaLoHi) DrawMap(iToCh, chToPos,
"QALoHi",
false,
false,
true, DumMapF, chToCellStat, markLoHi);
167 if (QaSkew) DrawMap(iToCh, chToPos,
"QASkew",
false,
false,
true, DumMapF, chToCellStat, markSkew);
168 if (QaBadF || QaCold || QaLoHi || QaSkew)
170 const char* QAout = Form(
"QAMap_i%i", iIter);
171 gSystem->Exec(Form(
"mkdir %s", QAout));
172 gSystem->Exec(Form(
"mv *.png %s", QAout));
173 gSystem->Exec(Form(
"mv %s %s", QAout, outPath));
181 PrintFitResults(Form(
"out_fmsCalibFit_%i.txt", iIter), iToCh, chToFitR);
182 gSystem->Exec(Form(
"mv out_fmsCalibFit_%i.txt %s", iIter, outPath));
184 PrintGainCorr(Form(
"FmsGainCorrNew_%i.txt", iIter), chToGainCorrNext);
185 gSystem->Exec(Form(
"mv FmsGainCorr.txt %s/FmsGainCorr_%i.txt", outPath, iIter));
186 gSystem->Exec(Form(
"mv FmsGainCorrNew_%i.txt FmsGainCorr.txt", iIter));
188 PrintSkewMonitor(Form(
"out_fmsCalibSkew_%i.txt", iIter), iToCh, chToSkew);
189 gSystem->Exec(Form(
"mv out_fmsCalibSkew_%i.txt %s", iIter, outPath));
191 TestConvergence(iIter, iToCh, chToCellStat, outPath);