CMS 3D CMS Logo

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