104#include "TRestDataSetOdds.h"
106#include "TRestDataSet.h"
107#include "TRestTools.h"
157 TiXmlElement* obsDefinition =
GetElement(
"observable");
158 while (obsDefinition !=
nullptr) {
160 if (obsName.empty() || obsName ==
"Not defined") {
161 RESTError <<
"< observable variable key does not contain a name!" <<
RESTendl;
168 if (range.empty() || range ==
"Not defined") {
169 RESTError <<
"< observable key does not contain a range value!" <<
RESTendl;
177 if (nBins.empty() || nBins ==
"Not defined") {
178 RESTError <<
"< observable key does not contain a nBins value!" <<
RESTendl;
188 RESTError <<
"No observables provided, exiting..." <<
RESTendl;
215 for (
size_t i = 0; i <
fObsName.size(); i++) {
216 const std::string obsName =
fObsName[i];
218 const std::string histName =
"h" + std::string(GetName()) +
"_" + obsName;
220 RESTDebug <<
"\tGenerating PDF for " << obsName <<
" with range: (" << range.X() <<
", "
221 << range.Y() <<
") and nBins: " << nBins <<
RESTendl;
223 DF.Histo1D({histName.c_str(), histName.c_str(), nBins, range.X(), range.Y()}, obsName);
224 TH1F* h =
static_cast<TH1F*
>(histo->DrawClone());
225 RESTDebug <<
"\tNormalizing by integral = " << h->Integral() <<
RESTendl;
226 h->Scale(1. / h->Integral());
230 TFile* f = TFile::Open(
fOddsFile.c_str());
236 for (
size_t i = 0; i <
fObsName.size(); i++) {
237 const std::string obsName =
fObsName[i];
238 const std::string histName =
"h" + std::string(GetName()) +
"_" + obsName;
239 TH1F* h = (TH1F*)f->Get(histName.c_str());
245 std::string totName =
"";
247 for (
const auto& [obsName, histo] :
fHistos) {
248 const std::string oddsName = std::string(GetName()) +
"_" + obsName;
249 auto GetLogOdds = [&histo = histo](
double val) {
250 double odds = histo->GetBinContent(histo->GetXaxis()->FindBin(val));
251 if (odds == 0)
return 1000.;
252 return log(1. - odds) - log(odds);
255 if (df.GetColumnType(obsName) !=
"Double_t") {
256 RESTWarning <<
"Column " << obsName <<
" is not of type 'double'. It will be converted."
258 df = df.Redefine(obsName,
"static_cast<double>(" + obsName +
")");
260 df = df.Define(oddsName, GetLogOdds, {obsName});
261 auto h = df.Histo1D(oddsName);
263 if (!totName.empty()) totName +=
"+";
267 RESTDebug <<
"Computing total log odds" <<
RESTendl;
268 RESTDebug <<
"\tTotal log odds = " << totName <<
RESTendl;
269 df = df.Define(std::string(GetName()) +
"_total", totName);
271 dataSet.SetDataFrame(df);
279 RESTError << file.Error() <<
RESTendl;
284 for (
const auto& [obsName, histo] :
fHistos) histo->Write();
285 if (!file.Close()) RESTError << file.Error() <<
RESTendl;
290std::vector<std::tuple<std::string, TVector2, int>> TRestDataSetOdds::GetOddsObservables() {
291 std::vector<std::tuple<std::string, TVector2, int>> obs;
292 for (
size_t i = 0; i <
fObsName.size(); i++) {
294 RESTError <<
"Sizes for observables names, ranges and bins do not match!" << RESTendl;
302void TRestDataSetOdds::AddOddsObservable(
const std::string& name,
const TVector2& range,
int nbins) {
308void TRestDataSetOdds::SetOddsObservables(
const std::vector<std::tuple<std::string, TVector2, int>>& obs) {
312 for (
const auto& [name, range, nbins] : obs) AddOddsObservable(name, range, nbins);
315void TRestDataSetOdds::WriteHistograms(TFile* f)
const {
318 for (
const auto& [name, histo] :
fHistos) {
319 if (histo) histo->Write();
333 RESTMetadata <<
" Observables to compute: " <<
RESTendl;
334 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...
static TRestRootFileHandle Open(const std::string &filename, TRestRootFileMode mode)
@ 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.