Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-12-18 09:22:12

0001 #include <cdbobjects/CDBTTree.h>
0002 #include <calobase/TowerInfoDefs.h>
0003 
0004 #include <TH2.h>
0005 
0006 R__LOAD_LIBRARY(libcdbobjects)
0007 R__LOAD_LIBRARY(libcalo_io.so)
0008 
0009 void histToCaloCDBTree(const std::string &outputfile, const std::string &fieldName, int icalo, TH2* hist)
0010 {
0011   int neta;
0012   int nphi;
0013 
0014   if (icalo != 0 && icalo != 1) return;
0015 
0016   if (icalo == 0)
0017   {
0018     neta = 96;
0019     nphi = 256;
0020   }
0021   if (icalo == 1)
0022   {
0023     neta = 24;
0024     nphi = 64;
0025   }
0026 
0027   CDBTTree* cdbttree = new CDBTTree(outputfile);
0028 
0029   float mean = 0;
0030   int count = 0;
0031 
0032   for (int ie = 0; ie < neta; ie++)
0033   {
0034     for (int ip = 0; ip < nphi; ip++)
0035     {
0036       unsigned int key;
0037       if (icalo == 0) key = TowerInfoDefs::encode_emcal(ie, ip);
0038       if (icalo == 1) key = TowerInfoDefs::encode_hcal(ie, ip);
0039       float val = hist->GetBinContent(ie + 1, ip + 1);
0040       cdbttree->SetFloatValue(key, fieldName, val);
0041       mean += val;
0042       count++;
0043     }
0044   }
0045 
0046   std::cout << "Writing " << outputfile.c_str() << "   with mean=" << mean / count << std::endl;
0047   cdbttree->Commit();
0048   cdbttree->WriteCDBTTree();
0049   // cdbttree->Print();
0050   delete cdbttree;
0051 }
0052 
0053 TH2* CaloCDBTreeToHist(const std::string &inputfile, const std::string &fieldName, int icalo)
0054 {
0055   CDBTTree* cdbttree = new CDBTTree(inputfile);
0056   if (!cdbttree)
0057   {
0058     std::cout << "CaloCDBTreeToHist: could not get cdb tree" << std::endl;
0059     return nullptr;
0060   }
0061 
0062   int neta;
0063   int nphi;
0064 
0065   if (icalo != 0 && icalo != 1) return nullptr;
0066 
0067   if (icalo == 0)
0068   {
0069     neta = 96;
0070     nphi = 256;
0071   }
0072   if (icalo == 1)
0073   {
0074     neta = 24;
0075     nphi = 64;
0076   }
0077 
0078   TH2* h2 = new TH2F("h_temp", "", neta, 0, neta, nphi, 0, nphi);
0079 
0080   for (int ie = 0; ie < neta; ie++)
0081   {
0082     for (int ip = 0; ip < nphi; ip++)
0083     {
0084       unsigned int key;
0085       if (icalo == 0) key = TowerInfoDefs::encode_emcal(ie, ip);
0086       if (icalo == 1) key = TowerInfoDefs::encode_hcal(ie, ip);
0087 
0088       float val = cdbttree->GetFloatValue(key, fieldName);
0089       h2->SetBinContent(ie + 1, ip + 1, val);
0090     }
0091   }
0092 
0093   return h2;
0094 }
0095 
0096 void swtichFieldName(const std::string &infile, const std::string &outfile, const std::string &infield, const std::string &outfield, int icalo)
0097 {
0098   TH2* h = CaloCDBTreeToHist(infile, infield, icalo);
0099   histToCaloCDBTree(outfile, outfield, icalo, h);
0100 }
0101 
0102 // makes a default calibration (all 0s are set to average calibration factor)
0103 // input: input cdb file name, output cdb file name, field name, calo index (0==CEMC 1==HCAL)
0104 void makeDefaultCalib(const std::string &infile, const std::string &outfile, const std::string &infield, int icalo)
0105 {
0106   TH2* h = CaloCDBTreeToHist(infile, infield, icalo);
0107 
0108   int c = 0;
0109   float avg = 0;
0110   for (int ie = 1; ie < h->GetXaxis()->GetNbins() + 1; ie++)
0111   {
0112     for (int ip = 1; ip < h->GetYaxis()->GetNbins() + 1; ip++)
0113     {
0114       float val = h->GetBinContent(ie, ip);
0115       if (val != 0)
0116       {
0117         avg += val;
0118         c++;
0119       }
0120     }
0121   }
0122   avg /= c;
0123   for (int ie = 1; ie < h->GetXaxis()->GetNbins() + 1; ie++)
0124   {
0125     for (int ip = 1; ip < h->GetYaxis()->GetNbins() + 1; ip++)
0126     {
0127       float val = h->GetBinContent(ie, ip);
0128       if (val == 0) h->SetBinContent(ie, ip, avg);
0129     }
0130   }
0131 
0132   histToCaloCDBTree(outfile, infield, icalo, h);
0133 }