83 #include "TRestComponentDataSet.h"
152 RESTError <<
"TRestComponentDataSet::GetRate. The component has no nodes!" <<
RESTendl;
153 RESTError <<
"Try calling TRestComponentDataSet::LoadDataSets" <<
RESTendl;
155 RESTInfo <<
"Trying to load datasets" <<
RESTendl;
164 RESTError <<
"TRestComponentDataSet::GetRate. Active node has not been defined" <<
RESTendl;
169 GetDensityForActiveNode()->GetBinContent(GetDensityForActiveNode()->GetBin(point.data()));
176 return norm * density;
184 THnD* dHist = GetDensityForActiveNode();
186 Double_t integral = 0;
187 if (dHist !=
nullptr) integral = dHist->ComputeIntegral();
190 for (
size_t n = 0; n <
fNbins.size(); n++)
200 std::vector<std::string> scanVariables, Int_t binScanSize,
201 TString drawOption) {
202 if (drawVariables.size() > 2 || drawVariables.size() == 0) {
203 RESTError <<
"TRestComponentDataSet::DrawComponent. The number of variables to be drawn must "
208 if (scanVariables.size() > 2 || scanVariables.size() == 0) {
209 RESTError <<
"TRestComponentDataSet::DrawComponent. The number of variables to be scanned must "
215 std::vector<int> scanIndexes;
216 for (
const auto& x : scanVariables) scanIndexes.push_back(
GetVariableIndex(x));
220 for (
const auto& x : scanIndexes) {
221 if (
fNbins[x] % binScanSize != 0) {
222 RESTWarning <<
"The variable " << scanVariables[n] <<
" contains " <<
fNbins[x]
223 <<
" bins and it doesnt match with a bin size " << binScanSize <<
RESTendl;
224 RESTWarning <<
"The bin size must be a multiple of the number of bins." <<
RESTendl;
225 RESTWarning <<
"Redefining bin size to 1." <<
RESTendl;
228 nPlots *=
fNbins[x] / binScanSize;
236 if (scanIndexes.size() == 2) {
237 nPlotsX =
fNbins[scanIndexes[0]] / binScanSize;
238 nPlotsY =
fNbins[scanIndexes[1]] / binScanSize;
244 RESTInfo <<
"Number of plots to be generated: " << nPlots <<
RESTendl;
245 RESTInfo <<
"Canvas size : " << nPlotsX <<
" x " << nPlotsY <<
RESTendl;
253 fCanvas =
new TCanvas(this->GetName(), this->GetName(), 0, 0, nPlotsX * 640, nPlotsY * 480);
254 fCanvas->Divide(nPlotsX, nPlotsY, 0.01, 0.01);
256 std::vector<int> variableIndexes;
257 for (
const auto& x : drawVariables) variableIndexes.push_back(
GetVariableIndex(x));
259 for (
int n = 0; n < nPlotsX; n++)
260 for (
int m = 0; m < nPlotsY; m++) {
261 fCanvas->cd(n * nPlotsY + m + 1);
263 THnD* hnd = GetDensity();
265 int binXo = binScanSize * n + 1;
266 int binXf = binScanSize * n + binScanSize;
267 int binYo = binScanSize * m + 1;
268 int binYf = binScanSize * m + binScanSize;
270 if (scanVariables.size() == 2) {
271 hnd->GetAxis(scanIndexes[0])->SetRange(binXo, binXf);
272 hnd->GetAxis(scanIndexes[1])->SetRange(binYo, binYf);
273 }
else if (scanVariables.size() == 1) {
274 binXo = binScanSize * nPlotsY * n + binScanSize * m + 1;
275 binXf = binScanSize * nPlotsY * n + binScanSize * m + binScanSize;
276 hnd->GetAxis(scanIndexes[0])->SetRange(binXo, binXf);
279 if (variableIndexes.size() == 1) {
280 TH1D* h1 = hnd->Projection(variableIndexes[0]);
283 if (scanIndexes.size() == 2)
288 if (scanIndexes.size() == 1)
292 TH1D* newh = (TH1D*)h1->Clone(hName.c_str());
293 newh->SetTitle(hName.c_str());
294 newh->SetStats(
false);
295 newh->GetXaxis()->SetTitle((TString)drawVariables[0]);
296 newh->SetMarkerStyle(kFullCircle);
297 newh->Draw(
"PLC PMC");
299 TString entriesStr =
"Entries: " +
IntegerToString(newh->GetEntries());
300 TLatex* textLatex =
new TLatex(0.62, 0.825, entriesStr);
302 textLatex->SetTextColor(1);
303 textLatex->SetTextSize(0.05);
304 textLatex->Draw(
"same");
308 if (variableIndexes.size() == 2) {
309 TH2D* h2 = hnd->Projection(variableIndexes[0], variableIndexes[1]);
312 if (scanIndexes.size() == 2)
317 if (scanIndexes.size() == 1)
321 TH2D* newh = (TH2D*)h2->Clone(hName.c_str());
322 newh->SetStats(
false);
323 newh->GetXaxis()->SetTitle((TString)drawVariables[0]);
324 newh->GetYaxis()->SetTitle((TString)drawVariables[1]);
325 newh->SetTitle(hName.c_str());
326 newh->Draw(drawOption);
328 TString entriesStr =
"Entries: " +
IntegerToString(newh->GetEntries());
329 TLatex* textLatex =
new TLatex(0.62, 0.825, entriesStr);
331 textLatex->SetTextColor(1);
332 textLatex->SetTextSize(0.05);
333 textLatex->Draw(
"same");
349 RESTMetadata <<
" == Dataset filenames ==" <<
RESTendl;
357 RESTMetadata <<
"This component has no nodes!" <<
RESTendl;
358 RESTMetadata <<
" Use: LoadDataSets() to initialize the nodes" <<
RESTendl;
363 RESTMetadata <<
" == Weights ==" <<
RESTendl;
370 RESTMetadata <<
" Use : PrintStatistics() to check node statistics" <<
RESTendl;
381 RESTWarning <<
"TRestComponentDataSet::PrintStatistics. Empty nodes and no dataset loaded!"
383 RESTWarning <<
"Invoking TRestComponentDataSet::LoadDataSets might solve the problem" <<
RESTendl;
388 std::cout <<
"Total counts : " << result << std::endl;
389 std::cout << std::endl;
391 std::cout <<
" Parameter node statistics (" <<
fParameter <<
")" << std::endl;
394 std::cout <<
" - Value : " << p <<
" Counts: " <<
fNSimPerNode[n] << std::endl;
406 while (ele !=
nullptr) {
420 RESTError <<
"TRestComponentDataSet::FillHistograms. Dataset has not been initialized!" <<
RESTendl;
425 RESTWarning <<
"Nodes have not been defined" <<
RESTendl;
426 RESTWarning <<
"The full dataset will be used to generate the density distribution" <<
RESTendl;
430 RESTInfo <<
"Generating N-dim histograms" <<
RESTendl;
433 ROOT::RDF::RNode df = ROOT::RDataFrame(0);
436 if (fParameterizationNodes.size() == 1 && node == -137) {
437 RESTInfo <<
"Creating component with no parameters (full dataset used)" <<
RESTendl;
439 fParameterizationNodes.clear();
447 Int_t* bins =
new Int_t[
fNbins.size()];
448 Double_t* xmin =
new Double_t[
fNbins.size()];
449 Double_t* xmax =
new Double_t[
fNbins.size()];
451 for (
size_t n = 0; n <
fNbins.size(); n++) {
458 if (fParameterizationNodes.empty()) hName =
"full";
460 std::vector<std::string> varsAndWeight =
fVariables;
463 std::string weightsStr =
"";
464 for (
size_t n = 0; n <
fWeights.size(); n++) {
465 if (n > 0) weightsStr +=
"*";
469 df = df.Define(
"componentWeight", weightsStr);
470 varsAndWeight.push_back(
"componentWeight");
473 auto hn = df.HistoND({hName, hName, (int)
fNbins.size(), bins, xmin, xmax}, varsAndWeight);
474 THnD* hNd =
new THnD(*hn);
486 THnD* TRestComponentDataSet::GetDensityForNode(Double_t node) {
495 RESTError <<
"Parametric node : " << node <<
" was not found in component" <<
RESTendl;
503 THnD* TRestComponentDataSet::GetDensityForActiveNode() {
506 RESTError <<
"The active node is invalid" <<
RESTendl;
530 if (v1 >= 0 && GetDensityForActiveNode())
return GetDensityForActiveNode()->Projection(v1);
555 if (v1 >= 0 && v2 >= 0)
556 if (GetDensityForActiveNode())
return GetDensityForActiveNode()->Projection(v1, v2);
566 std::string varName3) {
583 if (v1 >= 0 && v2 >= 0 && v3 >= 0)
584 if (GetDensityForActiveNode())
return GetDensityForActiveNode()->Projection(v1, v2, v3);
597 if (!fParameterizationNodes.empty())
return fParameterizationNodes;
599 RESTInfo <<
"Extracting parameterization nodes" <<
RESTendl;
601 std::vector<double> vs;
603 RESTError <<
"TRestComponentDataSet::ExtractParameterizationNodes. Dataset has not been initialized!"
609 for (
const auto v : parValues) vs.push_back(v);
611 std::vector<double>::iterator ip;
612 ip = std::unique(vs.begin(), vs.begin() + vs.size());
613 vs.resize(std::distance(vs.begin(), ip));
614 std::sort(vs.begin(), vs.end());
615 ip = std::unique(vs.begin(), vs.end());
616 vs.resize(std::distance(vs.begin(), ip));
631 std::vector<Int_t> stats;
633 RESTError <<
"TRestComponentDataSet::ExtractNodeStatistics. Dataset has not been initialized!"
638 RESTInfo <<
"Counting statistics for each node ..." <<
RESTendl;
639 RESTInfo <<
"Number of nodes : " << fParameterizationNodes.size() <<
RESTendl;
640 for (
const auto& p : fParameterizationNodes) {
643 stats.push_back(*nEv);
655 RESTWarning <<
"Dataset filename was not defined. You may still use "
656 "TRestComponentDataSet::LoadDataSet( filename );" <<
RESTendl;
661 RESTInfo <<
"Loading datasets" <<
RESTendl;
663 std::vector<std::string> fullFileNames;
671 if (fileName.empty()) {
672 RESTError <<
"TRestComponentDataSet::LoadDataSet. Error loading file : " << name <<
RESTendl;
673 RESTError <<
"Does the file exist?" <<
RESTendl;
674 RESTError <<
"You may use `<globals> <searchPath ...` to indicate the path location" <<
RESTendl;
677 fullFileNames.push_back(fileName);
684 RESTError <<
"Problem loading dataset from file list :" <<
RESTendl;
685 for (
const auto& f : fDataSetFileNames) RESTError <<
" - " << f <<
RESTendl;
708 if (std::count(cNames.begin(), cNames.end(), var) == 0) {
709 RESTError <<
"Variable ---> " << var <<
" <--- NOT found on dataset" <<
RESTendl;
723 if (std::count(cNames.begin(), cNames.end(), var) == 0) {
724 RESTError <<
"Weight ---> " << var <<
" <--- NOT found on dataset" <<
RESTendl;
736 RESTWarning <<
"TRestComponentDataSet::ValidDataSet. Dataset has not been loaded" <<
RESTendl;
737 RESTWarning <<
"Try calling TRestComponentDataSet::LoadDataSets" <<
RESTendl;
739 RESTInfo <<
"Trying to load datasets" <<
RESTendl;
744 RESTError <<
"Failed loading datasets" <<
RESTendl;
750 RESTError <<
"TRestComponentDataSet::ValidDataSet. Active node has not been defined" <<
RESTendl;
It defines a background/signal model distribution in a given parameter space (tipically x...
std::vector< Double_t > fParameterizationNodes
It defines the nodes of the parameterization (Initialized by the dataset)
void Initialize() override
It will initialize the data frame with the filelist and column names (or observables) that have been ...
std::string IntegerToString(Int_t n, std::string format="%d")
Gets a string from an integer.
std::vector< TVector2 > fRanges
The range of each of the variables used to create the PDF distribution.
std::vector< THnD * > fNodeDensity
The generated N-dimensional variable space density for a given node.
Bool_t IsDataSetLoaded()
This method should go to TRestComponentDataSet.
Bool_t ValidDataSet()
Takes care of initializing datasets if have not been initialized. On sucess it returns true...
~TRestComponentDataSet()
Default destructor.
TRestDataSet fDataSet
The dataset used to initialize the distribution.
std::string fParameter
It is used to parameterize a set of distribution densities (e.g. WIMP or axion mass) ...
void Import(const std::string &fileName)
This function imports metadata from a root file it import metadata info from the previous dataSet whi...
TCanvas * fCanvas
A canvas for drawing the active node component.
Double_t GetTotalRate() override
This method integrates the rate to all the parameter space defined in the density function...
std::vector< Int_t > fNbins
The number of bins in which we should divide each variable.
Bool_t LoadDataSets()
A method responsible to import a list of TRestDataSet into fDataSet and check that the variables and ...
std::vector< Int_t > ExtractNodeStatistics()
It returns a vector with the number of entries found for each parameterization node.
void PrintStatistics()
It prints out the statistics available for each parametric node.
void PrintMetadata() override
Prints on screen the information about the metadata members of TRestAxionSolarFlux.
Bool_t HasNodes()
It returns true if any nodes have been defined.
Int_t fActiveNode
It is used to define the node that will be accessed for rate retrieval.
void FillHistograms()
It will produce a histogram with the distribution defined using the variables and the weights for eac...
ROOT::RDF::RNode GetDataFrame() const
Gives access to the RDataFrame.
Double_t GetRate(std::vector< Double_t > point) override
It returns the intensity/rate (in seconds) corresponding to the generated distribution or formula eva...
void PrintMetadata() override
Prints on screen the information about the metadata members of TRestAxionSolarFlux.
std::string DoubleToString(Double_t d, std::string format="%8.6e")
Gets a string from a double.
void InitFromConfigFile() override
It customizes the retrieval of XML data values of this class.
+show most of the information for each steps
Int_t SetActiveNode(Double_t node)
It returns the position of the fParameterizationNodes element for the variable name given by argument...
Bool_t VariablesOk()
It returns true if all variables have been found inside TRestDataSet.
Bool_t WeightsOk()
It returns true if all weights have been found inside TRestDataSet.
std::vector< std::string > fVariables
A list with the branches that will be used to create the distribution space.
void InitFromConfigFile() override
It customizes the retrieval of XML data values of this class.
TCanvas * DrawComponent(std::vector< std::string > drawVariables, std::vector< std::string > scanVariables, Int_t binScanSize=1, TString drawOption="")
std::vector< Int_t > fNSimPerNode
It defines the number of entries for each parameterization node (Initialized by the dataset) ...
std::vector< std::string > fWeights
A list with the dataset columns used to weight the distribution density and define rate...
Bool_t fDataSetLoaded
It is true of the dataset was loaded without issues.
std::vector< Double_t > ExtractParameterizationNodes()
It returns a vector with all the different values found on the dataset column for the user given para...
void PrintNodes()
It prints out on screen the values of the parametric node.
void PrintMetadata() override
Prints on screen the information about the metadata members of TRestDataSet.
It defines a background/signal model distribution in a given parameter space (tipically x...
TH1D * GetHistogram(Double_t node, std::string varName)
It returns a 1-dimensional projected histogram for the variable names provided in the argument...
std::vector< std::string > fDataSetFileNames
The filename of the dataset used.
TTree * GetTree() const
Gives access to the tree.
void Initialize() override
It will initialize the data frame with the filelist and column names (or observables) that have been ...
Int_t GetVariableIndex(std::string varName)
It returns the position of the fVariable element for the variable name given by argument.
TRestComponentDataSet()
Default constructor.