CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_3/src/DQM/SiStripMonitorClient/bin/ls_cert.cc

Go to the documentation of this file.
00001 #include <Riostream.h>
00002 #include <TDirectory.h>
00003 #include <TFile.h>
00004 #include <TROOT.h>
00005 #include <TStyle.h>
00006 #include <TKey.h>
00007 #include <TH1.h>
00008 #include <TH2.h>
00009 #include <TH2D.h>
00010 #include <TCanvas.h>
00011 #include <TGraph.h>
00012 #include <TPaveStats.h>
00013 #include <TText.h>
00014 #include <TLegend.h>
00015 #include <string.h>
00016 #include <utility>
00017 #include <vector>
00018 #include <sstream>
00019 #include <algorithm>
00020 #include <TString.h>
00021 #include <TColor.h>
00022 
00023 using namespace std;
00024 
00025 //global vars
00026 int numlumis = -1;
00027 
00028 int     nlumis     ( string filename ); //get number of run lumisections
00029 string  runnum_str ( string filename ); //read the run number, return in string
00030 void    ls_cert( float threshold_pixel , float threshold , string filename ) ;
00031 
00032 int main(int argc , char *argv[]) {
00033 
00034   if(argc==4) {
00035     char* cpixel = argv[1];
00036     char* cthr = argv[2];
00037     char* filename = argv[3];
00038 
00039     float threshold_pixel = 0;
00040     sscanf(cpixel,"%f",&threshold_pixel);
00041     float threshold = 0;
00042     sscanf(cthr,"%f",&threshold);
00043 
00044     std::cout << "ready to run ls_cert: pixel thr  " << threshold_pixel 
00045               << " threshold " << threshold
00046               << " filename " << filename << std::endl;
00047 
00048     ls_cert(threshold_pixel,threshold,filename);
00049 
00050   }
00051   else {std::cout << "Too few arguments: " << argc << std::endl; return -1; }
00052   return 0;
00053 
00054 }
00055 
00056 void    ls_cert( float threshold_pixel , float threshold , string filename ) 
00057 {
00058   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> > >& );
00059   void cert_plot    ( float threshold_pixel , float threshold , string filename , 
00060                       vector <string>& , vector <string>& , vector <string>& , vector<pair<string,vector<float> > >& , vector<pair<string,vector<float> > >& , vector<pair<string,vector<float> > >& );
00061 
00062   //presets
00063   numlumis = -1;
00064 
00065   //certifications
00066   vector <string> cert_strip;
00067   vector <string> cert_track;
00068   vector <string> cert_pixel;
00069   
00070   //good lumisections
00071   vector < pair < string,vector <float> > > gLS_strip;
00072   vector < pair < string,vector <float> > > gLS_track;
00073   vector < pair < string,vector <float> > > gLS_pixel;
00074 
00075   //bad lumisections
00076   vector < pair < string,vector <float> > > bLS_strip;
00077   vector < pair < string,vector <float> > > bLS_track;
00078   vector < pair < string,vector <float> > > bLS_pixel;
00079 
00080   //missing lumisections
00081   vector < pair < string,vector <float> > > mLS_strip;
00082   vector < pair < string,vector <float> > > mLS_track;
00083   vector < pair < string,vector <float> > > mLS_pixel;
00084 
00085   ls_cert_type( "SiStrip"  , threshold       , filename , cert_strip , gLS_strip , bLS_strip , mLS_strip );
00086   ls_cert_type( "Tracking" , threshold       , filename , cert_track , gLS_track , bLS_track , mLS_track );
00087   ls_cert_type( "Pixel"    , threshold_pixel , filename , cert_pixel , gLS_pixel , bLS_pixel , mLS_pixel );
00088 
00089   ofstream outfile;
00090   string namefile = "Certification_run_" + runnum_str( filename ) + ".txt";
00091   outfile.open(namefile.c_str());
00092   outfile << "Lumisection Certification (GOOD: >= " << threshold_pixel << " [Pixel]; >= " << threshold << " [SiStrip,Tracking] " 
00093           << ", otherwise BAD):" << endl << endl;
00094   outfile << "GOOD Lumisections:" << endl;
00095   char line[200];
00096   for (int ityp = 0; ityp < 4; ityp++)
00097     {
00098       sprintf( line, " Pixel    %*sSummary: %s" , 13 , cert_pixel[ityp].c_str() , gLS_pixel[ityp].first.c_str() );
00099       outfile << line << endl;
00100     }
00101   for (int ityp = 0; ityp < 4; ityp++)
00102     {
00103       sprintf( line, " SiStrip  %*sSummary: %s" , 13 , cert_strip[ityp].c_str() , gLS_strip[ityp].first.c_str() );
00104       outfile << line << endl;
00105     }
00106   for (int ityp = 0; ityp < 1; ityp++)
00107     {
00108       sprintf( line, " Tracking %*sSummary: %s" , 13 , cert_track[ityp].c_str() , gLS_track[ityp].first.c_str() );
00109       outfile << line << endl;
00110     }
00111 
00112   outfile << "\nBAD Lumisections:" << endl;
00113   for (int ityp = 0; ityp < 4; ityp++)
00114     {
00115       sprintf( line, " Pixel    %*sSummary: %s" , 13 , cert_pixel[ityp].c_str() , bLS_pixel[ityp].first.c_str() );
00116       outfile << line << endl;
00117     }
00118   for (int ityp = 0; ityp < 4; ityp++)
00119     {
00120       sprintf( line, " SiStrip  %*sSummary: %s" , 13 , cert_strip[ityp].c_str() , bLS_strip[ityp].first.c_str() );
00121       outfile << line << endl;
00122     }
00123   for (int ityp = 0; ityp < 1; ityp++)
00124     {
00125       sprintf( line, " Tracking %*sSummary: %s" , 13 , cert_track[ityp].c_str() , bLS_track[ityp].first.c_str() );
00126       outfile << line << endl;
00127     }
00128   
00129   outfile << "\nMISSING Lumisections:" << endl;
00130   for (int ityp = 0; ityp < 4; ityp++)
00131     {
00132       sprintf( line, " Pixel    %*sSummary: %s" , 13 , cert_pixel[ityp].c_str() , mLS_pixel[ityp].first.c_str() );
00133       outfile << line << endl;
00134     }
00135   for (int ityp = 0; ityp < 4; ityp++)
00136     {
00137       sprintf( line, " SiStrip  %*sSummary: %s" , 13 , cert_strip[ityp].c_str() , mLS_strip[ityp].first.c_str() );
00138       outfile << line << endl;
00139     }
00140   for (int ityp = 0; ityp < 1; ityp++)
00141     {
00142       sprintf( line, " Tracking %*sSummary: %s" , 13 , cert_track[ityp].c_str() , mLS_track[ityp].first.c_str() );
00143       outfile << line << endl;
00144     }
00145   
00146   outfile.close();
00147   std::cout << "Lumisection Certification summary saved in " << namefile << std::endl;
00148 
00149   cert_plot ( threshold_pixel , threshold , filename , cert_strip , cert_track , cert_pixel , gLS_strip , gLS_track , gLS_pixel );
00150 }
00151 
00152 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 ) 
00153 {  
00154   void Cleaning(vector<int> &);
00155   string ListOut(vector<int> &);
00156 
00157   bool debug = false;
00158   string run = runnum_str( filename );
00159   if (debug) std::cout << filename.c_str() << std::endl;
00160 
00161   TDirectory* topDir; 
00162   vector<float> ls;
00163   
00164   TFile* file = TFile::Open(filename.c_str());
00165   if (!file->IsOpen()) {
00166     std::cerr << "Failed to open " << filename << std::endl; 
00167     return;
00168   }
00169 
00170   string dir = "DQMData/Run " + run + "/" + iDir;
00171   topDir = dynamic_cast<TDirectory*>( file->Get(dir.c_str()));
00172   topDir->cd();
00173   if (debug) std::cout << topDir->GetTitle() << std::endl;
00174 
00175   //
00176   // Reading the LS directory    
00177   //
00178   TIter next(topDir->GetListOfKeys());
00179   TKey *key;
00180   while  ( (key = dynamic_cast<TKey*>(next())) ) {
00181     string clName(key->GetClassName());
00182     if (clName == "TDirectoryFile") {
00183       TDirectory *curr_dir = dynamic_cast<TDirectory*>(key->ReadObj());
00184       string name = curr_dir->GetName();
00185       if (name == "Run summary") continue;
00186       name = name.substr(name.find("-")+1);
00187       float temp1 = atof(name.c_str()); 
00188       ls.push_back(temp1);
00189     }
00190   }
00191   sort(ls.begin(),ls.end());   
00192   int vecsize = ls.size();
00193 
00194   //
00195   // Definition of vectors for LS certification
00196   //
00197   Float_t * lsd = new Float_t[vecsize];
00198   
00199   Float_t ** v = new Float_t*[4];
00200   for(int k=0;k<4;k++) {
00201     v[k] = new Float_t[vecsize];
00202   }
00203   //string certflag[4] = {"CertificationSummary","DAQSummary","DCSSummary","reportSummary"};
00204   //string certflagPrint[4] = {"Certification","DAQ","DCS","DQM"};
00205   string certflag[4] = {"DAQSummary","DCSSummary","reportSummary","CertificationSummary"};
00206   string certflagPrint[4] = {"DAQ","DCS","DQM","Certification"};
00207 
00208   int smax = 2;
00209   if ( iDir == "SiStrip" || iDir == "Pixel" ) smax = 4;
00210   
00211   if ( iDir == "Tracking" )
00212     {
00213       certflag[0] = "CertificationSummary";
00214       certflagPrint[0] = "Certification";
00215       certflag[1] = "reportSummary";
00216       certflagPrint[1] = "DQM";
00217     }
00218 
00219   for (int icert_type = 0; icert_type < smax; icert_type++)
00220     {
00221       cert.push_back( certflagPrint[icert_type] );
00222     }
00223 
00224   if (debug) std::cout << gDirectory->GetName() << std::endl;
00225   
00226   for (int i=0; i < vecsize; i++){
00227     stringstream lsdir;
00228     lsdir << dir << "/By Lumi Section " << ls[i] <<"-"<<ls[i]<<"/EventInfo";
00229     if (debug) std::cout << lsdir.str().c_str() << std::endl;
00230     float templs = ls[i];
00231     lsd[i] = templs;
00232     TDirectory *tempDir = dynamic_cast<TDirectory*>( file->Get(lsdir.str().c_str()));
00233     tempDir->cd();
00234     int j = 0;      
00235     TIter nextTemp(tempDir->GetListOfKeys());
00236     TKey *keyTemp;
00237     while  ( (keyTemp = dynamic_cast<TKey*>(nextTemp())) ) {
00238       float tempvalue = -1.;
00239       string classname(keyTemp->GetClassName());
00240       if (classname=="TObjString" ){
00241         string sflag = keyTemp->GetName();
00242         string tempname = sflag.substr(sflag.find("f=")+2);
00243         size_t pos1 = tempname.find("<");
00244         size_t pos2 = sflag.find_first_of(">");
00245         string detvalue = tempname.substr(0,pos1);
00246         string typecert = sflag.substr(1,pos2-1);
00247         if (debug) std::cout << typecert.c_str() << std::endl;
00248         tempvalue = atof(detvalue.c_str());
00249         
00250         for (j=0; j<smax; j++){
00251           if ( strstr(typecert.c_str(),certflag[j].c_str())!=NULL)
00252             v[j][i] = tempvalue;
00253           if (debug) std::cout << "Entering value " << tempvalue << " " << v[j][i] << " for " << certflag[j].c_str() << std::endl;
00254         }
00255         j = j + 1;
00256       }
00257     }
00258   }
00259 
00260   int nLS_run = nlumis ( filename );
00261 
00262   for (int iS = 0; iS < smax; iS++)
00263     {
00264       
00265       vector<int> goodLS;
00266       vector<int> badLS;
00267       vector<int> missingLS;
00268       vector<float> allLSthr;
00269 
00270       //loop over all available lumisections and fill good/bad lists
00271       for (int iLS = 0; iLS < vecsize; iLS++)
00272         {
00273           if ( v[iS][iLS] >= threshold ) 
00274             goodLS.push_back(lsd[iLS]);
00275           else                                  
00276             if ( v[iS][iLS] > -1 ) //protect from flagging non-tested LS as bad
00277               badLS.push_back(lsd[iLS]);
00278         }
00279 
00280       int last_idx = 0;
00281       for (int i_ls = 1; i_ls <= nLS_run; i_ls++)
00282         {
00283           for (int j = last_idx; j < vecsize; j++)
00284             {
00285               if ( lsd[j] == i_ls ) 
00286                 {
00287                   last_idx = j+1;
00288                   if ( v[iS][j] == 0 ) allLSthr.push_back(0.00001);
00289                   else
00290                     allLSthr.push_back(v[iS][j]);
00291                   break;
00292                 }
00293               if ( lsd[j] > i_ls )
00294                 {
00295                   last_idx = j;
00296                   missingLS.push_back(i_ls);
00297                   allLSthr.push_back(-1);
00298                   break;
00299                 }
00300             }
00301         }
00302 
00303       Cleaning( goodLS    );
00304       Cleaning( badLS     );
00305       Cleaning( missingLS );
00306 
00307       string goodList    = ListOut( goodLS    );
00308       string badList     = ListOut( badLS     );
00309       string missingList = ListOut( missingLS );
00310 
00311       //save lumisections for this certification type
00312       gLS.push_back ( make_pair ( goodList    , allLSthr ) );
00313       bLS.push_back ( make_pair ( badList     , allLSthr ) );
00314       mLS.push_back ( make_pair ( missingList , allLSthr ) );      
00315     }
00316 }
00317 
00318 void cert_plot( float threshold_pixel , float threshold , string filename , vector <string>& cert_strip , vector <string>& cert_track , 
00319                 vector <string>& cert_pixel , vector<pair<string,vector<float> > >& LS_strip , vector<pair<string,vector<float> > >& LS_track , 
00320                 vector<pair<string,vector<float> > >& LS_pixel )
00321 {
00322   int nLumiSections = nlumis( filename );
00323 
00324   char plottitles[200];
00325   sprintf( plottitles , "Lumisection Certification: Run %s;Luminosity Section;" , runnum_str( filename ).c_str() );
00326   TH2D *cert_plot = new TH2D( "cert_plot" , plottitles , nLumiSections , 1 , nLumiSections + 1 , 5 , 1 , 6 );
00327   cert_plot->SetStats(0);
00328   char label[100];
00329   for (int ityp = 0; ityp < 4; ityp++)
00330     {
00331       sprintf( label , "SiStrip %s" , cert_strip[ityp].c_str() );
00332       cert_plot->GetYaxis()->SetBinLabel( 5 - ityp , label );
00333 
00334       for (unsigned int idx = 0; idx <  LS_strip[ityp].second.size() ; idx++)
00335         if ( LS_strip[ityp].second[idx] > -1 ) cert_plot->SetBinContent(idx+1, 5 - ityp, LS_strip[ityp].second[idx]);
00336     }
00337   for (int ityp = 0; ityp < 1; ityp++)
00338     {
00339       sprintf( label , "Tracking %s" , cert_track[ityp].c_str() );
00340       cert_plot->GetYaxis()->SetBinLabel( 1 - ityp , label );
00341       for (unsigned int idx = 0; idx <  LS_track[ityp].second.size() ; idx++)
00342         if ( LS_track[ityp].second[idx] > -1 ) cert_plot->SetBinContent(idx+1, 1 - ityp, LS_track[ityp].second[idx]);
00343     }
00344 
00345   const Int_t colNum = 20; // defining of a new palette
00346   Int_t palette[colNum];
00347   float rgb[colNum][3];
00348   int col_thr = colNum * threshold;
00349   for (Int_t i=0; i<colNum; i++)
00350     {
00351       if ( i >= col_thr )
00352         {
00353           // green
00354           rgb[i][0] = 0.00;
00355           rgb[i][1] = 0.80;
00356           rgb[i][2] = 0.00;
00357         }
00358       else
00359         {
00360           // red to yellow   //yellow red
00361           rgb[i][0] = 0.80 + ( 0.98 - 0.80 ) / ( col_thr - 1 ) * i ;  //0.98   0.80
00362           rgb[i][1] = 0.00 + ( 0.79 - 0.00 ) / ( col_thr - 1 ) * i ;  //0.79   0.00
00363           rgb[i][2] = 0.00;  //0.00
00364         }
00365 
00366       palette[i] = 9001+i;
00367 
00368       TColor *color = gROOT->GetColor(9001+i);
00369       if (!color) color = new TColor(9001 + i, 0, 0, 0, "");
00370       color->SetRGB(rgb[i][0], rgb[i][1], rgb[i][2]);
00371     }
00372   gStyle->SetPalette(colNum,palette);
00373   gROOT->SetStyle("Plain");
00374 
00375   TCanvas *cc = new TCanvas( "name" , "title" , 1000 , 600 );
00376   cert_plot->Draw("colz");
00377   gPad->SetLeftMargin(0.17);
00378   string plotfilename = "Certification_run_" +  runnum_str( filename ) + ".png";
00379   cc->Print( plotfilename.c_str() );
00380 
00381 
00382   //PIXEL plot
00383 
00384   TH2D *cert_plot_pixel = new TH2D( "cert_plot_pixel" , plottitles , nLumiSections , 1 , nLumiSections + 1 , 4 , 1 , 5 );
00385   cert_plot_pixel->SetStats(0);
00386 
00387   for (int ityp = 0; ityp < 4; ityp++)
00388     {
00389       sprintf( label , "Pixel %s" , cert_pixel[ityp].c_str() );
00390       cert_plot_pixel->GetYaxis()->SetBinLabel( 4 - ityp , label );
00391 
00392       for (unsigned int idx = 0; idx <  LS_pixel[ityp].second.size() ; idx++)
00393         if ( LS_pixel[ityp].second[idx] > -1 ) cert_plot_pixel->SetBinContent(idx+1, 4 - ityp, LS_pixel[ityp].second[idx]);
00394     }
00395 
00396   int col_thr_pixel = colNum * threshold_pixel;
00397 
00398   for (Int_t i=0; i<colNum; i++)
00399     {
00400       if ( i >= col_thr_pixel )
00401         {
00402           // green
00403           rgb[i][0] = 0.00;
00404           rgb[i][1] = 0.80;
00405           rgb[i][2] = 0.00;
00406         }
00407       else
00408         {
00409           // red to yellow   //yellow red
00410           rgb[i][0] = 0.80 + ( 0.98 - 0.80 ) / ( col_thr - 1 ) * i ;  //0.98   0.80
00411           rgb[i][1] = 0.00 + ( 0.79 - 0.00 ) / ( col_thr - 1 ) * i ;  //0.79   0.00
00412           rgb[i][2] = 0.00;  //0.00
00413         }
00414 
00415       palette[i] = 10001+i;
00416 
00417       TColor *color = gROOT->GetColor(10001+i);
00418       if (!color) color = new TColor(10001 + i, 0, 0, 0, "");
00419       color->SetRGB(rgb[i][0], rgb[i][1], rgb[i][2]);
00420     }
00421   gStyle->SetPalette(colNum,palette);
00422   gROOT->SetStyle("Plain");
00423 
00424   cert_plot_pixel->Draw("colz");
00425   //gPad->SetLeftMargin(0.17);
00426   string plotfilename_pixel = "Certification_run_" +  runnum_str( filename ) + "_pixel.png";
00427   cc->Print( plotfilename_pixel.c_str() );
00428 
00429   delete cc;
00430 }
00431 
00432 
00433 int nlumis( string filename )
00434 {
00435   if ( numlumis > -1 ) 
00436     return numlumis;
00437 
00438   //TDirectory* topDir;
00439   vector<float> ls;
00440 
00441   TFile* file = TFile::Open(filename.c_str());
00442   if (!file->IsOpen()) {
00443     std::cerr << "Failed to open " << filename << std::endl;
00444     return -1;
00445   }
00446   
00447   string run = runnum_str( filename );
00448 
00449   //check if HIRun or pp run
00450   bool isHIRun = false;
00451   if ( filename.find ( "HIRun" ) != string::npos )
00452     isHIRun = true;
00453 
00454   //valid up to the end of 2011 pp collisions
00455   if ( !isHIRun )
00456     {
00457       string EventInfoDir = "DQMData/Run " + run + "/SiStrip/Run summary/EventInfo";
00458       TDirectory *rsEventInfoDir = dynamic_cast<TDirectory*>( file->Get(EventInfoDir.c_str()));
00459       rsEventInfoDir->cd();
00460       TIter eiKeys(rsEventInfoDir->GetListOfKeys());
00461       TKey *eiKey;
00462       while  ( (eiKey = dynamic_cast<TKey*>(eiKeys())) )
00463         {
00464           string classname(eiKey->GetClassName());
00465           if (classname=="TObjString" )
00466             {
00467               string sflag = eiKey->GetName();
00468               string tempname = sflag.substr(sflag.find("i=")+2);
00469               size_t pos1 = tempname.find("<");
00470               size_t pos2 = sflag.find_first_of(">");
00471               string detvalue = tempname.substr(0,pos1);
00472               string numlumisec = sflag.substr(1,pos2-1);
00473               if ( numlumisec.c_str() == (string)"iLumiSection" )
00474                 {
00475                   numlumis = atoi( detvalue.c_str() );
00476                   break;
00477                 }
00478             }
00479         }
00480     }
00481   else
00482     {
00483       //valid since 2011 HI running (iLumiSection variable not there anymore)  
00484       string EventInfoDirHist = "DQMData/Run " + run + "/Info/Run summary/EventInfo/ProcessedLS";
00485       TH1F* allLS = (TH1F*) file->Get( EventInfoDirHist.c_str() );
00486       numlumis = allLS->GetEntries() - 1;
00487       delete allLS;
00488     }
00489 
00490   return numlumis;
00491 }
00492 
00493 string runnum_str( string filename )
00494 {
00495   return filename.substr(filename.find("_R000")+5, 6);  
00496 }
00497 
00498 void Cleaning( vector<int> &LSlist)
00499 {
00500   if ( LSlist.size() == 0 ) return;
00501 
00502   //cleaning: keep only 1st and last lumisection in the range
00503   int refLS = LSlist[0];
00504   for (unsigned int at = 1; at < LSlist.size() - 1; at++) 
00505     {
00506       //delete LSnums in between a single continuous range
00507       if ( refLS + 1 == LSlist[at] && LSlist[at] + 1 == LSlist[at+1] )
00508         {
00509           refLS = LSlist[at];
00510           LSlist[at] = -1;
00511         }
00512       else
00513         {
00514           refLS = LSlist[at];
00515         }
00516     }
00517   
00518   
00519 }
00520 
00521 string ListOut(vector<int> &LSlist)
00522 {
00523   
00524   string strout = "";
00525   bool rangeset = false;
00526 
00527   for (unsigned int at = 0; at < LSlist.size(); at++)
00528     {
00529       if ( LSlist[at] != -1 ) 
00530         {
00531           if ( at > 0 && LSlist[at-1] != -1 ) strout += ",";
00532           stringstream lsnum;
00533           lsnum << LSlist[at];
00534           strout += lsnum.str();
00535           rangeset = false;
00536         }
00537       if ( LSlist[at] == -1 && !rangeset )
00538         {
00539           strout += "-";
00540           rangeset = true;
00541         }
00542     }
00543 
00544   return strout;
00545 }