Back to home page

sPhenix code displayed by LXR

 
 

    


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

0001 #include <string>
0002 #include"tsc_cos_merge.C"
0003 
0004 struct TowerData {
0005     int tower_ieta;
0006     int tower_iphi;
0007     float calib_factor;
0008     int run_range_low;
0009     int run_range_high;
0010 };
0011 void loadCSV(const std::string& filename, std::vector<TowerData>& data);
0012 std::vector<std::vector<float>> getTowersForRun(const std::vector<TowerData>& data, int run_number);
0013 
0014 
0015 //make sure TS_list has relative or full paths to actual root files
0016 void genFinalCalib(const char *runs = "cos_runs.txt")
0017 {
0018 
0019   ///////////////////////////////////
0020   // Load the half hieght data from the CSV file
0021   ///////////////////////////////////////
0022   std::vector<TowerData> hh_data;
0023   loadCSV("halfHieghtList.csv", hh_data);
0024 
0025 
0026   /////////////////////////////////////////////////
0027   // Get cosmic calibrations
0028   ///////////////////////////////////////
0029   ifstream runlist(runs);
0030   std::string line;
0031   vector<int> cosmic_runnumbers;
0032   while(getline(runlist,line))
0033   {
0034     cosmic_runnumbers.push_back(stoi(line));
0035 
0036     //std::string oh_out = "fitResults/oh_cosmicsMerged_fitted_" + line + ".root";
0037     std::string oh_out = "../tsc_cos_comb/fitResults/avgCorr.root";
0038     std::string ih_out = "fitResults/ih_cosmicsMerged_fitted_" + line + ".root";
0039 
0040     string cosmic_CDB_file_oh = Form("/sphenix/u/bseidlitz/work/macros/calibrations/calo/hcal_towerSlope_y2/cosmicCalibFiles/ohcal_cosmic_calibration_%s.root",line.c_str());
0041 
0042     tsc_cos_merge(oh_out,cosmic_CDB_file_oh,Form("cdbFiles/ohcal_cdb_tsc_cos_calib_%s.root",line.c_str()),0);
0043 
0044   }
0045 
0046 
0047   ///////////////////////////////////////
0048   // temperature correction run-by-run
0049   ///////////////////////////////////////
0050   ifstream runlistBeam("runList.txt");
0051   set<int> tempset;
0052   while(getline(runlistBeam,line)){
0053     int runnumber = stoi(line);
0054     tempset.insert(runnumber);
0055   }
0056   vector<int> runnumbersBeam(tempset.begin(),tempset.end());
0057    
0058   TFile* ftemp = new TFile("../fileQA/output/anaOut.root");
0059 
0060   TH1F* h_runnumbers_temp = (TH1F*) ftemp->Get("runNumberHist");
0061   TH1F* h_temp_run = (TH1F*) ftemp->Get("h_temp_run");
0062 
0063 
0064   ///////////////////////////
0065   // get cosmic run temps
0066   ///////////////////////////
0067   // get the temperature of the cosmics runs
0068   // could be improved, right now it uses the cosest beam run
0069   vector<float> cosmic_temps;
0070   for (int cos_run : cosmic_runnumbers){
0071     int minDelta = 1000000;
0072     int minRun = 0;
0073     float cos_temp = 0;
0074     for( int it=0; it<h_runnumbers_temp->GetNbinsX(); it++){   
0075       int delta = abs(cos_run - h_runnumbers_temp->GetBinContent(it));
0076       if (delta < minDelta  && h_temp_run->GetBinContent(it) > 10){
0077         minRun = h_runnumbers_temp->GetBinContent(it);
0078         minDelta = delta;
0079         cos_temp =h_temp_run->GetBinContent(it);
0080       }
0081     }
0082     cosmic_temps.push_back(cos_temp);
0083     cout << "cosmics run " << cos_run << "  temp="  << cos_temp << " size=" << cosmic_temps.size() << endl;
0084   }
0085 
0086 
0087   ///////////////////////////////////////
0088   // Generate the run-by-run calibrations
0089   ///////////////////////////////////////
0090   // This follows the logic laid out in the note
0091   // cosmics calibration is the base
0092   // the tsc is the used to adjust the cosmic calib
0093   // then the temperture difference between the cosmic
0094   // run and the run of interest to correct the 
0095   // adjusted cosmics for the rinal calibration
0096   for(int irb=0; irb<runnumbersBeam.size(); irb++)
0097   {
0098     int runnumber = runnumbersBeam[irb];  
0099 
0100     int runnumber_last;
0101     if (irb+1  < runnumbersBeam.size() ) runnumber_last = runnumbersBeam[irb+1]-1;
0102     else runnumber_last = 53880;
0103 
0104     cout << "----------------------------------" << endl;
0105     cout << "starting run " << runnumber << endl;
0106 
0107     int asoCosmic = 0;
0108     int minCosDelta = 1000000;
0109     float asoCosmicTemp = 0;
0110     for(int ic=0; ic< cosmic_runnumbers.size(); ic++){
0111        int cos_run = cosmic_runnumbers[ic]; 
0112        int delta = runnumber - cos_run;
0113        if (minCosDelta  > delta && delta > 0){
0114          asoCosmic     = cos_run; 
0115          asoCosmicTemp =  cosmic_temps[ic]; 
0116          minCosDelta = delta;
0117        }
0118     }
0119     cout << "closeset cosmic run " << asoCosmic << endl;  
0120 
0121 
0122     for( int it=0; it<h_runnumbers_temp->GetNbinsX(); it++){   
0123       if(runnumber == h_runnumbers_temp->GetBinContent(it)){
0124          float temp = h_temp_run->GetBinContent(it);
0125          float alpha = 0.045;
0126          float deltaT =  temp - asoCosmicTemp; 
0127 
0128          cout << "of this run = " << temp << "   temp of cosmic run =" << asoCosmicTemp << endl;
0129 
0130          float correction = 1+ deltaT*alpha;
0131          if (temp < 10) continue;
0132          cout << "correction=" << correction << endl;
0133 
0134          string cdbFileName = Form("cdbFiles/cdb_ohcal_tsc_temp_cosmic_%d.root",runnumber);
0135          //string cosmic_CDB_file_oh = Form("/sphenix/u/bseidlitz/work/macros/calibrations/calo/hcal_towerSlope_y2/cosmicCalibFiles/ohcal_cosmic_calibration_%d.root",asoCosmic);
0136          string cosmic_CDB_file_oh = Form("cdbFiles/ohcal_cdb_tsc_cos_calib_%d.root",asoCosmic);
0137 
0138           // gen calibration file with cosmics, tsc and temp correction
0139           // but no half hieght corrections
0140          //genCdbCorr(correction,cosmic_CDB_file_oh,cdbFileName,0); 
0141 
0142          // with half hieght correction
0143          std::vector<std::vector<float>> hh_towersForRun = getTowersForRun(hh_data, runnumber);
0144          string cdbFileName_hh = Form("final_ohcal_calib_y2_namceChange/cdb_ohcal_tsc_temp_cosmic_hh_%d_%d.root",runnumber,runnumber_last);
0145          genCdbCorr_HH(correction,cosmic_CDB_file_oh,cdbFileName_hh,0,hh_towersForRun); 
0146       }
0147     }
0148   }
0149 
0150 
0151 
0152 }//end macro
0153 
0154 
0155 
0156 void loadCSV(const std::string& filename, std::vector<TowerData>& data) {
0157     std::ifstream file(filename);
0158     std::string line;
0159     if(!file) {cout << endl << "ccan't find file" << endl; exit; }
0160 
0161     while (std::getline(file, line)) {
0162         std::stringstream ss(line);
0163         TowerData towerData;
0164 
0165         // Parse the line and fill the struct
0166         char comma;  // To consume the commas between values
0167         ss >> towerData.tower_ieta >> comma
0168            >> towerData.tower_iphi >> comma
0169            >> towerData.calib_factor >> comma
0170            >> towerData.run_range_low >> comma
0171            >> towerData.run_range_high;
0172 
0173         // Store the parsed data
0174         data.push_back(towerData);
0175     }
0176 }
0177 
0178 
0179 std::vector<std::vector<float>> getTowersForRun(const std::vector<TowerData>& data, int run_number) {
0180     std::vector<std::vector<float>> towersForRun;
0181 
0182     // Loop through all the entries and check if the run_number is within the range
0183     for (const auto& towerData : data) {
0184         if (run_number > towerData.run_range_low && run_number < towerData.run_range_high) {
0185             // If it matches the range, store the tower data
0186             towersForRun.push_back({static_cast<float>(towerData.tower_ieta),
0187                                     static_cast<float>(towerData.tower_iphi),
0188                                     towerData.calib_factor});
0189         }
0190     }
0191 
0192     return towersForRun;
0193 }
0194