Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:08:40

0001 #include <Riostream.h>
0002 #include <TDirectory.h>
0003 #include <TFile.h>
0004 #include <TROOT.h>
0005 #include <TStyle.h>
0006 #include <TKey.h>
0007 #include <TH1.h>
0008 #include <TH2.h>
0009 #include <TH2D.h>
0010 #include <TCanvas.h>
0011 #include <TGraph.h>
0012 #include <TPaveStats.h>
0013 #include <TText.h>
0014 #include <TLegend.h>
0015 #include <cstring>
0016 #include <utility>
0017 #include <vector>
0018 #include <sstream>
0019 #include <algorithm>
0020 #include <TString.h>
0021 #include <TColor.h>
0022 #include <dirent.h>
0023 
0024 using namespace std;
0025 
0026 int read_badmodlist(int, string, vector<int>&);
0027 void get_difference(vector<int>, vector<int>, vector<int>&);
0028 int get_filename(int, string, string&);
0029 int search_closest_run(int, string);
0030 void modulediff(int run_2, string repro_run2);
0031 
0032 int main(int argc, char* argv[]) {
0033   if (argc == 3) {
0034     char* crun2 = argv[1];
0035     char* repro_run2 = argv[2];
0036 
0037     int run2 = 0;
0038     sscanf(crun2, "%d", &run2);
0039 
0040     std::cout << "ready to run modulediff " << run2 << " repro run " << repro_run2 << std::endl;
0041 
0042     modulediff(run2, repro_run2);
0043 
0044   } else {
0045     std::cout << "Too few arguments: " << argc << std::endl;
0046     return -1;
0047   }
0048   return 0;
0049 }
0050 
0051 void modulediff(int run_2, string repro_run2) {
0052   vector<int> badmodlist_run1;
0053   vector<int> badmodlist_run2;
0054   vector<int> modules_recovered;
0055   vector<int> modules_malformed;
0056   int run_1;
0057   string repro_run1;
0058 
0059   int res = search_closest_run(run_2, repro_run2);
0060   if (res > 0) {
0061     run_1 = res;
0062     repro_run1 = repro_run2;
0063   } else {
0064     printf("closest run not found, exiting.\n");
0065     return;
0066   }
0067 
0068   int flag1 = read_badmodlist(run_1, repro_run1, badmodlist_run1);
0069   int flag2 = read_badmodlist(run_2, repro_run2, badmodlist_run2);
0070 
0071   if (flag1 < 0 || flag2 < 0) {
0072     cout << "Error: file not found." << endl;
0073     return;
0074   }
0075 
0076   get_difference(badmodlist_run1, badmodlist_run2, modules_recovered);
0077   get_difference(badmodlist_run2, badmodlist_run1, modules_malformed);
0078 
0079   //save into file
0080   std::ofstream outfile;
0081   string namefile = "modulediff_emailbody.txt";
0082   outfile.open(namefile.c_str());
0083 
0084   outfile << "Recovered modules in run " << run_2 << ": " << (int)modules_recovered.size() << endl;
0085   outfile << "New bad modules in run   " << run_2 << ": " << (int)modules_malformed.size() << endl;
0086   outfile << "Using reference run " << run_1 << endl << endl;
0087 
0088   outfile << "Recovered modules in run " << run_2 << ":" << endl;
0089   if (modules_recovered.empty())
0090     outfile << " -" << endl;
0091   for (unsigned int i = 0; i < modules_recovered.size(); i++)
0092     outfile << " " << modules_recovered[i] << endl;
0093 
0094   outfile << "New bad modules that appeared in run " << run_2 << ":" << endl;
0095   if (modules_malformed.empty())
0096     outfile << " -" << endl;
0097   for (unsigned int i = 0; i < modules_malformed.size(); i++)
0098     outfile << " " << modules_malformed[i] << endl;
0099 
0100   outfile.close();
0101 
0102   //create two flat files to run the locatemodule script on later
0103 
0104   if (!modules_recovered.empty()) {
0105     std::ofstream outfile_good;
0106     outfile_good.open("modulediff_good.txt");
0107     for (unsigned int i = 0; i < modules_recovered.size(); i++)
0108       outfile_good << " " << modules_recovered[i] << endl;
0109     outfile_good.close();
0110   }
0111 
0112   if (!modules_malformed.empty()) {
0113     std::ofstream outfile_bad;
0114     outfile_bad.open("modulediff_bad.txt");
0115     for (unsigned int i = 0; i < modules_malformed.size(); i++)
0116       outfile_bad << " " << modules_malformed[i] << endl;
0117     outfile_bad.close();
0118   }
0119 }
0120 
0121 void get_difference(vector<int> badlist1, vector<int> badlist2, vector<int>& difflist) {
0122   //check if any element of badlist1 is in badlist2
0123   for (unsigned int i1 = 0; i1 < badlist1.size(); i1++) {
0124     bool thisrecovered = true;
0125     for (unsigned int i2 = 0; i2 < badlist2.size(); i2++)
0126       if (badlist1[i1] == badlist2[i2]) {
0127         thisrecovered = false;
0128         break;
0129       }
0130     if (thisrecovered)
0131       difflist.push_back(badlist1[i1]);
0132   }
0133 }
0134 
0135 int read_badmodlist(int run, string repro_type, vector<int>& badlist) {
0136   string filename;
0137   int flag = get_filename(run, repro_type, filename);
0138 
0139   if (flag < 0) {
0140     cout << "reading problem" << endl;
0141     return -1;
0142   }
0143 
0144   vector<string> subdet;
0145   subdet.push_back("TIB");
0146   subdet.push_back("TID/MINUS");
0147   subdet.push_back("TID/PLUS");
0148   subdet.push_back("TOB");
0149   subdet.push_back("TEC/MINUS");
0150   subdet.push_back("TEC/PLUS");
0151 
0152   string nrun = filename.substr(filename.find("_R000") + 5, 6);
0153 
0154   TFile* dqmfile = TFile::Open(filename.c_str(), "READ");
0155   string topdir = "DQMData/Run " + nrun + "/SiStrip/Run summary/MechanicalView";
0156   gDirectory->cd(topdir.c_str());
0157   TDirectory* mec1 = gDirectory;
0158 
0159   for (unsigned int i = 0; i < subdet.size(); i++) {
0160     string badmodule_dir = subdet[i] + "/BadModuleList";
0161     if (gDirectory->cd(badmodule_dir.c_str())) {
0162       TIter next(gDirectory->GetListOfKeys());
0163       TKey* key;
0164 
0165       while ((key = dynamic_cast<TKey*>(next()))) {
0166         string sflag = key->GetName();
0167         if (sflag.empty())
0168           continue;
0169 
0170         string detid = sflag.substr(sflag.find('<') + 1, 9);
0171         badlist.push_back(atoi(detid.c_str()));
0172       }
0173     } else {
0174       //cout << "no dir " << badmodule_dir << " in filename " << filename << endl;
0175     }
0176     mec1->cd();
0177   }
0178   dqmfile->Close();
0179   return 0;
0180 }
0181 
0182 int get_filename(int run, string repro_type, string& filename) {
0183   stringstream runstr;
0184   runstr << run;
0185 
0186   stringstream rundirprefix;
0187   rundirprefix << "000" << run / 100 << "xx/";
0188 
0189   stringstream thisdir;
0190   //thisdir << "/afs/cern.ch/cms/CAF/CMSCOMM/COMM_DQM/data/OfflineData/Run2011/" << repro_type.c_str() << "/" << rundirprefix.str();
0191   thisdir << "/afs/cern.ch/cms/CAF/CMSCOMM/COMM_DQM/data/OfflineData/HIRun2011/" << repro_type.c_str() << "/"
0192           << rundirprefix.str();
0193 
0194   string thisdir2 = thisdir.str();
0195   DIR* dp;
0196 
0197   if ((dp = opendir(thisdir2.c_str())) == nullptr) {
0198     cout << "dir " << thisdir2.c_str() << " not found" << endl;
0199     return -1;
0200   }
0201 
0202   struct dirent* dirp;
0203 
0204   string dqmfile;
0205 
0206   while ((dirp = readdir(dp)) != nullptr) {
0207     string dirfile = string(dirp->d_name);
0208     if (dirfile.find("__DQM") != string::npos && dirfile.find(runstr.str()) != string::npos) {
0209       dqmfile = dirfile;
0210       break;
0211     }
0212   }
0213 
0214   closedir(dp);
0215 
0216   if (dqmfile.size() < 10) {
0217     //cout << "file " << dqmfile << " not found" << endl;
0218     return -1;
0219   }
0220 
0221   filename = thisdir.str() + dqmfile;
0222 
0223   return 0;
0224 }
0225 
0226 int search_closest_run(int thisrun, string repro_type) {
0227   string filename;
0228 
0229   for (int test_run = thisrun - 1; test_run > thisrun - 1000; test_run--) {
0230     int res = get_filename(test_run, repro_type, filename);
0231     if (res == 0)
0232       return test_run;
0233   }
0234 
0235   return -1;
0236 }