Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-05 08:12:18

0001 // c++ includes --
0002 #include <string>
0003 #include <iostream>
0004 #include <iomanip>
0005 #include <fstream>
0006 #include <unordered_set>
0007 #include <filesystem>
0008 
0009 // root includes --
0010 #include <TSystem.h>
0011 #include <TROOT.h>
0012 #include <TH2F.h>
0013 #include <TFile.h>
0014 #include <TMath.h>
0015 #include <TDatime.h>
0016 #include <TGraph.h>
0017 
0018 #include <calobase/TowerInfoDefs.h>
0019 #include <cdbobjects/CDBTTree.h>
0020 #include <ffamodules/CDBInterface.h>
0021 #include <phool/recoConsts.h>
0022 
0023 using std::cout;
0024 using std::cerr;
0025 using std::endl;
0026 using std::string;
0027 using std::stringstream;
0028 using std::ofstream;
0029 using std::vector;
0030 using std::pair;
0031 using std::min;
0032 using std::max;
0033 
0034 namespace fs = std::filesystem;
0035 
0036 R__LOAD_LIBRARY(libcalo_io.so)
0037 R__LOAD_LIBRARY(libcdbobjects.so)
0038 R__LOAD_LIBRARY(libffamodules.so)
0039 R__LOAD_LIBRARY(libphool.so)
0040 
0041 namespace myAnalysis {
0042 
0043     Int_t init(const string &input);
0044     void init_hists();
0045 
0046     Int_t process_event(const string &outputRunStats);
0047     void finalize(const string &i_output, const string &outputMissingHotMap, const string &outputMissingBadChi2);
0048 
0049     TH1F* hBadTowers;
0050     TH1F* hBadTowersDead;
0051     TH1F* hBadTowersHot;
0052     TH1F* hBadTowersHotChi2;
0053     TH1F* hBadTowersCold;
0054 
0055     TH2F* h2BadTowers;
0056     TH2F* h2BadTowersDead;
0057     TH2F* h2BadTowersHot;
0058     TH2F* h2BadTowersHotChi2;
0059     TH2F* h2BadTowersCold;
0060 
0061     TH1F* hHotTowerStatus;
0062 
0063     TH1F* hSigmaNeverHot;
0064     TH1F* hSigmaHot;
0065     TH1F* hSigmaFreqHot;
0066 
0067     TH1F* hAcceptance;
0068     TH1F* hFracHot;
0069     TH1F* hFracDead;
0070     TH1F* hFracCold;
0071     TH1F* hFracBadChi2;
0072 
0073     TH1F* hAcceptanceVsTime;
0074     TH1F* hHotVsTime;
0075     TH1F* hDeadVsTime;
0076     TH1F* hColdVsTime;
0077     TH1F* hBadChi2VsTime;
0078 
0079     TH1F* hRunStatus;
0080 
0081     vector<TH2F*> h2HotTowerFrequency_vec;
0082     vector<TH1F*> hHotTowerSigma_vec;
0083 
0084     vector<pair<UInt_t,string>> runs;
0085     vector<UInt_t> runsMissingHotMap;
0086     vector<UInt_t> runsMissingBadChi2;
0087     recoConsts* rc;
0088 
0089     UInt_t m_ntowers     = 24576;
0090     UInt_t m_bins_phi    = 256;
0091     UInt_t m_bins_eta    = 96;
0092     UInt_t m_bins_status = 2;
0093     UInt_t m_nStatus     = 5;
0094 
0095     UInt_t m_bins_sigma  = 1000;
0096     Float_t m_sigma_low  = -50;
0097     Float_t m_sigma_high = 50;
0098 
0099     UInt_t m_bins_acceptance = 101;
0100     UInt_t m_bins_time       = 50000;
0101     Int_t m_maxRuns = 0;  // default process all
0102 
0103     // run threshold above which towers are considered to be frequently hot
0104     UInt_t m_threshold = 400;
0105     UInt_t m_threshold_low = 100;
0106 
0107     Float_t m_fraction_badChi2_threshold = 0.01;
0108     string m_detector = "CEMC";
0109     // string m_detector = "HCALIN";
0110     // string m_detector = "HCALOUT";
0111     string m_calibName_chi2 = m_detector + "_hotTowers_fracBadChi2";
0112     string m_fieldname_chi2 = "fraction";
0113 
0114     string m_calibName_hotMap = m_detector + "_BadTowerMap";
0115     string m_fieldname_hotMap = "status";
0116     string m_sigma_hotMap     = m_detector + "_sigma";
0117 
0118     Bool_t m_doHotChi2 = true;
0119     Bool_t m_doTime    = true;
0120 }
0121 
0122 void myAnalysis::init_hists() {
0123 
0124     hBadTowers   = new TH1F("hBadTowers", "Bad Towers; Tower Index; Runs", m_ntowers, -0.5, m_ntowers-0.5);
0125     hBadTowersDead = new TH1F("hBadTowersDead", "Bad Towers: Dead; Tower Index; Runs", m_ntowers, -0.5, m_ntowers-0.5);
0126     hBadTowersHot = new TH1F("hBadTowersHot", "Bad Towers: Hot; Tower Index; Runs", m_ntowers, -0.5, m_ntowers-0.5);
0127     hBadTowersHotChi2 = new TH1F("hBadTowersHotChi2", "Bad Towers: Hot Chi2; Tower Index; Runs", m_ntowers, -0.5, m_ntowers-0.5);
0128     hBadTowersCold = new TH1F("hBadTowersCold", "Bad Towers: Cold; Tower Index; Runs", m_ntowers, -0.5, m_ntowers-0.5);
0129 
0130     h2BadTowers   = new TH2F("h2BadTowers", "Bad Towers; #phi Index; #eta Index", m_bins_phi, -0.5, m_bins_phi-0.5, m_bins_eta, -0.5, m_bins_eta-0.5);
0131     h2BadTowersDead = new TH2F("h2BadTowersDead", "Bad Towers: Dead; #phi Index; #eta Index", m_bins_phi, -0.5, m_bins_phi-0.5, m_bins_eta, -0.5, m_bins_eta-0.5);
0132     h2BadTowersHot = new TH2F("h2BadTowersHot", "Bad Towers: Hot; #phi Index; #eta Index", m_bins_phi, -0.5, m_bins_phi-0.5, m_bins_eta, -0.5, m_bins_eta-0.5);
0133     h2BadTowersHotChi2 = new TH2F("h2BadTowersHotChi2", "Bad Towers: Hot Chi2; #phi Index; #eta Index", m_bins_phi, -0.5, m_bins_phi-0.5, m_bins_eta, -0.5, m_bins_eta-0.5);
0134     h2BadTowersCold = new TH2F("h2BadTowersCold", "Bad Towers: Cold; #phi Index; #eta Index", m_bins_phi, -0.5, m_bins_phi-0.5, m_bins_eta, -0.5, m_bins_eta-0.5);
0135 
0136     hHotTowerStatus = new TH1F("hHotTowerStatus", "Hot Tower Status; Status; Towers", m_nStatus, -0.5, m_nStatus-0.5);
0137 
0138     hSigmaHot        = new TH1F("hSigmaHot", "Towers (Flagged Hot #leq 50% of Runs); sigma; Counts", m_bins_sigma, m_sigma_low, m_sigma_high);
0139     hSigmaNeverHot    = new TH1F("hSigmaNeverHot", "Towers (Flagged Hot for 0% of Runs); sigma; Counts", m_bins_sigma, m_sigma_low, m_sigma_high);
0140     hSigmaFreqHot = new TH1F("hSigmaFreqHot", "Towers (Flagged Hot > 50% of Runs); sigma; Counts", m_bins_sigma, m_sigma_low, m_sigma_high);
0141 
0142     hAcceptance  = new TH1F("hAcceptance", "Acceptance; Acceptance [%]; Runs", m_bins_acceptance, 0, m_bins_acceptance);
0143     hFracHot     = new TH1F("hFracHot", "Fraction of Hot Towers; Hot Towers [%]; Runs", m_bins_acceptance, 0, m_bins_acceptance);
0144     hFracDead    = new TH1F("hFracDead", "Fraction of Dead Towers; Dead Towers [%]; Runs", m_bins_acceptance, 0, m_bins_acceptance);
0145     hFracCold    = new TH1F("hFracCold", "Fraction of Cold Towers; Cold Towers [%]; Runs", m_bins_acceptance, 0, m_bins_acceptance);
0146     hFracBadChi2 = new TH1F("hFracBadChi2", "Fraction of Bad Chi2 Towers; Bad Chi2 Towers [%]; Runs", m_bins_acceptance, 0, m_bins_acceptance);
0147 
0148     hRunStatus = new TH1F("hRunStatus", "Run Status; Status; Runs", m_bins_status, 0, m_bins_status);
0149 
0150     if(m_doTime) {
0151       TDatime d(runs[0].second.c_str());
0152       UInt_t start = d.Convert();
0153 
0154       d.Set(runs[runs.size()-1].second.c_str());
0155       UInt_t end = d.Convert();
0156 
0157       hAcceptanceVsTime = new TH1F("hAcceptanceVsTime", "Acceptance; Time; Acceptance [%]", m_bins_time, start, end);
0158       hAcceptanceVsTime->SetTitle("Acceptance; Time; Acceptance [%]");
0159       hAcceptanceVsTime->GetXaxis()->SetTimeDisplay(1);  // The X axis is a time axis
0160       hAcceptanceVsTime->GetXaxis()->SetTimeFormat("%m/%d");
0161       // hAcceptanceVsTime->GetXaxis()->SetTimeFormat("%m/%d %H");
0162 
0163       hHotVsTime = new TH1F("hHotVsTime", "Hot; Time; Hot Towers [%]", m_bins_time, start, end);
0164       hHotVsTime->GetXaxis()->SetTimeDisplay(1);  // The X axis is a time axis
0165       hHotVsTime->GetXaxis()->SetTimeFormat("%m/%d");
0166 
0167       hDeadVsTime = new TH1F("hDeadVsTime", "Dead; Time; Dead Towers [%]", m_bins_time, start, end);
0168       hDeadVsTime->GetXaxis()->SetTimeDisplay(1);  // The X axis is a time axis
0169       hDeadVsTime->GetXaxis()->SetTimeFormat("%m/%d");
0170 
0171       hColdVsTime = new TH1F("hColdVsTime", "Cold; Time; Cold Towers [%]", m_bins_time, start, end);
0172       hColdVsTime->GetXaxis()->SetTimeDisplay(1);  // The X axis is a time axis
0173       hColdVsTime->GetXaxis()->SetTimeFormat("%m/%d");
0174 
0175       hBadChi2VsTime = new TH1F("hBadChi2VsTime", "BadChi2; Time; Bad Chi2 Towers [%]", m_bins_time, start, end);
0176       hBadChi2VsTime->GetXaxis()->SetTimeDisplay(1);  // The X axis is a time axis
0177       hBadChi2VsTime->GetXaxis()->SetTimeFormat("%m/%d");
0178     }
0179 
0180     stringstream name, title;
0181     for (UInt_t i = 0; i < m_ntowers; ++i) {
0182       UInt_t key = (m_detector == "CEMC") ? TowerInfoDefs::encode_emcal(i) : TowerInfoDefs::encode_hcal(i);
0183       UInt_t etabin = TowerInfoDefs::getCaloTowerEtaBin(key);
0184       UInt_t phibin = TowerInfoDefs::getCaloTowerPhiBin(key);
0185       name.str("");
0186       title.str("");
0187       name << "h2HotTowerFrequency_" << phibin << "_" << etabin;
0188       title << "Hot Tower Run Frequency: (" << phibin << "," << etabin << "); Run; isHot";
0189 
0190       auto h2 = new TH2F(name.str().c_str(), title.str().c_str(), runs.size(), 0, runs.size(), m_bins_status, -0.5, m_bins_status - 0.5);
0191 
0192       h2HotTowerFrequency_vec.push_back(h2);
0193 
0194       name.str("");
0195       title.str("");
0196 
0197       name << "hHotTowerSigma_" << phibin << "_" << etabin;
0198       title << "Hot Tower Sigma: (" << phibin << "," << etabin << "); sigma; Runs";
0199 
0200       auto h1 = new TH1F(name.str().c_str(), title.str().c_str(), m_bins_sigma, m_sigma_low, m_sigma_high);
0201 
0202       hHotTowerSigma_vec.push_back(h1);
0203     }
0204 }
0205 
0206 Int_t myAnalysis::init(const string &input) {
0207     rc = recoConsts::instance();
0208     rc->set_StringFlag("CDB_GLOBALTAG", "ProdA_2024");
0209     CDBInterface::instance()->Verbosity(0);
0210 
0211     std::ifstream file(input);
0212 
0213     // Check if the file was successfully opened
0214     if (!file.is_open()) {
0215         cerr << "Failed to open file: " << input << endl;
0216         return 1;
0217     }
0218 
0219     string line;
0220 
0221     // loop over each run
0222     while (std::getline(file, line)) {
0223         std::istringstream lineStream(line);
0224 
0225         UInt_t runnumber;
0226         string timestamp;
0227         string date;
0228         string time;
0229         char comma;
0230 
0231         if (m_doTime && lineStream >> runnumber >> comma >> date >> time) {
0232             timestamp = date+" "+time;
0233             runs.push_back(make_pair(runnumber,timestamp));
0234         }
0235         else if (lineStream >> runnumber) {
0236             timestamp = "";
0237             runs.push_back(make_pair(runnumber,timestamp));
0238         }
0239         else {
0240             cerr << "Failed to parse line: " << line << endl;
0241             return 1;
0242         }
0243     }
0244 
0245     // Close the file
0246     file.close();
0247 
0248     init_hists();
0249 
0250     return 0;
0251 }
0252 
0253 Int_t myAnalysis::process_event(const string &outputRunStats) {
0254 
0255     UInt_t bin_run = 1;
0256     Float_t sigma_min = 9999;
0257     Float_t sigma_max = 0;
0258     UInt_t ctr_overwrite = 0;
0259     std::unordered_set<UInt_t> badSigma;
0260 
0261     ofstream file(outputRunStats);
0262     file << "Run,Acceptance,Dead,Hot,Cold,BadChi2" << endl;
0263 
0264     for(auto p : runs) {
0265         if (m_maxRuns && bin_run > m_maxRuns) break;
0266 
0267         UInt_t run = p.first;
0268         string timestamp = p.second;
0269         TDatime d;
0270 
0271         if(m_doTime) {
0272           d.Set(timestamp.c_str());
0273           cout << "Run: " << run << ", Timestamp: " << timestamp << endl;
0274         }
0275         else cout << "Run: " << run << endl;
0276 
0277         rc->set_uint64Flag("TIMESTAMP", run);
0278 
0279         Bool_t hasHotMap = true;
0280         string calibdir = CDBInterface::instance()->getUrl(m_calibName_hotMap);
0281 
0282         std::unique_ptr<CDBTTree> m_cdbttree_hotMap = nullptr;
0283 
0284         if (!calibdir.empty()) {
0285           m_cdbttree_hotMap = std::make_unique<CDBTTree>(calibdir);
0286           hRunStatus->Fill(0);
0287         }
0288         else {
0289             cout << "Run: " << run << ", Missing: " << m_calibName_hotMap << endl;
0290             runsMissingHotMap.push_back(run);
0291             hasHotMap = false;
0292         }
0293 
0294         std::unique_ptr<CDBTTree> m_cdbttree_chi2 = nullptr;
0295         Bool_t hasHotChi2 = m_doHotChi2;
0296 
0297         if(m_doHotChi2) {
0298             calibdir = CDBInterface::instance()->getUrl(m_calibName_chi2);
0299 
0300             if(!calibdir.empty()) {
0301               m_cdbttree_chi2 = std::make_unique<CDBTTree>(calibdir);
0302               hRunStatus->Fill(1);
0303             }
0304             else {
0305               cout << "Run: " << run << ", Missing: " << m_calibName_chi2 << endl;
0306               runsMissingBadChi2.push_back(run);
0307               hasHotChi2 = false;
0308             }
0309         }
0310 
0311         Bool_t hasBadSigma = false;
0312         UInt_t acceptance = 0;
0313         UInt_t bad_ctr[4] = {0};
0314 
0315         for (UInt_t channel = 0; channel < m_ntowers; ++channel) {
0316             UInt_t key = (m_detector == "CEMC") ? TowerInfoDefs::encode_emcal(channel) : TowerInfoDefs::encode_hcal(channel);
0317             UInt_t etabin = TowerInfoDefs::getCaloTowerEtaBin(key);
0318             UInt_t phibin = TowerInfoDefs::getCaloTowerPhiBin(key);
0319 
0320             Float_t fraction_badChi2 = 0;
0321             if(hasHotChi2) {
0322                 fraction_badChi2 = m_cdbttree_chi2->GetFloatValue(key, m_fieldname_chi2);
0323             }
0324 
0325             Int_t hotMap_val = 0;
0326             if (hasHotMap) {
0327                 hotMap_val = m_cdbttree_hotMap->GetIntValue(key, m_fieldname_hotMap);
0328             }
0329 
0330             // ensure hotMap_val is within range
0331             // 0: Good
0332             // 1: Dead
0333             // 2: Hot
0334             // 3: Cold
0335             if(hotMap_val > 3) {
0336               cout << "ERROR: Unknown hotMap value: " << hotMap_val << endl;
0337               return 1;
0338             }
0339 
0340             Float_t sigma_val = 0;
0341             if(!hasBadSigma && hasHotMap) {
0342               sigma_val = m_cdbttree_hotMap->GetFloatValue(key, m_sigma_hotMap);
0343               if(std::isnan(sigma_val) || std::isinf(sigma_val)) {
0344                 cout << "WARNING: sigma does not exist for channel: " << channel << ", key: " << key << endl;
0345                 hasBadSigma = true;
0346                 badSigma.insert(run);
0347               }
0348               else {
0349                 hHotTowerSigma_vec[channel]->Fill(sigma_val);
0350               }
0351             }
0352 
0353             if (hasHotMap && hotMap_val != 0) {
0354                 hBadTowers->Fill(channel);
0355                 h2BadTowers->Fill(phibin, etabin);
0356                 hHotTowerStatus->Fill(hotMap_val);
0357 
0358                 // Dead
0359                 if(hotMap_val == 1) {
0360                     hBadTowersDead->Fill(channel);
0361                     h2BadTowersDead->Fill(phibin, etabin);
0362                     ++bad_ctr[0];
0363                 }
0364                 // Hot
0365                 if(hotMap_val == 2) {
0366                     hBadTowersHot->Fill(channel);
0367                     h2BadTowersHot->Fill(phibin, etabin);
0368                     h2HotTowerFrequency_vec[channel]->SetBinContent(bin_run,2,1);
0369                     ++bad_ctr[1];
0370 
0371                     if(!hasBadSigma) {
0372                       sigma_min = min(sigma_min, sigma_val);
0373                       if(sigma_val < 100) sigma_max = max(sigma_max, sigma_val);
0374                     }
0375                 }
0376                 // Cold
0377                 if(hotMap_val == 3) {
0378                     hBadTowersCold->Fill(channel);
0379                     h2BadTowersCold->Fill(phibin, etabin);
0380                     ++bad_ctr[2];
0381                 }
0382             }
0383             // Normal Status but Hot due to badChi2
0384             else if (hasHotChi2 && fraction_badChi2 > m_fraction_badChi2_threshold) {
0385                 hotMap_val = 4; // assign a separate status for Hot due to badChi2
0386 
0387                 hBadTowers->Fill(channel);
0388                 h2BadTowers->Fill(phibin, etabin);
0389 
0390                 hBadTowersHotChi2->Fill(channel);
0391                 h2BadTowersHotChi2->Fill(phibin, etabin);
0392 
0393                 hHotTowerStatus->Fill(hotMap_val);
0394                 ++bad_ctr[3];
0395             }
0396 
0397             if(hasHotMap && hotMap_val == 0) {
0398                 h2HotTowerFrequency_vec[channel]->SetBinContent(bin_run,1,1);
0399                 ++acceptance;
0400             }
0401         }
0402 
0403         if(hasHotMap) {
0404           stringstream s;
0405           Float_t acc = (Int_t)(acceptance*1e4/m_ntowers)/100.;
0406           hAcceptance->Fill(acc);
0407 
0408           Float_t accDead = (Int_t)(bad_ctr[0]*1e4/m_ntowers)/100.;
0409           hFracDead->Fill(accDead);
0410 
0411           Float_t accHot = (Int_t)(bad_ctr[1]*1e4/m_ntowers)/100.;
0412           hFracHot->Fill(accHot);
0413 
0414           Float_t accCold = (Int_t)(bad_ctr[2]*1e4/m_ntowers)/100.;
0415           hFracCold->Fill(accCold);
0416 
0417           Float_t accBadChi2 = (Int_t)(bad_ctr[3]*1e4/m_ntowers)/100.;
0418           hFracBadChi2->Fill(accBadChi2);
0419 
0420           if(m_doTime) {
0421             Int_t bin_time = hAcceptanceVsTime->FindBin(d.Convert());
0422             if(hAcceptanceVsTime->GetBinContent(bin_time)) {
0423                cout << "WARNING: Bin Overwrite" << endl;
0424                ++ctr_overwrite;
0425             }
0426             hAcceptanceVsTime->SetBinContent(bin_time, acc);
0427 
0428             hHotVsTime->SetBinContent(bin_time, accHot);
0429             hDeadVsTime->SetBinContent(bin_time, accDead);
0430             hColdVsTime->SetBinContent(bin_time, accCold);
0431             hBadChi2VsTime->SetBinContent(bin_time, accBadChi2);
0432           }
0433           file << run << "," << acc
0434                       << "," << accDead
0435                       << "," << accHot
0436                       << "," << accCold
0437                       << "," << accBadChi2
0438                              << endl;
0439         }
0440 
0441         ++bin_run;
0442     }
0443 
0444     file.close();
0445 
0446     cout << "###################" << endl;
0447     cout << "Runs with Bad Sigma" << endl;
0448     for(auto run : badSigma) {
0449       cout << "Run: " << run << endl;
0450     }
0451 
0452     cout << "###################" << endl;
0453 
0454   // print used DB files
0455   CDBInterface::instance()->Print();
0456 
0457   UInt_t runs_processed = bin_run-runsMissingHotMap.size()-1;
0458   cout << "Sigma Min: " << sigma_min << ", Sigma Max: " << sigma_max << endl;
0459   cout << "Runs with bad sigma (nan or inf): " << badSigma.size() << endl;
0460   cout << "Runs Missing HotMap: " << runsMissingHotMap.size() << endl;
0461   cout << "Runs Missing Bad Chi2: " << runsMissingBadChi2.size() << endl;
0462   cout << "Runs with overlap time bin: " << ctr_overwrite << endl;
0463   cout << "Runs Processed: " << runs_processed << ", " << runs_processed*100./(bin_run-1) << " %" << endl;
0464 
0465   return 0;
0466 }
0467 
0468 void myAnalysis::finalize(const string &i_output, const string &outputMissingHotMap, const string &outputMissingBadChi2) {
0469 
0470   m_threshold = min(m_threshold, (UInt_t)(runs.size() - runsMissingHotMap.size()) / 2);
0471   if(m_maxRuns) m_threshold = min(m_threshold, (UInt_t)(m_maxRuns - runsMissingHotMap.size()) / 2);
0472   cout << "threshold: " << m_threshold << endl;
0473 
0474   ofstream file(outputMissingHotMap);
0475 
0476   for(auto run : runsMissingHotMap) file << run << endl;
0477 
0478   file.close();
0479 
0480   file.open(outputMissingBadChi2);
0481 
0482   for(auto run : runsMissingBadChi2) file << run << endl;
0483 
0484   file.close();
0485 
0486   TFile output(i_output.c_str(), "recreate");
0487   output.cd();
0488 
0489   hRunStatus->Write();
0490   hBadTowers->Write();
0491   hBadTowersDead->Write();
0492   hBadTowersHot->Write();
0493   hBadTowersCold->Write();
0494   hBadTowersHotChi2->Write();
0495 
0496   h2BadTowers->Write();
0497   h2BadTowersDead->Write();
0498   h2BadTowersHot->Write();
0499   h2BadTowersCold->Write();
0500   h2BadTowersHotChi2->Write();
0501 
0502   hHotTowerStatus->Write();
0503 
0504   hAcceptance->Write();
0505   hFracDead->Write();
0506   hFracHot->Write();
0507   hFracCold->Write();
0508   hFracBadChi2->Write();
0509   if(m_doTime) {
0510     hAcceptanceVsTime->Write();
0511     hDeadVsTime->Write();
0512     hHotVsTime->Write();
0513     hColdVsTime->Write();
0514     hBadChi2VsTime->Write();
0515   }
0516 
0517   output.mkdir("h2HotTowerFrequency");
0518   output.mkdir("hHotTowerSigma");
0519   output.mkdir("hHotTowerSigmaLessFreq");
0520 
0521   UInt_t ctr[5] = {0};
0522   UInt_t ctr_freq[5] = {0};
0523   for (UInt_t i = 0; i < m_ntowers; ++i) {
0524         if (hBadTowersHot->GetBinContent(i + 1))     ++ctr[0];
0525         else hSigmaNeverHot->Add(hHotTowerSigma_vec[i]);
0526 
0527         if (hBadTowersDead->GetBinContent(i + 1))    ++ctr[1];
0528         if (hBadTowersCold->GetBinContent(i + 1))    ++ctr[2];
0529         if (hBadTowersHotChi2->GetBinContent(i + 1)) ++ctr[3];
0530         if (hBadTowers->GetBinContent(i + 1))        ++ctr[4];
0531 
0532         if (hBadTowersHot->GetBinContent(i + 1) >= m_threshold) {
0533             for (UInt_t j = 1; j <= runs.size(); ++j) {
0534                 h2HotTowerFrequency_vec[i]->GetYaxis()->SetBinLabel(1, "0");
0535                 h2HotTowerFrequency_vec[i]->GetYaxis()->SetBinLabel(2, "1");
0536             }
0537             output.cd("h2HotTowerFrequency");
0538             h2HotTowerFrequency_vec[i]->Write();
0539 
0540             output.cd("hHotTowerSigma");
0541             hHotTowerSigma_vec[i]->Write();
0542 
0543             hSigmaFreqHot->Add(hHotTowerSigma_vec[i]);
0544             ++ctr_freq[0];
0545         }
0546         else if (hBadTowersHot->GetBinContent(i + 1) >= m_threshold_low) {
0547             output.cd("hHotTowerSigmaLessFreq");
0548             hHotTowerSigma_vec[i]->Write();
0549         }
0550         else hSigmaHot->Add(hHotTowerSigma_vec[i]);
0551 
0552         if (hBadTowersDead->GetBinContent(i + 1)    >= m_threshold) ++ctr_freq[1];
0553         if (hBadTowersCold->GetBinContent(i + 1)    >= m_threshold) ++ctr_freq[2];
0554         if (hBadTowersHotChi2->GetBinContent(i + 1) >= m_threshold) ++ctr_freq[3];
0555         if (hBadTowers->GetBinContent(i + 1) >= m_threshold)        ++ctr_freq[4];
0556   }
0557 
0558   output.cd();
0559   hSigmaNeverHot->Write();
0560   hSigmaHot->Write();
0561   hSigmaFreqHot->Write();
0562 
0563   cout << "Towers flagged hot: " << ctr[0] << ", " << ctr[0]*100./m_ntowers << " %" << endl;
0564   cout << "Towers flagged hot more than 50% of runs: " << ctr_freq[0] << ", " << ctr_freq[0]*100./m_ntowers << " %" << endl;
0565 
0566   cout << "Towers flagged dead: " << ctr[1] << ", " << ctr[1]*100./m_ntowers << " %" << endl;
0567   cout << "Towers flagged dead more than 50% of runs: " << ctr_freq[1] << ", " << ctr_freq[1]*100./m_ntowers << " %" << endl;
0568 
0569   cout << "Towers flagged cold: " << ctr[2] << ", " << ctr[2]*100./m_ntowers << " %" << endl;
0570   cout << "Towers flagged cold more than 50% of runs: " << ctr_freq[2] << ", " << ctr_freq[2]*100./m_ntowers << " %" << endl;
0571 
0572   cout << "Towers flagged hot chi2: " << ctr[3] << ", " << ctr[3]*100./m_ntowers << " %" << endl;
0573   cout << "Towers flagged hot chi2 more than 50% of runs: " << ctr_freq[3] << ", " << ctr_freq[3]*100./m_ntowers << " %" << endl;
0574 
0575   cout << "Towers flagged bad: " << ctr[4] << ", " << ctr[4]*100./m_ntowers << " %" << endl;
0576   cout << "Towers flagged bad more than 50% of runs: " << ctr_freq[4] << ", " << ctr_freq[4]*100./m_ntowers << " %" << endl;
0577 
0578   output.Close();
0579 }
0580 
0581 void HotTowerAnalysis(const string &input,
0582                       const string &output = "test.root",
0583                       const Bool_t doTime = true,
0584                       const Int_t maxRuns = 0,
0585                       string outputMissingHotMap = "missingHotMap.csv",
0586                       string outputMissingBadChi2 = "missingBadChi2.csv",
0587                       string outputRunStats = "acceptance.csv") {
0588 
0589     cout << "#############################" << endl;
0590     cout << "Input Parameters" << endl;
0591     cout << "input: "  << input << endl;
0592     cout << "output: " << output << endl;
0593     cout << "doTime: " << doTime << endl;
0594     cout << "outputMissingHotMap: " << outputMissingHotMap << endl;
0595     cout << "outputMissingBadChi2: " << outputMissingBadChi2 << endl;
0596     cout << "outputRunStats: " << outputRunStats << endl;
0597 
0598     myAnalysis::m_doTime = doTime;
0599     myAnalysis::m_maxRuns = maxRuns;
0600     string m_outputDir = fs::absolute(output).parent_path().string();
0601 
0602     fs::create_directories(m_outputDir);
0603 
0604     outputRunStats = m_outputDir + "/" + outputRunStats;
0605     outputMissingHotMap = m_outputDir + "/" + outputMissingHotMap;
0606     outputMissingBadChi2 = m_outputDir + "/" + outputMissingBadChi2;
0607 
0608     if (myAnalysis::m_detector == "HCALIN" || myAnalysis::m_detector == "HCALOUT") {
0609       myAnalysis::m_bins_phi = 64;
0610       myAnalysis::m_bins_eta = 24;
0611       myAnalysis::m_ntowers = myAnalysis::m_bins_phi * myAnalysis::m_bins_eta;
0612     }
0613 
0614     cout << "Detector: " << myAnalysis::m_detector << endl;
0615 
0616     Int_t ret = myAnalysis::init(input);
0617     if(ret) return;
0618 
0619     if(maxRuns) cout << "Processing: " << min((Int_t) myAnalysis::runs.size(), maxRuns) << " Runs" << endl;
0620     else        cout << "Processing: " << myAnalysis::runs.size() << " Runs" << endl;
0621     cout << "#############################" << endl;
0622 
0623     ret = myAnalysis::process_event(outputRunStats);
0624     if(ret) return;
0625 
0626     myAnalysis::finalize(output, outputMissingHotMap, outputMissingBadChi2);
0627 
0628     cout << "======================================" << endl;
0629     cout << "done" << endl;
0630     std::quick_exit(0);
0631 }
0632 
0633 # ifndef __CINT__
0634 Int_t main(Int_t argc, char* argv[]) {
0635 if(argc < 2 || argc > 8) {
0636         cout << "usage: ./hotAna inputFile [outputFile] [doTime] [maxRuns] [outputMissingHotMap] [outputMissingBadChi2] [outputRunStats]" << endl;
0637         cout << "inputFile: containing list of run numbers" << endl;
0638         cout << "outputFile: location of output file. Default: test.root." << endl;
0639         cout << "doTime: Do Time Analysis, requires input list to be <run>, <timestamp>. Default: true." << endl;
0640         cout << "maxRuns: Max runs to process (useful when testing). Default: 0 (means run all)." << endl;
0641         cout << "outputMissingHotMap: location of outputMissingHotMap file. Default: missingHotMap.csv." << endl;
0642         cout << "outputMissingBadChi2: location of outputMissingBadChi2 file. Default: missingBadChi2.csv." << endl;
0643         cout << "outputRunStats: location of outputRunStats file. Default: acceptance.csv." << endl;
0644         return 1;
0645     }
0646 
0647     string outputFile           = "test.root";
0648     Bool_t doTime               = true;
0649     Int_t maxRuns               = 0;
0650     string outputMissingHotMap  = "missingHotMap.csv";
0651     string outputMissingBadChi2 = "missingBadChi2.csv";
0652     string outputRunStats       = "acceptance.csv";
0653 
0654     if (argc >= 3) {
0655         outputFile = argv[2];
0656     }
0657     if (argc >= 4) {
0658         doTime = atoi(argv[3]);
0659     }
0660     if (argc >= 5) {
0661         maxRuns = atoi(argv[4]);
0662     }
0663     if (argc >= 6) {
0664         outputMissingHotMap = argv[5];
0665     }
0666     if (argc >= 7) {
0667         outputMissingBadChi2 = argv[6];
0668     }
0669     if (argc >= 8) {
0670         outputRunStats = argv[7];
0671     }
0672 
0673     HotTowerAnalysis(argv[1], outputFile, doTime, maxRuns, outputMissingHotMap, outputMissingBadChi2, outputRunStats);
0674 
0675     return 0;
0676 }
0677 # endif