5 #include "TGraphErrors.h"
11 #include "TPaveText.h"
23 void NormalizeBsHisto(TH2F* H2)
26 const float max = H2->GetMaximum();
30 for (
int x=0; x<H2->GetNbinsX(); x++)
31 for (
int y=0; y<H2->GetNbinsY(); y++)
33 const float BC = H2->GetBinContent(x+1, y+1);
36 H2->SetBinContent(x+1, y+1, 0);
37 H2->SetBinError (x+1, y+1, 0);
45 vector<int> GetListBad(TH2F* ADC, TH2F* BS, TH2F* ChMap,
int detId,
int runNo,
bool SHOW =
false,
bool PRINT =
false)
48 if (BS->GetMaximum() != 1) { cout <<
"WARNING! BS QA histogram is NOT normalized!" <<endl;
return List; }
51 const float nCh = (ADC->GetNbinsX()>300)?394:238;
52 const float nHitAll = ADC->GetEntries();
53 const float nHitAvg = nHitAll/nCh;
56 for (
int x=0; x<BS->GetNbinsX(); x++)
64 bool chValid = ChMap->GetBinContent(x+1, 1);
65 const int nBit = BS->GetNbinsY();
67 for (
int y=0; y<nBit; y++) {
if (BS->GetBinContent(x+1, y+1) == 1) nBitF++; }
68 if (chValid==
false || nBitF==nBit)
71 List.push_back(flagStat);
76 TH1F* ADC_1ch = (TH1F*)ADC->ProjectionY(
"", x+1, x+1);
77 const float tHit = ADC_1ch->GetEntries();
78 const float tRMS = ADC_1ch->GetRMS();
79 if (tHit < nHitAvg*1.e-2) flagStat = 1;
80 else if (tRMS < 1.) flagStat = 2;
83 List.push_back(flagStat);
91 for (
unsigned int x=0; x<List.size(); x++)
93 if (List[x] ==
false)
continue;
94 if (ChMap->GetBinContent(x+1, 1) ==
false)
continue;
95 TH1F* ADC_1ch = (TH1F*)ADC->ProjectionY(
"", x+1, x+1);
96 const int nHit = ADC_1ch->GetEntries();
97 cout <<Form(
"Bad: detId = %2i, ch = %3i / Flag = %i", detId, x+1, List[x]) <<endl;
103 TCanvas *c1 =
new TCanvas(
"c1",
"", 800, 800);
104 for (
unsigned int x=0; x<List.size(); x++)
106 if (List[x] ==
false)
continue;
107 if (ChMap->GetBinContent(x+1, 1) ==
false)
continue;
109 c1->SetName(Form(
"Bad_run%i_d%i_ch%i", runNo, detId, x+1));
111 TH1F* ADC_1ch = (TH1F*)ADC->ProjectionY(
"", x+1, x+1);
112 ADC_1ch->SetTitle(Form(
"Bad: run = %i detId = %2i, ch = %3i, flag = %i", runNo, detId, x+1, List[x]));
113 ADC_1ch->GetXaxis()->SetRangeUser(0,250);
114 ADC_1ch->DrawCopy(
"hist e");
115 c1->Print(Form(
"%s.png", c1->GetName()));
124 vector<int> GetListBS(TH2F* BS, TH2F* ChMap,
int detId,
bool SHOW =
false)
127 if (BS->GetMaximum() != 1) { cout <<
"WARNING! BS QA histogram is NOT normalized!" <<endl;
return List; }
130 const int nBit = BS->GetNbinsY();
131 for (
int x=0; x<BS->GetNbinsX(); x++)
134 bool chValid = ChMap->GetBinContent(x+1, 1);
135 if (!chValid) { List.push_back(bsValue);
continue; }
140 for (
int y=0; y<nBit; y++)
142 const int BC = BS->GetBinContent(x+1, y+1);
143 if (BC==1) nBitFilled++;
145 if (y==0 && BC==1) bsPos =
true;
146 if (y==nBit-1 && BC==1 && bsPos==
false) bsNeg =
true;
149 if (nBitFilled!=0 || nBitFilled!=nBit)
153 for (
int y=0; y<nBit; y++)
155 if (BS->GetBinContent(x+1, y+1) == 1) bsValue++;
159 else if (bsNeg==
true)
161 for (
int y=nBit-1; y>-1; y--)
163 if (BS->GetBinContent(x+1, y+1) == 1) bsValue--;
169 List.push_back(bsValue);
170 if (SHOW) cout <<Form(
"BS: %2i, %3i / %2i", detId, x+1, bsValue) <<endl;
177 void DrawAdc(TH2F* H2,
int runNo,
int detId,
int ch,
bool PRINT =
true)
179 TH1F* H1 = (TH1F*)H2->ProjectionY(
"", ch, ch);
180 H1->SetTitle(Form(
"d%i_ch%i_run%i", detId, ch, runNo));
183 for (
int x=0; x<H1->GetNbinsX(); x++) {
if (H1->GetBinContent(x+1) != 0) xMax = x+1; }
184 H1->GetXaxis()->SetRangeUser(0, xMax + 10);
185 H1->GetXaxis()->SetLabelSize(0.05);
186 H1->GetXaxis()->SetTitleOffset(1.25);
187 H1->GetYaxis()->SetLabelSize(0.05);
189 gStyle->SetOptDate(0);
190 gStyle->SetOptStat(
"emr");
191 TCanvas *c1 =
new TCanvas(
"c1", H1->GetTitle(), 800*1.5, 600*1.5);
193 c1->cd(1)->SetLogy();
194 H1->DrawCopy(
"hist e");
196 H1->GetXaxis()->SetRangeUser(0, 32);
197 H1->DrawCopy(
"hist e");
200 if (PRINT) c1->Print(Form(
"%s.png", c1->GetTitle()));
207 void bsQa(
const char* inList =
"inFiles_pptrans.list",
int LogLv = 1,
int PRINT = 0)
223 const int ListSkip[][4] =
229 {16073030, 16073037, 8, 483},
230 {16068056, 16082011, 8, 566},
231 {16073032, 16073034, 10, 9},
232 {16078028, 16078042, 10, 13},
233 {16073034, 16077021, 10, 47},
234 {16066035, 16073030, 10, 48},
235 {16078032, 16078033, 10, 51},
236 {16066050, 16066050, 10, 65},
237 {16077027, 16077040, 10, 73},
238 {16077032, 16093019, 10, 78},
239 {16073030, 16073033, 10, 151},
240 {16073030, 16073037, 10, 176},
241 {16073037, 16073038, 10, 186},
242 {16073031, 16073040, 10, 282},
243 {16073035, 16073040, 11, 118},
244 {16086050, 16086051, 11, 213},
245 {16067021, 16072024, 11, 287},
246 {16073031, 16077021, 11, 287},
250 {16129050, 16129050, 10, 211},
251 {16129050, 16129051, 10, 221},
255 {16164006, 16167091, 10, 30},
256 {16160020, 16169041, 10, 31},
257 {16160023, 16169041, 10, 32},
258 {16160018, 16162040, 10, 43},
259 {16160024, 16169041, 10, 177}
261 const int nListSkip =
sizeof(ListSkip)/
sizeof(ListSkip[0]);
266 vector<int> ListBad[4];
267 vector<int> ListBS[4];
272 if (!in.is_open()) { cout <<
"Cannot open " <<inList <<endl;
return; }
276 int runNoPrevious = 0;
282 if (!in.good()) {
break; in.close(); }
283 F = TFile::Open(inFile.c_str());
284 if (!F || F->IsZombie()) { cout <<
"Cannot open the file " <<inFile.c_str() <<endl;
return; }
285 cout <<Form(
"Processing... %s, %3i", F->GetName(), iRun) <<endl;
288 std::size_t strPos = inFile.find(
".root");
289 string runNoStr = inFile.substr(strPos-8, 8);
290 const int runNo = std::atoi(runNoStr.c_str());
293 if (iRun==0) out.open(
"log_fmsBsQa.txt");
294 else out.open(
"log_fmsBsQa.txt", ios::app);
295 out <<Form(
"Run %i", runNo) <<endl;
301 bool FlagNew_dead =
false;
302 bool FlagNew_bs =
false;
303 for (
int a=0; a<4; a++)
305 TH2F* H2_ADC = (TH2F*)F->Get(Form(
"Adc_d%i", a+8));
306 TH2F* H2_BS_DB = (TH2F*)F->Get(Form(
"BitShift_DB_d%i", a+8));
307 TH2F* H2_BS_data = (TH2F*)F->Get(Form(
"BitShift_data_d%i", a+8));
308 TH2F* H2_ChMap = (TH2F*)F->Get(Form(
"ChMap_d%i", a+8));
309 if (!H2_ADC || !H2_BS_DB || !H2_BS_data || !H2_ChMap) { cout <<
"Cannot open histogram!" <<endl;
return; }
312 NormalizeBsHisto(H2_BS_DB);
313 NormalizeBsHisto(H2_BS_data);
316 vector<int> tempBad = GetListBad(H2_ADC, H2_BS_data, H2_ChMap, a+8, runNo,
false,
false);
317 if (iRun!=0 && ListBad[a].size() != tempBad.size()) { cout <<
"Size mismatch! (1)" <<endl;
return; }
318 if (iRun==0 || !std::equal(ListBad[a].begin(), ListBad[a].end(), tempBad.begin()))
320 if (iRun!=0 && LogLv>2)
322 for (
unsigned int x=0; x<ListBad[a].size(); x++)
324 bool dead_old = ListBad[a][x];
325 bool dead_new = tempBad[x];
326 if (dead_old != dead_new)
328 out <<Form(
"Updating dead: %2i, %3i, %i -> %i", a+8,x+1,dead_old,dead_new) <<endl;
333 ListBad[a] = tempBad;
340 vector<int> tempBS_db = GetListBS(H2_BS_DB, H2_ChMap, a+8,
false);
341 vector<int> tempBS_data = GetListBS(H2_BS_data, H2_ChMap, a+8,
false);
342 vector<int> tempBS_new;
343 if (tempBS_db.size() != tempBS_data.size()) { cout <<
"Size mismatch! (2)" <<endl;
return; }
344 for (
unsigned int x=0; x<tempBS_db.size(); x++)
346 int bs_db = tempBS_db[x];
347 int bs_data = tempBS_data[x];
348 if (abs(bs_db)>12 || abs(bs_data)>12) { cout <<Form(
"Invalid BS found! %2i %2i\n", bs_db,bs_data); }
351 if (bs_db!=bs_data && tempBad[x]==
false)
354 if (bs_data >= 0) bs_new = bs_data;
357 if (bs_db >= 0) bs_new = 0;
360 if (abs(bs_data) < abs(bs_db)) bs_new = bs_data;
364 if (LogLv>1 && bs_db!=bs_new)
366 out <<Form(
"BS mismatch: %2i, %3i | data: %3i, DB: %2i -> %2i",
367 a+8,x+1,bs_data,bs_db,bs_new) <<endl;
372 bool FlagSkip =
false;
373 for (
int i=0; i<nListSkip; i++)
375 if (runNo>=ListSkip[i][0] && runNo<=ListSkip[i][1] &&
376 a+8==ListSkip[i][2] && x+1==(
unsigned int)ListSkip[i][3]) FlagSkip =
true;
378 if (FlagSkip==
true) bs_new = ListBS[a][x];
380 tempBS_new.push_back(bs_new);
384 if (iRun==0 || !std::equal(ListBS[a].begin(), ListBS[a].end(), tempBS_new.begin()))
386 if (iRun!=0 && LogLv>0)
388 for (
unsigned int x=0; x<tempBS_new.size(); x++)
390 int bs_old = ListBS[a][x];
391 int bs_new = tempBS_new[x];
392 if (bs_old != bs_new)
394 out <<Form(
"Updating BS: %2i, %3i | %2i -> %2i", a+8,x+1,bs_old,bs_new) <<endl;
400 ListBS[a] = tempBS_new;
406 H2_BS_data->Delete();
410 if (LogLv>0) { out <<endl; out.close(); }
415 if (PRINT>1 && FlagNew_dead)
417 cout <<Form(
"List updated: run %i -> run %i, Dead channels", runNoPrevious, runNo) <<endl;
418 out.open(Form(
"FmsDead_y2015_run%i.txt", runNo));
419 for (
int a=0; a<4; a++)
420 for (
unsigned int b=0; b<ListBad[a].size(); b++)
422 out <<Form(
"%2i %3i %i", a+8, b+1, ListBad[a][b]) <<endl;
426 if (PRINT>0 && FlagNew_bs)
428 cout <<Form(
"List updated: run %i -> run %i, Bit shift", runNoPrevious, runNo) <<endl;
429 out.open(Form(
"FmsBsGain_y2015_run%i.txt", runNo));
430 for (
int a=0; a<4; a++)
431 for (
unsigned int b=0; b<ListBS[a].size(); b++)
433 out <<Form(
"%2i %3i %2i", a+8, b+1, ListBS[a][b]) <<endl;
438 runNoPrevious = runNo;