finish implementation of and use DrawVar

This commit is contained in:
Marius Pfeiffer 2023-11-08 17:25:56 +01:00
parent 0990514af4
commit 6a86a76608

View File

@ -32,34 +32,58 @@ const int nBins = 200;
template <typename TVar> template <typename TVar>
class DrawVar class DrawVar
{ {
private: private:
std::string _identifier; std::string _identifier;
TVar _min; TVar _min;
TVar _max; TVar _max;
TVar _value = TVar{}; TVar _value = TVar{};
TH1 *_histogram = nullptr; TH1 *_histogram = nullptr;
public:
DrawVar(std::string identifier, TVar min, TVar max)
: _identifier { identifier }, _min { min }, _max { max }
{
}
std::string getIdentifier() { public:
return _identifier; DrawVar(std::string identifier, TVar min, TVar max)
} : _identifier{identifier}, _min{min}, _max{max}
{
}
TVar getMin() { std::string getId()
return _min; {
} return _identifier;
}
TVar getMax() { const char *getIdStr()
return _max; {
} return _identifier.c_str();
}
TVar* getValuePointer() { TVar getMin()
return &_value; {
} return _min;
}
TVar getMax()
{
return _max;
}
TVar *getValuePointer()
{
return &_value;
}
TVar getValue()
{
return _value;
}
TH1 *getHist()
{
return _histogram;
}
void setHist(TH1D *hist)
{
_histogram = hist;
}
}; };
int general_cuts() int general_cuts()
@ -84,8 +108,7 @@ int general_cuts()
gStyle->SetOptFit(0); gStyle->SetOptFit(0);
std::vector<DrawVar<Float_t>> float_vars{ std::vector<DrawVar<Float_t>> float_vars{
DrawVar<Float_t>{"B0_PT", 0., 20000.} DrawVar<Float_t>("B0_PT", 0., 20000.)};
};
std::vector<DrawVar<Double_t>> double_vars{ std::vector<DrawVar<Double_t>> double_vars{
}; };
@ -107,16 +130,14 @@ int general_cuts()
for (size_t i = 0; i < float_vars.size(); i++) for (size_t i = 0; i < float_vars.size(); i++)
{ {
auto the_var = &float_vars[i]; chain->SetBranchAddress(float_vars[i].getIdStr(), float_vars[i].getValuePointer());
chain->SetBranchAddress(the_var->identifier.c_str(), &(the_var->value)); float_vars[i].setHist(new TH1D(TString::Format("h1_%s", float_vars[i].getIdStr()).Data(), float_vars[i].getIdStr(), nBins, float_vars[i].getMin(), float_vars[i].getMax()));
the_var->histogram = new TH1D(TString::Format("h1_%s", the_var->identifier.c_str()).Data(), the_var->identifier.c_str(), nBins, the_var->min, the_var->max);
} }
for (size_t i = 0; i < double_vars.size(); i++) for (size_t i = 0; i < double_vars.size(); i++)
{ {
auto the_var = &double_vars[i]; chain->SetBranchAddress(double_vars[i].getIdStr(), double_vars[i].getValuePointer());
chain->SetBranchAddress(the_var->identifier.c_str(), &(the_var->value)); double_vars[i].setHist(new TH1D(TString::Format("h1_%s", double_vars[i].getIdStr()).Data(), double_vars[i].getIdStr(), nBins, double_vars[i].getMin(), double_vars[i].getMax()));
the_var->histogram = new TH1D(TString::Format("h1_%s", the_var->identifier.c_str()).Data(), the_var->identifier.c_str(), nBins, the_var->min, the_var->max);
} }
unsigned int entries = chain->GetEntries(); unsigned int entries = chain->GetEntries();
@ -128,12 +149,12 @@ int general_cuts()
for (size_t i = 0; i < float_vars.size(); i++) for (size_t i = 0; i < float_vars.size(); i++)
{ {
float_vars[i].histogram->Fill(float_vars[i].value); float_vars[i].getHist()->Fill(float_vars[i].getValue());
} }
for (size_t i = 0; i < double_vars.size(); i++) for (size_t i = 0; i < double_vars.size(); i++)
{ {
double_vars[i].histogram->Fill(double_vars[i].value); double_vars[i].getHist()->Fill(double_vars[i].getValue());
} }
} }
@ -143,28 +164,33 @@ int general_cuts()
c1->Draw(); c1->Draw();
TCanvas *c2 = new TCanvas("c2", "Canvas 2", 0, 0, 1200, 600); if (float_vars.size() > 0)
c2->Divide(4, 2);
for (size_t i = 0; i < float_vars.size(); i++)
{ {
c2->cd(i+1); TCanvas *c2 = new TCanvas("c2", "Canvas 2", 0, 0, 1200, 600);
float_vars[i].histogram->Draw(); c2->Divide(4, 2);
for (size_t i = 0; i < float_vars.size(); i++)
{
c2->cd(i + 1);
float_vars[i].getHist()->Draw();
}
c2->Draw();
} }
c2->Draw(); if (double_vars.size() > 0)
TCanvas *c3 = new TCanvas("c3", "Canvas 3", 0, 0, 1200, 600);
c3->Divide(4, 2);
for (size_t i = 0; i < double_vars.size(); i++)
{ {
c3->cd(i+1); TCanvas *c3 = new TCanvas("c3", "Canvas 3", 0, 0, 1200, 600);
double_vars[i].histogram->Draw(); c3->Divide(4, 2);
for (size_t i = 0; i < double_vars.size(); i++)
{
c3->cd(i + 1);
double_vars[i].getHist()->Draw();
}
c3->Draw();
} }
c3->Draw();
return 0; return 0;
} }