Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-05 08:11:10

0001 // -- c++ includes --
0002 #include <filesystem>
0003 #include <fstream>
0004 #include <iomanip>
0005 #include <iostream>
0006 #include <string>
0007 
0008 // -- root includes --
0009 #include <TFile.h>
0010 #include <TProfile2D.h>
0011 #include <TLatex.h>
0012 
0013 #include <calobase/TowerInfoDefs.h>
0014 #include <cdbobjects/CDBTTree.h>
0015 
0016 using std::cerr;
0017 using std::cout;
0018 using std::endl;
0019 using std::max;
0020 using std::min;
0021 using std::ofstream;
0022 using std::pair;
0023 using std::string;
0024 using std::stringstream;
0025 using std::to_string;
0026 using std::vector;
0027 
0028 namespace myAnalysis
0029 {
0030 
0031   void histToCaloCDBTree(string outputfile, string fieldName, Int_t icalo, TProfile2D *hist);
0032   void analyze(const string &output);
0033 
0034   // utils
0035   pair<string, string> getRunDataset(const string &input);
0036   Int_t readHists(const string &input);
0037 
0038   pair<string, string> run_dataset;
0039 
0040   TProfile2D *h_CaloValid_cemc_etaphi_badChi2 = nullptr;
0041   TProfile2D *h_CaloValid_ihcal_etaphi_badChi2 = nullptr;
0042   TProfile2D *h_CaloValid_ohcal_etaphi_badChi2 = nullptr;
0043 
0044   TProfile2D *h_CaloValid_cemc_etaphi_time_raw = nullptr;
0045   TProfile2D *h_CaloValid_ihcal_etaphi_time_raw = nullptr;
0046   TProfile2D *h_CaloValid_ohcal_etaphi_time_raw = nullptr;
0047 
0048   Int_t cemc_bins_eta = 96;
0049   Int_t cemc_bins_phi = 256;
0050   Int_t hcal_bins_eta = 24;
0051   Int_t hcal_bins_phi = 64;
0052 }  // namespace myAnalysis
0053 
0054 pair<string, string> myAnalysis::getRunDataset(const string &input)
0055 {
0056   string basename = std::filesystem::path(input).filename();
0057   string run = basename.substr(0, basename.find("_"));
0058   string dataset = basename.substr(basename.find("_") + 1, basename.size() - basename.find("_") - 6);
0059   return make_pair(run, dataset);
0060 }
0061 
0062 Int_t myAnalysis::readHists(const string &input)
0063 {
0064   // Create an input stream
0065   std::ifstream file(input);
0066 
0067   // Check if the file was successfully opened
0068   if (!file.is_open())
0069   {
0070     cerr << "Failed to open file list: " << input << endl;
0071     return 1;
0072   }
0073 
0074   run_dataset = getRunDataset(input);
0075 
0076   cout << "Reading Hists" << endl;
0077   cout << "======================================" << endl;
0078 
0079   delete h_CaloValid_cemc_etaphi_badChi2;
0080   delete h_CaloValid_ihcal_etaphi_badChi2;
0081   delete h_CaloValid_ohcal_etaphi_badChi2;
0082 
0083   delete h_CaloValid_cemc_etaphi_time_raw;
0084   delete h_CaloValid_ihcal_etaphi_time_raw;
0085   delete h_CaloValid_ohcal_etaphi_time_raw;
0086 
0087   h_CaloValid_cemc_etaphi_badChi2 = new TProfile2D("cemc_etaphi_badChi2","", cemc_bins_eta, 0, cemc_bins_eta, cemc_bins_phi, 0, cemc_bins_phi);
0088   h_CaloValid_ihcal_etaphi_badChi2 = new TProfile2D("ihcal_etaphi_badChi2","", hcal_bins_eta, 0, hcal_bins_eta, hcal_bins_phi, 0, hcal_bins_phi);
0089   h_CaloValid_ohcal_etaphi_badChi2 = new TProfile2D("ohcal_etaphi_badChi2","", hcal_bins_eta, 0, hcal_bins_eta, hcal_bins_phi, 0, hcal_bins_phi);
0090 
0091   h_CaloValid_cemc_etaphi_time_raw = new TProfile2D("cemc_etaphi_time_raw","", cemc_bins_eta, 0, cemc_bins_eta, cemc_bins_phi, 0, cemc_bins_phi);
0092   h_CaloValid_ihcal_etaphi_time_raw = new TProfile2D("ihcal_etaphi_time_raw","", hcal_bins_eta, 0, hcal_bins_eta, hcal_bins_phi, 0, hcal_bins_phi);
0093   h_CaloValid_ohcal_etaphi_time_raw = new TProfile2D("ohcal_etaphi_time_raw","", hcal_bins_eta, 0, hcal_bins_eta, hcal_bins_phi, 0, hcal_bins_phi);
0094 
0095   string line;
0096   while (std::getline(file, line))
0097   {
0098     cout << "Reading File: " << line << endl;
0099     auto tf = TFile::Open(line.c_str());
0100     auto h = static_cast<TProfile2D*>(tf->Get("h_CaloValid_cemc_etaphi_badChi2"));
0101 
0102     if (h) h_CaloValid_cemc_etaphi_badChi2->Add(h);
0103 
0104     h = static_cast<TProfile2D*>(tf->Get("h_CaloValid_ihcal_etaphi_badChi2"));
0105 
0106     if (h) h_CaloValid_ihcal_etaphi_badChi2->Add(h);
0107 
0108     h = static_cast<TProfile2D*>(tf->Get("h_CaloValid_ohcal_etaphi_badChi2"));
0109 
0110     if (h) h_CaloValid_ohcal_etaphi_badChi2->Add(h);
0111 
0112     h = static_cast<TProfile2D*>(tf->Get("h_CaloValid_cemc_etaphi_time_raw"));
0113 
0114     if (h) h_CaloValid_cemc_etaphi_time_raw->Add(h);
0115 
0116     h = static_cast<TProfile2D*>(tf->Get("h_CaloValid_ihcal_etaphi_time_raw"));
0117 
0118     if (h) h_CaloValid_ihcal_etaphi_time_raw->Add(h);
0119 
0120     h = static_cast<TProfile2D*>(tf->Get("h_CaloValid_ohcal_etaphi_time_raw"));
0121 
0122     if (h) h_CaloValid_ohcal_etaphi_time_raw->Add(h);
0123 
0124     tf->Close();
0125   }
0126 
0127   // Close the file
0128   file.close();
0129 
0130   return 0;
0131 }
0132 
0133 void myAnalysis::histToCaloCDBTree(string outputfile, string fieldName, Int_t icalo, TProfile2D *hist)
0134 {
0135   UInt_t neta, nphi;
0136 
0137   if (icalo != 0 && icalo != 1) return;
0138 
0139   if (icalo == 0)
0140   {
0141     neta = 96;
0142     nphi = 256;
0143   }
0144   if (icalo == 1)
0145   {
0146     neta = 24;
0147     nphi = 64;
0148   }
0149 
0150   CDBTTree *cdbttree = new CDBTTree(outputfile);
0151 
0152   Double_t mean = 0;
0153   Int_t count = 0;
0154 
0155   for (UInt_t ie = 0; ie < neta; ie++)
0156   {
0157     for (UInt_t ip = 0; ip < nphi; ip++)
0158     {
0159       UInt_t key;
0160       if (icalo == 0) key = TowerInfoDefs::encode_emcal(ie, ip);
0161       if (icalo == 1) key = TowerInfoDefs::encode_hcal(ie, ip);
0162       Float_t val = static_cast<Float_t>(hist->GetBinContent(static_cast<Int_t>(ie) + 1, static_cast<Int_t>(ip) + 1));
0163       cdbttree->SetFloatValue(static_cast<Int_t>(key), fieldName, val);
0164       mean += val;
0165       count++;
0166     }
0167   }
0168 
0169   cout << "Writing " << outputfile.c_str() << "   with mean=" << mean / count << endl;
0170   cdbttree->Commit();
0171   cdbttree->WriteCDBTTree();
0172   // cdbttree->Print();
0173   delete cdbttree;
0174 }
0175 
0176 void myAnalysis::analyze(const string &output)
0177 {
0178   stringstream t;
0179 
0180   t.str("");
0181 
0182   string run = run_dataset.first;
0183   string dataset = run_dataset.second;
0184 
0185   cout << "Processing: Run: " << run << ", Dataset: " << dataset << endl;
0186 
0187   t << output << "/" << run << "_" << dataset;
0188 
0189   std::filesystem::create_directories(t.str());
0190 
0191   string detector = "CEMC";
0192   // fracBadChi2
0193   string payloadName = t.str() + "/" + detector + "_hotTowers_fracBadChi2" + "_" + dataset + "_" + run + ".root";
0194   if (h_CaloValid_cemc_etaphi_badChi2) histToCaloCDBTree(payloadName, "fraction", 0, h_CaloValid_cemc_etaphi_badChi2);
0195   // time
0196   payloadName = t.str() + "/" + detector + "_meanTime" + "_" + dataset + "_" + run + ".root";
0197   if (h_CaloValid_cemc_etaphi_time_raw) histToCaloCDBTree(payloadName, "time", 0, h_CaloValid_cemc_etaphi_time_raw);
0198 
0199   detector = "HCALIN";
0200   // fracBadChi2
0201   payloadName = t.str() + "/" + detector + "_hotTowers_fracBadChi2" + "_" + dataset + "_" + run + ".root";
0202   if (h_CaloValid_ihcal_etaphi_badChi2) histToCaloCDBTree(payloadName, "fraction", 1, h_CaloValid_ihcal_etaphi_badChi2);
0203   // time
0204   payloadName = t.str() + "/" + detector + "_meanTime" + "_" + dataset + "_" + run + ".root";
0205   if (h_CaloValid_ihcal_etaphi_time_raw) histToCaloCDBTree(payloadName, "time", 1, h_CaloValid_ihcal_etaphi_time_raw);
0206 
0207   detector = "HCALOUT";
0208   // fracBadChi2
0209   payloadName = t.str() + "/" + detector + "_hotTowers_fracBadChi2" + "_" + dataset + "_" + run + ".root";
0210   if (h_CaloValid_ohcal_etaphi_badChi2) histToCaloCDBTree(payloadName, "fraction", 1, h_CaloValid_ohcal_etaphi_badChi2);
0211   // time
0212   payloadName = t.str() + "/" + detector + "_meanTime" + "_" + dataset + "_" + run + ".root";
0213   if (h_CaloValid_ohcal_etaphi_time_raw) histToCaloCDBTree(payloadName, "time", 1, h_CaloValid_ohcal_etaphi_time_raw);
0214 }
0215 
0216 void genStatus(const string &input, const string &output = "output")
0217 {
0218   cout << "#############################" << endl;
0219   cout << "Run Parameters" << endl;
0220   cout << "input: " << input << endl;
0221   cout << "output: " << output << endl;
0222   cout << "#############################" << endl;
0223 
0224   // merges individal qa into one per run
0225   myAnalysis::readHists(input);
0226   myAnalysis::analyze(output);
0227 }
0228 
0229 #ifndef __CINT__
0230 Int_t main(Int_t argc, const char* const argv[])
0231 {
0232   if (argc < 2 || argc > 3)
0233   {
0234     cout << "usage: ./genStatus input [output]" << endl;
0235     cout << "input: input list" << endl;
0236     cout << "output: output directory" << endl;
0237     return 1;
0238   }
0239 
0240   string output = "output";
0241 
0242   if (argc >= 3)
0243   {
0244     output = argv[2];
0245   }
0246 
0247   genStatus(argv[1], output);
0248 
0249   cout << "======================================" << endl;
0250   cout << "done" << endl;
0251   return 0;
0252 }
0253 #endif