Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 #include <fstream>
0002 #include <iostream>
0003 #include <iomanip>
0004 #include <sstream>
0005 #include <cassert>
0006 #include <map>
0007 #include <vector>
0008 
0009 #include "TH1F.h"
0010 #include "TH2F.h"
0011 #include "TLegend.h"
0012 #include "TFile.h"
0013 #include "TCanvas.h"
0014 #include "TROOT.h"
0015 #include "TStyle.h"
0016 #include "TString.h"
0017 #include "TDirectory.h"
0018 
0019 int main(int argc, char** argv) {//main
0020 
0021   if (argc < 4){
0022     std::cout << "Usage: " << argv[0] 
0023           << " <path to file> <number of runs> <run number list>"  
0024           << std::endl;
0025     return 0;
0026   }
0027 
0028 
0029   unsigned int nRuns;
0030   std::istringstream(argv[2])>>nRuns;
0031 
0032   std::vector<unsigned int> runs;
0033 
0034   //std::map<unsigned int,std::vector<std::pair<unsigned int,float> > > lMap;
0035   //std::pair<std::map<unsigned int,std::vector<std::pair<unsigned int,float> > >::iterator,bool> lMapIter;
0036 
0037   //std::map<unsigned int,double> lMapFED; 
0038 
0039   unsigned int nDirNotFound = 0;
0040   unsigned int nFileNotFound = 0;
0041 
0042   std::ofstream txtoutfile;                                                                                         
0043   txtoutfile.open("SpyErrors.dat",std::ios::out);                                                                   
0044   txtoutfile << "| Error type | run # | fedID | rate of error |" << std::endl;
0045 
0046   TString baseName = argv[1];
0047   baseName += "/";
0048 
0049   unsigned int nErr = 0;
0050 
0051   for (unsigned int r(0); r<nRuns; r++){//loop on runs
0052 
0053     unsigned int lRun;
0054     std::istringstream(argv[3+r])>>lRun;
0055 
0056     std::cout << "Processing run " << lRun << std::endl;
0057 
0058     runs.push_back(lRun);
0059 
0060 
0061     TString fileName  = baseName;
0062     fileName += lRun;
0063     fileName += "/DQMStore.root";
0064 
0065     TFile *f = TFile::Open(fileName);
0066     
0067     if (!f) {
0068       std::cout << " -- File " << fileName << " not found." << std::endl;
0069       return 0;
0070     }
0071 
0072     TString dirName = "DQMData/SiStrip/ReadoutView/";
0073     std::cout << " -- Directory " << dirName << std::endl;
0074 
0075     if (!f->cd(dirName)) {
0076       std::cerr << " -- Folder not found. Check if file valid or source code valid. Going to next run." << std::endl;
0077       nDirNotFound++;
0078       //return 0;
0079       continue;
0080     }
0081 
0082     //looking for object with name chNames etc...
0083     TString normDir = dirName;
0084     normDir += "SpyMonitoringSummary";
0085     if (!f->cd(normDir)) {
0086       std::cerr << "Folder not found, please check file " << __FILE__ << ", line " << __LINE__ << std::endl;
0087       nDirNotFound++;
0088       return 0;
0089     };
0090 
0091     TH1F *hNorm = (TH1F*)gDirectory->Get("nNoData");
0092     double norm = hNorm->GetEntries();
0093 
0094     //lMapFED.insert(std::pair<unsigned int,double>(lRun,hNorm->GetMean()));                                          
0095                                                                                                                      
0096     //find out which FEDs are in error, and which error
0097     const unsigned int nErrs = 11;
0098     TString lFedName[nErrs] = {"MinZero","MaxSat",
0099                    "LowTrimDAC","HighTrimDAC",
0100                    "LowRange","HighRange",
0101                    "OutOfSync","OtherPbs",
0102                    "ApvErrorBit","ApvAddressError",
0103                    "NegativePeds"};                           
0104                                                                                                                      
0105     for (unsigned int iH(0); iH<nErrs; iH++){                                                                           
0106       TH1F *FEDobj = (TH1F*)gDirectory->Get(lFedName[iH]);
0107       if (!FEDobj) { 
0108     std::cout << "Error, histogram " << lFedName[iH] << " not found. Continue..." << std::endl;
0109     continue;//return 0;
0110       }
0111       else { 
0112     if (FEDobj->GetEntries() != 0) {
0113       for (int bin(1); bin<FEDobj->GetNbinsX()+1; bin++){
0114         if (FEDobj->GetBinContent(bin)>0){
0115           unsigned int iFed = bin+49;
0116           float lStat = FEDobj->GetBinContent(bin)*1./norm;
0117           txtoutfile << lFedName[iH] << " " << lRun << " " << iFed << " " << lStat << std::endl;
0118         }
0119       }
0120     }
0121       }
0122     }
0123 
0124     
0125     for (unsigned int ifed(50); ifed<500;ifed++){//loop on FEDs
0126 
0127       TString fedDir = dirName;
0128       fedDir += "FrontEndDriver";
0129       fedDir += ifed;
0130 
0131       if (!f->cd(fedDir)) continue;
0132       else {
0133     std::cout << " - Errors detected for FED " << ifed << std::endl;
0134     txtoutfile << " ***************************************** " << std::endl
0135            << " **** Channel/APV errors for FED " << ifed << " **** " << std::endl
0136            << " ***************************************** " << std::endl;
0137       }
0138       
0139       TDirectory *current = gDirectory;
0140 
0141       //channel histos
0142       for (unsigned int iH(0); iH<nErrs; iH++){                                                                           
0143     TString objName = lFedName[iH];
0144     objName += "ForFED";
0145     objName += ifed;
0146     TH1F *obj = (TH1F*)current->Get(objName);
0147     
0148     if (!obj) {
0149       std::cout << "Error, histogram " << objName << " not found. Exiting..." << std::endl;
0150       continue;//return 0;
0151     }
0152     else {
0153       if (obj->GetEntries() != 0) {
0154         for (int bin(1); bin<obj->GetNbinsX()+1; bin++){
0155           unsigned int iCh = bin-1;
0156           //if (obj->GetNbinsX() < 100) iCh = 96*ifed+bin-1;
0157           //else iCh = 192*ifed+bin-1;
0158           if (obj->GetBinContent(bin)>0){ 
0159         float lStat = obj->GetBinContent(bin)/norm*1.;
0160         //std::vector<std::pair<unsigned int, float> > lVec;
0161         //lVec.push_back(std::pair<unsigned int, float>(lRun,lStat));
0162         //lMapIter = lMap.insert(std::pair<unsigned int,std::vector<std::pair<unsigned int,float> > >(iCh,lVec));
0163         //if (!lMapIter.second) ((lMapIter.first)->second).push_back(std::pair<unsigned int, float>(lRun,lStat));
0164         
0165         txtoutfile << objName << " " << lRun << " " << ifed << " " << iCh << " " << lStat << std::endl;
0166         nErr++;
0167           }
0168         }
0169       }
0170     }
0171       }//loop on errors
0172     }//loop on feds
0173 
0174   }//loop on runs
0175 
0176   //unsigned int nErr =  lMap.size();
0177 
0178   assert (runs.size() == nRuns);
0179 
0180   std::cout << "Found " << nErr << " Channels/APVs with errors in " << nRuns << " runs processed." << std::endl;
0181   std::cout << "Number of runs where file was not found : " << nFileNotFound << std::endl;
0182   std::cout << "Number of runs where folder was not found : " << nDirNotFound << std::endl;
0183 
0184 //   TFile *outfile = TFile::Open("APVAddressErrors.root","RECREATE");
0185 //   outfile->cd();
0186   
0187 //   TH1F *h[nErr];
0188 //   std::map<unsigned int,std::vector<std::pair<unsigned int,float> > >::iterator lIter = lMap.begin();
0189 
0190 //   unsigned int e = 0;
0191 
0192 //   for (;lIter!=lMap.end(); lIter++){
0193 
0194 //     unsigned int lCh = lIter->first;
0195 //     std::ostringstream lName;
0196 //     lName << "ErrorFed" << static_cast<unsigned int>(lCh/192.) << "Ch" << static_cast<unsigned int>(lCh%192/2.) << "APV" << lCh%2;
0197 
0198 //     h[e] = new TH1F(lName.str().c_str(),"rate of APVAddressError vs run",runs[nRuns-1]-runs[0]+1,runs[0],runs[nRuns-1]+1);
0199 
0200 //     std::vector<std::pair<unsigned int,float> > lVec = lIter->second;
0201 //     unsigned int nBins = lVec.size();
0202 
0203 //     for (unsigned int b(0); b<nBins; b++){
0204 //       //std::cout <<"Run " << lVec.at(b).first << ", runs[0] = " << runs[0] << ", runs[" << nRuns-1 << "] = " << runs[nRuns-1] << std::endl; 
0205 
0206 //       h[e]->SetBinContent(lVec.at(b).first-runs[0]+1,lVec.at(b).second);
0207 //       //std::cout << "Setting bin " << lVec.at(b).first-runs[0]+1 << " content to " << lVec.at(b).second << std::endl;
0208 //     }
0209 
0210 //     //h[e]->Write();
0211 
0212 //     e++;
0213 
0214 //   }
0215 
0216 //   TH1F *hRate = new TH1F("hRate",";run #",runs[nRuns-1]-runs[0]+1,runs[0],runs[nRuns-1]+1);
0217 //   std::map<unsigned int,double>::iterator lIterFED = lMapFED.begin();
0218 //   for (;lIterFED!=lMapFED.end(); lIterFED++){
0219 //     hRate->SetBinContent(lIterFED->first-runs[0]+1,lIterFED->second);
0220 //   }
0221 
0222 
0223 
0224 //   outfile->Write();
0225 //   outfile->Close();
0226 
0227   txtoutfile.close();
0228 
0229   return 1;
0230   
0231 }//main