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
00026 bool debug = false;
00027 int numlumis = -1;
00028
00029 int nlumis ( string filename );
00030 string runnum_str ( string filename );
00031 int getplot ( string filename , string iDir , string strplot , TH1F& plot );
00032 void Cleaning ( vector<int> & );
00033 string ListOut ( vector<int> & );
00034 void vector_AND ( vector<int> & , vector<int> );
00035 void lsbs_cert( string filename );
00036
00037 int main(int argc , char *argv[]) {
00038
00039 if(argc==2) {
00040 char* filename = argv[1];
00041
00042 std::cout << "ready to run lsbs filename " << filename << std::endl;
00043
00044 lsbs_cert(filename);
00045
00046 }
00047 else {std::cout << "Too few arguments: " << argc << std::endl; return -1; }
00048 return 0;
00049
00050 }
00051
00052 void lsbs_cert( string filename )
00053 {
00054 void check_offset ( string filename , string iDir , string plot , float limit_min , float limit_max , vector <int>& );
00055 void check_sigma ( string filename , string iDir , string plot , float limit_err , vector <int>& );
00056 bool check_isgood ( vector<int> & , int ls );
00057
00058
00059 numlumis = -1;
00060
00061 float limit_x = 0.002;
00062 float limit_y = 0.002;
00063 float limit_z = 0.5;
00064 float limit_dx = 0.002;
00065 float limit_dy = 0.002;
00066 float limit_dz = 0.5;
00067 float limit_errdx = 0.002;
00068 float limit_errdy = 0.002;
00069 float limit_errdz = 0.5;
00070
00071
00072
00073 vector <int> ls_x_bad;
00074 vector <int> ls_y_bad;
00075 vector <int> ls_z_bad;
00076
00077 vector <int> ls_xsc_bad;
00078 vector <int> ls_ysc_bad;
00079 vector <int> ls_zsc_bad;
00080
00081 vector <int> ls_dx_bad;
00082 vector <int> ls_dy_bad;
00083 vector <int> ls_dz_bad;
00084
00085 vector <int> ls_dxsc_bad;
00086 vector <int> ls_dysc_bad;
00087 vector <int> ls_dzsc_bad;
00088
00089 vector <int> ls_errdx_bad;
00090 vector <int> ls_errdy_bad;
00091 vector <int> ls_errdz_bad;
00092
00093 vector <int> ls_errdxsc_bad;
00094 vector <int> ls_errdysc_bad;
00095 vector <int> ls_errdzsc_bad;
00096
00097 vector <int> ls_good;
00098 vector <int> ls_bad;
00099
00100
00101 check_offset ( filename, "Validation" , "hxLumibased PrimaryVertex-DataBase" , -limit_x , limit_x , ls_x_bad );
00102 check_offset ( filename, "Validation" , "hyLumibased PrimaryVertex-DataBase" , -limit_y , limit_y , ls_y_bad );
00103 check_offset ( filename, "Validation" , "hzLumibased PrimaryVertex-DataBase" , -limit_z , limit_z , ls_z_bad );
00104
00105
00106 check_offset ( filename, "Validation" , "hxLumibased Scalers-DataBase fit" , -limit_x , limit_x , ls_xsc_bad );
00107 check_offset ( filename, "Validation" , "hyLumibased Scalers-DataBase fit" , -limit_y , limit_y , ls_ysc_bad );
00108 check_offset ( filename, "Validation" , "hzLumibased Scalers-DataBase fit" , -limit_z , limit_z , ls_zsc_bad );
00109
00110 check_offset ( filename, "Debug" , "hsigmaXLumibased PrimaryVertex-DataBase fit" , -limit_dx , limit_dx , ls_dx_bad );
00111 check_offset ( filename, "Debug" , "hsigmaYLumibased PrimaryVertex-DataBase fit" , -limit_dy , limit_dy , ls_dy_bad );
00112 check_offset ( filename, "Debug" , "hsigmaZLumibased PrimaryVertex-DataBase fit" , -limit_dz , limit_dz , ls_dz_bad );
00113
00114 check_offset ( filename, "Validation" , "hsigmaXLumibased Scalers-DataBase fit" , -limit_dx , limit_dx , ls_dxsc_bad );
00115 check_offset ( filename, "Validation" , "hsigmaYLumibased Scalers-DataBase fit" , -limit_dy , limit_dy , ls_dysc_bad );
00116 check_offset ( filename, "Validation" , "hsigmaZLumibased Scalers-DataBase fit" , -limit_dz , limit_dz , ls_dzsc_bad );
00117
00118 check_sigma ( filename, "Debug" , "hsigmaXLumibased PrimaryVertex-DataBase fit" , limit_errdx , ls_errdx_bad );
00119 check_sigma ( filename, "Debug" , "hsigmaYLumibased PrimaryVertex-DataBase fit" , limit_errdy , ls_errdy_bad );
00120 check_sigma ( filename, "Debug" , "hsigmaZLumibased PrimaryVertex-DataBase fit" , limit_errdz , ls_errdz_bad );
00121
00122 check_sigma ( filename, "Validation" , "hsigmaXLumibased Scalers-DataBase fit" , limit_errdx , ls_errdxsc_bad );
00123 check_sigma ( filename, "Validation" , "hsigmaYLumibased Scalers-DataBase fit" , limit_errdy , ls_errdysc_bad );
00124 check_sigma ( filename, "Validation" , "hsigmaZLumibased Scalers-DataBase fit" , limit_errdz , ls_errdzsc_bad );
00125
00126
00127 vector_AND ( ls_x_bad , ls_xsc_bad );
00128 vector_AND ( ls_y_bad , ls_ysc_bad );
00129 vector_AND ( ls_z_bad , ls_zsc_bad );
00130 vector_AND ( ls_dx_bad , ls_dxsc_bad );
00131 vector_AND ( ls_dy_bad , ls_dysc_bad );
00132 vector_AND ( ls_dz_bad , ls_dzsc_bad );
00133 vector_AND ( ls_errdx_bad , ls_errdxsc_bad );
00134 vector_AND ( ls_errdy_bad , ls_errdysc_bad );
00135 vector_AND ( ls_errdz_bad , ls_errdzsc_bad );
00136
00137
00138 for ( int i = 1; i <= nlumis ( filename ) ; i++ )
00139 {
00140 if ( !check_isgood ( ls_x_bad , i ) && !check_isgood ( ls_xsc_bad , i ) )
00141 {
00142 ls_bad.push_back ( i );
00143 continue;
00144 }
00145 else
00146 if ( !check_isgood ( ls_y_bad , i ) && !check_isgood ( ls_ysc_bad , i ) )
00147 {
00148 ls_bad.push_back ( i );
00149 continue;
00150 }
00151 else
00152 if ( !check_isgood ( ls_z_bad , i ) && !check_isgood ( ls_zsc_bad , i ) )
00153 {
00154 ls_bad.push_back ( i );
00155 continue;
00156 }
00157 else
00158 if ( !check_isgood ( ls_dx_bad , i ) && !check_isgood ( ls_dxsc_bad , i ) )
00159 {
00160 ls_bad.push_back ( i );
00161 continue;
00162 }
00163 else
00164 if ( !check_isgood ( ls_dy_bad , i ) && !check_isgood ( ls_dysc_bad , i ) )
00165 {
00166 ls_bad.push_back ( i );
00167 continue;
00168 }
00169 else
00170 if ( !check_isgood ( ls_dz_bad , i ) && !check_isgood ( ls_dzsc_bad , i ) )
00171 {
00172 ls_bad.push_back ( i );
00173 continue;
00174 }
00175 else
00176 if ( !check_isgood ( ls_errdx_bad , i ) && !check_isgood ( ls_errdxsc_bad , i ) )
00177 {
00178 ls_bad.push_back ( i );
00179 continue;
00180 }
00181 else
00182 if ( !check_isgood ( ls_errdy_bad , i ) && !check_isgood ( ls_errdysc_bad , i ) )
00183 {
00184 ls_bad.push_back ( i );
00185 continue;
00186 }
00187 else
00188 if ( !check_isgood ( ls_errdz_bad , i ) && !check_isgood ( ls_errdzsc_bad , i ) )
00189 {
00190 ls_bad.push_back ( i );
00191 continue;
00192 }
00193
00194
00195 ls_good.push_back( i );
00196 }
00197
00198 ofstream outfile;
00199 string namefile = "Certification_BS_run_" + runnum_str( filename ) + ".txt";
00200 outfile.open(namefile.c_str());
00201 outfile << "Lumibased BeamSpot Calibration Certification for run " << runnum_str( filename ) << ":" << endl << endl;
00202
00203 char line[2000];
00204 sprintf( line, " GOOD Lumisections (values within limits): %s" , ListOut( ls_good ).c_str() );
00205 outfile << line << endl;
00206
00207 if ( ls_bad.size() > 0 )
00208 {
00209 sprintf( line, " BAD Lumisections (values outside limits): %s" , ListOut( ls_bad ).c_str() );
00210 outfile << line << endl;
00211
00212 sprintf( line, " --- histogram name --- --- bad lumisection list(*) ---" );
00213 outfile << line << endl;
00214 sprintf( line, " hxLumibased PrimaryVertex-DataBase (mean): %s" , ListOut( ls_x_bad ).c_str() );
00215 if ( ls_x_bad.size() > 0 ) outfile << line << endl;
00216 sprintf( line, " hyLumibased PrimaryVertex-DataBase (mean): %s" , ListOut( ls_y_bad ).c_str() );
00217 if ( ls_y_bad.size() > 0 ) outfile << line << endl;
00218 sprintf( line, " hzLumibased PrimaryVertex-DataBase (mean): %s" , ListOut( ls_z_bad ).c_str() );
00219 if ( ls_z_bad.size() > 0 ) outfile << line << endl;
00220
00221 sprintf( line, " hsigmaXLumibased PrimaryVertex-DataBase fit (mean): %s" , ListOut( ls_dx_bad ).c_str() );
00222 if ( ls_dx_bad.size() > 0 ) outfile << line << endl;
00223 sprintf( line, " hsigmaYLumibased PrimaryVertex-DataBase fit (mean): %s" , ListOut( ls_dy_bad ).c_str() );
00224 if ( ls_dy_bad.size() > 0 ) outfile << line << endl;
00225 sprintf( line, " hsigmaZLumibased PrimaryVertex-DataBase fit (mean): %s" , ListOut( ls_dz_bad ).c_str() );
00226 if ( ls_dz_bad.size() > 0 ) outfile << line << endl;
00227
00228 sprintf( line, " hsigmaXLumibased PrimaryVertex-DataBase fit (error): %s" , ListOut( ls_errdx_bad ).c_str() );
00229 if ( ls_errdx_bad.size() > 0 ) outfile << line << endl;
00230 sprintf( line, " hsigmaYLumibased PrimaryVertex-DataBase fit (error): %s" , ListOut( ls_errdy_bad ).c_str() );
00231 if ( ls_errdy_bad.size() > 0 ) outfile << line << endl;
00232 sprintf( line, " hsigmaZLumibased PrimaryVertex-DataBase fit (error): %s" , ListOut( ls_errdz_bad ).c_str() );
00233 if ( ls_errdz_bad.size() > 0 ) outfile << line << endl;
00234
00235 sprintf( line, " (*) also bad in the corresponding 'Scalers-Database fit' histograms" );
00236 outfile << line << endl;
00237 }
00238
00239 outfile.close();
00240 std::cout << "Lumibased BeamSpot Calibration Certification summary saved in " << namefile << endl;
00241 }
00242
00243 void check_offset ( string filename , string iDir , string plot , float limit_min , float limit_max , vector <int>& badLS )
00244 {
00245 TH1F checkPlot;
00246 if ( getplot ( filename , iDir , plot , checkPlot ) < 0 ) return;
00247
00248
00249
00250 for ( int i = 1; i <= checkPlot.GetNbinsX() ; i++ )
00251 {
00252 float value = checkPlot.GetBinContent( i );
00253 if ( value < limit_min || value > limit_max )
00254 {
00255 badLS.push_back( (int)checkPlot.GetBinCenter( i ) );
00256 }
00257 }
00258 }
00259
00260
00261 void check_sigma ( string filename , string iDir , string plot , float limit_err , vector <int>& badLS )
00262 {
00263 TH1F checkPlot;
00264 if ( getplot ( filename , iDir , plot , checkPlot ) < 0 ) return;
00265
00266
00267 for ( int i = 1; i <= checkPlot.GetNbinsX() ; i++ )
00268 {
00269 float value = checkPlot.GetBinError( i );
00270 if ( value > limit_err )
00271 {
00272 badLS.push_back( (int)checkPlot.GetBinCenter( i ) );
00273 }
00274 }
00275
00276 }
00277
00278 bool check_isgood ( vector<int> & ls_badlist, int ls )
00279 {
00280
00281 for ( unsigned int i = 0; i < ls_badlist.size() ; i++ )
00282 {
00283 if ( ls == ls_badlist[i] ) return false;
00284 }
00285 return true;
00286 }
00287
00288 int nlumis( string filename )
00289 {
00290 if ( numlumis > -1 )
00291 return numlumis;
00292
00293 TFile* file = TFile::Open(filename.c_str());
00294 if (!file->IsOpen()) {
00295 cerr << "Failed to open " << filename << endl;
00296 return -1;
00297 }
00298
00299 string run = runnum_str( filename );
00300 string EventInfoDir = "DQMData/Run " + run + "/SiStrip/Run summary/EventInfo";
00301 TDirectory *rsEventInfoDir = dynamic_cast<TDirectory*>( file->Get(EventInfoDir.c_str()));
00302 rsEventInfoDir->cd();
00303 TIter eiKeys(rsEventInfoDir->GetListOfKeys());
00304 TKey *eiKey;
00305 while ( (eiKey = dynamic_cast<TKey*>(eiKeys())) )
00306 {
00307 string classname(eiKey->GetClassName());
00308 if (classname=="TObjString" )
00309 {
00310 string sflag = eiKey->GetName();
00311 string tempname = sflag.substr(sflag.find("i=")+2);
00312 size_t pos1 = tempname.find("<");
00313 size_t pos2 = sflag.find_first_of(">");
00314 string detvalue = tempname.substr(0,pos1);
00315 string numlumisec = sflag.substr(1,pos2-1);
00316 if ( numlumisec.c_str() == (string)"iLumiSection" )
00317 {
00318 numlumis = atoi( detvalue.c_str() );
00319 break;
00320 }
00321 }
00322 }
00323
00324 return numlumis;
00325 }
00326
00327 string runnum_str( string filename )
00328 {
00329 return filename.substr(filename.find("_R000")+5, 6);
00330 }
00331
00332 int getplot( string filename , string iDir , string strplot , TH1F& plot )
00333 {
00334 string run = runnum_str( filename );
00335 if (debug) std::cout << filename.c_str() << endl;
00336
00337 TFile* file = TFile::Open(filename.c_str());
00338 if (!file->IsOpen()) {
00339 cerr << "Failed to open " << filename << endl;
00340 return -1;
00341 }
00342
00343 string dir = "DQMData/Run " + run + "/AlcaBeamMonitor/Run summary/" + iDir;
00344
00345 file->cd( dir.c_str() );
00346
00347 string theplot = strplot + ";1";
00348 TH1F* thisplot;
00349 gDirectory->GetObject ( theplot.c_str() , thisplot );
00350
00351 if ( !thisplot )
00352 {
00353 std::cout << "Error: plot " << dir << "/" << theplot.c_str() << " not found!" << endl;
00354 return -2;
00355 }
00356
00357 plot = *thisplot;
00358 thisplot = NULL;
00359 delete thisplot;
00360
00361 return 0;
00362 }
00363
00364 void Cleaning( vector<int> &LSlist)
00365 {
00366 if ( LSlist.size() == 0 ) return;
00367
00368
00369 int refLS = LSlist[0];
00370 for (unsigned int at = 1; at < LSlist.size() - 1; at++)
00371 {
00372
00373 if ( refLS + 1 == LSlist[at] && LSlist[at] + 1 == LSlist[at+1] )
00374 {
00375 refLS = LSlist[at];
00376 LSlist[at] = -1;
00377 }
00378 else
00379 {
00380 refLS = LSlist[at];
00381 }
00382 }
00383 }
00384
00385 string ListOut(vector<int> &LSlist)
00386 {
00387
00388 Cleaning( LSlist );
00389
00390 string strout = "";
00391 bool rangeset = false;
00392 for (unsigned int at = 0; at < LSlist.size(); at++)
00393 {
00394 if ( LSlist[at] != -1 )
00395 {
00396 if ( at > 0 && LSlist[at-1] != -1 ) strout += ",";
00397 stringstream lsnum;
00398 lsnum << LSlist[at];
00399 strout += lsnum.str();
00400 rangeset = false;
00401 }
00402 if ( LSlist[at] == -1 && !rangeset )
00403 {
00404 strout += "-";
00405 rangeset = true;
00406 }
00407 }
00408
00409 return strout;
00410 }
00411
00412 void vector_AND ( vector<int> & bad_def, vector<int> bad_sc)
00413 {
00414 vector <int> temp;
00415
00416 int def_size = bad_def.size();
00417 for ( int i = 0; i < def_size; i++ )
00418 for ( unsigned int j = 0; j < bad_sc.size(); j++ )
00419 if ( bad_def[ i ] == bad_sc[ j ] )
00420 {
00421 temp.push_back( bad_def[ i ] );
00422 break;
00423 }
00424
00425 bad_def = temp;
00426 }