CMS 3D CMS Logo

modulediff.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 #include <dirent.h>
23 
24 using namespace std;
25 
26 int read_badmodlist ( int , string , vector < int >& );
27 void get_difference ( vector < int > , vector < int > , vector < int >& );
28 int get_filename ( int , string , string& );
29 int search_closest_run ( int , string );
30 void modulediff ( int run_2 , string repro_run2 );
31 
32 int main(int argc , char *argv[]) {
33 
34  if(argc==3) {
35  char* crun2 = argv[1];
36  char* repro_run2 = argv[2];
37 
38  int run2 = 0;
39  sscanf(crun2,"%d",&run2);
40 
41  std::cout << "ready to run modulediff " << run2 << " repro run " << repro_run2 << std::endl;
42 
43  modulediff(run2,repro_run2);
44 
45  }
46  else {std::cout << "Too few arguments: " << argc << std::endl; return -1; }
47  return 0;
48 
49 }
50 
51 void modulediff ( int run_2 , string repro_run2 )
52 {
53  vector < int > badmodlist_run1;
54  vector < int > badmodlist_run2;
55  vector < int > modules_recovered;
56  vector < int > modules_malformed;
57  int run_1;
58  string repro_run1;
59 
60  int res = search_closest_run ( run_2 , repro_run2 );
61  if ( res > 0 )
62  {
63  run_1 = res;
64  repro_run1 = repro_run2;
65  }
66  else
67  {
68  printf("closest run not found, exiting.\n");
69  return;
70  }
71 
72 
73  int flag1 = read_badmodlist ( run_1 , repro_run1 , badmodlist_run1 );
74  int flag2 = read_badmodlist ( run_2 , repro_run2 , badmodlist_run2 );
75 
76  if ( flag1 < 0 || flag2 < 0 )
77  {
78  cout << "Error: file not found." << endl;
79  return;
80  }
81 
82  get_difference ( badmodlist_run1 , badmodlist_run2 , modules_recovered );
83  get_difference ( badmodlist_run2 , badmodlist_run1 , modules_malformed );
84 
85  //save into file
86  std::ofstream outfile;
87  string namefile = "modulediff_emailbody.txt";
88  outfile.open(namefile.c_str());
89 
90  outfile << "Recovered modules in run " << run_2 << ": " << (int)modules_recovered.size() << endl;
91  outfile << "New bad modules in run " << run_2 << ": " << (int)modules_malformed.size() << endl;
92  outfile << "Using reference run " << run_1 << endl << endl;
93 
94  outfile << "Recovered modules in run " << run_2 << ":" << endl;
95  if ( modules_recovered.empty() )
96  outfile << " -" << endl;
97  for ( unsigned int i = 0; i < modules_recovered.size() ; i++ )
98  outfile << " " << modules_recovered[ i ] << endl;
99 
100  outfile << "New bad modules that appeared in run " << run_2 << ":" << endl;
101  if ( modules_malformed.empty() )
102  outfile << " -" << endl;
103  for ( unsigned int i = 0; i < modules_malformed.size() ; i++ )
104  outfile << " " << modules_malformed[ i ] << endl;
105 
106  outfile.close();
107 
108  //create two flat files to run the locatemodule script on later
109 
110  if ( !modules_recovered.empty() )
111  {
112  std::ofstream outfile_good;
113  outfile_good.open("modulediff_good.txt");
114  for ( unsigned int i = 0; i < modules_recovered.size() ; i++ )
115  outfile_good << " " << modules_recovered[ i ] << endl;
116  outfile_good.close();
117  }
118 
119  if ( !modules_malformed.empty() )
120  {
121  std::ofstream outfile_bad;
122  outfile_bad.open("modulediff_bad.txt");
123  for ( unsigned int i = 0; i < modules_malformed.size() ; i++ )
124  outfile_bad << " " << modules_malformed[ i ] << endl;
125  outfile_bad.close();
126  }
127 }
128 
129 void get_difference ( vector < int > badlist1 , vector < int > badlist2 , vector < int > &difflist )
130 {
131  //check if any element of badlist1 is in badlist2
132  for ( unsigned int i1 = 0; i1 < badlist1.size() ; i1++ )
133  {
134  bool thisrecovered = true;
135  for ( unsigned int i2 = 0; i2 < badlist2.size() ; i2++ )
136  if ( badlist1[ i1 ] == badlist2[ i2 ] )
137  {
138  thisrecovered = false;
139  break;
140  }
141  if ( thisrecovered )
142  difflist.push_back ( badlist1[ i1 ] );
143  }
144 }
145 
146 
147 int read_badmodlist ( int run , string repro_type , vector < int >& badlist )
148 {
149  string filename;
150  int flag = get_filename ( run , repro_type , filename );
151 
152  if ( flag < 0 )
153  {
154  cout << "reading problem" << endl;
155  return -1;
156  }
157 
158  vector<string> subdet;
159  subdet.push_back("TIB");
160  subdet.push_back("TID/MINUS");
161  subdet.push_back("TID/PLUS");
162  subdet.push_back("TOB");
163  subdet.push_back("TEC/MINUS");
164  subdet.push_back("TEC/PLUS");
165 
166  string nrun = filename.substr ( filename.find( "_R000" ) + 5 , 6 );
167 
168  TFile *dqmfile = TFile::Open ( filename.c_str() , "READ" );
169  string topdir = "DQMData/Run " + nrun + "/SiStrip/Run summary/MechanicalView";
170  gDirectory->cd(topdir.c_str());
171  TDirectory* mec1 = gDirectory;
172 
173  for ( unsigned int i=0; i < subdet.size(); i++ )
174  {
175  string badmodule_dir = subdet[ i ] + "/BadModuleList";
176  if ( gDirectory->cd ( badmodule_dir.c_str() ) )
177  {
178  TIter next ( gDirectory->GetListOfKeys() );
179  TKey *key;
180 
181  while ( ( key = dynamic_cast<TKey*> ( next() ) ) )
182  {
183  string sflag = key->GetName();
184  if ( sflag.empty() ) continue;
185 
186  string detid = sflag.substr ( sflag.find ( "<" ) + 1 , 9 );
187  badlist.push_back ( atoi ( detid.c_str() ) );
188  }
189  }
190  else
191  {
192  //cout << "no dir " << badmodule_dir << " in filename " << filename << endl;
193  }
194  mec1->cd();
195  }
196  dqmfile->Close();
197  return 0;
198 }
199 
200 
201 int get_filename ( int run , string repro_type , string& filename )
202 {
203  stringstream runstr;
204  runstr << run;
205 
206  stringstream rundirprefix;
207  rundirprefix << "000" << run / 100 << "xx/";
208 
209  stringstream thisdir;
210  //thisdir << "/afs/cern.ch/cms/CAF/CMSCOMM/COMM_DQM/data/OfflineData/Run2011/" << repro_type.c_str() << "/" << rundirprefix.str();
211  thisdir << "/afs/cern.ch/cms/CAF/CMSCOMM/COMM_DQM/data/OfflineData/HIRun2011/" << repro_type.c_str() << "/" << rundirprefix.str();
212 
213  string thisdir2 = thisdir.str();
214  DIR *dp;
215 
216  if ( ( dp = opendir( thisdir2.c_str() ) ) == nullptr )
217  {
218  cout << "dir " << thisdir2.c_str() << " not found" << endl;
219  return -1;
220  }
221 
222  struct dirent *dirp;
223 
224  string dqmfile;
225 
226  while ( ( dirp = readdir ( dp ) ) != nullptr )
227  {
228  string dirfile = string ( dirp->d_name );
229  if (
230  dirfile.find ( "__DQM" ) != string::npos &&
231  dirfile.find ( runstr.str() ) != string::npos
232  )
233  {
234  dqmfile = dirfile;
235  break;
236  }
237  }
238 
239  closedir( dp );
240 
241  if ( dqmfile.size() < 10 )
242  {
243  //cout << "file " << dqmfile << " not found" << endl;
244  return -1;
245  }
246 
247  filename = thisdir.str() + dqmfile;
248 
249  return 0;
250 }
251 
252 
253 int search_closest_run ( int thisrun , string repro_type )
254 {
255  string filename;
256 
257  for ( int test_run = thisrun - 1; test_run > thisrun - 1000; test_run-- )
258  {
259  int res = get_filename ( test_run , repro_type , filename );
260  if ( res == 0 )
261  return test_run;
262  }
263 
264  return -1;
265 }
int read_badmodlist(int, string, vector< int > &)
Definition: modulediff.cc:147
int search_closest_run(int, string)
Definition: modulediff.cc:253
void get_difference(vector< int >, vector< int >, vector< int > &)
Definition: modulediff.cc:129
void modulediff(int run_2, string repro_run2)
Definition: modulediff.cc:51
#define nullptr
Definition: Electron.h:6
int get_filename(int, string, string &)
Definition: modulediff.cc:201
auto dp
Definition: deltaR.h:22
int main(int argc, char *argv[])
Definition: modulediff.cc:32