CMS 3D CMS Logo

lsbs_cert.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 
23 using namespace std;
24 
25 //global vars
26 bool debug = false;
27 int numlumis = -1;
28 
29 int nlumis(string filename); //get number of run lumisections
30 string runnum_str(string filename); //read the run number, return in string
31 int getplot(string filename, string iDir, string strplot, TH1F& plot); //read given plot
32 void Cleaning(vector<int>&);
33 string ListOut(vector<int>&);
34 void vector_AND(vector<int>&, vector<int>);
35 void lsbs_cert(string filename);
36 
37 int main(int argc, char* argv[]) {
38  if (argc == 2) {
39  char* filename = argv[1];
40 
41  std::cout << "ready to run lsbs filename " << filename << std::endl;
42 
44 
45  } else {
46  std::cout << "Too few arguments: " << argc << std::endl;
47  return -1;
48  }
49  return 0;
50 }
51 
52 void lsbs_cert(string filename) {
53  void check_offset(string filename, string iDir, string plot, float limit_min, float limit_max, vector<int>&);
54  void check_sigma(string filename, string iDir, string plot, float limit_err, vector<int>&);
55  bool check_isgood(vector<int>&, int ls); //check if this LS is good
56 
57  //presets
58  numlumis = -1;
59 
60  float limit_x = 0.002;
61  float limit_y = 0.002;
62  float limit_z = 0.5;
63  float limit_dx = 0.002;
64  float limit_dy = 0.002;
65  float limit_dz = 0.5;
66  float limit_errdx = 0.002;
67  float limit_errdy = 0.002;
68  float limit_errdz = 0.5;
69 
70  //LS certification
71  vector<int> ls_x_bad;
72  vector<int> ls_y_bad;
73  vector<int> ls_z_bad;
74 
75  vector<int> ls_xsc_bad;
76  vector<int> ls_ysc_bad;
77  vector<int> ls_zsc_bad;
78 
79  vector<int> ls_dx_bad;
80  vector<int> ls_dy_bad;
81  vector<int> ls_dz_bad;
82 
83  vector<int> ls_dxsc_bad;
84  vector<int> ls_dysc_bad;
85  vector<int> ls_dzsc_bad;
86 
87  vector<int> ls_errdx_bad;
88  vector<int> ls_errdy_bad;
89  vector<int> ls_errdz_bad;
90 
91  vector<int> ls_errdxsc_bad;
92  vector<int> ls_errdysc_bad;
93  vector<int> ls_errdzsc_bad;
94 
95  vector<int> ls_good;
96  vector<int> ls_bad;
97 
98  //beamspot vs primary vertex
99  check_offset(filename, "Validation", "hxLumibased PrimaryVertex-DataBase", -limit_x, limit_x, ls_x_bad);
100  check_offset(filename, "Validation", "hyLumibased PrimaryVertex-DataBase", -limit_y, limit_y, ls_y_bad);
101  check_offset(filename, "Validation", "hzLumibased PrimaryVertex-DataBase", -limit_z, limit_z, ls_z_bad);
102 
103  //beamspot vs scalers
104  check_offset(filename, "Validation", "hxLumibased Scalers-DataBase fit", -limit_x, limit_x, ls_xsc_bad);
105  check_offset(filename, "Validation", "hyLumibased Scalers-DataBase fit", -limit_y, limit_y, ls_ysc_bad);
106  check_offset(filename, "Validation", "hzLumibased Scalers-DataBase fit", -limit_z, limit_z, ls_zsc_bad);
107 
108  check_offset(filename, "Debug", "hsigmaXLumibased PrimaryVertex-DataBase fit", -limit_dx, limit_dx, ls_dx_bad);
109  check_offset(filename, "Debug", "hsigmaYLumibased PrimaryVertex-DataBase fit", -limit_dy, limit_dy, ls_dy_bad);
110  check_offset(filename, "Debug", "hsigmaZLumibased PrimaryVertex-DataBase fit", -limit_dz, limit_dz, ls_dz_bad);
111 
112  check_offset(filename, "Validation", "hsigmaXLumibased Scalers-DataBase fit", -limit_dx, limit_dx, ls_dxsc_bad);
113  check_offset(filename, "Validation", "hsigmaYLumibased Scalers-DataBase fit", -limit_dy, limit_dy, ls_dysc_bad);
114  check_offset(filename, "Validation", "hsigmaZLumibased Scalers-DataBase fit", -limit_dz, limit_dz, ls_dzsc_bad);
115 
116  check_sigma(filename, "Debug", "hsigmaXLumibased PrimaryVertex-DataBase fit", limit_errdx, ls_errdx_bad);
117  check_sigma(filename, "Debug", "hsigmaYLumibased PrimaryVertex-DataBase fit", limit_errdy, ls_errdy_bad);
118  check_sigma(filename, "Debug", "hsigmaZLumibased PrimaryVertex-DataBase fit", limit_errdz, ls_errdz_bad);
119 
120  check_sigma(filename, "Validation", "hsigmaXLumibased Scalers-DataBase fit", limit_errdx, ls_errdxsc_bad);
121  check_sigma(filename, "Validation", "hsigmaYLumibased Scalers-DataBase fit", limit_errdy, ls_errdysc_bad);
122  check_sigma(filename, "Validation", "hsigmaZLumibased Scalers-DataBase fit", limit_errdz, ls_errdzsc_bad);
123 
124  //BAD LS only if bad in both histos (wrt PV, Scalers)
125  vector_AND(ls_x_bad, ls_xsc_bad);
126  vector_AND(ls_y_bad, ls_ysc_bad);
127  vector_AND(ls_z_bad, ls_zsc_bad);
128  vector_AND(ls_dx_bad, ls_dxsc_bad);
129  vector_AND(ls_dy_bad, ls_dysc_bad);
130  vector_AND(ls_dz_bad, ls_dzsc_bad);
131  vector_AND(ls_errdx_bad, ls_errdxsc_bad);
132  vector_AND(ls_errdy_bad, ls_errdysc_bad);
133  vector_AND(ls_errdz_bad, ls_errdzsc_bad);
134 
135  //good LS = all LS minus BAD LS
136  for (int i = 1; i <= nlumis(filename); i++) {
137  if (!check_isgood(ls_x_bad, i) && !check_isgood(ls_xsc_bad, i)) {
138  ls_bad.push_back(i);
139  continue;
140  } else if (!check_isgood(ls_y_bad, i) && !check_isgood(ls_ysc_bad, i)) {
141  ls_bad.push_back(i);
142  continue;
143  } else if (!check_isgood(ls_z_bad, i) && !check_isgood(ls_zsc_bad, i)) {
144  ls_bad.push_back(i);
145  continue;
146  } else if (!check_isgood(ls_dx_bad, i) && !check_isgood(ls_dxsc_bad, i)) {
147  ls_bad.push_back(i);
148  continue;
149  } else if (!check_isgood(ls_dy_bad, i) && !check_isgood(ls_dysc_bad, i)) {
150  ls_bad.push_back(i);
151  continue;
152  } else if (!check_isgood(ls_dz_bad, i) && !check_isgood(ls_dzsc_bad, i)) {
153  ls_bad.push_back(i);
154  continue;
155  } else if (!check_isgood(ls_errdx_bad, i) && !check_isgood(ls_errdxsc_bad, i)) {
156  ls_bad.push_back(i);
157  continue;
158  } else if (!check_isgood(ls_errdy_bad, i) && !check_isgood(ls_errdysc_bad, i)) {
159  ls_bad.push_back(i);
160  continue;
161  } else if (!check_isgood(ls_errdz_bad, i) && !check_isgood(ls_errdzsc_bad, i)) {
162  ls_bad.push_back(i);
163  continue;
164  }
165 
166  //check also that LS is not missing!!!
167  ls_good.push_back(i);
168  }
169 
170  std::ofstream outfile;
171  string namefile = "Certification_BS_run_" + runnum_str(filename) + ".txt";
172  outfile.open(namefile.c_str());
173  outfile << "Lumibased BeamSpot Calibration Certification for run " << runnum_str(filename) << ":" << endl << endl;
174 
175  char line[2000];
176  sprintf(line, " GOOD Lumisections (values within limits): %s", ListOut(ls_good).c_str());
177  outfile << line << endl;
178 
179  if (!ls_bad.empty()) {
180  sprintf(line, " BAD Lumisections (values outside limits): %s", ListOut(ls_bad).c_str());
181  outfile << line << endl;
182 
183  sprintf(line, " --- histogram name --- --- bad lumisection list(*) ---");
184  outfile << line << endl;
185  sprintf(line, " hxLumibased PrimaryVertex-DataBase (mean): %s", ListOut(ls_x_bad).c_str());
186  if (!ls_x_bad.empty())
187  outfile << line << endl;
188  sprintf(line, " hyLumibased PrimaryVertex-DataBase (mean): %s", ListOut(ls_y_bad).c_str());
189  if (!ls_y_bad.empty())
190  outfile << line << endl;
191  sprintf(line, " hzLumibased PrimaryVertex-DataBase (mean): %s", ListOut(ls_z_bad).c_str());
192  if (!ls_z_bad.empty())
193  outfile << line << endl;
194 
195  sprintf(line, " hsigmaXLumibased PrimaryVertex-DataBase fit (mean): %s", ListOut(ls_dx_bad).c_str());
196  if (!ls_dx_bad.empty())
197  outfile << line << endl;
198  sprintf(line, " hsigmaYLumibased PrimaryVertex-DataBase fit (mean): %s", ListOut(ls_dy_bad).c_str());
199  if (!ls_dy_bad.empty())
200  outfile << line << endl;
201  sprintf(line, " hsigmaZLumibased PrimaryVertex-DataBase fit (mean): %s", ListOut(ls_dz_bad).c_str());
202  if (!ls_dz_bad.empty())
203  outfile << line << endl;
204 
205  sprintf(line, " hsigmaXLumibased PrimaryVertex-DataBase fit (error): %s", ListOut(ls_errdx_bad).c_str());
206  if (!ls_errdx_bad.empty())
207  outfile << line << endl;
208  sprintf(line, " hsigmaYLumibased PrimaryVertex-DataBase fit (error): %s", ListOut(ls_errdy_bad).c_str());
209  if (!ls_errdy_bad.empty())
210  outfile << line << endl;
211  sprintf(line, " hsigmaZLumibased PrimaryVertex-DataBase fit (error): %s", ListOut(ls_errdz_bad).c_str());
212  if (!ls_errdz_bad.empty())
213  outfile << line << endl;
214 
215  sprintf(line, " (*) also bad in the corresponding 'Scalers-Database fit' histograms");
216  outfile << line << endl;
217  }
218 
219  outfile.close();
220  std::cout << "Lumibased BeamSpot Calibration Certification summary saved in " << namefile << endl;
221 }
222 
223 void check_offset(string filename, string iDir, string plot, float limit_min, float limit_max, vector<int>& badLS) {
224  TH1F checkPlot;
225  if (getplot(filename, iDir, plot, checkPlot) < 0)
226  return;
227 
228  //look at each LS, save the bad one
229  for (int i = 1; i <= checkPlot.GetNbinsX(); i++) {
230  float value = checkPlot.GetBinContent(i);
231  if (value < limit_min || value > limit_max) {
232  badLS.push_back((int)checkPlot.GetBinCenter(i));
233  }
234  }
235 }
236 
237 void check_sigma(string filename, string iDir, string plot, float limit_err, vector<int>& badLS) {
238  TH1F checkPlot;
239  if (getplot(filename, iDir, plot, checkPlot) < 0)
240  return;
241 
242  //look at each LS
243  for (int i = 1; i <= checkPlot.GetNbinsX(); i++) {
244  float value = checkPlot.GetBinError(i);
245  if (value > limit_err) {
246  badLS.push_back((int)checkPlot.GetBinCenter(i));
247  }
248  }
249 }
250 
251 bool check_isgood(vector<int>& ls_badlist, int ls) {
252  //check if this LS is found in the BAD list
253  for (unsigned int i = 0; i < ls_badlist.size(); i++) {
254  if (ls == ls_badlist[i])
255  return false;
256  }
257  return true;
258 }
259 
260 int nlumis(string filename) {
261  if (numlumis > -1)
262  return numlumis;
263 
264  TFile* file = TFile::Open(filename.c_str());
265  if (!file->IsOpen()) {
266  cerr << "Failed to open " << filename << endl;
267  return -1;
268  }
269 
270  string run = runnum_str(filename);
271  string EventInfoDir = "DQMData/Run " + run + "/SiStrip/Run summary/EventInfo";
272  TDirectory* rsEventInfoDir = dynamic_cast<TDirectory*>(file->Get(EventInfoDir.c_str()));
273  rsEventInfoDir->cd();
274  TIter eiKeys(rsEventInfoDir->GetListOfKeys());
275  TKey* eiKey;
276  while ((eiKey = dynamic_cast<TKey*>(eiKeys()))) {
277  string classname(eiKey->GetClassName());
278  if (classname == "TObjString") {
279  string sflag = eiKey->GetName();
280  string tempname = sflag.substr(sflag.find("i=") + 2);
281  size_t pos1 = tempname.find('<');
282  size_t pos2 = sflag.find_first_of('>');
283  string detvalue = tempname.substr(0, pos1);
284  string numlumisec = sflag.substr(1, pos2 - 1);
285  if (numlumisec == (string) "iLumiSection") {
286  numlumis = atoi(detvalue.c_str());
287  break;
288  }
289  }
290  }
291 
292  return numlumis;
293 }
294 
295 string runnum_str(string filename) { return filename.substr(filename.find("_R000") + 5, 6); }
296 
297 int getplot(string filename, string iDir, string strplot, TH1F& plot) {
298  string run = runnum_str(filename);
299  if (debug)
300  std::cout << filename.c_str() << endl;
301 
302  TFile* file = TFile::Open(filename.c_str());
303  if (!file->IsOpen()) {
304  cerr << "Failed to open " << filename << endl;
305  return -1;
306  }
307 
308  string dir = "DQMData/Run " + run + "/AlcaBeamMonitor/Run summary/" + iDir;
309 
310  file->cd(dir.c_str());
311 
312  string theplot = strplot + ";1";
313  TH1F* thisplot;
314  gDirectory->GetObject(theplot.c_str(), thisplot);
315 
316  if (!thisplot) {
317  std::cout << "Error: plot " << dir << "/" << theplot.c_str() << " not found!" << endl;
318  return -2;
319  }
320 
321  plot = *thisplot;
322  thisplot = nullptr;
323  delete thisplot;
324 
325  return 0;
326 }
327 
328 void Cleaning(vector<int>& LSlist) {
329  if (LSlist.empty())
330  return;
331 
332  //cleaning: keep only 1st and last lumisection in the range
333  int refLS = LSlist[0];
334  for (unsigned int at = 1; at < LSlist.size() - 1; at++) {
335  //delete LSnums in between a single continuous range
336  if (refLS + 1 == LSlist[at] && LSlist[at] + 1 == LSlist[at + 1]) {
337  refLS = LSlist[at];
338  LSlist[at] = -1;
339  } else {
340  refLS = LSlist[at];
341  }
342  }
343 }
344 
345 string ListOut(vector<int>& LSlist) {
346  Cleaning(LSlist);
347 
348  string strout = "";
349  bool rangeset = false;
350  for (unsigned int at = 0; at < LSlist.size(); at++) {
351  if (LSlist[at] != -1) {
352  if (at > 0 && LSlist[at - 1] != -1)
353  strout += ",";
354  stringstream lsnum;
355  lsnum << LSlist[at];
356  strout += lsnum.str();
357  rangeset = false;
358  }
359  if (LSlist[at] == -1 && !rangeset) {
360  strout += "-";
361  rangeset = true;
362  }
363  }
364 
365  return strout;
366 }
367 
368 void vector_AND(vector<int>& bad_def, vector<int> bad_sc) {
369  vector<int> temp;
370 
371  int def_size = bad_def.size();
372  for (int i = 0; i < def_size; i++)
373  for (unsigned int j = 0; j < bad_sc.size(); j++)
374  if (bad_def[i] == bad_sc[j]) {
375  temp.push_back(bad_def[i]);
376  break;
377  }
378 
379  bad_def = temp;
380 }
eostools.ls
def ls(path, rec=False)
Definition: eostools.py:349
cmsBatch.argv
argv
Definition: cmsBatch.py:279
mps_fire.i
i
Definition: mps_fire.py:428
dir2webdir.argc
argc
Definition: dir2webdir.py:39
gather_cfg.cout
cout
Definition: gather_cfg.py:144
groupFilesInBlocks.temp
list temp
Definition: groupFilesInBlocks.py:142
Cleaning
void Cleaning(vector< int > &)
Definition: lsbs_cert.cc:328
plotFactory.plot
plot
Definition: plotFactory.py:109
lsbs_cert
void lsbs_cert(string filename)
Definition: lsbs_cert.cc:52
data-class-funcs.classname
classname
Definition: data-class-funcs.py:38
ListOut
string ListOut(vector< int > &)
Definition: lsbs_cert.cc:345
debug
bool debug
Definition: lsbs_cert.cc:26
corrVsCorr.filename
filename
Definition: corrVsCorr.py:123
numlumis
int numlumis
Definition: lsbs_cert.cc:27
geometryDiff.file
file
Definition: geometryDiff.py:13
value
Definition: value.py:1
main
int main(int argc, char *argv[])
Definition: lsbs_cert.cc:37
check_offset
void check_offset(string filename, string iDir, string plot, float limit_min, float limit_max, vector< int > &badLS)
Definition: lsbs_cert.cc:223
check_sigma
void check_sigma(string filename, string iDir, string plot, float limit_err, vector< int > &badLS)
Definition: lsbs_cert.cc:237
nlumis
int nlumis(string filename)
Definition: lsbs_cert.cc:260
std
Definition: JetResolutionObject.h:76
writedatasetfile.run
run
Definition: writedatasetfile.py:27
timingPdfMaker.outfile
outfile
Definition: timingPdfMaker.py:350
dqmiolumiharvest.j
j
Definition: dqmiolumiharvest.py:66
vector_AND
void vector_AND(vector< int > &, vector< int >)
Definition: lsbs_cert.cc:368
mps_splice.line
line
Definition: mps_splice.py:76
EcnaPython_AdcPeg12_S1_10_R170298_1_0_150_Dee0.cerr
cerr
Definition: EcnaPython_AdcPeg12_S1_10_R170298_1_0_150_Dee0.py:8
getplot
int getplot(string filename, string iDir, string strplot, TH1F &plot)
Definition: lsbs_cert.cc:297
check_isgood
bool check_isgood(vector< int > &ls_badlist, int ls)
Definition: lsbs_cert.cc:251
runnum_str
string runnum_str(string filename)
Definition: lsbs_cert.cc:295
DeadROC_duringRun.dir
dir
Definition: DeadROC_duringRun.py:23