Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2024-09-27 07:03:26

0001 // SPDX-License-Identifier: LGPL-3.0-or-later
0002 // Copyright (C) 2023 Christopher Dilks, Duane Byer
0003 
0004 #include "Histos.h"
0005 
0006 ClassImp(Histos)
0007 
0008 using std::map;
0009 using std::vector;
0010 using std::cout;
0011 using std::cerr;
0012 using std::endl;
0013 
0014 // constructor
0015 Histos::Histos(TString setname_, TString settitle_)
0016   : setname(setname_)
0017   , settitle(settitle_)
0018 {
0019   this->SetName(setname);
0020   if(settitle!="settitle") cout << "Histos:  " << settitle << endl;
0021 };
0022 
0023 
0024 // define a 1D histogram
0025 void Histos::DefineHist1D(
0026     TString varname,
0027     TString vartitle,
0028     TString units,
0029     Int_t numBins, Double_t lowerBound, Double_t upperBound,
0030     Bool_t logx, Bool_t logy
0031     ) {
0032   if(units!="") units=" ["+units+"]";
0033   TString histT;
0034   if(varname.Contains("_xsec")) histT = "d#sigma/d"+vartitle;
0035   else if(varname.Contains("_fuu")) histT = "F_{UU} vs. "+vartitle;
0036   else if(varname.Contains("_fut")) histT = "F_{UT} vs. "+vartitle;
0037   else histT = vartitle+" distribution";
0038   TH1D *hist = new TH1D(
0039       setname+"_hist_"+varname,
0040       histT+", "+settitle+";"+vartitle+units,
0041       numBins,lowerBound,upperBound
0042       );
0043   if(logx) BinSet::BinLog(hist->GetXaxis());
0044   HistConfig *config = new HistConfig();
0045   config->logx = logx;
0046   config->logy = logy;
0047   this->RegisterHist(varname,hist,config);
0048 };
0049 
0050 
0051 // define a 2D histogram
0052 void Histos::DefineHist2D(
0053     TString varname,
0054     TString vartitlex, TString vartitley,
0055     TString unitsx, TString unitsy,
0056     Int_t numBinsx, Double_t lowerBoundx, Double_t upperBoundx,
0057     Int_t numBinsy, Double_t lowerBoundy, Double_t upperBoundy,
0058     Bool_t logx, Bool_t logy, Bool_t logz
0059     ) {
0060   if(unitsx!="") unitsx=" ["+unitsx+"]";
0061   if(unitsy!="") unitsy=" ["+unitsy+"]";
0062   TString histT;
0063   if(varname.Contains("_xsec")) histT = "d^{2}#sigma/d"+vartitlex+vartitley;
0064   else if(varname.Contains("_fuu")) histT = "F_{UU} vs. "+vartitley+" vs. "+vartitlex;
0065   else if(varname.Contains("_fut")) histT = "F_{UT} vs. "+vartitley+" vs. "+vartitlex;
0066   else histT = vartitley+" vs. "+vartitlex+" distribution";
0067   TH2D *hist = new TH2D(
0068       setname+"_hist_"+varname,
0069       histT+", "+settitle+";"+vartitlex+unitsx+";"+vartitley+unitsy,
0070       numBinsx,lowerBoundx,upperBoundx,
0071       numBinsy,lowerBoundy,upperBoundy
0072       );
0073   if(logx) BinSet::BinLog(hist->GetXaxis());
0074   if(logy) BinSet::BinLog(hist->GetYaxis());
0075   HistConfig *config = new HistConfig();
0076   config->logx = logx;
0077   config->logy = logy;
0078   config->logz = logz;
0079   this->RegisterHist(varname,hist,config);
0080 };
0081 // define 2D histogram with custom bins
0082 void Histos::DefineHist2D(
0083     TString varname,
0084     TString vartitlex, TString vartitley,
0085     TString unitsx, TString unitsy,
0086     Int_t numBinsx, Double_t *xBins, 
0087     Int_t numBinsy, Double_t *yBins, 
0088     Bool_t logx, Bool_t logy, Bool_t logz
0089     ) {
0090   if(unitsx!="") unitsx=" ["+unitsx+"]";
0091   if(unitsy!="") unitsy=" ["+unitsy+"]";
0092   TString histT;
0093   TH2D *hist = new TH2D(
0094       setname+"_hist_"+varname,
0095       histT+", "+settitle+";"+vartitlex+unitsx+";"+vartitley+unitsy,
0096       numBinsx,xBins,
0097       numBinsy,yBins
0098       );
0099   HistConfig *config = new HistConfig();
0100   config->logx = logx;
0101   config->logy = logy;
0102   config->logz = logz;
0103   this->RegisterHist(varname,hist,config);
0104 };
0105 
0106 // define a 3D histogram
0107 void Histos::DefineHist3D(
0108     TString varname,
0109     TString vartitlex, TString vartitley, TString vartitlez,
0110     TString unitsx, TString unitsy, TString unitsz,
0111     Int_t numBinsx, Double_t lowerBoundx, Double_t upperBoundx,
0112     Int_t numBinsy, Double_t lowerBoundy, Double_t upperBoundy,
0113     Int_t numBinsz, Double_t lowerBoundz, Double_t upperBoundz,
0114     Bool_t logx, Bool_t logy, Bool_t logz
0115     ) {
0116   if(unitsx!="") unitsx=" ["+unitsx+"]";
0117   if(unitsy!="") unitsy=" ["+unitsy+"]";
0118   if(unitsz!="") unitsz=" ["+unitsz+"]";
0119   TString histT;
0120   if(varname.Contains("_xsec")) histT = "d^{3}#sigma/d"+vartitlex+vartitley+vartitlez;
0121   else if(varname.Contains("_fuu")) histT = "F_{UU} vs. "+vartitlez+" vs. "+vartitley+" vs. "+vartitlex;
0122   else if(varname.Contains("_fut")) histT = "F_{UT} vs. "+vartitlez+" vs. "+vartitley+" vs. "+vartitlez;
0123   else histT = vartitlez+" vs. "+vartitley+" vs. "+vartitlex+" distribution";
0124   TH3D *hist = new TH3D(
0125       setname+"_hist_"+varname,
0126       histT+", "+settitle+";"+vartitlex+unitsx+";"+vartitley+unitsy+";"+vartitlez+unitsz,
0127       numBinsx,lowerBoundx,upperBoundx,
0128       numBinsy,lowerBoundy,upperBoundy,
0129       numBinsz,lowerBoundz,upperBoundz
0130       );
0131   if(logx) BinSet::BinLog(hist->GetXaxis());
0132   if(logy) BinSet::BinLog(hist->GetYaxis());
0133   if(logz) BinSet::BinLog(hist->GetZaxis());
0134   HistConfig *config = new HistConfig();
0135   config->logx = logx;
0136   config->logy = logy;
0137   config->logz = logz;
0138   this->RegisterHist(varname,hist,config);
0139 };
0140 
0141 
0142 // define a 4D histogram
0143 void Histos::DefineHist4D(
0144     TString varname,
0145     TString vartitlew, TString vartitlex, TString vartitley, TString vartitlez,
0146     TString unitsw, TString unitsx, TString unitsy, TString unitsz,
0147     Int_t numBinsw, Double_t lowerBoundw, Double_t upperBoundw,
0148     Int_t numBinsx, Double_t lowerBoundx, Double_t upperBoundx,
0149     Int_t numBinsy, Double_t lowerBoundy, Double_t upperBoundy,
0150     Int_t numBinsz, Double_t lowerBoundz, Double_t upperBoundz,
0151     Bool_t logw, Bool_t logx, Bool_t logy, Bool_t logz
0152     ) {
0153   if(unitsw!="") unitsw=" ["+unitsw+"]";
0154   if(unitsx!="") unitsx=" ["+unitsx+"]";
0155   if(unitsy!="") unitsy=" ["+unitsy+"]";
0156   if(unitsz!="") unitsz=" ["+unitsz+"]";
0157   TString histT;
0158   if(varname.Contains("_xsec")) histT = "d^{4}#sigma/d"+vartitlew+vartitlex+vartitley+vartitlez;
0159   else if(varname.Contains("_fuu")) histT = "F_{UU} vs. "+vartitlez+" vs. "+vartitley+" vs. "+vartitlex+" vs. "+vartitlew;
0160   else if(varname.Contains("_fut")) histT = "F_{UT} vs. "+vartitlez+" vs. "+vartitley+" vs. "+vartitlez+" vs. "+vartitlew;
0161   else histT = vartitlez+" vs. "+vartitley+" vs. "+vartitlex+" vs. "+vartitlew+" distribution";
0162   Hist4D *hist = new Hist4D(
0163       setname+"_hist_"+varname,
0164       histT+", "+settitle+";"+vartitlew+unitsw+";"+vartitlex+unitsx+";"+vartitley+unitsy+";"+vartitlez+unitsz,
0165       numBinsw,lowerBoundw,upperBoundw,
0166       numBinsx,lowerBoundx,upperBoundx,
0167       numBinsy,lowerBoundy,upperBoundy,
0168       numBinsz,lowerBoundz,upperBoundz
0169       );
0170   if(logw) BinSet::BinLog(hist->GetWaxis());
0171   if(logx) BinSet::BinLog(hist->GetXaxis());
0172   if(logy) BinSet::BinLog(hist->GetYaxis());
0173   if(logz) BinSet::BinLog(hist->GetZaxis());
0174   HistConfig *config = new HistConfig();
0175   config->logw = logw;
0176   config->logx = logx;
0177   config->logy = logy;
0178   config->logz = logz;
0179   this->RegisterHist4(varname,hist,config);
0180 };
0181 
0182 
0183 // add histogram to containers
0184 void Histos::RegisterHist(TString varname_, TH1 *hist_, HistConfig *config_) {
0185   VarNameList.push_back(varname_);
0186   histConfigMap.insert({varname_,config_});
0187   histMap.insert({varname_,hist_});
0188 };
0189 
0190 void Histos::RegisterHist4(TString varname_, Hist4D *hist_, HistConfig *config_) {
0191   VarNameList.push_back(varname_);
0192   hist4ConfigMap.insert({varname_,config_});
0193   hist4Map.insert({varname_,hist_});
0194 };
0195 
0196 
0197 // access histogram by name
0198 TH1 *Histos::Hist(TString histName, Bool_t silence) {
0199   TH1 *retHist;
0200   try { retHist = histMap.at(histName); }
0201   catch(const std::out_of_range &ex) {
0202     if(!silence)
0203       cerr << "ERROR: histMap does not have " 
0204            << histName << " histogram" << endl;
0205     return nullptr;
0206   };
0207   return retHist;
0208 };
0209 
0210 Hist4D *Histos::Hist4(TString histName, Bool_t silence) {
0211   Hist4D *retHist;
0212   try { retHist = hist4Map.at(histName); }
0213   catch(const std::out_of_range &ex) {
0214     if(!silence)
0215       cerr << "ERROR: hist4Map does not have " 
0216            << histName << " histogram" << endl;
0217     return nullptr;
0218   };
0219   return retHist;
0220 };
0221 
0222 
0223 // access histogram config by name
0224 HistConfig *Histos::GetHistConfig(TString histName) {
0225   HistConfig *retConfig;
0226   try { retConfig = histConfigMap.at(histName); }
0227   catch(const std::out_of_range &ex) {
0228     cerr << "ERROR: histConfigMap does not have " 
0229          << histName << "histogram" << endl;
0230     return nullptr;
0231   };
0232   return retConfig;
0233 };
0234 
0235 HistConfig *Histos::GetHist4Config(TString histName) {
0236   HistConfig *retConfig;
0237   try { retConfig = hist4ConfigMap.at(histName); }
0238   catch(const std::out_of_range &ex) {
0239     cerr << "ERROR: hist4ConfigMap does not have " 
0240          << histName << "histogram" << endl;
0241     return nullptr;
0242   };
0243   return retConfig;
0244 };
0245 
0246 
0247 // get a specific CutDef
0248 CutDef *Histos::GetCutDef(TString varName) {
0249   for(auto cut : CutDefList) {
0250     if(cut->GetVarName() == varName) return cut;
0251   };
0252   cerr << "ERROR: cannot find cut " << varName << " in Histos" << endl;
0253   return nullptr;
0254 };
0255 
0256 // add histograms of `in_histos` to histograms in `this`
0257 void Histos::AddHistos(Histos *in_histos) {
0258   for(auto [key,this_hist] : histMap) {
0259     auto in_hist = in_histos->Hist(key);
0260     if(in_hist==nullptr) continue;
0261     this_hist->Add(in_hist);
0262   }
0263   // for(auto [key,this_hist] : hist4Map) {
0264   //   auto in_hist = in_histos->Hist4(key);
0265   //   if(in_hist==nullptr) continue;
0266   //   this_hist->Add(in_hist); // TODO: no add method
0267   // }
0268 }
0269 
0270 Histos::~Histos() {
0271   for(auto it : CutDefList)
0272     if(it) delete it;
0273   auto del = [] (auto m) {
0274     for(auto [k,v] : m)
0275       if(v) delete v;
0276     m.clear();
0277   };
0278   del(histMap);
0279   del(hist4Map);
0280   del(histConfigMap);
0281   del(hist4ConfigMap);
0282 }