Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Helpers.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file Helpers.cpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2019-2023 CERN for the benefit of the Acts project
4 //
5 // This Source Code Form is subject to the terms of the Mozilla Public
6 // License, v. 2.0. If a copy of the MPL was not distributed with this
7 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 
10 
11 #include <cassert>
12 
13 #include <TAxis.h>
14 #include <TEfficiency.h>
15 #include <TFitResult.h>
16 #include <TFitResultPtr.h>
17 #include <TH1.h>
18 #include <TH2.h>
19 #include <TProfile.h>
20 
21 namespace ActsExamples {
22 
23 namespace PlotHelpers {
24 TH1F* bookHisto(const char* histName, const char* histTitle,
25  const Binning& varBinning) {
26  TH1F* hist =
27  new TH1F(histName, histTitle, varBinning.nBins(), varBinning.data());
28  hist->GetXaxis()->SetTitle(varBinning.title().c_str());
29  hist->GetYaxis()->SetTitle("Entries");
30  hist->Sumw2();
31  return hist;
32 }
33 
34 TH2F* bookHisto(const char* histName, const char* histTitle,
35  const Binning& varXBinning, const Binning& varYBinning) {
36  TH2F* hist =
37  new TH2F(histName, histTitle, varXBinning.nBins(), varXBinning.data(),
38  varYBinning.nBins(), varYBinning.data());
39  hist->GetXaxis()->SetTitle(varXBinning.title().c_str());
40  hist->GetYaxis()->SetTitle(varYBinning.title().c_str());
41  hist->Sumw2();
42  return hist;
43 }
44 
45 void fillHisto(TH1F* hist, float value, float weight) {
46  assert(hist != nullptr);
47  hist->Fill(value, weight);
48 }
49 
50 void fillHisto(TH2F* hist, float xValue, float yValue, float weight) {
51  assert(hist != nullptr);
52  hist->Fill(xValue, yValue, weight);
53 }
54 
55 void anaHisto(TH1D* inputHist, int j, TH1F* meanHist, TH1F* widthHist) {
56  // evaluate mean and width via the Gauss fit
57  assert(inputHist != nullptr);
58  if (inputHist->GetEntries() > 0) {
59  TFitResultPtr r = inputHist->Fit("gaus", "QS0");
60  if ((r.Get() != nullptr) and ((r->Status() % 1000) == 0)) {
61  // fill the mean and width into 'j'th bin of the meanHist and widthHist,
62  // respectively
63  meanHist->SetBinContent(j, r->Parameter(1));
64  meanHist->SetBinError(j, r->ParError(1));
65  widthHist->SetBinContent(j, r->Parameter(2));
66  widthHist->SetBinError(j, r->ParError(2));
67  }
68  }
69 }
70 
71 TEfficiency* bookEff(const char* effName, const char* effTitle,
72  const Binning& varBinning) {
73  TEfficiency* efficiency =
74  new TEfficiency(effName, effTitle, varBinning.nBins(), varBinning.data());
75  return efficiency;
76 }
77 
78 TEfficiency* bookEff(const char* effName, const char* effTitle,
79  const Binning& varXBinning, const Binning& varYBinning) {
80  TEfficiency* efficiency = new TEfficiency(
81  effName, effTitle, varXBinning.nBins(), varXBinning.data(),
82  varYBinning.nBins(), varYBinning.data());
83  return efficiency;
84 }
85 
86 void fillEff(TEfficiency* efficiency, float value, bool status) {
87  assert(efficiency != nullptr);
88  efficiency->Fill(status, value);
89 }
90 
91 void fillEff(TEfficiency* efficiency, float xValue, float yValue, bool status) {
92  assert(efficiency != nullptr);
93  efficiency->Fill(status, xValue, yValue);
94 }
95 
96 TProfile* bookProf(const char* profName, const char* profTitle,
97  const Binning& varXBinning, const Binning& varYBinning) {
98  TProfile* prof =
99  new TProfile(profName, profTitle, varXBinning.nBins(), varXBinning.data(),
100  varYBinning.low(), varYBinning.high());
101  prof->GetXaxis()->SetTitle(varXBinning.title().c_str());
102  prof->GetYaxis()->SetTitle(varYBinning.title().c_str());
103  return prof;
104 }
105 
106 void fillProf(TProfile* profile, float xValue, float yValue, float weight) {
107  assert(profile != nullptr);
108  profile->Fill(xValue, yValue, weight);
109 }
110 
111 } // namespace PlotHelpers
112 
113 } // namespace ActsExamples