REST-for-Physics  v2.3
Rare Event Searches ToolKit for Physics
TRestComponentFormula.cxx
1/*************************************************************************
2 * This file is part of the REST software framework. *
3 * *
4 * Copyright (C) 2016 GIFNA/TREX (University of Zaragoza) *
5 * For more information see https://gifna.unizar.es/trex *
6 * *
7 * REST is free software: you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation, either version 3 of the License, or *
10 * (at your option) any later version. *
11 * *
12 * REST is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU General Public License for more details. *
16 * *
17 * You should have a copy of the GNU General Public License along with *
18 * REST in $REST_PATH/LICENSE. *
19 * If not, see https://www.gnu.org/licenses/. *
20 * For the list of contributors see $REST_PATH/CREDITS. *
21 *************************************************************************/
22
55#include "TRestComponentFormula.h"
56
57#include <numeric>
58
59#include "TKey.h"
60
61ClassImp(TRestComponentFormula);
62
67
82TRestComponentFormula::TRestComponentFormula(const char* cfgFileName, const std::string& name)
83 : TRestComponent(cfgFileName) {
84 Initialize();
85
87
89}
90
95
102
103 SetSectionName(this->ClassName());
104
106}
107
116Double_t TRestComponentFormula::GetFormulaRate(std::vector<Double_t> point) {
117 if (fVariables.size() != point.size()) {
118 RESTError << "Point should have same dimensions as number of variables!" << RESTendl;
119 return 0;
120 }
121
122 Double_t result = 0;
123 for (auto& formula : fFormulas) {
124 for (size_t n = 0; n < fVariables.size(); n++) formula.SetParameter(fVariables[n].c_str(), point[n]);
125
126 result += formula.EvalPar(nullptr);
127 }
128
129 Double_t normFactor = 1;
130 for (size_t n = 0; n < GetDimensions(); n++) {
131 normFactor *= (fRanges[n].Y() - fRanges[n].X()) / fNbins[n];
132 }
133
134 return normFactor * result / units(fUnits);
135}
136
150 if (fFormulas.empty()) return;
151
152 RESTInfo << "Generating N-dim histogram for " << GetName() << RESTendl;
153
154 TString hName = "formula";
155
156 Int_t* bins = new Int_t[fNbins.size()];
157 Double_t* xlow = new Double_t[fNbins.size()];
158 Double_t* xhigh = new Double_t[fNbins.size()];
159
160 for (size_t n = 0; n < fNbins.size(); n++) {
161 bins[n] = fNbins[n];
162 xlow[n] = fRanges[n].X();
163 xhigh[n] = fRanges[n].Y();
164 }
165
166 THnD* hNd = new THnD(hName, hName, fNbins.size(), bins, xlow, xhigh);
167
168 // Calculate the bin width in each dimension
169 std::vector<double> binWidths;
170 for (size_t i = 0; i < fNbins.size(); ++i) {
171 double width = static_cast<double>(xhigh[i] - xlow[i]) / bins[i];
172 binWidths.push_back(width);
173 }
174
175 // Nested loop to iterate over each bin and print its center
176 std::vector<int> binIndices(fNbins.size(), 0); // Initialize bin indices to 0 in each dimension
177
178 bool carry = false;
179 while (!carry) {
180 // Calculate the center of the current bin in each dimension
181 std::vector<double> binCenter;
182 for (size_t i = 0; i < fNbins.size(); ++i)
183 binCenter.push_back(xlow[i] + (binIndices[i] + 0.5) * binWidths[i]);
184
185 hNd->Fill(binCenter.data(), GetFormulaRate(binCenter));
186
187 // Update bin indices for the next iteration
188 carry = true;
189 for (size_t i = 0; i < fNbins.size(); ++i) {
190 binIndices[i]++;
191 if (binIndices[i] < bins[i]) {
192 carry = false;
193 break;
194 }
195 binIndices[i] = 0;
196 }
197 }
198
199 fNodeDensity.clear();
200 fNodeDensity.push_back(hNd);
201 fActiveNode = 0; // For the moment only 1-node!
202}
203
209
210 RESTMetadata << " " << RESTendl;
211 RESTMetadata << "Formula units: " << fUnits << RESTendl;
212
213 if (!fFormulas.empty()) {
214 RESTMetadata << " " << RESTendl;
215 RESTMetadata << " == Contributions implemented inside the component ==" << RESTendl;
216
217 for (const auto& x : fFormulas)
218 RESTMetadata << "- " << x.GetName() << " = " << x.GetExpFormula() << RESTendl;
219
220 RESTMetadata << " " << RESTendl;
221 }
222
223 RESTMetadata << "----" << RESTendl;
224}
225
231
232 if (!fFormulas.empty()) return;
233
234 auto ele = GetElement("formula");
235 while (ele != nullptr) {
236 std::string name = GetParameter("name", ele, "");
237 std::string expression = GetParameter("expression", ele, "");
238
239 if (expression.empty()) {
240 RESTWarning << "TRestComponentFormula::InitFromConfigFile. Invalid formula" << RESTendl;
241 } else {
242 TFormula formula(name.c_str(), expression.c_str());
243
244 for (Int_t n = 0; n < formula.GetNpar(); n++) {
245 if (std::find(fVariables.begin(), fVariables.end(), formula.GetParName(n)) ==
246 fVariables.end()) {
247 RESTError << "Variable : " << formula.GetParName(n) << " not found in component! "
248 << RESTendl;
249 RESTError << "TRestComponentFormula evaluation will lead to wrong results!" << RESTendl;
250 }
251 }
252
253 for (const auto& varName : fVariables) formula.SetParameter(varName.c_str(), 0.0);
254
255 fFormulas.push_back(formula);
256 }
257
258 ele = GetNextElement(ele);
259 }
260}
It defines an analytical component model distribution in a given parameter space (tipically x,...
std::string fUnits
The formulas should be expressed in the following units.
std::vector< TFormula > fFormulas
A vector of formulas that will be added up to integrate a given rate.
~TRestComponentFormula()
Default destructor.
void Initialize() override
It will initialize the data frame with the filelist and column names (or observables) that have been ...
void InitFromConfigFile() override
It customizes the retrieval of XML data values of this class.
void FillHistograms() override
It will produce a histogram with the distribution using the formula contributions.
void PrintMetadata() override
Prints on screen the information about the metadata members of TRestAxionSolarFlux.
Double_t GetFormulaRate(std::vector< Double_t > point)
It returns the intensity/rate (in seconds) corresponding to the formula evaluated at the position of ...
TRestComponentFormula()
Default constructor.
It defines a background/signal model distribution in a given parameter space (tipically x,...
void InitFromConfigFile() override
It customizes the retrieval of XML data values of this class.
void PrintMetadata() override
Prints on screen the information about the metadata members of TRestAxionSolarFlux.
Int_t fActiveNode
It is used to define the node that will be accessed for rate retrieval.
std::vector< Int_t > fNbins
The number of bins in which we should divide each variable.
void Initialize() override
It initializes the random number. We avoid to define the section name here since we will never define...
std::vector< TVector2 > fRanges
The range of each of the variables used to create the PDF distribution.
std::vector< std::string > fVariables
A list with the branches that will be used to create the distribution space.
std::vector< THnD * > fNodeDensity
The generated N-dimensional variable space density for a given node.
endl_t RESTendl
Termination flag object for TRestStringOutput.
TiXmlElement * GetElement(std::string eleDeclare, TiXmlElement *e=nullptr)
Get an xml element from a given parent element, according to its declaration.
Int_t LoadConfigFromFile(const std::string &configFilename, const std::string &sectionName="")
Give the file name, find out the corresponding section. Then call the main starter.
TRestStringOutput::REST_Verbose_Level GetVerboseLevel()
returns the verboselevel in type of REST_Verbose_Level enumerator
void SetSectionName(std::string sName)
set the section name, clear the section content
std::string fConfigFileName
Full name of the rml file.
TiXmlElement * GetNextElement(TiXmlElement *e)
Get the next sibling xml element of this element, with same eleDeclare.
std::string GetParameter(std::string parName, TiXmlElement *e, TString defaultValue=PARAMETER_NOT_FOUND_STR)
Returns the value for the parameter named parName in the given section.
@ REST_Info
+show most of the information for each steps