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 #include <cmath>
0009
0010 #include "TH1F.h"
0011 #include "TH2F.h"
0012 #include "TProfile.h"
0013 #include "TLegend.h"
0014 #include "TFile.h"
0015 #include "TCanvas.h"
0016 #include "TROOT.h"
0017 #include "TStyle.h"
0018 #include "TString.h"
0019 #include "TDirectory.h"
0020
0021
0022
0023
0024
0025
0026
0027 int main(int argc, char** argv) {
0028
0029 if (argc < 6){
0030 std::cout << "Usage: " << argv[0]
0031 << " <path to file (paths available given in getCommand script)> <first run (0=all)> <last run (0=all)> <total number of runs> <run number list>"
0032 << std::endl;
0033 return 0;
0034 }
0035 std::string lPath = argv[1];
0036
0037 bool lAfsFiles = true;
0038
0039 if (lPath.find("/afs") == lPath.npos) lAfsFiles = false;
0040
0041 std::string lFolder = lPath.substr(lPath.find("xxxx")-5,9);
0042 if (lAfsFiles) lFolder += "_afs";
0043 std::cout << " -- Folder = " << lFolder << std::endl;
0044
0045 unsigned int nRuns;
0046 std::istringstream(argv[4])>>nRuns;
0047
0048 unsigned int lRunMin=0;
0049 unsigned int lRunMax=10000000;
0050 std::istringstream(argv[2])>>lRunMin;
0051 std::istringstream(argv[3])>>lRunMax;
0052
0053 if (lRunMax==0) lRunMax=10000000;
0054
0055 std::vector<unsigned int> runs;
0056 const unsigned int nHistsDetailed = 5;
0057
0058 std::map<unsigned int,std::vector<std::pair<unsigned int,float> > > lMap[nHistsDetailed];
0059 std::pair<std::map<unsigned int,std::vector<std::pair<unsigned int,float> > >::iterator,bool> lMapIter;
0060 std::map<unsigned int,std::vector<std::pair<unsigned int,float> > > lMapRun[nHistsDetailed];
0061 std::pair<std::map<unsigned int,std::vector<std::pair<unsigned int,float> > >::iterator,bool> lMapRunIter;
0062
0063 std::map<unsigned int,double> lMapFED;
0064 std::map<unsigned int,std::pair<double,double> > lMapFEDChannels;
0065 std::map<unsigned int,std::pair<double,double> > lMapChannels;
0066 std::map<unsigned int,std::pair<double,double> > lMapAll;
0067
0068
0069
0070
0071
0072
0073
0074 unsigned int nDirNotFound = 0;
0075
0076 unsigned int nFileNotFound = 0;
0077
0078 std::ofstream txtoutfile;
0079 std::ostringstream txtname;
0080 txtname << "FEDErrors_" << lFolder << "_" << lRunMin << "_" << lRunMax << ".dat";
0081
0082 txtoutfile.open(txtname.str().c_str(),std::ios::out);
0083
0084 txtoutfile << "| Error type | run # | fedID | rate of error |" << std::endl;
0085
0086
0087 TString histName[nHistsDetailed] = {"FEMissing","APVAddressErrorBits","APVErrorBits","UnlockedBits","OOSBits"};
0088
0089 std::vector<double> lNormVal[nHistsDetailed];
0090 for (unsigned int r(0); r<nRuns; ++r){
0091
0092 unsigned int lRun;
0093 std::istringstream(argv[5+r])>>lRun;
0094
0095 std::cout << "Processing run " << lRun << std::endl;
0096
0097 if (lRun < lRunMin || lRun > lRunMax) continue;
0098
0099 runs.push_back(lRun);
0100
0101
0102 TString fileName = lPath;
0103
0104
0105
0106
0107
0108
0109
0110
0111 unsigned int lSubFolder = static_cast<unsigned int>(lRun/100.);
0112
0113 TString baseName = fileName;
0114 if (lAfsFiles) {
0115 std::cout << " Reading files from afs..." << std::endl;
0116 fileName += "/000";
0117 fileName += lSubFolder;
0118 fileName += "xx";
0119 }
0120 fileName += "/DQM_V0001_SiStrip_R000";
0121 fileName += lRun;
0122 fileName += ".root";
0123
0124
0125
0126
0127
0128
0129
0130 TFile *f = TFile::Open(fileName);
0131
0132
0133
0134
0135
0136
0137
0138
0139
0140
0141
0142
0143
0144
0145
0146
0147
0148
0149
0150
0151
0152
0153
0154
0155
0156
0157
0158
0159
0160
0161
0162
0163
0164 if (!f) {
0165 std::cout << "Cannot open file " << fileName << " for reading ! Continue ..." << std::endl;
0166 nFileNotFound++;
0167
0168 continue;
0169 }
0170
0171
0172
0173
0174
0175 TString dirName = "DQMData/Run ";
0176 dirName += lRun;
0177 dirName += "/SiStrip/Run summary/ReadoutView/";
0178 std::cout << "Directory " << dirName << std::endl;
0179
0180 if (!f->cd(dirName)) {
0181 std::cerr << "Folder not found. Check if file valid or source code valid in " << __FILE__ << ", variable dirName! Going to next run." << std::endl;
0182 nDirNotFound++;
0183
0184 continue;
0185 }
0186
0187
0188 TString normDir = dirName;
0189 normDir += "FedMonitoringSummary/";
0190 if (!f->cd(normDir)) {
0191 std::cerr << "Folder " << normDir << " not found, continuing... " << __FILE__ << ", line " << __LINE__ << std::endl;
0192 nDirNotFound++;
0193
0194 continue;
0195 };
0196
0197
0198
0199 TH1F *hNorm = (TH1F*)gDirectory->Get("FED/nFEDErrors");
0200 if (!hNorm) hNorm = (TH1F*)gDirectory->Get("FEDLevel/nFEDErrors");
0201 if (!hNorm) hNorm = (TH1F*)gDirectory->Get("nFEDErrors");
0202 if (!hNorm) {
0203 std::cout << "Error, histogram nFEDErrors not found. Continue..." << std::endl;
0204 continue;
0205 }
0206 double norm = hNorm->GetEntries();
0207
0208 lMapFED.insert(std::pair<unsigned int,double>(lRun,hNorm->GetMean()));
0209
0210 TH1F *hChannels = (TH1F*)gDirectory->Get("Fiber/nBadChannelStatusBits");
0211 if (!hChannels) hChannels = (TH1F*)gDirectory->Get("FiberLevel/nBadChannelStatusBits");
0212 if (!hChannels) hChannels = (TH1F*)gDirectory->Get("nBadChannelStatusBits");
0213 if (!hChannels){
0214 std::cout << "Error, histogram nBadChannelStatusBits not found. Continue..." << std::endl;
0215 continue;
0216 }
0217
0218 lMapChannels.insert(std::pair<unsigned int,std::pair<double,double> >(lRun,std::pair<double,double>(hChannels->GetMean(),hChannels->GetMeanError())));
0219
0220
0221
0222 TProfile *hAll = (TProfile*)gDirectory->Get("Trends/nTotalBadChannelsvsTime");
0223 if (!hAll) hAll = (TProfile*)gDirectory->Get("ErrorsVsTime/nTotalBadChannelsvsTime");
0224 if (!hAll) hAll = (TProfile*)gDirectory->Get("nTotalBadChannelsvsTime");
0225 if (!hAll) {
0226 std::cout << "Error, histogram nTotalBadChannelsvsTime not found. Continue..." << std::endl;
0227 continue;
0228 }
0229 lMapAll.insert(std::pair<unsigned int,std::pair<double,double> >(lRun,std::pair<double,double>(hAll->GetMean(2),hAll->GetMeanError(2))));
0230
0231 if (hAll->GetMean(2) < 0.000001) std::cout << " -- Run " << lRun << " has no errors." << std::endl;
0232
0233 lMapFEDChannels.insert(std::pair<unsigned int,std::pair<double,double> >(lRun,std::pair<double,double>(hAll->GetMean(2)-hChannels->GetMean(),sqrt(hAll->GetMeanError(2)*hAll->GetMeanError(2)+hChannels->GetMeanError()*hChannels->GetMeanError()))));
0234
0235
0236
0237
0238 const unsigned int nHistos = 5;
0239
0240 std::string lFedName[nHistos] = {"FED/VsId/AnyFEDErrors","FED/VsId/CorruptBuffers","FE/VsId/AnyFEProblems","FED/VsId/BadDAQCRCs","FED/VsId/DataMissing"};
0241 std::string lFedNameBis[nHistos] = {"FEDLevel/VsFedId/AnyFEDErrors","FEDLevel/VsFedId/CorruptBuffers","FrontEndLevel/VsFedId/AnyFEProblems","FEDLevel/VsFedId/BadDAQCRCs","FEDLevel/VsFedId/DataMissing"};
0242 std::string lFedNameTer[nHistos] = {"AnyFEDErrors","CorruptBuffers","AnyFEProblems","BadDAQCRCs","DataMissing"};
0243
0244 for (unsigned int iH(0); iH<nHistos; ++iH){
0245 TH1F *FEDobj = (TH1F*)gDirectory->Get(lFedName[iH].c_str());
0246 if (!FEDobj) FEDobj = (TH1F*)gDirectory->Get(lFedNameBis[iH].c_str());
0247 if (!FEDobj) FEDobj = (TH1F*)gDirectory->Get(lFedNameTer[iH].c_str());
0248
0249 if (!FEDobj) {
0250 std::cout << "Error, histogram " << lFedName[iH] << " not found. Continue..." << std::endl;
0251 continue;
0252 }
0253 else {
0254 if (FEDobj->GetEntries() != 0) {
0255 unsigned int nFeds = 0;
0256 for (int bin(1); bin<FEDobj->GetNbinsX()+1; ++bin){
0257 if (FEDobj->GetBinContent(bin)>0){
0258 ++nFeds;
0259 }
0260 }
0261 for (int bin(1); bin<FEDobj->GetNbinsX()+1; ++bin){
0262 if (FEDobj->GetBinContent(bin)>0){
0263 unsigned int iFed = bin+49;
0264 float lStat = FEDobj->GetBinContent(bin)/norm*1.;
0265 if (nFeds < 90) {
0266 txtoutfile << lFedName[iH] << " " << lRun << " " << iFed << " " << lStat << std::endl;
0267 }
0268 else {
0269 txtoutfile << lFedName[iH] << " " << lRun << " " << nFeds << " FEDs with rate: " << lStat << std::endl;
0270 break;
0271 }
0272 }
0273 }
0274 }
0275 }
0276 }
0277
0278 for (unsigned int ifed(50); ifed<500;++ifed){
0279
0280 TString fedDir = dirName;
0281 fedDir += "FrontEndDriver";
0282 fedDir += ifed;
0283
0284 if (!f->cd(fedDir)) continue;
0285 else {
0286
0287 }
0288
0289 TDirectory *current = gDirectory;
0290
0291
0292 for (unsigned int iH(0); iH<nHistsDetailed; ++iH){
0293
0294 TString objName = histName[iH];
0295 objName += "ForFED";
0296 objName += ifed;
0297 TH1F *obj = (TH1F*)current->Get(objName);
0298
0299 if (!obj) {
0300
0301 continue;
0302 }
0303 else {
0304 std::cout << "Processing histogram " << objName << ", nentries = " << obj->GetEntries() << std::endl;
0305 if (obj->GetEntries() != 0) {
0306 for (int bin(1); bin<obj->GetNbinsX()+1; ++bin){
0307 if (obj->GetBinContent(bin)>0){
0308
0309
0310 unsigned int iCh = bin-1;
0311 if (iH==0) iCh = iCh*12;
0312 unsigned int iAPV = 0;
0313 if (iH==1 || iH==2) {
0314 iAPV = iCh%2;
0315 iCh = static_cast<unsigned int>(iCh/2.);
0316 }
0317 float lStat = obj->GetBinContent(bin)/norm*1.;
0318 std::vector<std::pair<unsigned int, float> > lVec;
0319 lVec.push_back(std::pair<unsigned int, float>(lRun,lStat));
0320 std::vector<std::pair<unsigned int, float> > lVecRun;
0321 unsigned int lChIdx = 2*96*ifed+2*iCh+iAPV;
0322 lVecRun.push_back(std::pair<unsigned int, float>(lChIdx,lStat));
0323
0324 lMapIter = lMap[iH].insert(std::pair<unsigned int,std::vector<std::pair<unsigned int,float> > >(lChIdx,lVec));
0325 if (!lMapIter.second) ((lMapIter.first)->second).push_back(std::pair<unsigned int, float>(lRun,lStat));
0326 lMapRunIter = lMapRun[iH].insert(std::pair<unsigned int,std::vector<std::pair<unsigned int,float> > >(lRun,lVecRun));
0327 if (!lMapRunIter.second) ((lMapRunIter.first)->second).push_back(std::pair<unsigned int, float>(lChIdx,lStat));
0328 else {
0329 lNormVal[iH].push_back(norm);
0330 }
0331 }
0332 }
0333 }
0334 }
0335 }
0336
0337 }
0338
0339
0340
0341
0342
0343
0344
0345
0346
0347
0348
0349
0350
0351
0352
0353
0354
0355
0356
0357
0358
0359
0360
0361
0362
0363
0364
0365
0366
0367
0368
0369
0370
0371
0372
0373
0374
0375
0376
0377
0378
0379
0380 f->Close();
0381 }
0382
0383 const unsigned int nErr[nHistsDetailed] = {lMap[0].size(),lMap[1].size(),lMap[2].size(),lMap[3].size(),lMap[4].size()};
0384
0385
0386 nRuns = runs.size();
0387
0388
0389 for (unsigned int iH(0); iH<nHistsDetailed; ++iH){
0390 txtoutfile << "Found " << nErr[iH] << " channels with errors " << histName[iH] << " in " << lMapRun[iH].size() << " runs with this type of errors." << std::endl;
0391 }
0392 std::cout << "Number of runs where file was not found : " << nFileNotFound << std::endl;
0393 std::cout << "Number of runs where folder was not found : " << nDirNotFound << std::endl;
0394
0395
0396
0397
0398 std::ostringstream lrootname;
0399 lrootname << "MyHDQM_" << lFolder << "_" << runs[0] << "_" << runs[nRuns-1] << ".root";
0400
0401 TFile *outfile = TFile::Open(lrootname.str().c_str(),"RECREATE");
0402 outfile->cd();
0403
0404
0405 txtoutfile << " ****************************************** "
0406 << std::endl
0407 << " ** Summary of errors per FED/CH per run ** "
0408 << std::endl
0409 << " ***** for rate < 1% and nRuns < 5 ******** "
0410 << std::endl
0411 << " ****************************************** "
0412 <<std::endl;
0413
0414 for (unsigned int iH=0; iH<nHistsDetailed; ++iH){
0415
0416 TH1F *h[nErr[iH]];
0417 std::map<unsigned int,std::vector<std::pair<unsigned int,float> > >::iterator lIter = lMap[iH].begin();
0418 std::ostringstream lNameRun;
0419 lNameRun << "Number_" << histName[iH] << "_vsRun";
0420 TH1F *hRun = new TH1F(lNameRun.str().c_str(),"Number of channel with Error vs run; run #",runs[nRuns-1]-runs[0]+1,runs[0],runs[nRuns-1]+1);
0421 lNameRun.str("");
0422 lNameRun << "FEDS_" << histName[iH] << "_vsRun";
0423 TH2F *hFEDRun_apv0 = new TH2F((lNameRun.str()+"_apv0").c_str(),"Number of runs with FED channels APV0 with Errors; FED id ; ch id",440,50,490,96,0,96);
0424 TH2F *hFEDRun_apv1 = new TH2F((lNameRun.str()+"_apv1").c_str(),"Number of runs with FED channels APV1 with Errors; FED id ; ch id",440,50,490,96,0,96);
0425 lNameRun.str("");
0426 lNameRun << "FEDS_" << histName[iH] << "_vsRate";
0427 TH2F *hFEDRate_apv0 = new TH2F((lNameRun.str()+"_apv0").c_str(),"Mean rate of errors of FED channels APV0 with Errors; FED id ; ch id",440,50,490,96,0,96);
0428 TH2F *hFEDRate_apv1 = new TH2F((lNameRun.str()+"_apv1").c_str(),"Mean rate of errors of FED channels APV1 with Errors; FED id ; ch id",440,50,490,96,0,96);
0429 std::map<unsigned int,std::vector<std::pair<unsigned int,float> > >::iterator lIterRun = lMapRun[iH].begin();
0430
0431 assert(lNormVal[iH].size() == lMapRun[iH].size());
0432 unsigned int lIdx = 0;
0433 for (;lIterRun!=lMapRun[iH].end(); ++lIterRun,++lIdx){
0434
0435 std::vector<std::pair<unsigned int,float> > lVec = lIterRun->second;
0436 hRun->Fill(lIterRun->first,lVec.size());
0437 for (unsigned int ele(0);ele<lVec.size();++ele){
0438 unsigned int lCh = lVec[ele].first;
0439 unsigned int lElements = lMap[iH][lCh].size();
0440 unsigned int lApv = static_cast<unsigned int>(lCh%2);
0441 if (lElements<5 && lVec[ele].second < 0.01) {
0442 txtoutfile << histName[iH]
0443 << " Run " << lIterRun->first << " fed "
0444 << static_cast<unsigned int>(lCh/192.) << " ch "
0445 << static_cast<unsigned int>((lCh%192)/2.) << " apv "
0446 << lApv
0447 << " rate " << lVec[ele].second
0448 << " (" << 1/lNormVal[iH][lIdx] << ")"
0449 << " nRuns " << lElements
0450 << std::endl;
0451 }
0452 if (lApv==0) {
0453 hFEDRun_apv0->Fill(static_cast<unsigned int>(lCh/192.),static_cast<unsigned int>((lCh%192)/2.));
0454 hFEDRate_apv0->Fill(static_cast<unsigned int>(lCh/192.),static_cast<unsigned int>((lCh%192)/2.),lVec[ele].second/lElements);
0455 }
0456 else {
0457 hFEDRun_apv1->Fill(static_cast<unsigned int>(lCh/192.),static_cast<unsigned int>((lCh%192)/2.));
0458 hFEDRate_apv1->Fill(static_cast<unsigned int>(lCh/192.),static_cast<unsigned int>((lCh%192)/2.),lVec[ele].second/lElements);
0459 }
0460 }
0461 }
0462
0463
0464
0465
0466
0467
0468
0469
0470
0471
0472
0473
0474
0475
0476
0477
0478
0479
0480
0481
0482
0483
0484
0485
0486
0487
0488
0489
0490 }
0491
0492 TH1F *hRate = new TH1F("hRate",";run #",runs[nRuns-1]-runs[0]+1,runs[0],runs[nRuns-1]+1);
0493 std::map<unsigned int,double>::iterator lIterFED = lMapFED.begin();
0494 for (;lIterFED!=lMapFED.end(); ++lIterFED){
0495 hRate->SetBinContent(lIterFED->first-runs[0]+1,lIterFED->second);
0496 }
0497
0498
0499 TH1F *hRateFEDChannels = new TH1F("hRateFEDChannels",";run #",runs[nRuns-1]-runs[0]+1,runs[0],runs[nRuns-1]+1);
0500 std::map<unsigned int,std::pair<double,double> >::iterator lIterFEDCh = lMapFEDChannels.begin();
0501 for (;lIterFEDCh!=lMapFEDChannels.end(); ++lIterFEDCh){
0502 hRateFEDChannels->SetBinContent(lIterFEDCh->first-runs[0]+1,lIterFEDCh->second.first/36392.*100);
0503 hRateFEDChannels->SetBinError(lIterFEDCh->first-runs[0]+1,lIterFEDCh->second.second/36392.*100);
0504 }
0505
0506 TH1F *hRateChannels = new TH1F("hRateChannels",";run #",runs[nRuns-1]-runs[0]+1,runs[0],runs[nRuns-1]+1);
0507 std::map<unsigned int,std::pair<double,double> >::iterator lIterCh = lMapChannels.begin();
0508 for (;lIterCh!=lMapChannels.end(); ++lIterCh){
0509 hRateChannels->SetBinContent(lIterCh->first-runs[0]+1,lIterCh->second.first/36392.*100);
0510 hRateChannels->SetBinError(lIterCh->first-runs[0]+1,lIterCh->second.second/36392.*100);
0511 }
0512
0513 TH1F *hRateAll = new TH1F("hRateAll",";run #",runs[nRuns-1]-runs[0]+1,runs[0],runs[nRuns-1]+1);
0514 std::map<unsigned int,std::pair<double,double> >::iterator lIterAll = lMapAll.begin();
0515 for (;lIterAll!=lMapAll.end(); ++lIterAll){
0516 hRateAll->SetBinContent(lIterAll->first-runs[0]+1,lIterAll->second.first/36392.*100);
0517 hRateAll->SetBinError(lIterAll->first-runs[0]+1,lIterAll->second.second/36392.*100);
0518 }
0519
0520
0521
0522
0523
0524
0525
0526
0527
0528
0529
0530
0531
0532
0533
0534
0535
0536
0537 outfile->Write();
0538 outfile->Close();
0539 txtoutfile.close();
0540
0541 return 1;
0542
0543 }