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