CMS 3D CMS Logo

listbadmodule.cc
Go to the documentation of this file.
1 #include <iostream>
2 #include <cstdio>
3 #include <TDirectory.h>
4 #include <TFile.h>
5 #include <TKey.h>
6 #include <TH1.h>
7 #include <Riostream.h>
8 #include <string>
9 #include <vector>
10 #include <set>
11 #include <fstream>
12 #include <sstream>
13 #include <string>
14 #include <cstdint>
15 #include <cstdlib>
16 #include <cstdio>
17 
18 #include "listbadmodule.h"
19 
20 //using namespace std;
21 
22 int main(int argc , char *argv[]) {
23 
24  if(argc==3) {
25  char* filename = argv[1];
26  char* pclfilename = argv[2];
27 
28  std::cout << "ready to prepare list of bad modules " << filename << std::endl;
29 
30  listbadmodule(filename,pclfilename);
31 
32  }
33  else {std::cout << "Too few arguments: " << argc << std::endl; return -1; }
34  return 0;
35 
36 }
37 
38 
40 
41  int debug = 1;
42 
43  // extract fully bad modules from PCLBadComponents txt file
44 
45  std::set<unsigned int> pclbadmods;
46 
47  std::ifstream pclfile(pclfilename);
48 
49  char line[400];
50  unsigned int pcldetid;
51  char sixapvs[]="1 1 1 1 1 1";
52  char fourapvs[]="1 1 x x 1 1";
53 
54  while(pclfile.getline(line,400)) {
55  if(strstr(line,sixapvs) || strstr(line,fourapvs)) {
56  std::stringstream linestream;
57  linestream << line;
58  linestream >> pcldetid;
59  // std::cout << pcldetid << std::endl;
60  pclbadmods.insert(pcldetid);
61  }
62  }
63 
64  std::vector<std::string> subdet;
65  subdet.push_back("TIB");
66  subdet.push_back("TID/MINUS");
67  subdet.push_back("TID/PLUS");
68  subdet.push_back("TOB");
69  subdet.push_back("TEC/MINUS");
70  subdet.push_back("TEC/PLUS");
71 
72  std::string nrun = filename.substr(filename.find("_R000")+5, 6);
73  int fileNum = atoi(nrun.c_str());
74  std::cout << " ------ Run " << fileNum << std::endl;
75 
76  std::ofstream outfile;
77  std::string namefile;
78  namefile = "QualityTestOBSOLETE_run" + nrun + ".txt";
79  outfile.open(namefile.c_str());
80 
81  TFile *myfile = TFile::Open(filename.c_str());
82  if (debug == 1){
83  std::cout <<" Opened "<< filename << std::endl;
84  }
85  std::string topdir = "DQMData/Run " + nrun + "/SiStrip/Run summary/MechanicalView";
86  gDirectory->cd(topdir.c_str());
87  TDirectory* mec1 = gDirectory;
88 
89  //get the summary first
90  std::vector <int> nbadmod;
91  for (unsigned int i=0; i < subdet.size(); i++){
92  int nbad = 0;
93  std::string badmodule_dir = subdet[i] + "/BadModuleList";
94  if (gDirectory->cd(badmodule_dir.c_str())){
95  TIter next(gDirectory->GetListOfKeys());
96  TKey *key;
97  while ( (key = dynamic_cast<TKey*>(next())) ) {
98  std::string sflag = key->GetName();
99  if (sflag.empty()) continue;
100  nbad++;
101  }
102  }
103  nbadmod.push_back(nbad);
104  mec1->cd();
105  }
106 
107  outfile << "Number of bad modules in total excluding PCL-only bad modules:" << std::endl;
108  outfile << "--------------------------------------------------------------" << std::endl;
109  outfile << subdet.at(0) << ": " << nbadmod.at(0) << std::endl;
110  outfile << subdet.at(1) << ": " << nbadmod.at(1) << std::endl;
111  outfile << subdet.at(2) << ": " << nbadmod.at(2) << std::endl;
112  outfile << subdet.at(3) << ": " << nbadmod.at(3) << std::endl;
113  outfile << subdet.at(4) << ": " << nbadmod.at(4) << std::endl;
114  outfile << subdet.at(5) << ": " << nbadmod.at(5) << std::endl;
115  outfile << "-------------------------------" << std::endl;
116 
117  outfile << std::endl
118  << "List of bad modules per partition:" << std::endl;
119  outfile << "----------------------------------" << std::endl;
120 
121  std::set<unsigned int>::const_iterator pclbadmod = pclbadmods.begin();
122 
123  for (unsigned int i=0; i < subdet.size(); i++){
124  std::string badmodule_dir = subdet[i] + "/BadModuleList";
125  outfile << " " << std::endl;
126  outfile << "SubDetector " << subdet[i] << std::endl;
127  outfile << " " << std::endl;
128  std::cout << badmodule_dir.c_str() << std::endl;
129  if (gDirectory->cd(badmodule_dir.c_str())){
130  //
131  // Loop to find bad module for each partition
132  //
133  TIter next(gDirectory->GetListOfKeys());
134  TKey *key;
135 
136  while ( (key = dynamic_cast<TKey*>(next())) ) {
137  std::string sflag = key->GetName();
138  if (sflag.empty()) continue;
139  std::string detid = sflag.substr(sflag.find("<")+1,9);
140  size_t pos1 = sflag.find("/");
141  sflag = sflag.substr(sflag.find("<")+13,pos1-2);
142  int flag = atoi(sflag.c_str());
143  sscanf(detid.c_str(),"%u",&pcldetid);
144  // the following loop add modules which are bad only for the PCL to the list.
145  // It requires that the bad modules from QT are sorted as the one of the PCL
146  while(pclbadmod!= pclbadmods.end() && pcldetid > *pclbadmod) {
147  outfile << "Module " << *pclbadmod << " PCLBadModule " << std::endl;
148  pclbadmod++;
149  }
150  std::string message;
151  message = "Module " + detid;
152  if (((flag >> 0) & 0x1) > 0) message += " Fed BadChannel : ";
153  if (((flag >> 1) & 0x1) > 0) message += " # of Digi : ";
154  if (((flag >> 2) & 0x1) > 0) message += " # of Clusters :";
155  if (((flag >> 3) & 0x1) > 0) message += " Excluded FED Channel ";
156  if (((flag >> 4) & 0x1) > 0) message += " DCSError ";
157  if (pclbadmods.find(pcldetid) != pclbadmods.end()) {
158  message += " PCLBadModule ";
159  pclbadmod = pclbadmods.find(pcldetid);
160  pclbadmod++;
161  }
162  outfile << message.c_str() << std::endl;
163 
164  }
165  }
166  mec1->cd();
167  }
168  myfile->Close();
169  outfile.close();
170 }
171 
172 
173 
int main(int argc, char *argv[])
#define debug
Definition: HDRShower.cc:19
void listbadmodule(std::string filename, std::string pclfilename)