103#include "TRestDataSetOdds.h"
105#include "TRestDataSet.h"
155 TiXmlElement* obsDefinition =
GetElement(
"observable");
156 while (obsDefinition !=
nullptr) {
158 if (obsName.empty() || obsName ==
"Not defined") {
159 RESTError <<
"< observable variable key does not contain a name!" <<
RESTendl;
166 if (range.empty() || range ==
"Not defined") {
167 RESTError <<
"< observable key does not contain a range value!" <<
RESTendl;
175 if (nBins.empty() || nBins ==
"Not defined") {
176 RESTError <<
"< observable key does not contain a nBins value!" <<
RESTendl;
186 RESTError <<
"No observables provided, exiting..." <<
RESTendl;
213 for (
size_t i = 0; i <
fObsName.size(); i++) {
214 const std::string obsName =
fObsName[i];
216 const std::string histName =
"h" + obsName;
218 RESTDebug <<
"\tGenerating PDF for " << obsName <<
" with range: (" << range.X() <<
", "
219 << range.Y() <<
") and nBins: " << nBins <<
RESTendl;
221 DF.Histo1D({histName.c_str(), histName.c_str(), nBins, range.X(), range.Y()}, obsName);
222 TH1F* h =
static_cast<TH1F*
>(histo->DrawClone());
223 RESTDebug <<
"\tNormalizing by integral = " << h->Integral() <<
RESTendl;
224 h->Scale(1. / h->Integral());
228 TFile* f = TFile::Open(
fOddsFile.c_str());
234 for (
size_t i = 0; i <
fObsName.size(); i++) {
235 const std::string obsName =
fObsName[i];
236 const std::string histName =
"h" + obsName;
237 TH1F* h = (TH1F*)f->Get(histName.c_str());
243 std::string totName =
"";
245 for (
const auto& [obsName, histo] :
fHistos) {
246 const std::string oddsName =
"odds_" + obsName;
247 auto GetLogOdds = [&histo = histo](
double val) {
248 double odds = histo->GetBinContent(histo->GetXaxis()->FindBin(val));
249 if (odds == 0)
return 1000.;
250 return log(1. - odds) - log(odds);
253 if (df.GetColumnType(obsName) !=
"Double_t") {
254 RESTWarning <<
"Column " << obsName <<
" is not of type 'double'. It will be converted."
256 df = df.Redefine(obsName,
"static_cast<double>(" + obsName +
")");
258 df = df.Define(oddsName, GetLogOdds, {obsName});
259 auto h = df.Histo1D(oddsName);
261 if (!totName.empty()) totName +=
"+";
265 RESTDebug <<
"Computing total log odds" <<
RESTendl;
266 RESTDebug <<
"\tTotal log odds = " << totName <<
RESTendl;
267 df = df.Define(
"odds_total", totName);
269 dataSet.SetDataFrame(df);
278 for (
const auto& [obsName, histo] :
fHistos) histo->Write();
284std::vector<std::tuple<std::string, TVector2, int>> TRestDataSetOdds::GetOddsObservables() {
285 std::vector<std::tuple<std::string, TVector2, int>> obs;
286 for (
size_t i = 0; i <
fObsName.size(); i++) {
288 RESTError <<
"Sizes for observables names, ranges and bins do not match!" <<
RESTendl;
296void TRestDataSetOdds::AddOddsObservable(
const std::string& name,
const TVector2& range,
int nbins) {
302void TRestDataSetOdds::SetOddsObservables(
const std::vector<std::tuple<std::string, TVector2, int>>& obs) {
306 for (
const auto& [name, range, nbins] : obs) AddOddsObservable(name, range, nbins);
319 RESTMetadata <<
" Observables to compute: " <<
RESTendl;
320 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.