Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-05 08:15:34

0001 #include "LiteCaloEval.h"
0002 
0003 #include <calobase/RawTower.h>
0004 #include <calobase/RawTowerContainer.h>
0005 #include <calobase/RawTowerGeomContainer.h>
0006 #include <calobase/TowerInfo.h>
0007 #include <calobase/TowerInfoContainer.h>
0008 
0009 #include <calotrigger/TriggerAnalyzer.h>
0010 
0011 #include <fun4all/Fun4AllReturnCodes.h>
0012 #include <fun4all/SubsysReco.h>
0013 
0014 #include <phool/getClass.h>
0015 #include <phool/phool.h>
0016 
0017 #include <RtypesCore.h>  // for Double_t
0018 #include <TCanvas.h>
0019 #include <TF1.h>
0020 #include <TFile.h>
0021 #include <TGraph.h>
0022 #include <TGraphErrors.h>
0023 #include <TH1.h>
0024 #include <TH2.h>
0025 #include <TH3.h>
0026 #include <TLatex.h>
0027 #include <TLegend.h>
0028 #include <TStyle.h>
0029 #include <TSystem.h>
0030 
0031 #include <boost/format.hpp>
0032 
0033 #include <cmath>
0034 #include <cstdlib>
0035 #include <iostream>
0036 #include <map>      // for _Rb_tree_const_iterator
0037 #include <utility>  // for pair
0038 
0039 class RawTowerGeom;
0040 
0041 namespace
0042 {
0043   TGraph *LCE_grff{nullptr};
0044   /// This function is used for the histo fitting process. x is a 1d array that holds xaxis values.
0045   /// par is an array of 1d array of parameters we set our fit function to. So p[0] = p[1] = 1 unless otherwise noted
0046   double LCE_fitf(const Double_t *x, const Double_t *par)
0047   {
0048     return par[0] * LCE_grff->Eval(x[0] * par[1], nullptr, "S");
0049   }
0050 }  // namespace
0051 
0052 //____________________________________________________________________________..
0053 LiteCaloEval::LiteCaloEval(const std::string &name, const std::string &caloname, const std::string &filename)
0054   : SubsysReco(name)
0055   , _caloname(caloname)
0056   , _filename(filename)
0057 {
0058 }
0059 
0060 //____________________________________________________________________________..
0061 int LiteCaloEval::InitRun(PHCompositeNode * /*topNode*/)
0062 {
0063   std::cout << "In InitRun " << std::endl;
0064 
0065   // just quit if we forgot to set the calorimeter type
0066   if (calotype == LiteCaloEval::NONE)
0067   {
0068     std::cout << Name() << ": No Calo Type set" << std::endl;
0069     gSystem->Exit(1);
0070   }
0071 
0072   _ievent = 0;
0073 
0074   trigAna = new TriggerAnalyzer();
0075 
0076   cal_output = new TFile(_filename.c_str(), "RECREATE");
0077 
0078   h_event = new TH1F("h_event", "", 1, 0, 1);
0079 
0080   if (calotype == LiteCaloEval::HCALIN)
0081   {
0082     hcalin_energy_eta = new TH2F("hcalin_energy_eta", "hcalin energy eta", 100, 0, 10, 24, -0.5, 23.5);
0083     hcalin_e_eta_phi = new TH3F("hcalin_e_eta_phi", "hcalin e eta phi", 60, 0, 6, 24, -0.5, 23.5, 64, -0.5, 63.5);
0084 
0085     /// create tower histos
0086     for (int i = 0; i < 24; i++)
0087     {
0088       for (int j = 0; j < 64; j++)
0089       {
0090         std::string hist_name = "hcal_in_eta_" + std::to_string(i) + "_phi_" + std::to_string(j);
0091 
0092         hcal_in_eta_phi[i][j] = new TH1F(hist_name.c_str(), "Hcal_in_energy", 40000, 0, 4);
0093         hcal_in_eta_phi[i][j]->SetXTitle("Energy [GeV]");
0094       }
0095     }
0096 
0097     // create eta slice histos
0098     for (int i = 0; i < 25; i++)
0099     {
0100       std::string hist_name = "hcalin_eta_" + std::to_string(i);
0101 
0102       if (i < 24)
0103       {
0104         hcalin_eta[i] = new TH1F(hist_name.c_str(), "hcalin eta's", 4000, 0, 4.);
0105         hcalin_eta[i]->SetXTitle("Energy [GeV]");
0106       }
0107       else
0108       {
0109         hcalin_eta[i] = new TH1F(hist_name.c_str(), "hcalin eta's", 40000, 0, 4.);
0110         hcalin_eta[i]->SetXTitle("Energy [GeV]");
0111       }
0112     }
0113   }
0114 
0115   else if (calotype == LiteCaloEval::HCALOUT)
0116   {
0117     hcalout_energy_eta = new TH2F("hcalout_energy_eta", "hcalout energy eta", 100, 0, 10, 24, 0.5, 23.5);
0118     hcalout_e_eta_phi = new TH3F("hcalout_e_eta_phi", "hcalout e eta phi", 100, 0, 10, 24, -0.5, 23.5, 64, -0.5, 63.5);
0119 
0120     /// create tower histos
0121     for (int i = 0; i < 24; i++)
0122     {
0123       for (int j = 0; j < 64; j++)
0124       {
0125         std::string hist_name = "hcal_out_eta_" + std::to_string(i) + "_phi_" + std::to_string(j);
0126 
0127         hcal_out_eta_phi[i][j] = new TH1F(hist_name.c_str(), "Hcal_out energy", 10000, 0, 10);
0128         hcal_out_eta_phi[i][j]->SetXTitle("Energy [GeV]");
0129       }
0130     }
0131 
0132     /// create eta slice histos
0133     for (int i = 0; i < 25; i++)
0134     {
0135       std::string hist_name = "hcalout_eta_" + std::to_string(i);
0136       if (i < 24)
0137       {
0138         hcalout_eta[i] = new TH1F(hist_name.c_str(), "hcalout eta's", 10000, 0, 10);
0139         hcalout_eta[i]->SetXTitle("Energy [GeV]");
0140       }
0141       else
0142       {
0143         hcalout_eta[i] = new TH1F(hist_name.c_str(), "hcalout eta's", 100000, 0, 10);
0144         hcalout_eta[i]->SetXTitle("Energy [GeV]");
0145       }
0146     }
0147   }
0148 
0149   else if (calotype == LiteCaloEval::CEMC)
0150   {
0151     /// create tower histos
0152     for (int i = 0; i < 96; i++)
0153     {
0154       for (int j = 0; j < 256; j++)
0155       {
0156         std::string hist_name = "emc_ieta" + std::to_string(i) + "_phi" + std::to_string(j);
0157 
0158         cemc_hist_eta_phi[i][j] = new TH1F(hist_name.c_str(), "Hist_ieta_phi_leaf(e)", 400, 0, 2);
0159         cemc_hist_eta_phi[i][j]->SetXTitle("Energy [GeV]");
0160       }
0161     }
0162 
0163     // create eta slice histos
0164     for (int i = 0; i < 97; i++)
0165     {
0166       gStyle->SetOptFit(1);
0167       std::string b = "eta_" + std::to_string(i);
0168       eta_hist[i] = new TH1F(b.c_str(), "eta and all phi's", 400, 0, 2);
0169       eta_hist[i]->SetXTitle("Energy [GeV]");
0170     }
0171 
0172     // make 2d histo
0173     energy_eta_hist = new TH2F("energy_eta_hist", "energy eta and all phi", 100, 0, 10, 96, -0.5, 95.5);
0174 
0175     // make 3d histo
0176     e_eta_phi = new TH3F("e_eta_phi", "e v eta v phi", 100, 0, 10, 96, -0.5, 95.5, 256, -0.5, 255.5);
0177   }
0178 
0179   return Fun4AllReturnCodes::EVENT_OK;
0180 }
0181 
0182 //____________________________________________________________________________..
0183 int LiteCaloEval::process_event(PHCompositeNode *topNode)
0184 {
0185   if (_ievent % 100 == 0)
0186   {
0187     std::cout << "LiteCaloEval::process_event(PHCompositeNode *topNode) Processing Event " << _ievent << std::endl;
0188   }
0189 
0190   //--------------------------- trigger and GL1-------------------------------//
0191   trigAna->decodeTriggers(topNode);
0192 
0193   if (reqMinBias && trigAna->didTriggerFire(12) == false)
0194   {
0195     _ievent++;
0196     return Fun4AllReturnCodes::EVENT_OK;
0197   }
0198 
0199   h_event->Fill(0);
0200 
0201   //---------------------------- Get geometry -------------------------------------//
0202   // raw tower container
0203   std::string towernode = "TOWER_CALIB_" + _caloname;
0204   RawTowerContainer *towers = nullptr;
0205   RawTowerGeomContainer *towergeom = nullptr;
0206 
0207   // for tower energy
0208   RawTowerContainer::ConstRange begin_end;
0209   RawTowerContainer::ConstIterator rtiter;
0210 
0211   if (m_UseTowerInfo < 1)
0212   {
0213     towers = findNode::getClass<RawTowerContainer>(topNode, towernode);
0214     if (!towers)
0215     {
0216       std::cout << PHWHERE << " ERROR: Can't find " << towernode << std::endl;
0217       exit(-1);
0218     }
0219 
0220     // raw tower geom container
0221     std::string towergeomnode = "TOWERGEOM_" + _caloname;
0222     towergeom = findNode::getClass<RawTowerGeomContainer>(topNode, towergeomnode);
0223     if (!towergeom)
0224     {
0225       std::cout << PHWHERE << " ERROR: Can't find " << towergeomnode << std::endl;
0226       exit(-1);
0227     }
0228 
0229     begin_end = towers->getTowers();
0230     rtiter = begin_end.first;
0231   }
0232 
0233   if (mode && _ievent < 2)
0234   {
0235     std::cout << "mode is set " << std::endl;
0236   }
0237 
0238   TowerInfoContainer *towerinfos = nullptr;
0239 
0240   // if using towerinfo create a tower object
0241   if (m_UseTowerInfo)
0242   {
0243     towernode = _inputnodename;
0244 
0245     towerinfos = findNode::getClass<TowerInfoContainer>(topNode, towernode);
0246 
0247     if (!towerinfos)
0248     {
0249       std::cout << PHWHERE << " ERROR: Can't find " << towernode << std::endl;
0250       exit(-1);
0251     }
0252   }
0253 
0254   // getting size of node
0255   unsigned int nchannels = -1;
0256 
0257   if (m_UseTowerInfo)
0258   {
0259     nchannels = towerinfos->size();
0260   }
0261   else
0262   {
0263     nchannels = towers->size();
0264   }
0265 
0266   TowerInfo *tower_info;
0267   RawTower *tower;
0268   RawTowerGeom *tower_geom;
0269 
0270   for (unsigned int channel = 0; channel < nchannels; channel++)
0271   {
0272     if (!m_UseTowerInfo && rtiter == begin_end.second)
0273     {
0274       std::cout << "ERROR: Recheck iteration process with rtiter variable" << std::endl;
0275     }
0276 
0277     float e = -1.0;  // tower energy
0278     unsigned int ieta = 99999;
0279     unsigned int iphi = 99999;
0280 
0281     if (m_UseTowerInfo)
0282     {
0283       tower_info = towerinfos->get_tower_at_channel(channel);
0284       unsigned int towerkey = towerinfos->encode_key(channel);
0285 
0286       e = tower_info->get_energy();
0287       ieta = towerinfos->getTowerEtaBin(towerkey);
0288       iphi = towerinfos->getTowerPhiBin(towerkey);
0289 
0290       if (!tower_info->get_isGood())
0291       {
0292         continue;
0293       }
0294     }
0295 
0296     else
0297     {
0298       tower = rtiter->second;
0299 
0300       tower_geom = towergeom->get_tower_geometry(tower->get_id());
0301 
0302       if (!tower_geom)
0303       {
0304         std::cout << PHWHERE << " ERROR: Can't find tower geometry for this tower hit: ";
0305         tower->identify();
0306         exit(-1);
0307       }
0308 
0309       e = tower->get_energy();
0310       ieta = tower->get_bineta();
0311       iphi = tower->get_binphi();
0312 
0313     }  // end else for rawtower mode
0314 
0315     if (ieta > 95 || iphi > 256)
0316     {
0317       // rough check for all calos uing the largest emcal
0318       std::cout << "ieta or iphi not set correctly/ was less than 0 " << std::endl;
0319       break;
0320     }
0321 
0322     if (e <= 0)
0323     {
0324       continue;
0325     }
0326 
0327     if (calotype == LiteCaloEval::CEMC)
0328     {
0329       // mode variable is used for simulations only. Creates a decalibration in energy
0330       if (mode)
0331       {
0332         int ket = ieta / 8;
0333         int llet = ket % 6;
0334         int pket = iphi / 8;
0335         int ppkket = pket % 3;
0336 
0337         e *= 0.88 + llet * 0.04 - 0.01 + 0.01 * ppkket;
0338       }
0339 
0340       cemc_hist_eta_phi[ieta][iphi]->Fill(e);
0341 
0342       eta_hist[96]->Fill(e);
0343 
0344       eta_hist[ieta]->Fill(e);
0345 
0346       energy_eta_hist->Fill(e, ieta);
0347 
0348       e_eta_phi->Fill(e, ieta, iphi);
0349     }
0350 
0351     else if (calotype == LiteCaloEval::HCALOUT)
0352     {
0353       if (mode)
0354       {
0355         int ket = ieta / 2;
0356 
0357         int llet = ket % 6;
0358         e *= 0.945 + llet * 0.02;
0359         int pket = iphi / 4;
0360         if (pket % 2 == 0)
0361         {
0362           e *= 1.03;
0363         }
0364       }
0365 
0366       hcal_out_eta_phi[ieta][iphi]->Fill(e);
0367 
0368       hcalout_eta[24]->Fill(e);
0369 
0370       hcalout_eta[ieta]->Fill(e);
0371 
0372       hcalout_energy_eta->Fill(e, ieta);
0373 
0374       hcalout_e_eta_phi->Fill(e, ieta, iphi);
0375     }
0376 
0377     else if (calotype == LiteCaloEval::HCALIN)
0378     {
0379       if (mode)
0380       {
0381         int ket = ieta / 2;
0382 
0383         int llet = ket % 6;
0384         e *= 0.945 + llet * 0.02;
0385         int pket = iphi / 4;
0386         if (pket % 2 == 0)
0387         {
0388           e *= 1.03;
0389         }
0390       }
0391 
0392       hcal_in_eta_phi[ieta][iphi]->Fill(e);
0393 
0394       hcalin_eta[24]->Fill(e);
0395 
0396       hcalin_eta[ieta]->Fill(e);
0397 
0398       hcalin_energy_eta->Fill(e, ieta);
0399 
0400       hcalin_e_eta_phi->Fill(e, ieta, iphi);
0401     }
0402 
0403     if (!m_UseTowerInfo)
0404     {
0405       ++rtiter;
0406     }
0407 
0408   }  // end of for loop
0409 
0410   _ievent++;
0411 
0412   return Fun4AllReturnCodes::EVENT_OK;
0413 }
0414 
0415 //____________________________________________________________________________..
0416 int LiteCaloEval::End(PHCompositeNode * /*topNode*/)
0417 {
0418   cal_output->cd();
0419 
0420   std::cout << " writing lite calo file" << std::endl;
0421 
0422   cal_output->Write();
0423 
0424   return Fun4AllReturnCodes::EVENT_OK;
0425 }
0426 
0427 /// infile histos, outfile is output file name
0428 void LiteCaloEval::Get_Histos(const std::string &infile, const std::string &outfile)
0429 {
0430   std::cout << "Getting histograms... " << std::endl;
0431 
0432   if (infile.empty())
0433   {
0434     std::cout << "Need an input file to run LiteCaloEval::Get_Histos" << std::endl;
0435     exit(0);
0436   }
0437 
0438   if (!outfile.empty())
0439   {
0440     std::string ts = "cp ";
0441     ts += infile;
0442     ts += " ";
0443     ts += outfile;
0444     gSystem->Exec(ts.c_str());
0445     f_temp = new TFile(outfile.c_str(), "UPDATE");
0446   }
0447 
0448   else
0449   {
0450     f_temp = new TFile(infile.c_str());
0451   }
0452 
0453   int max_ieta = 96;
0454   if (calotype != LiteCaloEval::CEMC)
0455   {
0456     max_ieta = 24;
0457   }
0458 
0459   int max_iphi = 256;
0460   if (calotype != LiteCaloEval::CEMC)
0461   {
0462     max_iphi = 64;
0463   }
0464 
0465   /// start of eta loop
0466   for (int i = 0; i < max_ieta + 1; i++)
0467   {
0468     std::string b = "eta_" + std::to_string(i);
0469 
0470     if (calotype == LiteCaloEval::HCALOUT)
0471     {
0472       b.insert(0, "hcalout_");
0473     }
0474     else if (calotype == LiteCaloEval::HCALIN)
0475     {
0476       b.insert(0, "hcalin_");
0477     }
0478 
0479     /// holds the eta slice of histos
0480     TH1 *heta_temp{nullptr};
0481     f_temp->GetObject(b.c_str(), heta_temp);
0482 
0483     if (i == 0)
0484     {
0485       std::cout << "Old bin width for eta slice " << heta_temp->GetBinWidth(1) << std::endl;
0486     }
0487 
0488     // rebin histogram
0489     heta_temp->Rebin(binwidth / heta_temp->GetBinWidth(1));
0490 
0491     if (i == 0)
0492     {
0493       std::cout << "New bin width for eta slice " << heta_temp->GetBinWidth(1) << std::endl;
0494     }
0495 
0496     if (!heta_temp && i == 0)
0497     {
0498       std::cout << " warning hist " << b << " not found" << std::endl;
0499     }
0500 
0501     /// assign the eta slice histo to an array (these arrays are private members in LCE.h)
0502     eta_hist[i] = heta_temp;
0503 
0504     if (calotype == LiteCaloEval::HCALOUT)
0505     {
0506       hcalout_eta[i] = heta_temp;
0507     }
0508     else if (calotype == LiteCaloEval::HCALIN)
0509     {
0510       hcalin_eta[i] = heta_temp;
0511     }
0512 
0513     if (!(i < max_ieta))
0514     {
0515       continue;
0516     }
0517 
0518     /// start of phi loop
0519     for (int j = 0; j < max_iphi; j++)
0520     {
0521       /// create string to hold name of individual tower
0522       std::string hist_name_p = "emc_ieta" + std::to_string(i) + "_phi" + std::to_string(j);
0523 
0524       if (calotype == LiteCaloEval::HCALOUT)
0525       {
0526         hist_name_p = "hcal_out_eta_" + std::to_string(i) + "_phi_" + std::to_string(j);
0527       }
0528       else if (calotype == LiteCaloEval::HCALIN)
0529       {
0530         hist_name_p = "hcal_in_eta_" + std::to_string(i) + "_phi_" + std::to_string(j);
0531       }
0532 
0533       /// heta_tempp holds tower histogram
0534       TH1 *heta_tempp{nullptr};
0535       f_temp->GetObject(hist_name_p.c_str(), heta_tempp);
0536 
0537       if (i == 0 && j == 0)
0538       {
0539         std::cout << "Old bin width for towers " << heta_tempp->GetBinWidth(1) << std::endl;
0540       }
0541 
0542       // rebin histogram
0543       heta_tempp->Rebin(binwidth / heta_tempp->GetBinWidth(1));
0544 
0545       if (i == 0 && j == 0)
0546       {
0547         std::cout << "New bin width for towers " << heta_tempp->GetBinWidth(1) << std::endl;
0548       }
0549 
0550       if (!heta_tempp && i == 0)
0551       {
0552         std::cout << " warning hist " << hist_name_p.c_str() << " not found" << std::endl;
0553       }
0554 
0555       /// assign heta_tempp to array of tower histos
0556       cemc_hist_eta_phi[i][j] = heta_tempp;
0557 
0558       if (calotype == LiteCaloEval::HCALOUT)
0559       {
0560         hcal_out_eta_phi[i][j] = heta_tempp;
0561       }
0562       else if (calotype == LiteCaloEval::HCALIN)
0563       {
0564         hcal_in_eta_phi[i][j] = heta_tempp;
0565       }
0566     }  // phi loop
0567   }  // eta loop
0568 
0569   /*
0570   f_temp->Close();
0571   f_temp = nullptr;
0572   delete f_temp;
0573   */
0574 
0575   std::cout << "Grabbed all histograms." << std::endl;
0576 
0577 }  // end Get_Histos f'n
0578 
0579 void LiteCaloEval::FitRelativeShifts(LiteCaloEval *ref_lce, int modeFitShifts)
0580 {
0581   bool onlyEta = false;  // will determine if you only run over eta slices or not
0582 
0583   if (fitmin < 0.001)
0584   {
0585     fitmin = 0.15;
0586   }
0587   if (fitmax < 0.001)
0588   {
0589     fitmax = 1.3;
0590   }
0591 
0592   float par_value[96] = {0};  // gain value used only in the outer for loop
0593   float par_err[96] = {0};    // error on the gain
0594   float eta_value[96] = {0};  // ieta
0595   float eta_err[96] = {0};    // ieta err. just will be zero.
0596 
0597   if (f_temp)
0598   {
0599     f_temp->cd();
0600   }
0601 
0602   /// fitting - calls the LCE_fitf function at beginning of module
0603   TF1 *f1 = new TF1("myexpo", LCE_fitf, 0.1, 10, 2);
0604   f1->SetParameters(1.0, 1.0);
0605 
0606   /// looks at 1's palce of mFS parameter, if onlyEta = false will run phi loop (individual towers) below
0607   if (modeFitShifts % 10 == 1)
0608   {
0609     onlyEta = true;
0610     std::cout << "Running only eta slice mode....." << std::endl;
0611   }
0612 
0613   /// nsmooth is an automatic smoothing of histos
0614   int nsmooth = 1;
0615 
0616   /// if want more smoothing, look at tens place in mFS. If mFS=10, then addSmooth=1, and then there will be 2 total smoothings
0617   int addSmooth = modeFitShifts % 100 / 10;
0618   if (addSmooth)
0619   {
0620     nsmooth += addSmooth;
0621   }
0622 
0623   /// flag that will run fits at tower level to get shifts, or will run at ring level and fit towers to eta slice histo
0624   bool flag_fit_rings = false;
0625 
0626   // look at 100's place of mFS. Runs the eta slice flattening mode. If false, runs gain tracing mode
0627   if (modeFitShifts % 1000 / 100 == 1)
0628   {
0629     flag_fit_rings = true;
0630   }
0631 
0632   int max_ieta = 96;
0633   if (calotype != LiteCaloEval::CEMC)
0634   {
0635     max_ieta = 24;
0636   }
0637 
0638   int max_iphi = 256;
0639   if (calotype != LiteCaloEval::CEMC)
0640   {
0641     max_iphi = 64;
0642   }
0643 
0644   /// histo to hold the returned fit parameter value
0645   TH2 *corrPat = new TH2F("corrPat", "", max_ieta, 0, max_ieta, max_iphi, 0, max_iphi);
0646   corrPat->SetXTitle("#eta bin");
0647   corrPat->SetYTitle("#phi bin");
0648 
0649   TH2 *h2_failQA = new TH2F("h2_failQA", "", max_ieta, 0, max_ieta, max_iphi, 0, max_iphi);
0650 
0651   // 1d histo for gain shift values
0652   TH1 *gainvals = new TH1F("gainvals", "Towerslope Correction Values", 10000, 0, 10);
0653   gainvals->SetXTitle("Gain Shift Value");
0654   gainvals->SetYTitle("Counts");
0655 
0656   // 1d histo for gain shift error
0657   TH1 *h_gainErr = new TH1F("h_gainErr", "Towerslope Corrections Errors", 1000, 0, 1);
0658   h_gainErr->SetXTitle("error");
0659   h_gainErr->SetYTitle("Counts");
0660 
0661   int minbin = 0;
0662   int maxbin = max_ieta;
0663 
0664   if (m_myminbin > -1)
0665   {
0666     minbin = m_myminbin;
0667   }
0668   if (m_mymaxbin > -1)
0669   {
0670     maxbin = m_mymaxbin;
0671   }
0672 
0673   /// assign hnewf the eta slice histos when running in Gain Trace mode
0674   TH1 *hnewf = nullptr;
0675 
0676   /// Start of loop for eta slices
0677   for (int i = minbin; i < maxbin; i++)
0678   {
0679     std::cout << "===============================" << std::endl;
0680     std::cout << "Fitting towers in eta slice " << i << std::endl;
0681 
0682     /// hold the gain value, gain error, respectively from the fit
0683     double ieta_gain;
0684     double ieta_gain_err;
0685 
0686     /// names for eta slice histo
0687     std::string myClnm = "newhc_eta" + std::to_string(i);
0688 
0689     /// dummy indexing for string name
0690     int iik = i;
0691 
0692     /// will hold eta slice histo clone but with current tower being fitted removed from the eta slice
0693     TH1 *cleanEtaRef = nullptr;
0694     std::string cleanEta = "cleanEtaRef_";
0695 
0696     if (calotype == LiteCaloEval::CEMC)
0697     {
0698       if (flag_fit_rings == true)
0699       {
0700         cleanEta += std::to_string(iik);
0701 
0702         cleanEtaRef = (TH1 *) eta_hist[iik]->Clone(cleanEta.c_str());
0703       }
0704 
0705       else
0706       {
0707         hnewf = (TH1 *) ref_lce->eta_hist[iik]->Clone(myClnm.c_str());
0708       }
0709     }
0710 
0711     else if (calotype == LiteCaloEval::HCALOUT)
0712     {
0713       if (flag_fit_rings == true)
0714       {
0715         cleanEta += std::to_string(iik);
0716 
0717         cleanEtaRef = (TH1 *) hcalout_eta[iik]->Clone(cleanEta.c_str());
0718 
0719         // remove towers from eta slice reference associated with the chimney (i < 4) and support ring in high eta region(i>19)
0720         if (i < 4 || i > 19)
0721         {
0722           for (int phiCH = 14; phiCH < 20; phiCH++)
0723           {
0724             cleanEtaRef->Add((TH1 *) hcal_out_eta_phi[i][phiCH], -1.0);
0725           }
0726         }
0727       }
0728 
0729       else
0730       {
0731         hnewf = (TH1 *) ref_lce->hcalout_eta[iik]->Clone(myClnm.c_str());
0732       }
0733     }
0734 
0735     else if (calotype == LiteCaloEval::HCALIN)
0736     {
0737       if (flag_fit_rings == true)
0738       {
0739         cleanEta += std::to_string(iik);
0740 
0741         cleanEtaRef = (TH1 *) hcalin_eta[iik]->Clone(cleanEta.c_str());
0742       }
0743 
0744       else
0745       {
0746         hnewf = (TH1 *) ref_lce->hcalin_eta[iik]->Clone(myClnm.c_str());
0747       }
0748     }
0749 
0750     if (flag_fit_rings == true)
0751     {
0752       cleanEtaRef->Smooth(nsmooth);
0753 
0754       LCE_grff = new TGraph(cleanEtaRef);
0755     }
0756 
0757     else
0758     {
0759       hnewf->Smooth(nsmooth);
0760 
0761       LCE_grff = new TGraph(hnewf);
0762     }
0763 
0764     /// this function will be used to fit eta slice histos
0765     TF1 *f2f = nullptr;
0766 
0767     /// Fit the eta slices w/ our user defined f'n and then get the tf1
0768     if (calotype == LiteCaloEval::CEMC)
0769     {
0770       if (nsmooth > 1)
0771       {
0772         eta_hist[i]->Smooth(nsmooth);
0773       }
0774 
0775       eta_hist[i]->Fit("myexpo", "Q", "", fitmin, fitmax);
0776 
0777       f2f = eta_hist[i]->GetFunction("myexpo");
0778     }
0779 
0780     else if (calotype == LiteCaloEval::HCALOUT)
0781     {
0782       if (nsmooth > 1)
0783       {
0784         hcalout_eta[i]->Smooth(nsmooth);
0785       }
0786 
0787       hcalout_eta[i]->Fit("myexpo", "Q", "", fitmin, fitmax);
0788 
0789       f2f = hcalout_eta[i]->GetFunction("myexpo");
0790     }
0791 
0792     else if (calotype == LiteCaloEval::HCALIN)
0793     {
0794       if (nsmooth > 1)
0795       {
0796         hcalin_eta[i]->Smooth(nsmooth);
0797       }
0798 
0799       hcalin_eta[i]->Fit("myexpo", "Q", "", fitmin, fitmax);
0800 
0801       f2f = hcalin_eta[i]->GetFunction("myexpo");
0802     }
0803 
0804     if (!f2f)
0805     {
0806       std::cout << "Warning, f2f is null!" << std::endl;
0807       exit(-1);
0808     }
0809     else
0810     {
0811       ieta_gain = f2f->GetParameter(1);
0812     }
0813 
0814     ieta_gain_err = f2f->GetParError(1);
0815 
0816     par_value[i] = ieta_gain;
0817     par_err[i] = ieta_gain_err;
0818     eta_value[i] = i;
0819     eta_err[i] = 0.0;
0820 
0821     /// This if statement enables the tower fits to run
0822     if (onlyEta == true)
0823     {
0824       continue;
0825     }
0826 
0827     /////////////////////
0828     // QA -- Blair
0829     if (doQA)
0830     {
0831       if (flag_fit_rings == true)
0832       {
0833         if (calotype == LiteCaloEval::CEMC)
0834         {
0835           for (int j = 0; j < max_iphi; j++)
0836           {
0837             cleanEtaRef->Add((TH1 *) cemc_hist_eta_phi[i][j], -1.0);
0838 
0839             bool qa_res = spec_QA(cemc_hist_eta_phi[i][j], cleanEtaRef, true);
0840             if (qa_res == false)
0841             {
0842               std::cout << "failed QA " << i << "," << j << std::endl;
0843               cemc_hist_eta_phi[i][j]->Reset();
0844               h2_failQA->Fill(i, j);
0845             }
0846             else
0847             {
0848               cleanEtaRef->Add((TH1 *) cemc_hist_eta_phi[i][j], 1.0);
0849             }
0850           }
0851         }
0852       }
0853     }
0854     /****************************************************
0855     This is the nested forloop to start tower material
0856     ****************************************************/
0857 
0858     for (int j = 0; j < max_iphi; j++)
0859     {
0860       /// names of tower histo for cloning. used in gain trace mode
0861       std::string myClnmp = "newhc_eta" + std::to_string((1000 * (i + 2)) + j);
0862 
0863       /// histo to hold tower clone
0864       TH1 *hnewfp = nullptr;
0865 
0866       // check to see if tower in question is part of the chimney/high eta support ring
0867 
0868       bool _isChimney = chk_isChimney(i, j);
0869 
0870       if (calotype == LiteCaloEval::CEMC)
0871       {
0872         // skip tower if there are no entries
0873         if (!(cemc_hist_eta_phi[i][j]->GetEntries()))
0874         {
0875           std::cout << "No entries in EMCAL tower histogram (" << i << "," << j << "). Skipping fitting." << std::endl;
0876           continue;
0877         }
0878 
0879         if (flag_fit_rings == true)
0880         {
0881           cleanEtaRef->Add((TH1 *) cemc_hist_eta_phi[i][j], -1.0);
0882         }
0883         else
0884         {
0885           hnewfp = (TH1 *) ref_lce->cemc_hist_eta_phi[i][j]->Clone(myClnmp.c_str());
0886         }
0887       }
0888 
0889       else if (calotype == LiteCaloEval::HCALOUT)
0890       {
0891         // skip tower if there are no entries
0892         if (!(hcal_out_eta_phi[i][j]->GetEntries()))
0893         {
0894           std::cout << "No entries in OHCAL tower histogram (" << i << "," << j << "). Skipping fitting." << std::endl;
0895           continue;
0896         }
0897 
0898         if (flag_fit_rings == true)
0899         {
0900           // check to see if tower is part of chimney/high eta support. If it isnt, proceed to remove that tower from eta ref. This is to ensure we dont remove towers in these regions  twice from eta ref bc they already were in L717
0901 
0902           if (!_isChimney)
0903           {
0904             cleanEtaRef->Add((TH1 *) hcal_out_eta_phi[i][j], -1.0);
0905           }
0906         }
0907 
0908         else
0909         {
0910           hnewfp = (TH1 *) ref_lce->hcal_out_eta_phi[i][j]->Clone(myClnmp.c_str());
0911         }
0912       }
0913 
0914       else if (calotype == LiteCaloEval::HCALIN)
0915       {
0916         // skip tower if there are no entries
0917         if (!(hcal_in_eta_phi[i][j]->GetEntries()))
0918         {
0919           std::cout << "No entries in IHCAL tower histogram(" << i << "," << j << "). Skipping fitting." << std::endl;
0920           continue;
0921         }
0922 
0923         if (flag_fit_rings == true)
0924         {
0925           cleanEtaRef->Add((TH1 *) hcal_in_eta_phi[i][j], -1.0);
0926         }
0927         else
0928         {
0929           hnewfp = (TH1 *) ref_lce->hcal_in_eta_phi[i][j]->Clone(myClnmp.c_str());
0930         }
0931       }
0932 
0933       /*
0934         If false make tgraph out of tower and send to be fit with fit fuction.
0935         If true the towers are actually fit against the eta ring. This happends bc "myexpo" uses a tgraph
0936         to fit, and if we dont make LCE_grff for a tower, myexpo uses the latest version of LCE_grff, which is an eta slice tgraph
0937       */
0938 
0939       if (flag_fit_rings == false)
0940       {
0941         hnewfp->Smooth(nsmooth);
0942         LCE_grff = new TGraph(hnewfp);
0943       }
0944 
0945       if (flag_fit_rings == true)
0946       {
0947         // cleanEtaRef->Smooth(nsmooth);
0948         LCE_grff = new TGraph(cleanEtaRef);
0949       }
0950 
0951       /// make tf1 that will hold the resulting fit of towers
0952       TF1 *f2f2 = nullptr;
0953 
0954       // need to scale the reference histogram to allow it to start at an amplitude similar/at tower that is to be fit
0955       if (calotype == LiteCaloEval::CEMC)
0956       {
0957         double scaleP0 = cemc_hist_eta_phi[i][j]->Integral(cemc_hist_eta_phi[i][j]->FindBin(fitmin), cemc_hist_eta_phi[i][j]->FindBin(fitmax));
0958 
0959         if (flag_fit_rings == 1)
0960         {
0961           scaleP0 /= cleanEtaRef->Integral(cleanEtaRef->FindBin(fitmin), cleanEtaRef->FindBin(fitmax));
0962         }
0963         else
0964         {
0965           scaleP0 /= hnewfp->Integral(hnewfp->FindBin(fitmin), hnewfp->FindBin(fitmax));
0966         }
0967 
0968         f1->SetParameters(scaleP0, 1.0);
0969 
0970         cemc_hist_eta_phi[i][j]->Fit("myexpo", "Q", "", fitmin, fitmax);
0971 
0972         f2f2 = cemc_hist_eta_phi[i][j]->GetFunction("myexpo");
0973 
0974         // add back the just fitted tower to the eta slice reference
0975         if (flag_fit_rings == true)
0976         {
0977           cleanEtaRef->Add((TH1 *) cemc_hist_eta_phi[i][j], 1.0);
0978         }
0979       }
0980 
0981       else if (calotype == LiteCaloEval::HCALOUT)
0982       {
0983         double scaleP0 = hcal_out_eta_phi[i][j]->Integral(hcal_out_eta_phi[i][j]->FindBin(fitmin), hcal_out_eta_phi[i][j]->FindBin(fitmax));
0984 
0985         if (flag_fit_rings == 1)
0986         {
0987           scaleP0 /= cleanEtaRef->Integral(cleanEtaRef->FindBin(fitmin), cleanEtaRef->FindBin(fitmax));
0988         }
0989         else
0990         {
0991           scaleP0 /= hnewfp->Integral(hnewfp->FindBin(fitmin), hnewfp->FindBin(fitmax));
0992         }
0993 
0994         f1->SetParameters(scaleP0, 1.0);
0995 
0996         hcal_out_eta_phi[i][j]->Fit("myexpo", "Q", "", fitmin, fitmax);
0997 
0998         f2f2 = hcal_out_eta_phi[i][j]->GetFunction("myexpo");
0999 
1000         if (flag_fit_rings == true)
1001         {
1002           if (!_isChimney)
1003           {
1004             cleanEtaRef->Add((TH1 *) hcal_out_eta_phi[i][j], 1.0);
1005           }
1006         }
1007       }
1008 
1009       else if (calotype == LiteCaloEval::HCALIN)
1010       {
1011         double scaleP0 = hcal_in_eta_phi[i][j]->Integral(hcal_in_eta_phi[i][j]->FindBin(fitmin), hcal_in_eta_phi[i][j]->FindBin(fitmax));
1012 
1013         if (flag_fit_rings == 1)
1014         {
1015           scaleP0 /= cleanEtaRef->Integral(cleanEtaRef->FindBin(fitmin), cleanEtaRef->FindBin(fitmax));
1016         }
1017 
1018         else
1019         {
1020           scaleP0 /= hnewfp->Integral(hnewfp->FindBin(fitmin), hnewfp->FindBin(fitmax));
1021         }
1022 
1023         f1->SetParameters(scaleP0, 1.0);
1024 
1025         hcal_in_eta_phi[i][j]->Fit("myexpo", "Q", "", fitmin, fitmax);
1026 
1027         f2f2 = hcal_in_eta_phi[i][j]->GetFunction("myexpo");
1028 
1029         if (flag_fit_rings == true)
1030         {
1031           cleanEtaRef->Add((TH1 *) hcal_in_eta_phi[i][j], 1.0);
1032         }
1033       }
1034 
1035       float correction = 1;
1036       float corrErr    = 1;
1037 
1038       if(f2f2){
1039         correction =  f2f2->GetParameter(1);
1040         corrErr = f2f2->GetParError(1);
1041       }
1042 
1043       double errProp = corrErr / (correction * correction);
1044 
1045       /// fill corrpat, which has returned fit values from towers
1046       /// e.x. if you want to view the histo that is, say, eta=12, phi = 1,
1047       /// you really need to draw the histo with eta=11,phi=0 bc of the i+1, j+1
1048 
1049       corrPat->SetBinContent(i + 1, j + 1, 1 / correction);
1050 
1051       corrPat->SetBinError(i + 1, j + 1, errProp);
1052 
1053       gainvals->Fill(1.0 / correction);
1054 
1055       h_gainErr->Fill(errProp);
1056 
1057     }  // end of inner forloop (phi)
1058 
1059   }  // end of outter forloop (eta)
1060 
1061   // create graph that plots eta slice par values (this is only when looping over eta slices - not towers)
1062   TGraphErrors g1(max_ieta, eta_value, par_value, eta_err, par_err);
1063   g1.SetTitle("fitted shifts; eta; p1");
1064   g1.SetMarkerStyle(20);
1065   g1.Draw("ap");
1066   g1.SetName("Fit1_etaout");
1067   g1.Write();
1068 
1069   corrPat->Write();
1070   h2_failQA->Write();
1071   gainvals->Write();
1072   h_gainErr->Write();
1073 
1074   if (f_temp)
1075   {
1076     f_temp->Write();
1077   }
1078 
1079   f_temp->Close();
1080 }
1081 
1082 bool LiteCaloEval::chk_isChimney(int ieta, int iphi)
1083 {
1084   if ((ieta < 4 || ieta > 19) && (iphi > 13 && iphi < 20))
1085   {
1086     return true;
1087   }
1088 
1089   return false;
1090 }
1091 
1092 float LiteCaloEval::spec_QA(TH1 *h_spec, TH1 *h_ref, bool /*retFloat*/)
1093 {
1094   float norm = h_spec->Integral(h_spec->FindBin(0.4, 1.1), h_spec->FindBin(1.0));
1095   if (norm == 0)
1096   {
1097     std::cout << "no data for QA" << std::endl;
1098     return false;
1099   }
1100   TF1 *f_exp = new TF1("f_exp", "[0]*TMath::Exp(-1.0*x*[1])", 0.5, 1);
1101   h_spec->Fit(f_exp, "QNR", "", 0.4, 1.);
1102   float spec_p1 = f_exp->GetParameter(1);
1103   h_ref->Fit(f_exp, "QNR", "", 0.4, 1.);
1104   float ref_p1 = f_exp->GetParameter(1);
1105   float calib = ref_p1 / spec_p1;
1106   delete f_exp;
1107   return calib;
1108 }
1109 
1110 bool LiteCaloEval::spec_QA(TH1 *h_spec, TH1 *h_ref)
1111 {
1112   float calib = spec_QA(h_spec, h_ref, true);
1113   if (calib > 1.2 || calib < 0.8)
1114   {
1115     return false;
1116   }
1117 
1118   return true;
1119 }
1120 
1121 void LiteCaloEval::plot_cemc(const std::string &path)
1122 {
1123   TH2 *h_fail = new TH2F("h_fail", "", 96, 0, 96, 256, 0, 256);
1124   TH2 *h_corrPat = new TH2F("corrPat", "", 96, 0, 96, 256, 0, 256);
1125   TH2 *h_hits = new TH2F("h_hits", "", 96, 0, 96, 256, 0, 256);
1126   TH2 *h_expCalib = new TH2F("h_expCalib", "", 96, 0, 96, 256, 0, 256);
1127   h_hits->SetXTitle("#it{#eta}_{i}");
1128   h_hits->SetYTitle("#it{#phi}_{i}");
1129   TH2 *h_hits_clean = new TH2F("h_hits_clean", "", 96, 0, 96, 256, 0, 256);
1130   h_hits_clean->SetXTitle("#it{#eta}_{i}");
1131   h_hits_clean->SetYTitle("#it{#phi}_{i}");
1132 
1133   for (int ieta = 0; ieta < 96; ieta++)
1134   {
1135     for (int iphi = 0; iphi < 256; iphi++)
1136     {
1137       h_corrPat->SetBinContent(ieta + 1, iphi + 1, 1);
1138       cemc_hist_eta_phi[ieta][iphi]->Rebin(5);
1139       // record hits
1140       int binhit = cemc_hist_eta_phi[ieta][iphi]->FindBin(0.25);
1141       int binMax = cemc_hist_eta_phi[ieta][iphi]->GetNbinsX();
1142       float hits = cemc_hist_eta_phi[ieta][iphi]->Integral(binhit, binMax + 1);
1143       h_hits->SetBinContent(ieta + 1, iphi + 1, hits);
1144       h_hits->SetBinError(ieta + 1, iphi + 1, std::sqrt(hits));
1145       h_hits_clean->SetBinContent(ieta + 1, iphi + 1, hits);
1146       h_hits_clean->SetBinError(ieta + 1, iphi + 1, std::sqrt(hits));
1147       int bin = cemc_hist_eta_phi[ieta][iphi]->FindBin(0.2);
1148       if (cemc_hist_eta_phi[ieta][iphi]->GetBinContent(bin) == 0)
1149       {
1150         continue;
1151       }
1152       float calib = spec_QA(cemc_hist_eta_phi[ieta][iphi], eta_hist[ieta], true);
1153       h_expCalib->SetBinContent(ieta + 1, iphi + 1, calib);
1154       if (!spec_QA(cemc_hist_eta_phi[ieta][iphi], eta_hist[ieta], true))
1155       {
1156         h_fail->Fill(ieta, iphi);
1157         h_corrPat->SetBinContent(ieta + 1, iphi + 1, 0);
1158         h_hits_clean->SetBinContent(ieta + 1, iphi + 1, 0);
1159       }
1160     }
1161   }
1162 
1163   // find hot towers
1164   TH2 *h_hot = new TH2F("h_hot", "", 96, 0, 96, 256, 0, 256);
1165   TH1 *h1_hits[96];
1166   TH1 *h1_hits2[96];
1167   float max = h_hits_clean->GetBinContent(h_hits_clean->GetMaximumBin());
1168   float min = h_hits_clean->GetMinimum();
1169   if (min == 0)
1170   {
1171     min = 1;
1172   }
1173 
1174   TCanvas *c3 = new TCanvas("c3", "c3", 1600, 600);
1175 
1176   for (int ie = 0; ie < 96; ie++)
1177   {
1178     std::string name1 = "h1_hits" + std::to_string(ie);
1179     std::string name2 = "h1_hits2_" + std::to_string(ie);
1180 
1181     h1_hits[ie] = new TH1F(name1.c_str(), "", 200, min, max);
1182     h1_hits2[ie] = new TH1F(name2.c_str(), "", 200, min, max);
1183     for (int iphi = 0; iphi < 256; iphi++)
1184     {
1185       h1_hits[ie]->Fill(h_hits_clean->GetBinContent(ie + 1, iphi + 1));
1186     }
1187     float mean = h1_hits[ie]->GetMean();
1188     float std = h1_hits[ie]->GetStdDev();
1189     for (int iphi = 0; iphi < 256; iphi++)
1190     {
1191       float val = h_hits_clean->GetBinContent(ie + 1, iphi + 1);
1192       if (std::fabs(mean - val) / std > 3)
1193       {
1194         continue;
1195       }
1196       h1_hits2[ie]->Fill(val);
1197     }
1198     mean = h1_hits[ie]->GetMean();
1199     std = h1_hits[ie]->GetStdDev();
1200     for (int iphi = 0; iphi < 256; iphi++)
1201     {
1202       h_hot->SetBinContent(ie + 1, iphi + 1, 0);
1203       float val = h_hits_clean->GetBinContent(ie + 1, iphi + 1);
1204       if (val == 0)
1205       {
1206         continue;
1207       }
1208       if (std::fabs(mean - val) / std > 5)
1209       {
1210         h_hot->SetBinContent(ie + 1, iphi + 1, 1);
1211         h_corrPat->SetBinContent(ie + 1, iphi + 1, 0);
1212         h_hits_clean->SetBinContent(ie + 1, iphi + 1, 0);
1213       }
1214     }
1215     h1_hits[ie]->Write();
1216     h1_hits2[ie]->Write();
1217     if (ie < 90)
1218     {
1219       continue;
1220     }
1221     h1_hits[ie]->Draw("hist same");
1222     h1_hits[ie]->GetYaxis()->SetRangeUser(0.1, 100);
1223     h1_hits[ie]->GetXaxis()->SetTitle("Number of hits");
1224     h1_hits[ie]->GetXaxis()->SetNdivisions(505);
1225     h1_hits2[ie]->Draw("hist same");
1226     h1_hits2[ie]->SetLineColor(kBlue);
1227   }
1228   c3->SaveAs((path + "/hits.pdf").c_str());
1229 
1230   TLatex latex;
1231   latex.SetTextFont(42);
1232   latex.SetTextSize(0.04);
1233   latex.SetTextAlign(12);  // Align to the left
1234   latex.SetNDC();
1235 
1236   int ieta = 0;
1237   for (int iplot = 0; iplot < 10; iplot++)
1238   {
1239     TCanvas *c1 = new TCanvas("c1", "c1", 400, 600);
1240 
1241     for (int ie = 0; ie < 10; ie++)
1242     {
1243       if (ieta > 95)
1244       {
1245         continue;
1246       }
1247       for (int iphi = 0; iphi < 256; iphi++)
1248       {
1249         // scale hist
1250         int bin = cemc_hist_eta_phi[ieta][iphi]->FindBin(0.2);
1251         if (cemc_hist_eta_phi[ieta][iphi]->GetBinContent(bin) == 0)
1252         {
1253           continue;
1254         }
1255         cemc_hist_eta_phi[ieta][iphi]->Scale(1.0 / cemc_hist_eta_phi[ieta][iphi]->GetBinContent(bin) * pow(10, ie));
1256         // plot
1257         cemc_hist_eta_phi[ieta][iphi]->Draw("same hist");
1258         cemc_hist_eta_phi[ieta][iphi]->SetXTitle("pre-calib tower [GeV]");
1259         cemc_hist_eta_phi[ieta][iphi]->SetYTitle("N_{twr}/N_{twr}(E=0.2 GeV) x10^{ieta}");
1260         cemc_hist_eta_phi[ieta][iphi]->GetXaxis()->SetRangeUser(0, 1);
1261         cemc_hist_eta_phi[ieta][iphi]->GetYaxis()->SetRangeUser(0.01, 1e11);
1262         if (h_corrPat->GetBinContent(ieta + 1, iphi + 1) == 0)
1263         {
1264           cemc_hist_eta_phi[ieta][iphi]->SetLineColor(kBlue);
1265           h_fail->Fill(ieta, iphi);
1266           h_hits_clean->SetBinContent(ieta + 1, iphi + 1, 0);
1267         }
1268         latex.SetTextSize(0.02);
1269         latex.SetTextColor(kBlack);
1270         std::string result = "#eta_{" + std::to_string(ieta) + "}#times10^{" + std::to_string(ie) + "}";
1271         latex.DrawLatex(0.22 + (0.07 * ie), 0.92, result.c_str());
1272       }
1273 
1274       // eta_hist[ieta]->Rebin(5);
1275       int bin = eta_hist[ieta]->FindBin(0.2);
1276       if (eta_hist[ieta]->GetBinContent(bin) == 0)
1277       {
1278         ieta++;
1279         std::cout << "no data for eta=" << ieta << std::endl;
1280         continue;
1281       }
1282       eta_hist[ieta]->Scale(1.0 / eta_hist[ieta]->GetBinContent(bin) * pow(10, ie));
1283       eta_hist[ieta]->Draw("same hist");
1284       eta_hist[ieta]->SetLineColor(kRed);
1285 
1286       ieta++;
1287     }
1288 
1289     gPad->SetLogy();
1290 
1291     latex.SetTextSize(0.04);
1292 
1293     latex.SetTextColor(kBlack);
1294     latex.DrawLatex(0.6, 0.87, "#it{#bf{sPHENIX}} Internal");
1295     latex.SetTextColor(kRed);
1296     latex.DrawLatex(0.6, 0.83, "eta integrated");
1297     latex.SetTextColor(kBlue);
1298     latex.DrawLatex(0.6, 0.79, "failed QA");
1299 
1300     c1->SaveAs((path + "/tsc_spec" + std::to_string(iplot) + ".pdf").c_str());
1301     delete c1;
1302   }
1303 
1304   TCanvas *c2 = new TCanvas("c2", "c2", 600, 600);
1305   h_fail->Draw("COLZ");
1306 
1307   latex.SetTextColor(kBlack);
1308   std::string stuff = std::to_string(h_fail->GetEntries()) + " failed QA";
1309   latex.DrawLatex(0.2, 0.87, stuff.c_str());
1310 
1311   c2->SaveAs((path + "/failQA.pdf").c_str());
1312 
1313   TCanvas *c4 = new TCanvas("c4", "c4", 600, 600);
1314   h_expCalib->Draw("COLZ");
1315   h_expCalib->GetZaxis()->SetRangeUser(0.5, 2);
1316   gPad->SetRightMargin(0.15);
1317 
1318   latex.SetTextColor(kBlack);
1319 
1320   c4->SaveAs((path + "/expCalib.pdf").c_str());
1321 
1322   h_hot->Write();
1323   h_hits->Write();
1324   h_hits_clean->Write();
1325   h_corrPat->Write();
1326   h_expCalib->Write();
1327 
1328   f_temp->Close();
1329 }
1330 
1331 void LiteCaloEval::draw_spectra(const char *outfile)
1332 {
1333   if (calotype == LiteCaloEval::NONE)
1334   {
1335     std::cout << "Did not enter correct calotype. Exiting macro..." << std::endl;
1336     exit(-1);
1337   }
1338 
1339   TFile *fout = new TFile(outfile, "UPDATE");
1340 
1341   TH1 *h = nullptr;
1342   std::string histName;
1343 
1344   TH1 *h_etaSlice = nullptr;
1345   std::string etaSliceName;
1346 
1347   TH1 *h_cln = nullptr;  // for tower clone
1348   std::string h_cln_nm;
1349 
1350   TH1 *h_es_cln = nullptr;  // for es clone
1351   std::string h_es_nm;
1352 
1353   float scale = 1.0;
1354 
1355   float fitMin = getFitMin();
1356   float fitMax = getFitMax();
1357 
1358   double xaxisRange = 2.0;
1359 
1360   if (calotype == LiteCaloEval::HCALIN)
1361   {
1362     xaxisRange = 1.0;
1363   }
1364 
1365   if (calotype == LiteCaloEval::HCALIN || calotype == LiteCaloEval::HCALOUT)
1366   {
1367     std::cout << "Drawing hcal tower spectra" << std::endl;
1368 
1369     int starteta = 0;
1370     int maxeta = 12;
1371     int power = 23;
1372 
1373     TCanvas *c = new TCanvas();
1374     c->Divide(2);  // split canvas in two. Left pad holds ieta 0 - 11. Right holds 12-23
1375     c->SetName("hcal_spectra");
1376 
1377     int cntr = 1;  // allows histos to be separated by 10^2
1378 
1379     // this for loop places you in left or right pad
1380     for (int k = 1; k < 3; k++)
1381     {
1382       c->cd(k);
1383       gPad->SetLogy(1);
1384 
1385       if (k == 2)
1386       {
1387         power = 47;
1388         starteta = 12;
1389         maxeta = 24;
1390       }
1391 
1392       TLegend *t = new TLegend(0.5, 0.8, 0.75, 0.9);
1393       t->SetFillStyle(0);
1394       t->SetBorderSize(0);
1395       t->AddEntry("", (boost::format("ieta %d - %d") % starteta % (maxeta - 1)).str().c_str());
1396 
1397       // eta loop
1398       for (int i = starteta; i < maxeta; i++)
1399       {
1400         // get eta slice histogram and overlay in red onto tower spectra
1401         if (calotype == LiteCaloEval::HCALOUT)
1402         {
1403           etaSliceName = "hcalout_eta_" + std::to_string(i);
1404         }
1405         else if (calotype == LiteCaloEval::HCALIN)
1406         {
1407           etaSliceName = "hcalin_eta_" + std::to_string(i);
1408         }
1409 
1410         fout->GetObject(etaSliceName.c_str(), h_etaSlice);
1411 
1412         if (!h_etaSlice)
1413         {
1414           std::cout << "ERROR! Could not get hcal eta slice histogram " << i << " ." << std::endl;
1415           gSystem->Exit(1);
1416           exit(1);  // cppcheck does not know gSystem->Exit()
1417         }
1418 
1419         if (h_etaSlice->GetEntries() == 0.0)
1420         {
1421           std::cout << "WARNING! hcal eta slice " << i << " has no entries!" << std::endl;
1422           continue;
1423         }
1424 
1425         h_es_cln = (TH1 *) h_etaSlice->Clone();
1426 
1427         h_es_nm = "cln_es_" + std::to_string(i);
1428 
1429         h_es_cln->SetName(h_es_nm.c_str());
1430 
1431         // phi loop
1432         for (int j = 0; j < 64; j++)
1433         {
1434           if (calotype == LiteCaloEval::HCALOUT)
1435           {
1436             histName = "hcal_out_eta_" + std::to_string(i) + "_phi_" + std::to_string(j);
1437           }
1438           else if (calotype == LiteCaloEval::HCALIN)
1439           {
1440             histName = "hcal_in_eta_" + std::to_string(i) + "_phi_" + std::to_string(j);
1441           }
1442 
1443           fout->GetObject(histName.c_str(), h);
1444 
1445           if (!h)
1446           {
1447             std::cout << "ERROR! Could not find tower " << histName << "." << std::endl;
1448             gSystem->Exit(1);
1449           }
1450 
1451           if (h->GetEntries() == 0.0)
1452           {
1453             std::cout << "WARNING! No entries in hcal (" << i << "," << j << ").  Skipping this tower" << std::endl;
1454             continue;
1455           }
1456 
1457           h_cln = (TH1 *) h->Clone();
1458 
1459           h_cln_nm = (boost::format("h_cln_eta_%d_phi_%d") % i % j).str();
1460 
1461           h_cln->SetName(h_cln_nm.c_str());
1462 
1463           if (i == 0)
1464           {
1465             scale = pow(10, power - i);
1466           }
1467           else
1468           {
1469             scale = pow(10, power - i - cntr);
1470           }
1471          
1472 
1473           h_cln->Scale(h_es_cln->Integral(h_es_cln->FindBin(fitMin), h_es_cln->FindBin(fitMax)) / h_cln->Integral(h_cln->FindBin(fitMin), h_cln->FindBin(fitMax)));
1474           h_cln->Scale(scale);
1475           h_cln->GetXaxis()->SetRangeUser(0., xaxisRange);
1476           h_cln->GetYaxis()->SetRangeUser(10, 1e35);
1477           h_cln->Draw("same hist");
1478 
1479       if( (i==0 || i==12) && j==0)
1480         t->AddEntry(h_cln,"Tower","l");
1481 
1482           h_cln = nullptr;
1483 
1484         }  // phi loop
1485 
1486         // draw the combined eta slice spectrum
1487 
1488         h_es_cln->Scale(scale);
1489         h_es_cln->GetXaxis()->SetRangeUser(0., xaxisRange);
1490         h_es_cln->GetYaxis()->SetRangeUser(10, 1e35);
1491         h_es_cln->SetLineColor(2);
1492         h_es_cln->SetLineWidth(3);
1493         h_es_cln->Draw("same hist");
1494 
1495     if(i==0 || i==12)
1496       t->AddEntry(h_es_cln,"#Sigma towers","l");
1497      
1498         h_es_cln = nullptr;
1499 
1500         if (i > 0)
1501         {
1502           cntr++;
1503         }
1504 
1505       }  // eta loop
1506 
1507       t->Draw("same");
1508       t = nullptr;
1509       delete t;
1510 
1511     }  // k loop
1512 
1513     c->Write();
1514     c = nullptr;
1515     delete c;
1516 
1517   }  // end hcal caloflag
1518 
1519   if (calotype == LiteCaloEval::CEMC)
1520   {
1521     std::cout << "Drawing emcal tower spectra" << std::endl;
1522 
1523     int starteta = 0;
1524     int maxeta = 12;
1525 
1526     for (int k = 1; k < 9; k++)
1527     {
1528       int power = 36;
1529       int cntr = 3;  // cntr == 2 allows histos to be separated by 10^2, cntr == 3 gives separation by 10^3, etc.
1530 
1531       if (k > 1)
1532       {
1533         starteta = maxeta;
1534         maxeta = maxeta + 12;
1535       }
1536 
1537       TCanvas *c = new TCanvas();
1538       c->SetName((boost::format("emcal_eta%d_%d") % starteta % (maxeta - 1)).str().c_str());
1539       gPad->SetLogy(1);
1540 
1541       TLegend *t = new TLegend(0.5, 0.8, 0.75, 0.9);
1542       t->SetFillStyle(0);
1543       t->SetBorderSize(0);
1544       t->AddEntry("", (boost::format("ieta %d - %d") % starteta % (maxeta - 1)).str().c_str());
1545 
1546       // eta loop
1547       for (int i = starteta; i < maxeta; i++)
1548       {
1549         etaSliceName = "eta_" + std::to_string(i);
1550 
1551         fout->GetObject(etaSliceName.c_str(), h_etaSlice);
1552 
1553         if (!h_etaSlice)
1554         {
1555           std::cout << "ERROR! Could not get emcal eta slice " << i << ". Exiting analysis." << std::endl;
1556           exit(-1);
1557         }
1558 
1559         if (h_etaSlice->GetEntries() == 0.0)
1560         {
1561           std::cout << "WARNING! EMCal eta slice " << i << " has no entries!" << std::endl;
1562           continue;
1563         }
1564 
1565         h_es_cln = (TH1 *) h_etaSlice->Clone();
1566 
1567         h_es_nm = "cln_es_" + std::to_string(i);
1568 
1569         h_es_cln->SetName(h_es_nm.c_str());
1570 
1571         // phi loop
1572         for (int j = 0; j < 256; j++)
1573         {
1574           histName = "emc_ieta" + std::to_string(i) + "_phi" + std::to_string(j);
1575 
1576           fout->GetObject(histName.c_str(), h);
1577 
1578           if (!h)
1579           {
1580             std::cout << "ERROR! Could not find " << histName << ". Exiting macro." << std::endl;
1581             exit(-1);
1582           }
1583           if (h->GetEntries() == 0.0)
1584           {
1585             std::cout << "WARNING! No entries in emcal (" << i << "," << j << ").  Skipping this tower" << std::endl;
1586             continue;
1587           }
1588 
1589           h_cln = (TH1 *) h->Clone();
1590 
1591           h_cln_nm = (boost::format("h_cln_eta_%d_phi_%d") % i % j).str();
1592 
1593           h_cln->SetName(h_cln_nm.c_str());
1594 
1595           if (i == 0 || i % 12 == 0)
1596           {
1597             scale = pow(10, power);
1598           }
1599           else
1600           {
1601             scale = pow(10, power - cntr);
1602           }
1603 
1604 
1605           h_cln->Scale(h_es_cln->Integral(h_es_cln->FindBin(fitMin), h_es_cln->FindBin(fitMax)) / h_cln->Integral(h_cln->FindBin(fitMin), h_cln->FindBin(fitMax)));
1606           h_cln->Scale(scale);
1607           h_cln->GetXaxis()->SetRangeUser(0., xaxisRange);
1608           h_cln->GetYaxis()->SetRangeUser(0.00001, 1e40);
1609           h_cln->Draw("same hist");
1610 
1611       if( ((i+1)%12==0) && j==0)
1612         t->AddEntry(h_cln,"Tower","l");
1613 
1614           h_cln = nullptr;
1615 
1616         }  // phi loop
1617 
1618         h_es_cln->Scale(scale);
1619         h_es_cln->GetXaxis()->SetRangeUser(0., xaxisRange);
1620         h_es_cln->GetYaxis()->SetRangeUser(0.00001, 1e40);
1621         h_es_cln->SetLineColor(2);
1622         h_es_cln->SetLineWidth(3);
1623         h_es_cln->Draw("same hist");
1624 
1625     if((i+1)%12==0)
1626       t->AddEntry(h_es_cln,"#Sigma towers","l");
1627       
1628         h_es_cln = nullptr;
1629 
1630         if (!(i % 12 == 0))
1631         {
1632           cntr += 3;  // cntr++ gives separation by 100, +=2 gives 1000
1633         }
1634 
1635       }  // eta loop
1636 
1637       t->Draw("same");
1638 
1639       c->Write();
1640 
1641       t = nullptr;
1642       delete t;
1643 
1644       c = nullptr;
1645       delete c;
1646 
1647       std::cout << "Drew canvas " << k << std::endl;
1648     }  // k loop
1649 
1650   }  // end emcal flag
1651 
1652   fout->Close();
1653   fout = nullptr;
1654   delete fout;
1655 
1656   std::cout << "Drawing histos is complete." << std::endl;
1657 
1658 }  // end draw spectra f'n
1659 
1660 
1661 
1662 void LiteCaloEval::draw_spectra(const char *infile, const char *outfile)
1663 {
1664   
1665   if (calotype == LiteCaloEval::NONE)
1666     {
1667       std::cout << "Did not enter correct calotype. Exiting macro..." << std::endl;
1668       exit(-1);
1669     }
1670 
1671   TFile *fin = new TFile(infile,"READ");
1672   TFile *fout = new TFile(outfile,"UPDATE");
1673 
1674   TH1F *h = nullptr;
1675   std::string histName;
1676 
1677   TH1F *h_etaSlice = nullptr;
1678   std::string etaSliceName;
1679 
1680   TH1F *h_cln = nullptr; //for tower clone
1681   std::string h_cln_nm;
1682 
1683   TH1F *h_es_cln = nullptr; //for es clone
1684   std::string h_es_nm;
1685 
1686 
1687   float scale = 1.0;
1688 
1689   float fitMin = getFitMin();
1690   float fitMax = getFitMax();
1691 
1692   double xaxisRange = 2.0;
1693 
1694   if (calotype == LiteCaloEval::HCALIN)
1695     {
1696       xaxisRange = 3.0;
1697     }
1698 
1699   if (calotype == LiteCaloEval::HCALIN || calotype == LiteCaloEval::HCALOUT)
1700     {
1701       std::cout << "Drawing hcal tower spectra" << std::endl;
1702 
1703       int starteta = 0;
1704       int maxeta = 12;
1705       int power = 23;
1706 
1707       TCanvas *c = new TCanvas();
1708       c->Divide(2);  // split canvas in two. Left pad holds ieta 0 - 11. Right holds 12-23
1709       c->SetName("hcal_spectra");
1710 
1711       int cntr = 1;  // allows histos to be separated by 10^2
1712 
1713       // this for loop places you in left or right pad
1714       for (int k = 1; k < 3; k++)
1715     {
1716       c->cd(k);
1717       gPad->SetLogy(1);
1718 
1719       if (k == 2)
1720         {
1721           power = 47;
1722           starteta = 12;
1723           maxeta = 24;
1724         }
1725 
1726       TLegend *t = new TLegend(0.5, 0.8, 0.75, 0.9);
1727       t->SetFillStyle(0);
1728       t->SetBorderSize(0);
1729       t->AddEntry("", (boost::format("ieta %d - %d") % starteta % (maxeta - 1)).str().c_str(),"");
1730 
1731       // eta loop
1732       for (int i = starteta; i < maxeta; i++)
1733         {
1734           // get eta slice histogram and overlay in red onto tower spectra
1735           if (calotype == LiteCaloEval::HCALOUT)
1736         {
1737           etaSliceName = "hcalout_eta_" + std::to_string(i);
1738         }
1739           else if (calotype == LiteCaloEval::HCALIN)
1740         {
1741           etaSliceName = "hcalin_eta_" + std::to_string(i);
1742         }
1743 
1744           h_etaSlice = (TH1F *) fin->Get(etaSliceName.c_str());
1745 
1746           if (!h_etaSlice)
1747         {
1748           std::cout << "ERROR! Could not get hcal eta slice histogram " << i << " ." << std::endl;
1749           gSystem->Exit(1);
1750         }
1751 
1752           if (h_etaSlice->GetEntries() == 0.0)
1753         {
1754           std::cout << "WARNING! hcal eta slice " << i << " has no entries!" << std::endl;
1755           continue;
1756         }
1757           
1758           //rebin
1759           double esBW = h_etaSlice->GetBinWidth(1);
1760           
1761           double bW = get_spectra_binWidth();     
1762                   
1763           int rbn = std::ceil(bW/esBW);
1764 
1765           if(esBW < bW)
1766         {
1767           h_etaSlice->Rebin(rbn);
1768         }
1769           
1770           h_es_cln = (TH1F *)h_etaSlice->Clone();
1771 
1772           h_es_nm = "cln_es_" + std::to_string(i);
1773 
1774           h_es_cln->SetName(h_es_nm.c_str());
1775 
1776 
1777           // phi loop
1778           for (int j = 0; j < 64; j++)
1779         {
1780           if (calotype == LiteCaloEval::HCALOUT)
1781             {
1782               histName = "hcal_out_eta_" + std::to_string(i) + "_phi_" + std::to_string(j);
1783             }
1784           else if (calotype == LiteCaloEval::HCALIN)
1785             {
1786               histName = "hcal_in_eta_" + std::to_string(i) + "_phi_" + std::to_string(j);
1787             }
1788 
1789           h = (TH1F *) fin->Get(histName.c_str());
1790 
1791           if (!h)
1792             {
1793               std::cout << "ERROR! Could not find tower " << histName << "." << std::endl;
1794               gSystem->Exit(1);
1795             }
1796 
1797           if (h->GetEntries() == 0.0)
1798             {
1799               std::cout << "WARNING! No entries in hcal (" << i << "," << j << ").  Skipping this tower" << std::endl;
1800               continue;
1801             }
1802                   
1803           //rebin
1804           double tBW = h->GetBinWidth(1);
1805           double W = get_spectra_binWidth();          
1806           int rbn2 = std::ceil(W/tBW);
1807 
1808           if(tBW < W)
1809             {
1810               h->Rebin(rbn2);
1811             }
1812           
1813           h_cln = (TH1F *)h->Clone();
1814 
1815           h_cln_nm = (boost::format("h_cln_eta_%d_phi_%d") % i % j).str();
1816 
1817           h_cln->SetName(h_cln_nm.c_str());
1818 
1819 
1820           if (i == 0)
1821             {
1822               scale = pow(10, power - i);
1823             }
1824           else
1825             {
1826               scale = pow(10, power - i - cntr);
1827             }
1828       
1829           h_cln->Scale(h_es_cln->Integral(h_es_cln->FindBin(fitMin), h_es_cln->FindBin(fitMax) ) / h_cln->Integral(h_cln->FindBin(fitMin), h_cln->FindBin(fitMax) ));
1830           h_cln->Scale(scale);
1831           h_cln->GetXaxis()->SetRangeUser(0., xaxisRange);
1832           h_cln->GetYaxis()->SetRangeUser(10, 1e35);
1833           h_cln->Draw("same hist");
1834                   
1835           if( (i==0 || i==12) && j==0)
1836             t->AddEntry(h_cln,"Tower","l");
1837 
1838           h_cln = nullptr;
1839           
1840         }  // phi loop
1841 
1842           // draw the combined eta slice spectrum
1843 
1844           h_es_cln->Scale(scale);
1845           h_es_cln->GetXaxis()->SetRangeUser(0., xaxisRange);
1846           h_es_cln->GetYaxis()->SetRangeUser(10, 1e35);
1847           h_es_cln->SetLineColor(2);
1848           h_es_cln->SetLineWidth(3);
1849           h_es_cln->Draw("same hist");
1850 
1851           if(i==0 || i==12)
1852         t->AddEntry(h_es_cln,"#Sigma towers","l");
1853 
1854           h_es_cln = nullptr;
1855 
1856           if (i > 0)
1857         {
1858           cntr++;
1859         }
1860 
1861         }  // eta loop
1862 
1863       t->Draw("same");
1864       t = nullptr;
1865       delete t;
1866 
1867     }  // k loop
1868 
1869       fout->cd();
1870       c->Write();
1871       c = nullptr;
1872       delete c;
1873 
1874     }  // end hcal caloflag
1875 
1876   if (calotype == LiteCaloEval::CEMC)
1877     {
1878       std::cout << "Drawing emcal tower spectra" << std::endl;
1879 
1880       int starteta = 0;
1881       int maxeta = 12;
1882 
1883       for (int k = 1; k < 9; k++)
1884     {
1885       int power = 36;
1886       int cntr = 3;  // cntr == 2 allows histos to be separated by 10^2, cntr == 3 gives separation by 10^3, etc.
1887 
1888       if (k > 1)
1889         {
1890           starteta = maxeta;
1891           maxeta = maxeta + 12;
1892         }
1893 
1894       TCanvas *c = new TCanvas();
1895       c->SetName((boost::format("emcal_eta%d_%d") % starteta % (maxeta - 1)).str().c_str());
1896       gPad->SetLogy(1);
1897 
1898       TLegend *t = new TLegend(0.5, 0.8, 0.75, 0.9);
1899       t->AddEntry("", (boost::format("ieta %d - %d") % starteta % (maxeta - 1)).str().c_str());
1900 
1901       // eta loop
1902       for (int i = starteta; i < maxeta; i++)
1903         {
1904           etaSliceName = "eta_" + std::to_string(i);
1905 
1906           h_etaSlice = (TH1F *) fin->Get(etaSliceName.c_str());
1907 
1908           if (!h_etaSlice)
1909         {
1910           std::cout << "ERROR! Could not get emcal eta slice " << i << ". Exiting analysis." << std::endl;
1911           exit(-1);
1912         }
1913 
1914           if (h_etaSlice->GetEntries() == 0.0)
1915         {
1916           std::cout << "WARNING! EMCal eta slice " << i << " has no entries!" << std::endl;
1917           continue;
1918         }
1919 
1920             //rebin
1921           double esBW = h_etaSlice->GetBinWidth(1);//assume 0.001         
1922           double bW = get_spectra_binWidth();      //assume 0.01
1923           int rbn = std::ceil(bW/esBW);
1924 
1925           if(esBW < bW)
1926         {
1927           h_etaSlice->Rebin(rbn);
1928         }
1929           
1930           h_es_cln = (TH1F *)h_etaSlice->Clone();
1931 
1932           h_es_nm = "cln_es_" + std::to_string(i);
1933 
1934           h_es_cln->SetName(h_es_nm.c_str());
1935           
1936           // phi loop
1937           for (int j = 0; j < 256; j++)
1938         {
1939           histName = "emc_ieta" + std::to_string(i) + "_phi" + std::to_string(j);
1940 
1941           h = (TH1F *) fin->Get(histName.c_str());
1942 
1943           if (!h)
1944             {
1945               std::cout << "ERROR! Could not find " << histName << ". Exiting macro." << std::endl;
1946               exit(-1);
1947             }
1948           if (h->GetEntries() == 0.0)
1949             {
1950               std::cout << "WARNING! No entries in emcal (" << i << "," << j << ").  Skipping this tower" << std::endl;
1951               continue;
1952             }
1953 
1954           //rebin
1955           double tBW = h->GetBinWidth(1);//assume 0.001
1956           double W = get_spectra_binWidth();//assume 0.01         
1957           int rbn2 = std::ceil(W/tBW);
1958 
1959           if(tBW < W)
1960             {
1961               h->Rebin(rbn2);
1962             }
1963           
1964           h_cln = (TH1F *)h->Clone();
1965 
1966           h_cln_nm = (boost::format("h_cln_eta_%d_phi_%d") % i % j).str();
1967 
1968           h_cln->SetName(h_cln_nm.c_str());
1969 
1970           if (i == 0 || i % 12 == 0)
1971             {
1972               scale = pow(10, power);
1973             }
1974           else
1975             {
1976               scale = pow(10, power - cntr);
1977             }
1978 
1979           h_cln->Scale(h_es_cln->Integral(h_es_cln->FindBin(fitMin), h_es_cln->FindBin(fitMax) ) / h_cln->Integral(h_cln->FindBin(fitMin), h_cln->FindBin(fitMax)));
1980           h_cln->Scale(scale);
1981           h_cln->GetXaxis()->SetRangeUser(0., xaxisRange);
1982           h_cln->GetYaxis()->SetRangeUser(0.00001, 1e40);
1983           h_cln->Draw("same hist");
1984 
1985           if( ((i+1)%12==0) && j==0)
1986             t->AddEntry(h_cln,"Tower","l");
1987           
1988           h_cln = nullptr;
1989 
1990         }// phi loop
1991 
1992           h_es_cln->Scale(scale);
1993           h_es_cln->GetXaxis()->SetRangeUser(0., xaxisRange);
1994           h_es_cln->GetYaxis()->SetRangeUser(0.00001, 1e40);
1995           h_es_cln->SetLineColor(2);
1996           h_es_cln->SetLineWidth(3);
1997           h_es_cln->Draw("same hist");
1998 
1999           if( (i+1)%12==0 )
2000         t->AddEntry(h_es_cln,"#Sigma towers","l");
2001            
2002           h_es_cln = nullptr;
2003 
2004           if (!(i % 12 == 0))
2005         {
2006           cntr += 3;  // cntr++ gives separation by 100, +=2 gives 1000
2007         }
2008 
2009         }// eta loop
2010 
2011       t->Draw("same");
2012 
2013       fout->cd();
2014       
2015       c->Write();
2016 
2017       t = nullptr;
2018       delete t;
2019 
2020       c = nullptr;
2021       delete c;
2022 
2023       std::cout << "Drew canvas " << k << std::endl;
2024     }  // k loop
2025 
2026     }  // end emcal flag
2027 
2028 
2029   fout->Close();
2030   fout = nullptr;
2031   delete fout;
2032   
2033   std::cout << "Drawing histos is complete." << std::endl;
2034 }
2035 
2036 void LiteCaloEval::fit_info(const char *outfile, const int runNum)
2037 {
2038   TFile *fout = new TFile(outfile, "UPDATE");
2039 
2040   int eta;
2041   int phi;
2042   double towers;
2043   //  int badTowers = 0;
2044 
2045   if (calotype == LiteCaloEval::HCALIN || calotype == LiteCaloEval::HCALOUT)
2046   {
2047     eta = 24;
2048     phi = 64;
2049     towers = 1536.0;
2050   }
2051   else if (calotype == LiteCaloEval::CEMC)
2052   {
2053     eta = 96;
2054     phi = 256;
2055     towers = 24576.0;
2056   }
2057   else
2058   {
2059     std::cout << "calotype not set. Exiting." << std::endl;
2060     exit(-1);
2061   }
2062 
2063   TH2 *errMap = new TH2F("errMap", "", eta, 0, eta, phi, 0, phi);
2064   errMap->GetXaxis()->SetTitle("#eta Bin");
2065   errMap->GetYaxis()->SetTitle("#phi Bin");
2066 
2067   // make chi squared/ndf plots
2068   TH1 *chi2 = new TH1F("chi2", "", 500, 0, 50);
2069   chi2->GetXaxis()->SetTitle("#chi^{2} / NDF");
2070   chi2->GetYaxis()->SetTitle("Counts");
2071 
2072   // make chi squared/ndf map
2073   TH2 *chi2Map = new TH2F("chi2Map", "", eta, 0, eta, phi, 0, phi);
2074   chi2Map->GetXaxis()->SetTitle("#eta Bin");
2075   chi2Map->GetYaxis()->SetTitle("#phi Bin");
2076 
2077   // map of tower with failed fits
2078   TH2 *fitFail = new TH2F("fitFail", "", eta, 0, eta, phi, 0, phi);
2079   fitFail->GetXaxis()->SetTitle("#eta bin");
2080   fitFail->GetYaxis()->SetTitle("#phi bin");
2081 
2082   std::string histname;
2083   TH1 *htmp = nullptr;
2084   TF1 *fn = nullptr;
2085 
2086   // testing stuff
2087   TH2 *cp = nullptr;  // to hold corrpat
2088   fout->GetObject("corrPat", cp);
2089 
2090   if (!cp)
2091   {
2092     std::cout << "Error! Did not get corrPat histogram. Exiting analysis." << std::endl;
2093     exit(-1);
2094   }
2095 
2096   TGraphErrors *tmp_avgTSC = new TGraphErrors();
2097   tmp_avgTSC->SetName("g_avgTSC");
2098   tmp_avgTSC->GetXaxis()->SetTitle("run number");
2099   tmp_avgTSC->GetYaxis()->SetTitle("Mean Towerslope Correction");
2100   tmp_avgTSC->SetMarkerStyle(8);
2101   tmp_avgTSC->SetMarkerSize(1);
2102 
2103   double sum4avg = 0.0;
2104   double tscAvg = 0.0;
2105   double sum4SE = 0.0;
2106   double SE = 0.0;
2107 
2108   // phi loop
2109   for (int i = 0; i < eta; i++)
2110   {
2111     // eta loop
2112     for (int j = 0; j < phi; j++)
2113     {
2114       // for ohcal
2115       if (calotype == LiteCaloEval::HCALOUT)
2116       {
2117         histname = (boost::format("hcal_out_eta_%d_phi_%d") % i % j).str();
2118       }
2119 
2120       // ihcal
2121       if (calotype == LiteCaloEval::HCALIN)
2122       {
2123         histname = (boost::format("hcal_in_eta_%d_phi_%d") % i % j).str();
2124       }
2125 
2126       // emcal
2127       if (calotype == LiteCaloEval::CEMC)
2128       {
2129         histname = (boost::format("emc_ieta%d_phi%d") % i % j).str();
2130       }
2131 
2132       sum4avg += (cp->GetBinContent(i + 1, j + 1));
2133 
2134       fout->GetObject(histname.c_str(), htmp);
2135 
2136       fn = htmp->GetFunction("myexpo");
2137 
2138       errMap->SetBinContent(i + 1, j + 1, fn->GetParError(1));
2139 
2140       chi2->Fill(fn->GetChisquare() / fn->GetNDF());
2141 
2142       chi2Map->SetBinContent(i + 1, j + 1, fn->GetChisquare() / fn->GetNDF());
2143 
2144       if (fn->GetChisquare() / fn->GetNDF() > 5)
2145       {
2146         fitFail->Fill(i, j);
2147         //        badTowers++;
2148       }
2149 
2150     }  // end inner forloop
2151 
2152   }  // end outer forloop
2153 
2154   tscAvg = sum4avg / towers;
2155 
2156   // get standard error of avg TSC
2157   for (int i = 0; i < eta; i++)
2158   {
2159     for (int j = 0; j < phi; j++)
2160     {
2161       sum4SE += pow(cp->GetBinContent(i + 1, j + 1) - tscAvg, 2);  // getting part of the std dev
2162     }
2163   }
2164 
2165   SE = sqrt(sum4SE / towers) / sqrt(towers);
2166 
2167   tmp_avgTSC->SetPoint(0, runNum, tscAvg);
2168   tmp_avgTSC->SetPointError(0, 0.0, SE);
2169 
2170   errMap->Write();
2171   chi2->Write();
2172   chi2Map->Write();
2173   fitFail->Write();
2174   tmp_avgTSC->Write();
2175 
2176   errMap = nullptr;
2177   delete errMap;
2178 
2179   chi2 = nullptr;
2180   delete chi2;
2181 
2182   chi2Map = nullptr;
2183   delete chi2Map;
2184 
2185   fitFail = nullptr;
2186   delete fitFail;
2187 
2188   fout->Close();
2189   fout = nullptr;
2190   delete fout;
2191 
2192   std::cout << "Finished fit info" << std::endl;
2193 }
2194 
2195 /// used when one already has .root file with histograms fit. Other fit_info to be used at run-time with macro
2196 void LiteCaloEval::fit_info(const char *infile, const char *outfile, const int runNum)
2197 {
2198   TFile *fin = new TFile(infile, "READ");
2199   TFile *fout = new TFile(outfile, "UPDATE");
2200 
2201   int eta;
2202   int phi;
2203   double towers;
2204 
2205   if (calotype == LiteCaloEval::HCALIN || calotype == LiteCaloEval::HCALOUT)
2206   {
2207     eta = 24;
2208     phi = 64;
2209     towers = 1536.0;
2210   }
2211   else if (calotype == LiteCaloEval::CEMC)
2212   {
2213     eta = 96;
2214     phi = 256;
2215     towers = 24576.0;
2216   }
2217   else
2218   {
2219     std::cout << "calotype not set. Exiting." << std::endl;
2220     exit(-1);
2221   }
2222 
2223   TH2 *errMap = new TH2F("errMap", "", eta, 0, eta, phi, 0, phi);
2224   errMap->GetXaxis()->SetTitle("#eta Bin");
2225   errMap->GetYaxis()->SetTitle("#phi Bin");
2226 
2227   // make chi squared/ndf plots
2228   TH1 *chi2 = new TH1F("chi2", "", 500, 0, 50);
2229   chi2->GetXaxis()->SetTitle("#chi^{2} / NDF");
2230   chi2->GetYaxis()->SetTitle("Counts");
2231 
2232   // make chi squared/ndf map
2233   TH2 *chi2Map = new TH2F("chi2Map", "", eta, 0, eta, phi, 0, phi);
2234   chi2Map->GetXaxis()->SetTitle("#eta Bin");
2235   chi2Map->GetYaxis()->SetTitle("#phi Bin");
2236 
2237   // map of tower with failed fits
2238   TH2 *fitFail = new TH2F("fitFail", "", eta, 0, eta, phi, 0, phi);
2239   fitFail->GetXaxis()->SetTitle("#eta bin");
2240   fitFail->GetYaxis()->SetTitle("#phi bin");
2241 
2242   std::string histname;
2243   TH1 *htmp{nullptr};
2244   TF1 *fn{nullptr};
2245 
2246   // testing stuff
2247   TH2 *cp{nullptr};  // to hold corrpat
2248   fin->GetObject("corrPat", cp);
2249 
2250   if (!cp)
2251   {
2252     std::cout << "Error! Did not get corrPat histogram. Exiting analysis." << std::endl;
2253     exit(-1);
2254   }
2255 
2256   TGraphErrors *tmp_avgTSC = new TGraphErrors();
2257   tmp_avgTSC->SetName("g_avgTSC");
2258   tmp_avgTSC->GetXaxis()->SetTitle("run number");
2259   tmp_avgTSC->GetYaxis()->SetTitle("Mean Towerslope Correction");
2260   tmp_avgTSC->SetMarkerStyle(8);
2261   tmp_avgTSC->SetMarkerSize(1);
2262 
2263   double sum4avg = 0.0;
2264   double tscAvg = 0.0;
2265   double sum4SE = 0.0;
2266   double SE = 0.0;
2267 
2268   // phi loop
2269   for (int i = 0; i < eta; i++)
2270   {
2271     // eta loop
2272     for (int j = 0; j < phi; j++)
2273     {
2274       // for ohcal
2275       if (calotype == LiteCaloEval::HCALOUT)
2276       {
2277         histname = (boost::format("hcal_out_eta_%d_phi_%d") % i % j).str();
2278       }
2279 
2280       // ihcal
2281       if (calotype == LiteCaloEval::HCALIN)
2282       {
2283         histname = (boost::format("hcal_in_eta_%d_phi_%d") % i % j).str();
2284       }
2285 
2286       // emcal
2287       if (calotype == LiteCaloEval::CEMC)
2288       {
2289         histname = (boost::format("emc_ieta%d_phi%d") % i % j).str();
2290       }
2291 
2292       sum4avg += (cp->GetBinContent(i + 1, j + 1));
2293 
2294       fin->GetObject(histname.c_str(), htmp);
2295 
2296       fn = htmp->GetFunction("myexpo");
2297 
2298       errMap->SetBinContent(i + 1, j + 1, fn->GetParError(1));
2299 
2300       chi2->Fill(fn->GetChisquare() / fn->GetNDF());
2301 
2302       chi2Map->SetBinContent(i + 1, j + 1, fn->GetChisquare() / fn->GetNDF());
2303 
2304       if (fn->GetChisquare() / fn->GetNDF() > 5)
2305       {
2306         fitFail->Fill(i, j);
2307       }
2308 
2309     }  // end inner forloop
2310 
2311   }  // end outer forloop
2312 
2313   tscAvg = sum4avg / towers;
2314 
2315   // get standard error of avg TSC
2316   for (int i = 0; i < eta; i++)
2317   {
2318     for (int j = 0; j < phi; j++)
2319     {
2320       sum4SE += pow(cp->GetBinContent(i + 1, j + 1) - tscAvg, 2);  // getting part of the std dev
2321     }
2322   }
2323 
2324   SE = sqrt(sum4SE / towers) / sqrt(towers);
2325 
2326   tmp_avgTSC->SetPoint(0, runNum, tscAvg);
2327   tmp_avgTSC->SetPointError(0, 0.0, SE);
2328 
2329   errMap->Write();
2330   chi2->Write();
2331   chi2Map->Write();
2332   fitFail->Write();
2333   tmp_avgTSC->Write();
2334 
2335   errMap = nullptr;
2336   delete errMap;
2337 
2338   chi2 = nullptr;
2339   delete chi2;
2340 
2341   chi2Map = nullptr;
2342   delete chi2Map;
2343 
2344   fitFail = nullptr;
2345   delete fitFail;
2346 
2347   fin->Close();
2348   delete fin;
2349   fin = nullptr;
2350 
2351   fout->Close();
2352   fout = nullptr;
2353   delete fout;
2354 
2355   std::cout << "Finished fit info" << std::endl;
2356 }