CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_0/src/DQM/SiStripMonitorClient/bin/modulediff.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 #include <dirent.h>
00023 
00024 using namespace std;
00025 
00026 int  read_badmodlist     ( int            , string         , vector < int >& );
00027 void get_difference      ( vector < int > , vector < int > , vector < int >& );
00028 int  get_filename        ( int            , string         , string& );
00029 int  search_closest_run  ( int , string );
00030 void modulediff ( int run_2 , string repro_run2 );
00031 
00032 int main(int argc , char *argv[]) {
00033 
00034   if(argc==3) {
00035     char* crun2 = argv[1];
00036     char* repro_run2 = argv[2];
00037 
00038     int run2 = 0;
00039     sscanf(crun2,"%d",&run2);
00040 
00041     std::cout << "ready to run modulediff " << run2 << " repro run " << repro_run2 << std::endl;
00042 
00043     modulediff(run2,repro_run2);
00044 
00045   }
00046   else {std::cout << "Too few arguments: " << argc << std::endl; return -1; }
00047   return 0;
00048 
00049 }
00050 
00051 void modulediff ( int run_2 , string repro_run2 )
00052 {
00053   vector < int > badmodlist_run1;
00054   vector < int > badmodlist_run2;
00055   vector < int > modules_recovered;
00056   vector < int > modules_malformed;
00057   int run_1;
00058   string repro_run1;
00059 
00060   int res = search_closest_run ( run_2 , repro_run2 );
00061   if ( res > 0 )
00062     {
00063       run_1 = res;
00064       repro_run1 = repro_run2;
00065     }
00066   else 
00067     {
00068       printf("closest run not found, exiting.\n");
00069       return;
00070     }
00071   
00072 
00073   int flag1 = read_badmodlist ( run_1 , repro_run1 , badmodlist_run1 );
00074   int flag2 = read_badmodlist ( run_2 , repro_run2 , badmodlist_run2 );
00075 
00076   if ( flag1 < 0 || flag2 < 0 )
00077     {
00078       cout << "Error: file not found." << endl;
00079       return;
00080     }
00081 
00082   get_difference ( badmodlist_run1 , badmodlist_run2 , modules_recovered );  
00083   get_difference ( badmodlist_run2 , badmodlist_run1 , modules_malformed );  
00084 
00085   //save into file
00086   ofstream outfile;
00087   string namefile = "modulediff_emailbody.txt";
00088   outfile.open(namefile.c_str());
00089 
00090   outfile << "Recovered modules in run " << run_2 << ": " << (int)modules_recovered.size() << endl;
00091   outfile << "New bad modules in run   " << run_2 << ": " << (int)modules_malformed.size() << endl;
00092   outfile << "Using reference run " << run_1 << endl << endl;
00093 
00094   outfile << "Recovered modules in run " << run_2 << ":" << endl;
00095   if ( modules_recovered.size() == 0 ) 
00096     outfile << " -" << endl;
00097   for ( unsigned int i = 0; i < modules_recovered.size() ; i++ )
00098     outfile << " " << modules_recovered[ i ] << endl;
00099   
00100   outfile << "New bad modules that appeared in run " << run_2 << ":" << endl;
00101   if ( modules_malformed.size() == 0 ) 
00102     outfile << " -" << endl;
00103   for ( unsigned int i = 0; i < modules_malformed.size() ; i++ )
00104     outfile << " " << modules_malformed[ i ] << endl;
00105 
00106   outfile.close();
00107 
00108   //create two flat files to run the locatemodule script on later
00109   
00110   if ( modules_recovered.size() > 0 )
00111     {
00112       ofstream outfile_good;
00113       outfile_good.open("modulediff_good.txt");
00114       for ( unsigned int i = 0; i < modules_recovered.size() ; i++ )
00115         outfile_good << " " << modules_recovered[ i ] << endl;
00116       outfile_good.close();
00117     }
00118 
00119   if ( modules_malformed.size() > 0 )
00120     {
00121       ofstream outfile_bad;
00122       outfile_bad.open("modulediff_bad.txt");
00123       for ( unsigned int i = 0; i < modules_malformed.size() ; i++ )
00124         outfile_bad << " " << modules_malformed[ i ] << endl;
00125       outfile_bad.close();
00126     }
00127 }
00128 
00129 void get_difference  ( vector < int > badlist1 , vector < int > badlist2 , vector < int > &difflist )
00130 {
00131   //check if any element of badlist1 is in badlist2
00132   for ( unsigned int i1 = 0; i1 < badlist1.size() ; i1++ )
00133     {
00134       bool thisrecovered = true;
00135       for ( unsigned int i2 = 0; i2 < badlist2.size() ; i2++ )
00136         if ( badlist1[ i1 ] == badlist2[ i2 ] )
00137           {
00138             thisrecovered = false;
00139             break;
00140           }
00141       if ( thisrecovered )
00142         difflist.push_back ( badlist1[ i1 ] );
00143     }
00144 }
00145 
00146 
00147 int read_badmodlist ( int run , string repro_type , vector < int >& badlist )
00148 {
00149   string filename;
00150   int flag = get_filename ( run , repro_type , filename );
00151   
00152   if ( flag < 0 )
00153     {
00154       cout << "reading problem" << endl;
00155       return -1; 
00156     }
00157 
00158   vector<string> subdet;
00159   subdet.push_back("TIB");
00160   subdet.push_back("TID/side_1"); 
00161   subdet.push_back("TID/side_2");
00162   subdet.push_back("TOB");
00163   subdet.push_back("TEC/side_1");
00164   subdet.push_back("TEC/side_2");
00165 
00166   string nrun = filename.substr ( filename.find( "_R000" ) + 5 , 6 );
00167 
00168   TFile *dqmfile = TFile::Open ( filename.c_str() , "READ" );
00169   string topdir = "DQMData/Run " + nrun + "/SiStrip/Run summary/MechanicalView";
00170   gDirectory->cd(topdir.c_str());
00171   TDirectory* mec1 = gDirectory;
00172 
00173   for ( unsigned int i=0; i < subdet.size(); i++ )
00174     {
00175       string badmodule_dir = subdet[ i ] + "/BadModuleList";
00176       if ( gDirectory->cd ( badmodule_dir.c_str() ) ) 
00177         {
00178           TIter next ( gDirectory->GetListOfKeys() );
00179           TKey *key;
00180 
00181           while  ( ( key = dynamic_cast<TKey*> ( next() ) ) ) 
00182             {
00183               string sflag = key->GetName();
00184               if ( sflag.size() == 0 ) continue;
00185               
00186               string detid = sflag.substr ( sflag.find ( "<" ) + 1 , 9 ); 
00187               badlist.push_back ( atoi ( detid.c_str() ) );
00188             }
00189         }
00190       else
00191         {
00192           //cout << "no dir " << badmodule_dir << " in filename " << filename << endl;
00193         }
00194       mec1->cd();
00195     }
00196   dqmfile->Close();
00197   return 0;
00198 }
00199 
00200 
00201 int get_filename  ( int run , string repro_type , string& filename )
00202 {
00203   stringstream runstr;
00204   runstr << run;
00205   
00206   stringstream rundirprefix;
00207   rundirprefix << "000" << run / 100 << "xx/";
00208 
00209   stringstream thisdir;
00210   //thisdir << "/afs/cern.ch/cms/CAF/CMSCOMM/COMM_DQM/data/OfflineData/Run2011/" << repro_type.c_str() << "/" << rundirprefix.str();
00211   thisdir << "/afs/cern.ch/cms/CAF/CMSCOMM/COMM_DQM/data/OfflineData/HIRun2011/" << repro_type.c_str() << "/" << rundirprefix.str();
00212   
00213   string thisdir2 = thisdir.str();
00214   DIR *dp;
00215   
00216   if ( ( dp = opendir( thisdir2.c_str() ) ) == NULL )
00217     {
00218       cout << "dir " << thisdir2.c_str() << " not found" << endl;
00219       return -1;
00220     }
00221 
00222   struct dirent *dirp;
00223 
00224   string dqmfile;
00225 
00226   while ( ( dirp = readdir ( dp ) ) != NULL )
00227     {
00228       string dirfile = string ( dirp->d_name );
00229       if ( 
00230           dirfile.find ( "__DQM" ) != string::npos &&
00231           dirfile.find ( runstr.str() ) != string::npos
00232           )
00233         {
00234           dqmfile = dirfile;
00235           break;
00236         }
00237     }
00238 
00239   closedir( dp );
00240 
00241   if ( dqmfile.size() < 10 )
00242     {
00243       //cout << "file " << dqmfile << " not found" << endl;
00244       return -1;
00245     }
00246   
00247   filename = thisdir.str() + dqmfile;
00248   
00249   return 0;
00250 }
00251 
00252 
00253 int  search_closest_run  ( int thisrun , string repro_type )
00254 {
00255   string filename;
00256 
00257   for ( int test_run = thisrun - 1; test_run > thisrun - 1000; test_run-- )
00258     {
00259       int res = get_filename  ( test_run , repro_type , filename );
00260       if ( res == 0 )
00261           return test_run;
00262     }
00263 
00264   return -1;
00265 }