CMS 3D CMS Logo

ls_cert.cc
Go to the documentation of this file.
1 #include <Riostream.h>
2 #include <TDirectory.h>
3 #include <TFile.h>
4 #include <TROOT.h>
5 #include <TStyle.h>
6 #include <TKey.h>
7 #include <TH1.h>
8 #include <TH2.h>
9 #include <TH2D.h>
10 #include <TCanvas.h>
11 #include <TGraph.h>
12 #include <TPaveStats.h>
13 #include <TText.h>
14 #include <TLegend.h>
15 #include <cstring>
16 #include <utility>
17 #include <vector>
18 #include <sstream>
19 #include <algorithm>
20 #include <TString.h>
21 #include <TColor.h>
22 
23 using namespace std;
24 
25 //global vars
26 int numlumis = -1;
27 
28 int nlumis(string filename); //get number of run lumisections
29 string runnum_str(string filename); //read the run number, return in string
30 void ls_cert(float threshold_pixel, float threshold, string filename);
31 
32 int main(int argc, char* argv[]) {
33  if (argc == 4) {
34  char* cpixel = argv[1];
35  char* cthr = argv[2];
36  char* filename = argv[3];
37 
38  float threshold_pixel = 0;
39  sscanf(cpixel, "%f", &threshold_pixel);
40  float threshold = 0;
41  sscanf(cthr, "%f", &threshold);
42 
43  std::cout << "ready to run ls_cert: pixel thr " << threshold_pixel << " threshold " << threshold << " filename "
44  << filename << std::endl;
45 
46  ls_cert(threshold_pixel, threshold, filename);
47 
48  } else {
49  std::cout << "Too few arguments: " << argc << std::endl;
50  return -1;
51  }
52  return 0;
53 }
54 
55 void ls_cert(float threshold_pixel, float threshold, string filename) {
56  void ls_cert_type(string iDir,
57  float threshold,
58  string filename,
59  vector<string>&,
60  vector<pair<string, vector<float> > >&,
61  vector<pair<string, vector<float> > >&,
62  vector<pair<string, vector<float> > >&);
63  void cert_plot(float threshold_pixel,
64  float threshold,
65  string filename,
66  vector<string>&,
67  vector<string>&,
68  vector<string>&,
69  vector<pair<string, vector<float> > >&,
70  vector<pair<string, vector<float> > >&,
71  vector<pair<string, vector<float> > >&);
72 
73  //presets
74  numlumis = -1;
75 
76  //certifications
77  vector<string> cert_strip;
78  vector<string> cert_track;
79  vector<string> cert_pixel;
80 
81  //good lumisections
82  vector<pair<string, vector<float> > > gLS_strip;
83  vector<pair<string, vector<float> > > gLS_track;
84  vector<pair<string, vector<float> > > gLS_pixel;
85 
86  //bad lumisections
87  vector<pair<string, vector<float> > > bLS_strip;
88  vector<pair<string, vector<float> > > bLS_track;
89  vector<pair<string, vector<float> > > bLS_pixel;
90 
91  //missing lumisections
92  vector<pair<string, vector<float> > > mLS_strip;
93  vector<pair<string, vector<float> > > mLS_track;
94  vector<pair<string, vector<float> > > mLS_pixel;
95 
96  ls_cert_type("SiStrip", threshold, filename, cert_strip, gLS_strip, bLS_strip, mLS_strip);
97  ls_cert_type("Tracking", threshold, filename, cert_track, gLS_track, bLS_track, mLS_track);
98  ls_cert_type("Pixel", threshold_pixel, filename, cert_pixel, gLS_pixel, bLS_pixel, mLS_pixel);
99 
100  std::ofstream outfile;
101  string namefile = "Certification_run_" + runnum_str(filename) + ".txt";
102  outfile.open(namefile.c_str());
103  outfile << "Lumisection Certification (GOOD: >= " << threshold_pixel << " [Pixel]; >= " << threshold
104  << " [SiStrip,Tracking] "
105  << ", otherwise BAD):" << endl
106  << endl;
107  outfile << "GOOD Lumisections:" << endl;
108  char line[200];
109  for (int ityp = 0; ityp < 4; ityp++) {
110  sprintf(line, " Pixel %*sSummary: %s", 13, cert_pixel[ityp].c_str(), gLS_pixel[ityp].first.c_str());
111  outfile << line << endl;
112  }
113  for (int ityp = 0; ityp < 4; ityp++) {
114  sprintf(line, " SiStrip %*sSummary: %s", 13, cert_strip[ityp].c_str(), gLS_strip[ityp].first.c_str());
115  outfile << line << endl;
116  }
117  for (int ityp = 0; ityp < 1; ityp++) {
118  sprintf(line, " Tracking %*sSummary: %s", 13, cert_track[ityp].c_str(), gLS_track[ityp].first.c_str());
119  outfile << line << endl;
120  }
121 
122  outfile << "\nBAD Lumisections:" << endl;
123  for (int ityp = 0; ityp < 4; ityp++) {
124  sprintf(line, " Pixel %*sSummary: %s", 13, cert_pixel[ityp].c_str(), bLS_pixel[ityp].first.c_str());
125  outfile << line << endl;
126  }
127  for (int ityp = 0; ityp < 4; ityp++) {
128  sprintf(line, " SiStrip %*sSummary: %s", 13, cert_strip[ityp].c_str(), bLS_strip[ityp].first.c_str());
129  outfile << line << endl;
130  }
131  for (int ityp = 0; ityp < 1; ityp++) {
132  sprintf(line, " Tracking %*sSummary: %s", 13, cert_track[ityp].c_str(), bLS_track[ityp].first.c_str());
133  outfile << line << endl;
134  }
135 
136  outfile << "\nMISSING Lumisections:" << endl;
137  for (int ityp = 0; ityp < 4; ityp++) {
138  sprintf(line, " Pixel %*sSummary: %s", 13, cert_pixel[ityp].c_str(), mLS_pixel[ityp].first.c_str());
139  outfile << line << endl;
140  }
141  for (int ityp = 0; ityp < 4; ityp++) {
142  sprintf(line, " SiStrip %*sSummary: %s", 13, cert_strip[ityp].c_str(), mLS_strip[ityp].first.c_str());
143  outfile << line << endl;
144  }
145  for (int ityp = 0; ityp < 1; ityp++) {
146  sprintf(line, " Tracking %*sSummary: %s", 13, cert_track[ityp].c_str(), mLS_track[ityp].first.c_str());
147  outfile << line << endl;
148  }
149 
150  outfile.close();
151  std::cout << "Lumisection Certification summary saved in " << namefile << std::endl;
152 
153  cert_plot(threshold_pixel, threshold, filename, cert_strip, cert_track, cert_pixel, gLS_strip, gLS_track, gLS_pixel);
154 }
155 
156 void ls_cert_type(string iDir,
157  float threshold,
158  string filename,
159  vector<string>& cert,
160  vector<pair<string, vector<float> > >& gLS,
161  vector<pair<string, vector<float> > >& bLS,
162  vector<pair<string, vector<float> > >& mLS) {
163  void Cleaning(vector<int>&);
164  string ListOut(vector<int>&);
165 
166  bool debug = false;
167  string run = runnum_str(filename);
168  if (debug)
169  std::cout << filename.c_str() << std::endl;
170 
171  TDirectory* topDir;
172  vector<float> ls;
173 
174  TFile* file = TFile::Open(filename.c_str());
175  if (!file->IsOpen()) {
176  std::cerr << "Failed to open " << filename << std::endl;
177  return;
178  }
179 
180  string dir = "DQMData/Run " + run + "/" + iDir;
181  topDir = dynamic_cast<TDirectory*>(file->Get(dir.c_str()));
182  topDir->cd();
183  if (debug)
184  std::cout << topDir->GetTitle() << std::endl;
185 
186  //
187  // Reading the LS directory
188  //
189  TIter next(topDir->GetListOfKeys());
190  TKey* key;
191  while ((key = dynamic_cast<TKey*>(next()))) {
192  string clName(key->GetClassName());
193  if (clName == "TDirectoryFile") {
194  TDirectory* curr_dir = dynamic_cast<TDirectory*>(key->ReadObj());
195  string name = curr_dir->GetName();
196  if (name == "Run summary")
197  continue;
198  name = name.substr(name.find('-') + 1);
199  float temp1 = atof(name.c_str());
200  ls.push_back(temp1);
201  }
202  }
203  sort(ls.begin(), ls.end());
204  int vecsize = ls.size();
205 
206  //
207  // Definition of vectors for LS certification
208  //
209  Float_t* lsd = new Float_t[vecsize];
210 
211  Float_t** v = new Float_t*[4];
212  for (int k = 0; k < 4; k++) {
213  v[k] = new Float_t[vecsize];
214  }
215  //string certflag[4] = {"CertificationSummary","DAQSummary","DCSSummary","reportSummary"};
216  //string certflagPrint[4] = {"Certification","DAQ","DCS","DQM"};
217  string certflag[4] = {"DAQSummary", "DCSSummary", "reportSummary", "CertificationSummary"};
218  string certflagPrint[4] = {"DAQ", "DCS", "DQM", "Certification"};
219 
220  int smax = 2;
221  if (iDir == "SiStrip" || iDir == "Pixel")
222  smax = 4;
223 
224  if (iDir == "Tracking") {
225  certflag[0] = "CertificationSummary";
226  certflagPrint[0] = "Certification";
227  certflag[1] = "reportSummary";
228  certflagPrint[1] = "DQM";
229  }
230 
231  for (int icert_type = 0; icert_type < smax; icert_type++) {
232  cert.push_back(certflagPrint[icert_type]);
233  }
234 
235  if (debug)
236  std::cout << gDirectory->GetName() << std::endl;
237 
238  for (int i = 0; i < vecsize; i++) {
239  stringstream lsdir;
240  lsdir << dir << "/By Lumi Section " << ls[i] << "-" << ls[i] << "/EventInfo";
241  if (debug)
242  std::cout << lsdir.str().c_str() << std::endl;
243  float templs = ls[i];
244  lsd[i] = templs;
245  TDirectory* tempDir = dynamic_cast<TDirectory*>(file->Get(lsdir.str().c_str()));
246  tempDir->cd();
247  TIter nextTemp(tempDir->GetListOfKeys());
248  TKey* keyTemp;
249  while ((keyTemp = dynamic_cast<TKey*>(nextTemp()))) {
250  float tempvalue = -1.;
251  string classname(keyTemp->GetClassName());
252  if (classname == "TObjString") {
253  string sflag = keyTemp->GetName();
254  string tempname = sflag.substr(sflag.find("f=") + 2);
255  size_t pos1 = tempname.find('<');
256  size_t pos2 = sflag.find_first_of('>');
257  string detvalue = tempname.substr(0, pos1);
258  string typecert = sflag.substr(1, pos2 - 1);
259  if (debug)
260  std::cout << typecert.c_str() << std::endl;
261  tempvalue = atof(detvalue.c_str());
262 
263  for (int j = 0; j < smax; j++) {
264  if (strstr(typecert.c_str(), certflag[j].c_str()) != nullptr)
265  v[j][i] = tempvalue;
266  if (debug)
267  std::cout << "Entering value " << tempvalue << " " << v[j][i] << " for " << certflag[j].c_str()
268  << std::endl;
269  }
270  }
271  }
272  }
273 
274  int nLS_run = nlumis(filename);
275 
276  for (int iS = 0; iS < smax; iS++) {
277  vector<int> goodLS;
278  vector<int> badLS;
279  vector<int> missingLS;
280  vector<float> allLSthr;
281 
282  //loop over all available lumisections and fill good/bad lists
283  for (int iLS = 0; iLS < vecsize; iLS++) {
284  if (v[iS][iLS] >= threshold)
285  goodLS.push_back(lsd[iLS]);
286  else if (v[iS][iLS] > -1) //protect from flagging non-tested LS as bad
287  badLS.push_back(lsd[iLS]);
288  }
289 
290  int last_idx = 0;
291  for (int i_ls = 1; i_ls <= nLS_run; i_ls++) {
292  for (int j = last_idx; j < vecsize; j++) {
293  if (lsd[j] == i_ls) {
294  last_idx = j + 1;
295  if (v[iS][j] == 0)
296  allLSthr.push_back(0.00001);
297  else
298  allLSthr.push_back(v[iS][j]);
299  break;
300  }
301  if (lsd[j] > i_ls) {
302  last_idx = j;
303  missingLS.push_back(i_ls);
304  allLSthr.push_back(-1);
305  break;
306  }
307  }
308  }
309 
310  Cleaning(goodLS);
311  Cleaning(badLS);
312  Cleaning(missingLS);
313 
314  string goodList = ListOut(goodLS);
315  string badList = ListOut(badLS);
316  string missingList = ListOut(missingLS);
317 
318  //save lumisections for this certification type
319  gLS.push_back(make_pair(goodList, allLSthr));
320  bLS.push_back(make_pair(badList, allLSthr));
321  mLS.push_back(make_pair(missingList, allLSthr));
322  }
323 }
324 
325 void cert_plot(float threshold_pixel,
326  float threshold,
327  string filename,
328  vector<string>& cert_strip,
329  vector<string>& cert_track,
330  vector<string>& cert_pixel,
331  vector<pair<string, vector<float> > >& LS_strip,
332  vector<pair<string, vector<float> > >& LS_track,
333  vector<pair<string, vector<float> > >& LS_pixel) {
334  int nLumiSections = nlumis(filename);
335 
336  char plottitles[200];
337  sprintf(plottitles, "Lumisection Certification: Run %s;Luminosity Section;", runnum_str(filename).c_str());
338  TH2D* cert_plot = new TH2D("cert_plot", plottitles, nLumiSections, 1, nLumiSections + 1, 5, 1, 6);
339  cert_plot->SetStats(false);
340  char label[100];
341  for (int ityp = 0; ityp < 4; ityp++) {
342  sprintf(label, "SiStrip %s", cert_strip[ityp].c_str());
343  cert_plot->GetYaxis()->SetBinLabel(5 - ityp, label);
344 
345  for (unsigned int idx = 0; idx < LS_strip[ityp].second.size(); idx++)
346  if (LS_strip[ityp].second[idx] > -1)
347  cert_plot->SetBinContent(idx + 1, 5 - ityp, LS_strip[ityp].second[idx]);
348  }
349  for (int ityp = 0; ityp < 1; ityp++) {
350  sprintf(label, "Tracking %s", cert_track[ityp].c_str());
351  cert_plot->GetYaxis()->SetBinLabel(1 - ityp, label);
352  for (unsigned int idx = 0; idx < LS_track[ityp].second.size(); idx++)
353  if (LS_track[ityp].second[idx] > -1)
354  cert_plot->SetBinContent(idx + 1, 1 - ityp, LS_track[ityp].second[idx]);
355  }
356 
357  const Int_t colNum = 20; // defining of a new palette
358  Int_t palette[colNum];
359  float rgb[colNum][3];
360  int col_thr = colNum * threshold;
361  for (Int_t i = 0; i < colNum; i++) {
362  if (i >= col_thr) {
363  // green
364  rgb[i][0] = 0.00;
365  rgb[i][1] = 0.80;
366  rgb[i][2] = 0.00;
367  } else {
368  // red to yellow //yellow red
369  rgb[i][0] = 0.80 + (0.98 - 0.80) / (col_thr - 1) * i; //0.98 0.80
370  rgb[i][1] = 0.00 + (0.79 - 0.00) / (col_thr - 1) * i; //0.79 0.00
371  rgb[i][2] = 0.00; //0.00
372  }
373 
374  palette[i] = 9001 + i;
375 
376  TColor* color = gROOT->GetColor(9001 + i);
377  if (!color)
378  color = new TColor(9001 + i, 0, 0, 0, "");
379  color->SetRGB(rgb[i][0], rgb[i][1], rgb[i][2]);
380  }
381  gStyle->SetPalette(colNum, palette);
382  gROOT->SetStyle("Plain");
383 
384  TCanvas* cc = new TCanvas("name", "title", 1000, 600);
385  cert_plot->Draw("colz");
386  gPad->SetLeftMargin(0.17);
387  string plotfilename = "Certification_run_" + runnum_str(filename) + ".png";
388  cc->Print(plotfilename.c_str());
389 
390  //PIXEL plot
391 
392  TH2D* cert_plot_pixel = new TH2D("cert_plot_pixel", plottitles, nLumiSections, 1, nLumiSections + 1, 4, 1, 5);
393  cert_plot_pixel->SetStats(false);
394 
395  for (int ityp = 0; ityp < 4; ityp++) {
396  sprintf(label, "Pixel %s", cert_pixel[ityp].c_str());
397  cert_plot_pixel->GetYaxis()->SetBinLabel(4 - ityp, label);
398 
399  for (unsigned int idx = 0; idx < LS_pixel[ityp].second.size(); idx++)
400  if (LS_pixel[ityp].second[idx] > -1)
401  cert_plot_pixel->SetBinContent(idx + 1, 4 - ityp, LS_pixel[ityp].second[idx]);
402  }
403 
404  int col_thr_pixel = colNum * threshold_pixel;
405 
406  for (Int_t i = 0; i < colNum; i++) {
407  if (i >= col_thr_pixel) {
408  // green
409  rgb[i][0] = 0.00;
410  rgb[i][1] = 0.80;
411  rgb[i][2] = 0.00;
412  } else {
413  // red to yellow //yellow red
414  rgb[i][0] = 0.80 + (0.98 - 0.80) / (col_thr - 1) * i; //0.98 0.80
415  rgb[i][1] = 0.00 + (0.79 - 0.00) / (col_thr - 1) * i; //0.79 0.00
416  rgb[i][2] = 0.00; //0.00
417  }
418 
419  palette[i] = 10001 + i;
420 
421  TColor* color = gROOT->GetColor(10001 + i);
422  if (!color)
423  color = new TColor(10001 + i, 0, 0, 0, "");
424  color->SetRGB(rgb[i][0], rgb[i][1], rgb[i][2]);
425  }
426  gStyle->SetPalette(colNum, palette);
427  gROOT->SetStyle("Plain");
428 
429  cert_plot_pixel->Draw("colz");
430  //gPad->SetLeftMargin(0.17);
431  string plotfilename_pixel = "Certification_run_" + runnum_str(filename) + "_pixel.png";
432  cc->Print(plotfilename_pixel.c_str());
433 
434  delete cc;
435 }
436 
437 int nlumis(string filename) {
438  if (numlumis > -1)
439  return numlumis;
440 
441  //TDirectory* topDir;
442  vector<float> ls;
443 
444  TFile* file = TFile::Open(filename.c_str());
445  if (!file->IsOpen()) {
446  std::cerr << "Failed to open " << filename << std::endl;
447  return -1;
448  }
449 
450  string run = runnum_str(filename);
451 
452  //check if HIRun or pp run
453  bool isHIRun = false;
454  if (filename.find("HIRun") != string::npos)
455  isHIRun = true;
456 
457  //valid up to the end of 2011 pp collisions
458  if (!isHIRun) {
459  string EventInfoDir = "DQMData/Run " + run + "/SiStrip/Run summary/EventInfo";
460  TDirectory* rsEventInfoDir = dynamic_cast<TDirectory*>(file->Get(EventInfoDir.c_str()));
461  rsEventInfoDir->cd();
462  TIter eiKeys(rsEventInfoDir->GetListOfKeys());
463  TKey* eiKey;
464  while ((eiKey = dynamic_cast<TKey*>(eiKeys()))) {
465  string classname(eiKey->GetClassName());
466  if (classname == "TObjString") {
467  string sflag = eiKey->GetName();
468  string tempname = sflag.substr(sflag.find("i=") + 2);
469  size_t pos1 = tempname.find('<');
470  size_t pos2 = sflag.find_first_of('>');
471  string detvalue = tempname.substr(0, pos1);
472  string numlumisec = sflag.substr(1, pos2 - 1);
473  if (numlumisec == (string) "iLumiSection") {
474  numlumis = atoi(detvalue.c_str());
475  break;
476  }
477  }
478  }
479  } else {
480  //valid since 2011 HI running (iLumiSection variable not there anymore)
481  string EventInfoDirHist = "DQMData/Run " + run + "/Info/Run summary/EventInfo/ProcessedLS";
482  TH1F* allLS = (TH1F*)file->Get(EventInfoDirHist.c_str());
483  numlumis = allLS->GetEntries() - 1;
484  delete allLS;
485  }
486 
487  return numlumis;
488 }
489 
490 string runnum_str(string filename) { return filename.substr(filename.find("_R000") + 5, 6); }
491 
492 void Cleaning(vector<int>& LSlist) {
493  if (LSlist.empty())
494  return;
495 
496  //cleaning: keep only 1st and last lumisection in the range
497  int refLS = LSlist[0];
498  for (unsigned int at = 1; at < LSlist.size() - 1; at++) {
499  //delete LSnums in between a single continuous range
500  if (refLS + 1 == LSlist[at] && LSlist[at] + 1 == LSlist[at + 1]) {
501  refLS = LSlist[at];
502  LSlist[at] = -1;
503  } else {
504  refLS = LSlist[at];
505  }
506  }
507 }
508 
509 string ListOut(vector<int>& LSlist) {
510  string strout = "";
511  bool rangeset = false;
512 
513  for (unsigned int at = 0; at < LSlist.size(); at++) {
514  if (LSlist[at] != -1) {
515  if (at > 0 && LSlist[at - 1] != -1)
516  strout += ",";
517  stringstream lsnum;
518  lsnum << LSlist[at];
519  strout += lsnum.str();
520  rangeset = false;
521  }
522  if (LSlist[at] == -1 && !rangeset) {
523  strout += "-";
524  rangeset = true;
525  }
526  }
527 
528  return strout;
529 }
void Cleaning(vector< int > &LSlist)
Definition: ls_cert.cc:492
uint32_t cc[maxCellsPerHit]
Definition: gpuFishbone.h:49
string runnum_str(string filename)
Definition: ls_cert.cc:490
void cert_plot(float threshold_pixel, float threshold, string filename, vector< string > &cert_strip, vector< string > &cert_track, vector< string > &cert_pixel, vector< pair< string, vector< float > > > &LS_strip, vector< pair< string, vector< float > > > &LS_track, vector< pair< string, vector< float > > > &LS_pixel)
Definition: ls_cert.cc:325
int main(int argc, char *argv[])
Definition: ls_cert.cc:32
U second(std::pair< T, U > const &p)
char const * label
void ls_cert(float threshold_pixel, float threshold, string filename)
Definition: ls_cert.cc:55
string ListOut(vector< int > &LSlist)
Definition: ls_cert.cc:509
key
prepare the HTCondor submission files and eventually submit them
int numlumis
Definition: ls_cert.cc:26
#define debug
Definition: HDRShower.cc:19
int nlumis(string filename)
Definition: ls_cert.cc:437
def rgb(r, g, b, maximum=1.)
Definition: svgfig.py:40
void ls_cert_type(string iDir, float threshold, string filename, vector< string > &cert, vector< pair< string, vector< float > > > &gLS, vector< pair< string, vector< float > > > &bLS, vector< pair< string, vector< float > > > &mLS)
Definition: ls_cert.cc:156