Back to home page

EIC code displayed by LXR

 
 

    


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

0001 // SPDX-License-Identifier: LGPL-3.0-or-later
0002 // Copyright (C) 2023 Christopher Dilks, Connor Pecar, Duane Byer
0003 
0004 #pragma once
0005 
0006 #include <map>
0007 #include <iomanip>
0008 
0009 // root
0010 #include <TFile.h>
0011 #include <TCanvas.h>
0012 #include <TRegexp.h>
0013 #include <TSystem.h>
0014 #include <TROOT.h>
0015 #include <TStyle.h>
0016 #include <TGaxis.h>
0017 #include <TLegend.h>
0018 #include <TProfile.h>
0019 #include <TProfile2D.h>
0020 
0021 // adage
0022 #include <adage/CutDef.h>
0023 #include <adage/BinSet.h>
0024 
0025 // epic-analysis
0026 #include "Histos.h"
0027 #include "Kinematics.h"
0028 #include "HistosDAG.h"
0029 
0030 class PostProcessor
0031 {
0032   public:
0033     PostProcessor(
0034         TString infileN_,
0035         TString outfileN_=""
0036         );
0037     ~PostProcessor();
0038 
0039     // settings
0040     // - canvas dimensions [pixels]
0041     const Int_t dimx=800;
0042     const Int_t dimy=700;
0043     static const int nsumMax=3; // number of summary plots with formatting
0044 
0045 
0046     // DAG interfaces:
0047     HistosDAG *GetHistosDAG() { return HD; };
0048     HistosDAG *Op() { return GetHistosDAG(); }; // syntactic sugar
0049     // execute lambdas (if `clear`==false, lambda operators will not be removed after execution)
0050     void Execute(Bool_t clear=true) {
0051       if(clear) HD->ExecuteAndClearOps();
0052       else HD->ExecuteOps();
0053     };
0054 
0055 
0056     // cleanup and close open files and streams
0057     // - MUST be called at the end of any postprocessor macro, after
0058     //   all algorithms have finished
0059     void Finish();
0060 
0061 
0062     // algorithms: useful to run in loops over bins
0063     // - see PostProcessor.cxx for descriptions for how to use these
0064     // - these are general functions that operate either on Histos 
0065     //   objects, or on histograms
0066     // - they can be shared in any postprocessor macro
0067     // - they can be anything, such as taking ratios of each histogram
0068     //   from two different Histos objects (e.g., y>0.05 / y>0.00 sets)
0069     // - you are welcome to add your own algorithms
0070     void DumpHist(TString datFile, TString histSet, TString varName);
0071     void DumpAve(TString datFile, Histos *H, TString cutName);
0072     void DrawSingle(Histos *H, TString histName, TString drawFormat="", Int_t profileAxis=0, Bool_t profileOnly=false);
0073     void DrawSingle(TString histSet, TString histName);
0074     void DrawRatios(
0075         TString outName, Histos *numerSet, Histos *denomSet, Bool_t plotRatioOnly=false
0076         );
0077     void DrawInBins(
0078         TString outName,
0079         std::vector<std::vector<Histos*>>& histArr, TString histName,
0080         TString var1name, int nvar1, double var1low, double var1high, bool var1log,
0081         TString var2name, int nvar2, double var2low, double var2high, bool var2log,
0082         bool intgrid1=false, bool intgrid2=false,
0083         bool renormalize=false
0084         );
0085     void DrawInBins(
0086         TString outName,
0087         std::vector<std::vector<std::vector<Histos*>>>& histArrList, TString histName,
0088         TString var1name, int nvar1, double var1low, double var1high, bool var1log,
0089         TString var2name, int nvar2, double var2low, double var2high, bool var2log,
0090         bool intgrid1=false, bool intgrid2=false,
0091         bool renormalize=false
0092         );
0093     void DrawRatioInBins(
0094         TString outName,
0095         std::vector<std::vector<Histos*>>& histArr, TString histName,
0096         TString var1name, int nvar1, double var1low, double var1high, bool var1log,
0097         TString var2name, int nvar2, double var2low, double var2high, bool var2log,
0098         bool intgrid1=false, bool intgrid2=false,
0099         bool renormalize=false
0100         );
0101     void DrawRatioInBins(
0102         TString outName,
0103         std::vector<std::vector<std::vector<Histos*>>>& histArrList, TString histName,
0104         TString var1name, int nvar1, double var1low, double var1high, bool var1log,
0105         TString var2name, int nvar2, double var2low, double var2high, bool var2log,
0106         bool intgrid1=false, bool intgrid2=false,
0107         bool renormalize=false
0108         );
0109 
0110     // algorithm finish methods; to be called after loops
0111     void FinishDumpAve(TString datFile);
0112     void FinishDrawRatios(TString summaryDir);
0113 
0114     // vector of labels for a legend (push elements externally if you want to use this)
0115     std::vector<TString> legendLabels;
0116 
0117     // accessors
0118     TString GetPngDir() { return pngDir; };
0119     TString GetOutfileName() { return outfileN; };
0120     TFile *GetOutfile() { return outfile; };
0121     BinSet *GetBinSet(TString varName);
0122     CutDef *GetBinCut(TString varName, Int_t binNum);
0123     std::vector<int> GetBinNums(TString varName);
0124 
0125     // text file manipulation
0126     void StartTextFile(TString datFile, TString firstLine="");
0127     void AppendToTextFile(TString datFile, TString appendText);
0128     void Columnify(TString inputFile, TString outputFile);
0129     void PrintTextFile(TString datFile);
0130 
0131     // return true if the bin is "full" range, and it's not the only bin
0132     Bool_t SkipFull(TString varName, Int_t binNum);
0133 
0134     // reset algorithm-specific variables
0135     void ResetVars();
0136 
0137     
0138     // ------------
0139     // zoom out the vertical scale for the case where multiple
0140     // `TH1`s have been drawn with the "SAME" option, but the y-axis
0141     // range is improperly zoomed
0142     // - example: `UnzoomVertical(canvas->GetPad(3))`
0143     // - optionally specify a new title 
0144     // - set `min0` to true if you want to lock the minimum at zero
0145     static void UnzoomVertical(TVirtualPad *pad, TString title="", Bool_t min0=false, Bool_t logy=false) {
0146       Double_t max=-1e6;
0147       Double_t min=1e6;
0148       Double_t maxTmp,minTmp;
0149       for(auto obj : *pad->GetListOfPrimitives()) {
0150         if(obj->InheritsFrom(TH1::Class())) {
0151           maxTmp = ((TH1*)obj)->GetMaximum();
0152           minTmp = ((TH1*)obj)->GetMinimum();
0153           max = maxTmp > max ? maxTmp : max;
0154           min = minTmp < min ? minTmp : min;
0155         };
0156       };
0157       max += 0.05*(max-min);
0158       //min -= 0.05*(max-min);
0159       for(auto obj : *pad->GetListOfPrimitives()) {
0160         if(obj->InheritsFrom(TH1::Class())) {
0161           Double_t drawMin;
0162           if(logy) drawMin = 0.001*max;
0163           else drawMin = min0 ? 0:min;
0164           ((TH1*)obj)->GetYaxis()->SetRangeUser(drawMin,max);
0165           if(title!="") ((TH1*)obj)->SetTitle(title);
0166         };
0167       };
0168     };
0169     // ------------
0170 
0171 
0172 
0173   private:
0174 
0175     // files and names
0176     TString infileN, outfileN, pngDir;
0177     TFile *infile, *outfile;
0178 
0179     // DAGs
0180     HistosDAG *HD;
0181 
0182     // algorithm-specific variables
0183     std::map<TString,TCanvas*> summaryCanvMap;
0184     std::vector<TString> varList;
0185     int nsum,ndump;
0186     CutDef *dumpCut;
0187     TCanvas *summaryCanv;
0188     Color_t summaryColor[nsumMax];
0189     Style_t summaryStyle[nsumMax];
0190 
0191   ClassDef(PostProcessor,1);
0192 };