Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-05 08:10:55

0001 // ----------------------------------------------------------------------------
0002 // 'CheckDeltaPtWithBoundaryMasks.cxx
0003 // Derek Anderson
0004 // 10.30.2023
0005 //
0006 // Use to quickly check what the delta-pt/pt distribution
0007 // looks like if we mask the TPC sector boundaries.
0008 // ----------------------------------------------------------------------------
0009 
0010 // standard c libraries
0011 #include <array>
0012 #include <string>
0013 #include <vector>
0014 #include <utility>
0015 #include <iostream>
0016 // ROOT libraries
0017 #include <TH1.h>
0018 #include <TH2.h>
0019 #include <TF1.h>
0020 #include <TFile.h>
0021 #include <TError.h>
0022 #include <TNtuple.h>
0023 #include <TDirectory.h>
0024 #include <ROOT/RDataFrame.hxx>
0025 #include <ROOT/RDF/HistoModels.hxx>
0026 
0027 // make common namespaces implicit
0028 using namespace std;
0029 using namespace ROOT;
0030 
0031 // set up RDF aliases
0032 using TH1Mod = ROOT::RDF::TH1DModel;
0033 using TH2Mod = ROOT::RDF::TH2DModel;
0034 using RH1Ptr = ROOT::RDF::RResultPtr<::TH1D>;
0035 using RH2Ptr = ROOT::RDF::RResultPtr<::TH2D>;
0036 
0037 // set up tuple aliases
0038 using THBins = tuple<size_t, double, double>;
0039 using TH1Def = tuple<string, string, THBins>;
0040 using TH2Def = tuple<string, string, THBins, THBins>;
0041 using Leaves = pair<string, string>;
0042 
0043 // global constants
0044 static const size_t NSectors = 12;
0045 
0046 
0047 
0048 // check delta-pt/pt after masking sector boundaries --------------------------
0049 
0050 void CheckDeltaPtWithBoundaryMasks() {
0051 
0052   // lower verbosity
0053   gErrorIgnoreLevel = kError;
0054   cout << "\n  Beginning sector boundary-masking check..." << endl;
0055 
0056   // options ------------------------------------------------------------------
0057 
0058   // i/o parameters
0059   const string sOutput  = "test.root";
0060   const string sInput   = "../TruthMatching/input/merged/sPhenixG4_oneMatchPerParticle_oldEvaluator.pt020num5evt500pim.d19m10y2023.root";
0061   const string sInTuple = "ntp_track";
0062 
0063   // mask parameters
0064   const double maskSize = 0.02;
0065 
0066   // fit guesses
0067   const array<float, NSectors> arrMeanGuess = {
0068     -2.877,
0069     -2.354,
0070     -1.831,
0071     -1.308,
0072     -0.785,
0073     -0.262,
0074     0.262,
0075     0.785,
0076     1.308,
0077     1.831,
0078     2.354,
0079     2.877
0080   };
0081   const float sigmaGuess = maskSize;
0082   const float ampGuess   = 10.;
0083   const float fitRange   = 2. * maskSize;
0084 
0085   // histogram definitions ----------------------------------------------------
0086 
0087   // binning accessor
0088   enum Var {VEne, VPhi, VDPt, VFrac};
0089 
0090   // other variable binning
0091   //   <0> = no. of bins
0092   //   <1> = 1st bin lower edge
0093   //   <2> = last bin upper edge
0094   const vector<THBins> vecBins = {
0095     make_tuple(200,    0.,   100.),
0096     make_tuple(360,   -3.15, 3.15),
0097     make_tuple(5000,   0.,   5.),
0098     make_tuple(5000,   0.,   5.)
0099   };
0100 
0101   // histogram accessors
0102   enum Cut  {Before, Left, Cut};
0103   enum Hist {HPtReco, HPtTrue, HPtFrac, HPhi, HDPt, HDPtVsPhi};
0104 
0105   // before vs. after labels
0106   const vector<string> vecCutLabels = {
0107     "_beforeMask",
0108     "_afterMask_leftIn",
0109     "_afterMask_cutOut"
0110   };
0111 
0112   // axis titles
0113   const vector<string> vecAxisTitles = {
0114     ";p_{T}^{reco} [GeV/c];counts",
0115     ";p_{T}^{true} [GeV/c];counts",
0116     ";p_{T}^{reco}/p_{T}^{true};counts",
0117     ";#varphi^{trk};counts",
0118     ";#deltap_{T}^{reco}/p_{T}^{reco}",
0119     ";#varphi^{trk};#deltap_{T}^{reco}/p_{T}^{reco}" 
0120   };
0121 
0122   // leaves to draw
0123   const vector<Leaves> vecLeaves = {
0124     make_pair("pt",     ""),
0125     make_pair("gpt",    ""),
0126     make_pair("ptFrac", ""),
0127     make_pair("phi",    ""),
0128     make_pair("ptErr",  ""),
0129     make_pair("phi",    "ptErr")
0130   };
0131 
0132   // 1d histogram definitions
0133   //   first = leaves to draw
0134   //   second = hist definition
0135   //     <0> = histogram name
0136   //     <1> = axis titles
0137   //     <2> = x-axis binning
0138   const vector<pair<Leaves, TH1Def>> vecHistDef1D = {
0139     make_pair(vecLeaves[Hist::HPtReco], make_tuple("hPtReco",  vecAxisTitles[Hist::HPtReco].data(), vecBins[Var::VEne])),
0140     make_pair(vecLeaves[Hist::HPtTrue], make_tuple("hPtTrue",  vecAxisTitles[Hist::HPtTrue].data(), vecBins[Var::VEne])),
0141     make_pair(vecLeaves[Hist::HPtFrac], make_tuple("hPtFrac",  vecAxisTitles[Hist::HPtFrac].data(), vecBins[Var::VFrac])),
0142     make_pair(vecLeaves[Hist::HPhi],    make_tuple("hPhi",     vecAxisTitles[Hist::HPhi].data(),    vecBins[Var::VPhi])),
0143     make_pair(vecLeaves[Hist::HDPt],    make_tuple("hDeltaPt", vecAxisTitles[Hist::HDPt].data(),    vecBins[Var::VDPt]))
0144   };
0145 
0146   // 2d histogram definitions
0147   //   first  = leaves to draw
0148   //   second = hist definition
0149   //     <0> = histogram name
0150   //     <1> = axis titles
0151   //     <2> = x-axis binning
0152   //     <3> = y-axis binning
0153   const vector<pair<Leaves, TH2Def>> vecHistDef2D = {
0154     make_pair(vecLeaves[Hist::HDPtVsPhi], make_tuple("hDPtVsPhi", vecAxisTitles[Hist::HDPtVsPhi].data(), vecBins[Var::VPhi], vecBins[Var::VDPt]))
0155   };
0156   cout << "    Defined histograms." << endl;
0157 
0158   // create histograms
0159   const size_t nCuts = vecCutLabels.size();
0160 
0161   // for storing vectors
0162   vector<pair<Leaves, TH1Mod>>         vecArgAndHistRow1D;
0163   vector<pair<Leaves, TH2Mod>>         vecArgAndHistRow2D;
0164   vector<vector<pair<Leaves, TH1Mod>>> vecArgAndHist1D(nCuts);
0165   vector<vector<pair<Leaves, TH2Mod>>> vecArgAndHist2D(nCuts);
0166 
0167   // instantiate histograms & load into vectors
0168   for (const string cut : vecCutLabels) {
0169 
0170     // make 1d hists
0171     vecArgAndHistRow1D.clear();
0172     for (const auto histDef1D : vecHistDef1D) {
0173 
0174       // make name
0175       string name = get<0>(histDef1D.second);
0176       name += cut;
0177 
0178       // instantiate hist
0179       THBins bins = get<2>(histDef1D.second);
0180       TH1Mod hist = TH1Mod(name.data(), get<1>(histDef1D.second).data(), get<0>(bins), get<1>(bins), get <2>(bins));
0181       vecArgAndHistRow1D.push_back(make_pair(histDef1D.first, hist));
0182     }
0183     vecArgAndHist1D.push_back(vecArgAndHistRow1D);
0184 
0185     // make 2d hists
0186     vecArgAndHistRow2D.clear();
0187     for (const auto histDef2D : vecHistDef2D) {
0188 
0189       // make name
0190       string name = get<0>(histDef2D.second);
0191       name += cut;
0192 
0193       // instantiate hist
0194       THBins xBins = get<2>(histDef2D.second);
0195       THBins yBins = get<3>(histDef2D.second);
0196       TH2Mod hist  = TH2Mod(name.data(), get<1>(histDef2D.second).data(), get<0>(xBins), get<1>(xBins), get <2>(xBins), get<0>(yBins), get<1>(yBins), get<2>(yBins));
0197       vecArgAndHistRow2D.push_back(make_pair(histDef2D.first, hist));
0198     }
0199     vecArgAndHist2D.push_back(vecArgAndHistRow2D);
0200   }  // end cut loop
0201   cout << "    Created histograms." << endl;
0202 
0203   // open files and make frames -----------------------------------------------
0204 
0205   // open output file
0206   TFile* fOutput = new TFile(sOutput.data(), "recreate");
0207   if (!fOutput) {
0208     cerr << "PANIC: couldn't open a file!\n"
0209          << "       fOutput = " << fOutput <<  "\n"
0210          << endl;
0211     return;
0212   }
0213   cout << "    Opened output file." << endl;
0214 
0215   // create data frame
0216   RDataFrame frame(sInTuple.data(), sInput.data());
0217 
0218   // make sure file isn't empty
0219   auto tracks = frame.Count();
0220   if (tracks == 0) {
0221     cerr << "Error: No tracks found!" << endl;
0222     return;
0223   }
0224   cout << "    Set up data frame." << endl;
0225 
0226   // analysis lambdas ---------------------------------------------------------
0227 
0228   auto getFrac = [](const float a, const float b) { 
0229     return a / b;
0230   };  // end 'getFrac(float, float)'
0231 
0232   auto isInMask = [](const float phi, const double mask, const auto& sectors) {
0233     bool inMask = false;
0234     for (const float sector : sectors) {
0235       if ((phi > (sector - (mask / 2.))) && (phi < (sector + (mask / 2.)))) {
0236         inMask = true;
0237         break;
0238       }
0239     }
0240     return inMask;
0241   };  // end 'isInMask(float)'
0242 
0243   // run analyses -------------------------------------------------------------
0244 
0245   // get histograms before masking
0246   auto before = frame.Define("ptFrac", getFrac, {"pt", "gpt"})
0247                      .Define("ptErr",  getFrac, {"deltapt", "pt"});
0248 
0249   // get initial 1d histograms
0250   vector<vector<RH1Ptr>> vecHistResult1D(nCuts);
0251   vector<vector<RH2Ptr>> vecHistResult2D(nCuts);
0252   for (const auto argAndHistBefore1D : vecArgAndHist1D[Cut::Before]) {
0253     auto hist1D = before.Histo1D(argAndHistBefore1D.second, argAndHistBefore1D.first.first.data());
0254     vecHistResult1D[Cut::Before].push_back(hist1D);
0255   }
0256   for (const auto argAndHistBefore2D : vecArgAndHist2D[Cut::Before]) {
0257     auto hist2D = before.Histo2D(argAndHistBefore2D.second, argAndHistBefore2D.first.first.data(), argAndHistBefore2D.first.second.data());
0258     vecHistResult2D[Cut::Before].push_back(hist2D);
0259   }
0260 
0261   // retrieve results
0262   vector<vector<TH1D*>> vecOutHist1D(nCuts);
0263   vector<vector<TH2D*>> vecOutHist2D(nCuts);
0264   for (auto histResultBefore1D : vecHistResult1D[Cut::Before]) {
0265     TH1D* hist1D = (TH1D*) histResultBefore1D -> Clone();
0266     vecOutHist1D[Cut::Before].push_back(hist1D);
0267   }
0268   for (auto histResultBefore2D : vecHistResult2D[Cut::Before]) {
0269     TH2D* hist2D = (TH2D*) histResultBefore2D -> Clone();
0270     vecOutHist2D[Cut::Before].push_back(hist2D);
0271   }
0272 
0273   /* TODO
0274    *   - get sector bourndaries with fits
0275    */
0276 
0277   // for masking sector boundaries
0278   array<float, NSectors> arrSectors;
0279   arrSectors.fill(0.);
0280   
0281   // save & close -------------------------------------------------------------
0282 
0283   // save histograms
0284   fOutput -> cd();
0285   for (auto histRow1D : vecOutHist1D) {
0286     for (auto hist1D : histRow1D) {
0287       hist1D -> Write();
0288     }
0289   }
0290   for (auto histRow2D : vecOutHist2D) {
0291     for (auto hist2D : histRow2D) {
0292       hist2D -> Write();
0293     }
0294   }
0295   cout << "    Saved histograms." << endl;
0296 
0297   // close files
0298   fOutput -> cd();
0299   fOutput -> Close();
0300   cout << "  Finished sector boundary-masking check!\n" << endl;
0301 
0302 }
0303 
0304 // end ------------------------------------------------------------------------