Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-06 08:21:00

0001 #pragma once
0002 #if ROOT_VERSION_CODE >= ROOT_VERSION(6,00,0)
0003 #include <fun4all/SubsysReco.h>
0004 #include <fun4all/Fun4AllServer.h>
0005 #include <fun4all/Fun4AllInputManager.h>
0006 #include <fun4all/Fun4AllRunNodeInputManager.h>
0007 #include <fun4allraw/Fun4AllPrdfInputManager.h>
0008 #include <fun4all/Fun4AllDstInputManager.h>
0009 //#include <rawwaveformtowerbuilder/RawWaveformTowerBuilder.h>
0010 #include <fun4all/Fun4AllDstOutputManager.h>
0011 #include <cdbobjects/CDBTTree.h>  // for CDBTTree
0012 #include <ffamodules/CDBInterface.h>
0013 
0014 //#include <litecaloeval/LiteCaloEval.h>
0015 #include <caloreco/CaloTowerCalib.h>
0016 #include <caloreco/RawClusterBuilderTemplate.h>
0017 #include <calib_emc_pi0/CaloCalibEmc_Pi0.h>
0018 
0019 #include <phool/recoConsts.h>
0020 
0021 #include <fstream>
0022 #include <string>
0023 #include <TBranch.h>
0024 #include <TLeaf.h>
0025 #include <fstream>
0026 #include <TChain.h>
0027 #include <TTree.h>
0028 
0029 
0030 R__LOAD_LIBRARY(libfun4all.so)
0031 R__LOAD_LIBRARY(libfun4allraw.so)
0032 R__LOAD_LIBRARY(libcdbobjects)
0033 R__LOAD_LIBRARY(libcalo_reco.so)
0034 //R__LOAD_LIBRARY(libLiteCaloEvalTowSlope.so)
0035 R__LOAD_LIBRARY(libcalibCaloEmc_pi0.so)
0036 
0037 
0038 #endif
0039 
0040 void make_csv(const char * inf, const char * outf);
0041 TTree * GetTChainMacro(TString ifs);
0042 void make_cemcCDBTree(const char * infct, const char * outfct); 
0043 
0044 
0045 // to get files from my local area
0046 void wholeIter_Pi0EtaByEta(int nevents = -1,  
0047 const int iteration = 1 , 
0048 const char *fname = "pi0input.list", 
0049 const char * inputCDBfnameIn = "inputcdb1.root",  
0050 const char * outfileloc1 =  "./", 
0051 const char * outfileloc2 = "./")
0052 {
0053   //, const int runNumber = 0)
0054   
0055   
0056   //outfileloc1   -->  $CONDOR_SCRATCH --> throwaway temp files
0057   //outfileloc2   -->  user defined gfps to watch/monitor iterations  or debug
0058   
0059   gSystem->Load("libg4dst");
0060 
0061   std::string inputCDBfname = inputCDBfnameIn;
0062   
0063   const std::string defout1 = outfileloc1;
0064   const std::string defout2 = outfileloc2;
0065   // filenames, other than input
0066   std::string outntupfile     = defout1 + "/outntup_iter"+ std::to_string(iteration) + ".root"; 
0067   std::string outloopfile    = defout1 + "/outntup_loop" + std::to_string(iteration) + ".root"; 
0068   std::string outfitfile     = defout2 + "/outfit_"       + std::to_string(iteration) + ".root"; 
0069   std::string outcsvfile     = defout1 + "/csvoutfit" + std::to_string(iteration) + ".csv"; 
0070   std::string iterCDBfile    = defout2 + "/cdbtree"+ std::to_string(iteration) + ".root"; 
0071 
0072   // in other than the first iteration,  the previous iteration's corrections 
0073   // are needed both in CDBTree form (for input to clusters)  and in "native" CaloCalibEmc_Pi0 form 
0074   // (for aggregation of cumulative corrections across  each iteration)
0075   std::string prevoutfitfile       = defout2 + "/outfit_"       + std::to_string(iteration-1) + ".root";
0076   if (iteration == 1)
0077     prevoutfitfile = "";
0078 
0079   // for iterations other than 1, ignore input cdbfname and use previous iteration's
0080   if (iteration > 1)
0081     inputCDBfname = defout2 + "/cdbtree" + std::to_string(iteration-1) + ".root";
0082 
0083   Fun4AllServer *se = Fun4AllServer::instance();
0084 
0085   Fun4AllInputManager *in = new Fun4AllDstInputManager("in");
0086 
0087   TString infiletstr(fname);
0088   if (infiletstr.Contains(".list"))
0089     in->AddListFile(fname);
0090   else
0091     in->fileopen(fname);
0092 
0093 
0094   se->registerInputManager(in);
0095 
0096   // JF Nov23 ?? still needed ?
0097   Fun4AllInputManager *intrue2 = new Fun4AllRunNodeInputManager("DST_GEO");
0098   intrue2->AddFile("updated_geo.root");
0099   se->registerInputManager(intrue2);
0100 
0101   
0102 
0103   recoConsts *rc = recoConsts::instance();
0104   rc->set_StringFlag("CDB_GLOBALTAG","ProdA_2023"); // this points to the global tag in the CDB
0105   // The calibrations have a validity range set by the beam clock which is not read out of the prdfs as of now
0106   rc->set_uint64Flag("TIMESTAMP",0);
0107   //  rc->set_uint64Flag("TIMESTAMP",runNumber);
0108 
0109   
0110   CaloTowerCalib *calib = new CaloTowerCalib("CEMCCALIB");
0111   // calib->setCalibName("cemc_abscalib_cosmic");// these two lines are needed to choose your own calibration
0112 
0113   // JF to Blair --> below  change preferred fieldname for first towerslope iter
0114   if (iteration == 1) // blair-defined towerslope CBDtree 
0115     {
0116       calib->setFieldName("cemc_pi0_abs_calib");
0117     }
0118   else 
0119     {
0120       // leave this, at least at first, using Sijan's defined 
0121       // cdbtree outputs
0122       calib->setFieldName("cemc_pi0_abs_calib");
0123     }
0124   
0125   calib->set_directURL(inputCDBfname);
0126   
0127   se->registerSubsystem(calib);
0128   
0129 
0130    RawClusterBuilderTemplate *ClusterBuilder = new RawClusterBuilderTemplate("EmcRawClusterBuilderTemplate2");
0131   ClusterBuilder->Detector("CEMC");
0132   ClusterBuilder->Verbosity(10);
0133   ClusterBuilder->set_threshold_energy(0.030);  // This threshold should be the same as in CEMCprof_Thresh**.root file below
0134   std::string emc_prof = getenv("CALIBRATIONROOT");
0135   emc_prof += "/EmcProfile/CEMCprof_Thresh30MeV.root";
0136   ClusterBuilder->LoadProfile(emc_prof);
0137   ClusterBuilder->set_UseTowerInfo(1); // to use towerinfo objects rather than old RawTower
0138 
0139   //NOV 23 Blair : new
0140   ClusterBuilder->setInputTowerNodeName("TOWERINFO_CALIB_CEMC");
0141   ClusterBuilder->setOutputClusterNodeName("CLUSTERINFO2");  
0142 
0143   se->registerSubsystem(ClusterBuilder);
0144 
0145 
0146   CaloCalibEmc_Pi0 *eval_pi2 = new CaloCalibEmc_Pi0("dummy", outntupfile);
0147   eval_pi2->set_UseTowerInfo(1); // to use towerinfo objects rather than old RawTower
0148   eval_pi2->setInputTowerNodeName("TOWERINFO_CALIB_CEMC");
0149   eval_pi2->setInputClusterNodeName("CLUSTERINFO2");  
0150   se->registerSubsystem(eval_pi2);
0151   cout << "successful registration of pi0 " << endl;
0152 
0153   se->run(nevents);
0154   se->End();
0155 
0156   
0157   //  loop over ntuple file
0158   CaloCalibEmc_Pi0 calo_obj("CaloCalibEmc_Pi0", outloopfile);
0159   calo_obj.InitRun(0);
0160   //    TTree * intree1 =  GetTChainMacro(ifile);  // if parallelized
0161   calo_obj.Loop(nevents, outntupfile);
0162   calo_obj.End(0);
0163 
0164 
0165   //now fit:
0166   CaloCalibEmc_Pi0 calo_obj2("CaloCalibEmc_Pi0","");
0167   //for test using previous
0168   //  outloopfile    = "/sphenix/user/jfrantz/thdatNov22/pimacs/cutsg33_cpi1_21813.root"; 
0169 
0170   calo_obj2.Get_Histos(outloopfile.c_str() , outfitfile.c_str());   
0171   calo_obj2.Fit_Histos_Etas96(prevoutfitfile.c_str());   // see note at beginning during filenames settings
0172   calo_obj2.End(0);
0173   
0174   // now gen CDBTree file using Sijan's methods
0175   make_csv(outfitfile.c_str(), outcsvfile.c_str());
0176   make_cemcCDBTree(outcsvfile.c_str(), iterCDBfile.c_str());
0177   
0178 
0179   se->PrintTimer();
0180 
0181   //  gSystem->Exit(0);
0182 }
0183 
0184 
0185 TTree* GetTChainMacro(TString ifile="")
0186 {
0187     ifstream inFile;
0188     inFile.open(ifile, ios::in); // open to read
0189     if (!inFile)
0190     {
0191         cerr << "Unable to open file:\n ";
0192         exit(1);
0193     }
0194 
0195     TChain *pitree = new TChain("_eventTree");
0196     string root_file;
0197     int lines=0;
0198     while (std::getline(inFile, root_file))
0199     {
0200         pitree->Add(root_file.c_str());
0201         lines += 1;
0202     }
0203     printf("total lines: %d\n",lines);
0204     inFile.close();
0205     
0206     return (TTree *) pitree;
0207 
0208  }
0209 
0210 
0211 void make_csv( const char * input_filechar, const char * output_filechar)
0212 {
0213   // input file
0214   const string input_file = input_filechar;
0215   
0216   // output file
0217   const string output_file = output_filechar;   
0218     
0219             
0220   // Open the ROOT file
0221   TFile *file = TFile::Open(input_file.c_str(), "READ");
0222 
0223   // Create an output CSV file
0224   std::ofstream csvFile(output_file.c_str());
0225         
0226   // Get the TTree
0227   TTree *tree = (TTree*)file->Get("nt_corrVals");
0228   
0229   // Get the number of entries (rows) in the TTree
0230     Long64_t nEntries = tree->GetEntries();
0231     
0232     if (nEntries != 24576) {std::cout << "The number of entries does not match with EMCal. The number is " << nEntries << " ." << std::endl;}
0233     
0234     
0235     // Get the number of branches (columns) in the TTree
0236     TObjArray *branches = tree->GetListOfBranches();
0237     int nbranches = branches->GetEntries();
0238     
0239 
0240     // write the header for CSV file
0241     csvFile << "phibin" << " , " << "etabin" << " , " << "calib-factor" << std::endl;
0242     
0243     for (int j = 0; j < nEntries; j++) // for each entry in the Branches
0244       {
0245     tree->GetEntry(j);
0246     
0247     int ieta, iphi;
0248     float agg_corr_val;
0249     
0250     for (int b = 0; b < nbranches; ++b) 
0251       {
0252             TBranch *branch = (TBranch*)branches->At(b);
0253             TLeaf *leaf = (TLeaf*)branch->GetListOfLeaves()->At(0);
0254         
0255                         if (b == 0) {ieta = leaf->GetValue(0);}
0256                         if (b == 1) {iphi = leaf->GetValue(0);}
0257                         if (b == 3) {agg_corr_val = leaf->GetValue(0);}
0258       }
0259     csvFile << iphi << " , " << ieta << " , " << agg_corr_val << "\n";  
0260       }
0261     
0262     // Clean up
0263     csvFile.close();
0264     file->Close();
0265     delete file;
0266     std::cout << "All Done making csvfile:" << output_file << std::endl;
0267     //    return 0;
0268 }
0269 
0270 
0271 void make_cemcCDBTree(const char * input_file1, // csv file to be supplied
0272               const char * output_file1) // output root file having CDB Ttree
0273 {
0274   // file location, input file and output file
0275     
0276 
0277   const string input_file = input_file1;
0278   const string output_file = output_file1;
0279   
0280   std::cout << "start making CDBTree: " << output_file1 << std::endl;
0281   
0282   float corrArray[256][96];
0283   string line, cell1, cell2, cell3;
0284   
0285   // Open the CSV file
0286   ifstream file(input_file.c_str());
0287 
0288     // read header row and do nothing
0289     std::getline(file, line);
0290   while(getline(file, line))
0291     {
0292     stringstream lineStream(line);
0293 
0294     getline(lineStream, cell1, ',');
0295         //std::cout << "phi: " << std::stoi(cell1) << " ";
0296 
0297     getline(lineStream, cell2, ',');
0298         //std::cout << "eta: " << std::stoi(cell2) << " ";
0299 
0300         getline(lineStream, cell3, ',');
0301         //std::cout << "corr: " << std::stof(cell3) << std::endl;
0302 
0303         corrArray[std::stoi(cell1)][std::stoi(cell2)] = std::stof(cell3);
0304   }
0305   file.close();
0306 
0307     /* to check if my array was correctly filled    
0308      * --------------------------------------
0309     for (int iphi=0; iphi<256; iphi++)
0310     {
0311         for (int ieta=0; ieta<96; ieta++)
0312         {
0313             std::cout << iphi << " " << ieta << " " << corrArray[iphi][ieta] << std::endl;
0314         }
0315     }
0316     */  
0317     
0318     CDBTTree *cdbttree = new CDBTTree(output_file.c_str());
0319   for(int iphi=0; iphi<256; iphi++){
0320     for(int ieta=0; ieta<96; ieta++){
0321       int key = iphi + (ieta << 16);
0322       cdbttree->SetFloatValue(key, "cemc_pi0_abs_calib", corrArray[iphi][ieta]);
0323     }
0324   }
0325  
0326   cdbttree->Commit();
0327   //cdbttree->Print();
0328   cdbttree->WriteCDBTTree();
0329   delete cdbttree;
0330   std::cout << "cdb success " << std::endl;
0331     //  gSystem->Exit(0);
0332 }