REST-for-Physics  v2.3
Rare Event Searches ToolKit for Physics
TRestExperiment.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
41#include "TRestExperiment.h"
42
43ClassImp(TRestExperiment);
44
49
54
69TRestExperiment::TRestExperiment(const char* cfgFileName, const std::string& name)
70 : TRestMetadata(cfgFileName) {
72}
73
79 SetSectionName(this->ClassName());
80
81 if (!fRandom) {
82 delete fRandom;
83 fRandom = nullptr;
84 }
85
86 fRandom = new TRandom3(fSeed);
87 fSeed = fRandom->TRandom::GetSeed();
88}
89
90void TRestExperiment::GenerateMockDataSet() {
91 if (!fBackground) {
92 RESTError << "TRestExperiment::GenerateMockData. Background component was not initialized!"
93 << RESTendl;
94 return;
95 }
96
97 if (fExposureTime <= 0) {
98 RESTError << "The experimental exposure time has not been defined" << RESTendl;
99 RESTError << "This time is required to create the mock dataset" << RESTendl;
100 }
101
102 Double_t meanCounts = GetBackground()->GetTotalRate() * fExposureTime * units("s");
103
104 Int_t N = fRandom->Poisson(meanCounts);
105 RESTInfo << "Experiment: " << GetName() << " Generating mock dataset. Counts: " << N << RESTendl;
106
107 ROOT::RDF::RNode df = fBackground->GetMonteCarloDataFrame(N);
108
109 fExperimentalData.SetDataFrame(df);
110 fExperimentalData.SetTotalTimeInSeconds(fExposureTime * units("s"));
111
113
114 fMockData = true;
115 fDataReady = true;
116}
117
118void TRestExperiment::SetExperimentalDataSet(const std::string& filename) {
121
125
126 fMockData = false;
127 fDataReady = true;
128
129 if (!fSignal || !fBackground) {
130 RESTWarning << "TRestExperiment::SetExperimentalDataSet. Signal and background components must "
131 "be available before atempt to load experimental data"
132 << RESTendl;
133 fDataReady = false;
134 return;
135 }
136
137 std::vector<std::string> columns = fExperimentalData.GetDataFrame().GetColumnNames();
138 for (const auto& v : fSignal->GetVariables()) {
139 if (std::find(columns.begin(), columns.end(), v) == columns.end()) {
140 RESTError << "TRestExperiment::SetExperimentalDataSetFile a component variable was not found in "
141 "the dataset!"
142 << RESTendl;
143 fDataReady = false;
144 return;
145 }
146 }
147}
148
154
155 int cont = 0;
156 TRestComponent* comp = (TRestComponent*)this->InstantiateChildMetadata(cont, "Component");
157 while (comp != nullptr) {
158 if (ToLower(comp->GetNature()) == "background")
159 fBackground = comp;
160 else if (ToLower(comp->GetNature()) == "signal")
161 fSignal = comp;
162 else
163 RESTWarning << "TRestExperiment::InitFromConfigFile. Unknown component!" << RESTendl;
164
165 cont++;
166 comp = (TRestComponent*)this->InstantiateChildMetadata(cont, "Component");
167 }
168
169 auto ele = GetElement("addComponent");
170 if (ele != nullptr) {
171 std::string filename = GetParameter("filename", ele, "");
172 std::string component = GetParameter("component", ele, "");
173
174 if (filename.empty())
175 RESTWarning
176 << "TRestExperiment. There is a problem with `filename` definition inside <addComponent."
177 << RESTendl;
178 if (component.empty())
179 RESTWarning
180 << "TRestExperiment. There is a problem with `component` definition inside <addComponent."
181 << RESTendl;
182
183 if (TRestTools::fileExists(filename) && TRestTools::isRootFile(filename)) {
184 TFile* file = TFile::Open(filename.c_str(), "READ");
185 if (file != nullptr) {
186 TRestComponent* comp = file->Get<TRestComponent>(component.c_str());
187 if (comp) {
188 if (ToLower(comp->GetNature()) == "signal")
189 fSignal = comp;
190 else if (ToLower(comp->GetNature()) == "background")
191 fBackground = comp;
192 else
193 RESTError << "TRestExperiment::InitFromConfigFile. Component : " << component
194 << ". Nature unknown!" << RESTendl;
195 } else
196 RESTError << "TRestExperiment::InitFromConfigFile. Component : " << component
197 << " not found! File : " << filename << RESTendl;
198 }
199 }
200 }
201
202 if (fExposureTime > 0 && fExperimentalDataSet.empty()) {
203 GenerateMockDataSet();
204 } else if (fExposureTime == 0 && !fExperimentalDataSet.empty()) {
206 } else {
207 RESTWarning << "The exposure time is not zero but a experimental data filename was defined!"
208 << RESTendl;
209 RESTWarning << "The exposure time will be recovered from the dataset duration. To avoid confusion is"
210 << RESTendl;
211 RESTWarning << "better that you set exposure time to zero inside the RML definition," << RESTendl;
212 RESTError
213 << " or do not define a dataset if you wish to generate mock data using the exposure time given"
214 << RESTendl;
215 }
216
217 if (!fSignal) {
218 RESTError << "TRestExperiment : " << GetName() << RESTendl;
219 RESTError << "There was a problem initiazing the signal component!" << RESTendl;
220 return;
221 }
222
223 if (!fBackground) {
224 RESTError << "TRestExperiment : " << GetName() << RESTendl;
225 RESTError << "There was a problem initiazing the background component!" << RESTendl;
226 return;
227 }
228
230 if (fSignal->GetVariables() != fBackground->GetVariables()) {
231 RESTError << "TRestExperiment : " << GetName() << RESTendl;
232 RESTError << "Background and signal components got different variable names or variable ordering!"
233 << RESTendl;
234 RESTError << "This will lead to undesired results during Likelihood calculation!" << RESTendl;
235 return;
236 }
237
238 if (fSignal->GetNbins() != fBackground->GetNbins()) {
239 RESTError << "TRestExperiment : " << GetName() << RESTendl;
240 RESTError << "Background and signal components got different binning values!" << RESTendl;
241 RESTError << "This will lead to undesired results during Likelihood calculation!" << RESTendl;
242 return;
243 }
244
245 cont = 0;
246 std::vector<TVector2> bckRanges = fBackground->GetRanges();
247 std::vector<TVector2> sgnlRanges = fSignal->GetRanges();
248 for (const TVector2& sRange : sgnlRanges) {
249 if (sRange.X() != bckRanges[cont].X() || sRange.Y() != bckRanges[cont].Y()) {
250 RESTError << "TRestExperiment : " << GetName() << RESTendl;
251 RESTError << "Background and signal components got different range definitions!" << RESTendl;
252 RESTError << "This will lead to undesired results during Likelihood calculation!" << RESTendl;
253 return;
254 }
255 cont++;
256 }
257
258 Initialize();
259}
260
266
267 RESTMetadata << "Random seed : " << fSeed << RESTendl;
268 RESTMetadata << " " << RESTendl;
269 if (fExposureTime > 0) {
270 RESTMetadata << " - Exposure time : " << fExposureTime * units("s") << " seconds" << RESTendl;
271 RESTMetadata << " - Exposure time : " << fExposureTime * units("hr") << " hours" << RESTendl;
272 RESTMetadata << " - Exposure time : " << fExposureTime * units("day") << " days" << RESTendl;
273 }
274
275 if (fSignal) RESTMetadata << " - Signal component : " << fSignal->GetName() << RESTendl;
276
277 if (fBackground) RESTMetadata << " - Background component : " << fBackground->GetName() << RESTendl;
278
279 if (fMockData) {
280 RESTMetadata << " " << RESTendl;
281 if (fMockData)
282 RESTMetadata << "The dataset was MC-generated" << RESTendl;
283 else {
284 RESTMetadata << "The dataset was loaded from an existing dataset file" << RESTendl;
285 if (!fExperimentalDataSet.empty())
286 RESTMetadata << " - Experimental dataset file : " << fExperimentalDataSet << RESTendl;
287 }
288 }
289
290 RESTMetadata << " - Experimental counts : " << fExperimentalCounts << RESTendl;
291
292 RESTMetadata << "----" << RESTendl;
293}
It defines a background/signal model distribution in a given parameter space (tipically x,...
Double_t GetTotalRate()
This method integrates the rate to all the parameter space defined in the density function....
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.
Definition: TRestDataSet.h:127
Double_t GetTotalTimeInSeconds() const
It returns the accumulated run time in seconds.
Definition: TRestDataSet.h:164
It includes a model definition and experimental data used to obtain a final experimental sensitivity.
UInt_t fSeed
Seed used in random generator.
Int_t fExperimentalCounts
It keeps track on the number of counts inside the dataset.
TRestComponent * fBackground
A pointer to the background component.
Bool_t fDataReady
Only if it is true we will be able to calculate the LogLikelihood.
TRestComponent * fSignal
A pointer to the signal component.
void InitFromConfigFile() override
It customizes the retrieval of XML data values of this class.
void Initialize() override
It will initialize the data frame with the filelist and column names (or observables) that have been ...
~TRestExperiment()
Default destructor.
TRestExperiment()
Default constructor.
TRestDataSet fExperimentalData
It contains the experimental data (should contain same columns as the components)
void PrintMetadata() override
Prints on screen the information about the metadata members of TRestAxionSolarFlux.
TRandom3 * fRandom
Internal process random generator.
void SetExperimentalDataSet(const std::string &filename)
std::string fExperimentalDataSet
It defines the filename used to load the dataset.
Double_t fExposureTime
The exposure time. If 0 it will be extracted from the tracking dataset (In us, standard REST unit)
Bool_t fMockData
If enabled it means that the experimental data was MC-generated.
A base class for any REST metadata class.
Definition: TRestMetadata.h:70
virtual void PrintMetadata()
Implemented it in the derived metadata class to print out specific metadata information.
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.
TRestMetadata * InstantiateChildMetadata(int index, std::string pattern="")
This method will retrieve a new TRestMetadata instance of a child element of the present TRestMetadat...
virtual void InitFromConfigFile()
To make settings from rml file. This method must be implemented in the derived class.
void SetSectionName(std::string sName)
set the section name, clear the section content
std::string SearchFile(std::string filename)
Search files in current directory and directories specified in "searchPath" section.
std::string fConfigFileName
Full name of the rml file.
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.
static bool fileExists(const std::string &filename)
Returns true if the file (or directory) with path filename exists.
Definition: TRestTools.cxx:728
static bool isRootFile(const std::string &filename)
Returns true if the filename has *.root* extension.
Definition: TRestTools.cxx:733
std::string ToLower(std::string in)
Convert string to its lower case. Alternative of TString::ToLower.