Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-06 08:11:53

0001 // ----------------------------------------------------------------------------
0002 // 'QuickTreePlotter.C
0003 // Derek Anderson
0004 // 08.24.2023
0005 //
0006 // Use to quick plot some leaves from a TTree
0007 // ----------------------------------------------------------------------------
0008 
0009 #include <vector>
0010 #include <iostream>
0011 #include "TH1.h"
0012 #include "TH2.h"
0013 #include "TTree.h"
0014 #include "TFile.h"
0015 #include "TError.h"
0016 #include "TString.h"
0017 #include "TDirectory.h"
0018 
0019 using namespace std;
0020 
0021 
0022 
0023 void QuickTreePlotter() {
0024 
0025   // lower verbosity
0026   gErrorIgnoreLevel = kError;
0027   cout << "\n  Beginning quick tree plotting..." << endl;
0028 
0029   // i/o parameters
0030   const TString sOutput  = "newMatcher.allVsWeirdVsNormal.pt020n15pim.d24m8y2023.root";
0031   const TString sInput   = "input/merged/sPhenixG4_testingNewMatcher_newMatcher_run0.pt020n15pim.d14m8y2023.root";
0032   const TString sInTree = "T";
0033 
0034   // cuts to apply and labels
0035   const vector<TString> vecCutsToApply = {
0036     "(is_matched==1)",
0037     "(is_matched==1)",
0038     "(is_matched==1)"
0039   };
0040   const vector<TString> vecCutLabels = {
0041     "All",
0042     "Weird",
0043     "Normal"
0044   };
0045 
0046   // 1d things to draw and histogram names
0047   const vector<TString> vecToDraw1D = {
0048     "deltapt/pt",
0049     "pt/gpt",
0050     "gphi"
0051   };
0052   const vector<TString> vecHistNames1D = {
0053     "hDeltaPtOverPt",
0054     "hFracPt",
0055     "hTruthPhi"
0056   };
0057 
0058   // 2d things to draw, histogram names, and options
0059   const vector<TString> vecToDraw2D = {
0060     "deltapt/pt:pt",
0061     "deltapt/pt:pt/gpt",
0062     "gphi:pt/gpt"
0063   };
0064   const vector<TString> vecHistNames2D = {
0065     "hDeltaPtOverPtVsPt",
0066     "hDeltaPtOverPtVsFracPt",
0067     "hTruthPhiVsFracPt"
0068   };
0069   const vector<TString> vecHistOpts2D = {
0070     "colz",
0071     "colz",
0072     "colz"
0073   };
0074 
0075   // construct list of output histograms
0076   const Ssiz_t nToDraw1D    = vecToDraw1D.size();
0077   const Ssiz_t nToDraw2D    = vecToDraw2D.size();
0078   const Ssiz_t nCutsToApply = vecCutsToApply.size();
0079 
0080   TString         sHistToDraw1D;
0081   TString         sHistToDraw2D;
0082   vector<TString> vecHistToDraw1D;
0083   vector<TString> vecHistToDraw2D;
0084   for (Ssiz_t iCutToApply = 0; iCutToApply < nCutsToApply; iCutToApply++) {
0085     for (Ssiz_t iToDraw1D = 0; iToDraw1D < nToDraw1D; iToDraw1D++) {
0086 
0087       // construct 1d name
0088       sHistToDraw1D = vecHistNames1D[iToDraw1D].Data();
0089       sHistToDraw1D.Append("_");
0090       sHistToDraw1D.Append(vecCutLabels[iCutToApply].Data());
0091 
0092       // add to list
0093       vecHistToDraw1D.push_back(sHistToDraw1D.Data());
0094     } 
0095     for (Ssiz_t iToDraw2D = 0; iToDraw2D < nToDraw2D; iToDraw2D++) {
0096 
0097       // construct 2d name
0098       sHistToDraw2D = vecHistNames2D[iToDraw2D].Data();
0099       sHistToDraw2D.Append("_");
0100       sHistToDraw2D.Append(vecCutLabels[iCutToApply].Data());
0101 
0102       // add to list
0103       vecHistToDraw2D.push_back(sHistToDraw2D.Data());
0104     }
0105   }  // end cut loop
0106   cout << "    Constructed histogram lists." << endl;
0107 
0108   // open files
0109   TFile* fOutput = new TFile(sOutput.Data(), "recreate");
0110   TFile* fInput  = new TFile(sInput.Data(),  "read");
0111   if (!fOutput || !fInput) {
0112     cerr << "PANIC: couldn't open a file!\n"
0113          << "       fOutput = " << fOutput << ", fInput = " << fInput << "\n"
0114          << endl;
0115     return;
0116   }
0117   cout << "    Opened files." << endl;
0118 
0119   // grab input tuple
0120   TTree* tToDrawFrom = (TNtuple*) fInput -> Get(sInTree.Data());
0121   if (!tToDrawFrom) {
0122     cerr << "PANIC: couldn't grab input tuple!\n" << endl;
0123     return;
0124   }
0125   cout << "    Grabbed input tuple.\n"
0126        << "    Beginning draw loop..."
0127        << endl;
0128 
0129   // draw histograms
0130   Ssiz_t  iHistToDraw1D = 0;
0131   Ssiz_t  iHistToDraw2D = 0;
0132   TString sDrawArg1D    = "";
0133   TString sDrawArg2D    = "";
0134   for (Ssiz_t iCutToApply = 0; iCutToApply < nCutsToApply; iCutToApply++) {
0135     for (Ssiz_t iToDraw1D = 0; iToDraw1D < nToDraw1D; iToDraw1D++) {
0136 
0137       // construct draw arg
0138       sDrawArg1D = vecToDraw1D[iToDraw1D].Data();
0139       sDrawArg1D.Append(">>");
0140       sDrawArg1D.Append(vecHistToDraw1D[iHistToDraw1D].Data());
0141       cout << "      Drawing '" << sDrawArg1D.Data() << "'..." << endl;
0142 
0143       // draw to histogram
0144       tToDrawFrom -> Draw(sDrawArg1D.Data(), vecCutsToApply[iCutToApply].Data());
0145       ++iHistToDraw1D;
0146     } 
0147     for (Ssiz_t iToDraw2D = 0; iToDraw2D < nToDraw2D; iToDraw2D++) {
0148 
0149       // construct draw arg
0150       sDrawArg2D = vecToDraw2D[iToDraw2D].Data();
0151       sDrawArg2D.Append(">>");
0152       sDrawArg2D.Append(vecHistToDraw2D[iHistToDraw2D].Data());
0153       cout << "      Drawing '" << sDrawArg2D.Data() << "'..." << endl;
0154 
0155       // draw to histogram
0156       tToDrawFrom -> Draw(sDrawArg2D.Data(), vecCutsToApply[iCutToApply].Data());
0157       ++iHistToDraw2D;
0158     }
0159   }  // end cut loop
0160   cout << "    Drew histograms from tuple." << endl;
0161 
0162   // grab histograms and save
0163   const Ssiz_t nHistToDraw1D = vecHistToDraw1D.size();
0164   const Ssiz_t nHistToDraw2D = vecHistToDraw2D.size();
0165 
0166   TH1D* hHistToDraw1D[nHistToDraw1D];
0167   TH2D* hHistToDraw2D[nHistToDraw2D];
0168   for (Ssiz_t iHist1D = 0; iHist1D < nHistToDraw1D; iHist1D++) {
0169 
0170     // grab histogram
0171     fInput                 -> cd();
0172     hHistToDraw1D[iHist1D] = (TH1D*) gDirectory -> Get(vecHistToDraw1D[iHist1D].Data());
0173 
0174     // save histogram
0175     fOutput                -> cd();
0176     hHistToDraw1D[iHist1D] -> Write();
0177   }
0178   for (Ssiz_t iHist2D = 0; iHist2D < nHistToDraw2D; iHist2D++) {
0179 
0180     // grab histogram
0181     fInput                 -> cd();
0182     hHistToDraw2D[iHist2D] = (TH2D*) gDirectory -> Get(vecHistToDraw2D[iHist2D].Data());
0183 
0184     // save histogram
0185     fOutput                -> cd();
0186     hHistToDraw2D[iHist2D] -> Write();
0187   }
0188   cout << "    Saved histograms." << endl;
0189 
0190   // close files
0191   fOutput -> cd();
0192   fOutput -> Close();
0193   fInput  -> cd();
0194   fInput  -> Close();
0195   cout << "    Closed files." << endl;
0196 
0197   // announce end and exit
0198   cout << "  Finished quick tree plotting!\n" << endl;
0199   return;
0200 
0201 }
0202 
0203 // end ------------------------------------------------------------------------