104#include "TRestDataSetOdds.h"
106#include "TRestDataSet.h"
156 TiXmlElement* obsDefinition =
GetElement(
"observable");
157 while (obsDefinition !=
nullptr) {
159 if (obsName.empty() || obsName ==
"Not defined") {
160 RESTError <<
"< observable variable key does not contain a name!" <<
RESTendl;
167 if (range.empty() || range ==
"Not defined") {
168 RESTError <<
"< observable key does not contain a range value!" <<
RESTendl;
176 if (nBins.empty() || nBins ==
"Not defined") {
177 RESTError <<
"< observable key does not contain a nBins value!" <<
RESTendl;
187 RESTError <<
"No observables provided, exiting..." <<
RESTendl;
214 for (
size_t i = 0; i <
fObsName.size(); i++) {
215 const std::string obsName =
fObsName[i];
217 const std::string histName =
"h" + std::string(GetName()) +
"_" + obsName;
219 RESTDebug <<
"\tGenerating PDF for " << obsName <<
" with range: (" << range.X() <<
", "
220 << range.Y() <<
") and nBins: " << nBins <<
RESTendl;
222 DF.Histo1D({histName.c_str(), histName.c_str(), nBins, range.X(), range.Y()}, obsName);
223 TH1F* h =
static_cast<TH1F*
>(histo->DrawClone());
224 RESTDebug <<
"\tNormalizing by integral = " << h->Integral() <<
RESTendl;
225 h->Scale(1. / h->Integral());
229 TFile* f = TFile::Open(
fOddsFile.c_str());
235 for (
size_t i = 0; i <
fObsName.size(); i++) {
236 const std::string obsName =
fObsName[i];
237 const std::string histName =
"h" + std::string(GetName()) +
"_" + obsName;
238 TH1F* h = (TH1F*)f->Get(histName.c_str());
244 std::string totName =
"";
246 for (
const auto& [obsName, histo] :
fHistos) {
247 const std::string oddsName = std::string(GetName()) +
"_" + obsName;
248 auto GetLogOdds = [&histo = histo](
double val) {
249 double odds = histo->GetBinContent(histo->GetXaxis()->FindBin(val));
250 if (odds == 0)
return 1000.;
251 return log(1. - odds) - log(odds);
254 if (df.GetColumnType(obsName) !=
"Double_t") {
255 RESTWarning <<
"Column " << obsName <<
" is not of type 'double'. It will be converted."
257 df = df.Redefine(obsName,
"static_cast<double>(" + obsName +
")");
259 df = df.Define(oddsName, GetLogOdds, {obsName});
260 auto h = df.Histo1D(oddsName);
262 if (!totName.empty()) totName +=
"+";
266 RESTDebug <<
"Computing total log odds" <<
RESTendl;
267 RESTDebug <<
"\tTotal log odds = " << totName <<
RESTendl;
268 df = df.Define(std::string(GetName()) +
"_total", totName);
270 dataSet.SetDataFrame(df);
279 for (
const auto& [obsName, histo] :
fHistos) histo->Write();
285std::vector<std::tuple<std::string, TVector2, int>> TRestDataSetOdds::GetOddsObservables() {
286 std::vector<std::tuple<std::string, TVector2, int>> obs;
287 for (
size_t i = 0; i <
fObsName.size(); i++) {
289 RESTError <<
"Sizes for observables names, ranges and bins do not match!" <<
RESTendl;
297void TRestDataSetOdds::AddOddsObservable(
const std::string& name,
const TVector2& range,
int nbins) {
303void TRestDataSetOdds::SetOddsObservables(
const std::vector<std::tuple<std::string, TVector2, int>>& obs) {
307 for (
const auto& [name, range, nbins] : obs) AddOddsObservable(name, range, nbins);
310void TRestDataSetOdds::WriteHistograms(TFile* f)
const {
313 for (
const auto& [name, histo] :
fHistos) {
314 if (histo) histo->Write();
328 RESTMetadata <<
" Observables to compute: " <<
RESTendl;
329 for (
size_t i = 0; i <
fObsName.size(); i++) {
A class to help on cuts definitions. To be used with TRestAnalysisTree.
This class is meant to compute the log odds for different datasets.
std::string fOddsFile
Name of the odds file to be used to get the PDF.
std::vector< std::string > fObsName
Vector containing different obserbable names.
TRestDataSetOdds()
Default constructor.
void PrintMetadata() override
Prints on screen the information about the metadata members of TRestDataSetOdds.
std::map< std::string, TH1F * > fHistos
Map containing the PDF of the different observables.
std::vector< TVector2 > fObsRange
Vector containing different obserbable ranges.
void Initialize() override
Function to initialize input/output event members and define the section name.
~TRestDataSetOdds()
Default destructor.
TRestCut * fCut
Cuts over the dataset for PDF selection.
void ComputeLogOdds()
This function computes the log odds for a given dataSet. If no calibration odds file is provided it c...
std::string fOutputFileName
Name of the output file.
std::string fDataSetName
Name of the dataSet inside the config file.
std::vector< int > fObsNbins
Vector containing number of bins for the different observables.
void InitFromConfigFile() override
Function to initialize some variables from configfile.
It allows to group a number of runs that satisfy given metadata conditions.
void Import(const std::string &fileName)
This function imports metadata from a root file it import metadata info from the previous dataSet whi...
ROOT::RDF::RNode GetDataFrame() const
Gives access to the RDataFrame.
ROOT::RDF::RNode MakeCut(const TRestCut *cut)
This function applies a TRestCut to the dataframe and returns a dataframe with the applied cuts....
void Export(const std::string &filename, std::vector< std::string > excludeColumns={})
It will generate an output file with the dataset compilation. Only the selected branches and the file...
@ REST_Info
+show most of the information for each steps
Int_t StringToInteger(std::string in)
Gets an integer from a string.
TVector2 StringTo2DVector(std::string in)
Gets a 2D-vector from a string.