23 #include <TDirectory.h>
24 #include <ROOT/RDataFrame.hxx>
25 #include <ROOT/RDF/HistoModels.hxx>
34 using RH1Ptr = ROOT::RDF::RResultPtr<::TH1D>;
35 using RH2Ptr = ROOT::RDF::RResultPtr<::TH2D>;
38 using THBins = tuple<size_t, double, double>;
39 using TH1Def = tuple<string, string, THBins>;
40 using TH2Def = tuple<string, string, THBins, THBins>;
53 gErrorIgnoreLevel = kError;
54 cout <<
"\n Beginning sector boundary-masking check..." << endl;
59 const string sOutput =
"test.root";
60 const string sInput =
"../TruthMatching/input/merged/sPhenixG4_oneMatchPerParticle_oldEvaluator.pt020num5evt500pim.d19m10y2023.root";
61 const string sInTuple =
"ntp_track";
64 const double maskSize = 0.02;
67 const array<float, NSectors> arrMeanGuess = {
81 const float sigmaGuess = maskSize;
82 const float ampGuess = 10.;
83 const float fitRange = 2. * maskSize;
88 enum Var {VEne, VPhi, VDPt, VFrac};
94 const vector<THBins> vecBins = {
102 enum Cut {Before, Left, Cut};
103 enum Hist {HPtReco, HPtTrue, HPtFrac, HPhi, HDPt, HDPtVsPhi};
106 const vector<string> vecCutLabels = {
113 const vector<string> vecAxisTitles = {
114 ";p_{T}^{reco} [GeV/c];counts",
115 ";p_{T}^{true} [GeV/c];counts",
116 ";p_{T}^{reco}/p_{T}^{true};counts",
117 ";#varphi^{trk};counts",
118 ";#deltap_{T}^{reco}/p_{T}^{reco}",
119 ";#varphi^{trk};#deltap_{T}^{reco}/p_{T}^{reco}"
123 const vector<Leaves> vecLeaves = {
125 make_pair(
"gpt",
""),
126 make_pair(
"ptFrac",
""),
127 make_pair(
"phi",
""),
128 make_pair(
"ptErr",
""),
129 make_pair(
"phi",
"ptErr")
138 const vector<pair<Leaves, TH1Def>> vecHistDef1D = {
139 make_pair(vecLeaves[Hist::HPtReco],
make_tuple(
"hPtReco", vecAxisTitles[Hist::HPtReco].
data(), vecBins[Var::VEne])),
140 make_pair(vecLeaves[Hist::HPtTrue],
make_tuple(
"hPtTrue", vecAxisTitles[Hist::HPtTrue].
data(), vecBins[Var::VEne])),
141 make_pair(vecLeaves[Hist::HPtFrac],
make_tuple(
"hPtFrac", vecAxisTitles[Hist::HPtFrac].
data(), vecBins[Var::VFrac])),
142 make_pair(vecLeaves[Hist::HPhi],
make_tuple(
"hPhi", vecAxisTitles[Hist::HPhi].
data(), vecBins[Var::VPhi])),
143 make_pair(vecLeaves[Hist::HDPt],
make_tuple(
"hDeltaPt", vecAxisTitles[Hist::HDPt].
data(), vecBins[Var::VDPt]))
153 const vector<pair<Leaves, TH2Def>> vecHistDef2D = {
154 make_pair(vecLeaves[Hist::HDPtVsPhi],
make_tuple(
"hDPtVsPhi", vecAxisTitles[Hist::HDPtVsPhi].
data(), vecBins[Var::VPhi], vecBins[Var::VDPt]))
156 cout <<
" Defined histograms." << endl;
159 const size_t nCuts = vecCutLabels.size();
162 vector<pair<Leaves, TH1Mod>> vecArgAndHistRow1D;
163 vector<pair<Leaves, TH2Mod>> vecArgAndHistRow2D;
164 vector<vector<pair<Leaves, TH1Mod>>> vecArgAndHist1D(nCuts);
165 vector<vector<pair<Leaves, TH2Mod>>> vecArgAndHist2D(nCuts);
168 for (
const string cut : vecCutLabels) {
171 vecArgAndHistRow1D.clear();
172 for (
const auto histDef1D : vecHistDef1D) {
175 string name = get<0>(histDef1D.second);
181 vecArgAndHistRow1D.push_back(make_pair(histDef1D.first, hist));
183 vecArgAndHist1D.push_back(vecArgAndHistRow1D);
186 vecArgAndHistRow2D.clear();
187 for (
const auto histDef2D : vecHistDef2D) {
190 string name = get<0>(histDef2D.second);
194 THBins xBins = get<2>(histDef2D.second);
195 THBins yBins = get<3>(histDef2D.second);
196 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));
197 vecArgAndHistRow2D.push_back(make_pair(histDef2D.first, hist));
199 vecArgAndHist2D.push_back(vecArgAndHistRow2D);
201 cout <<
" Created histograms." << endl;
206 TFile* fOutput =
new TFile(sOutput.data(),
"recreate");
208 cerr <<
"PANIC: couldn't open a file!\n"
209 <<
" fOutput = " << fOutput <<
"\n"
213 cout <<
" Opened output file." << endl;
216 RDataFrame frame(sInTuple.data(), sInput.data());
219 auto tracks = frame.Count();
221 cerr <<
"Error: No tracks found!" << endl;
224 cout <<
" Set up data frame." << endl;
228 auto getFrac = [](
const float a,
const float b) {
232 auto isInMask = [](
const float phi,
const double mask,
const auto& sectors) {
234 for (
const float sector : sectors) {
235 if ((phi > (sector - (mask / 2.))) && (phi < (sector + (mask / 2.)))) {
246 auto before = frame.Define(
"ptFrac", getFrac, {
"pt",
"gpt"})
247 .Define(
"ptErr", getFrac, {
"deltapt",
"pt"});
250 vector<vector<RH1Ptr>> vecHistResult1D(nCuts);
251 vector<vector<RH2Ptr>> vecHistResult2D(nCuts);
252 for (
const auto argAndHistBefore1D : vecArgAndHist1D[Cut::Before]) {
253 auto hist1D = before.Histo1D(argAndHistBefore1D.second, argAndHistBefore1D.first.first.data());
254 vecHistResult1D[Cut::Before].push_back(hist1D);
256 for (
const auto argAndHistBefore2D : vecArgAndHist2D[Cut::Before]) {
257 auto hist2D = before.Histo2D(argAndHistBefore2D.second, argAndHistBefore2D.first.first.data(), argAndHistBefore2D.first.second.data());
258 vecHistResult2D[Cut::Before].push_back(hist2D);
262 vector<vector<TH1D*>> vecOutHist1D(nCuts);
263 vector<vector<TH2D*>> vecOutHist2D(nCuts);
264 for (
auto histResultBefore1D : vecHistResult1D[Cut::Before]) {
265 TH1D* hist1D = (TH1D*) histResultBefore1D -> Clone();
266 vecOutHist1D[Cut::Before].push_back(hist1D);
268 for (
auto histResultBefore2D : vecHistResult2D[Cut::Before]) {
269 TH2D* hist2D = (TH2D*) histResultBefore2D -> Clone();
270 vecOutHist2D[Cut::Before].push_back(hist2D);
278 array<float, NSectors> arrSectors;
285 for (
auto histRow1D : vecOutHist1D) {
286 for (
auto hist1D : histRow1D) {
290 for (
auto histRow2D : vecOutHist2D) {
291 for (
auto hist2D : histRow2D) {
295 cout <<
" Saved histograms." << endl;
300 cout <<
" Finished sector boundary-masking check!\n" << endl;