REST-for-Physics  v2.3
Rare Event Searches ToolKit for Physics
TRestResponse.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 "TRestResponse.h"
42 
43 ClassImp(TRestResponse);
44 
49 
64 TRestResponse::TRestResponse(const char* cfgFileName, const std::string& name) : TRestMetadata(cfgFileName) {
65  Initialize();
66 
68 
70 }
71 
76 
81 void TRestResponse::Initialize() { SetSectionName(this->ClassName()); }
82 
102 void TRestResponse::LoadResponse(Bool_t transpose) {
103  if (fFilename == "") {
104  RESTError << "TRestResponse::LoadResponse. The response filename was not defined" << RESTendl;
105  return;
106  }
107 
108  std::string fullFilename = SearchFile(fFilename);
109  if (fullFilename.empty()) {
110  RESTError << "TRestResponse::LoadResponse. The response filename was not found!" << RESTendl;
111  RESTError << "Filename : " << fFilename << RESTendl;
112  RESTError << "You may want to define <seachPath inside <globals> definition" << RESTendl;
113  return;
114  }
115 
116  std::string extension = TRestTools::GetFileNameExtension(fFilename);
117  if (!extension.empty() && extension[0] == 'N' && extension.back() == 'f') {
119 
120  fTransposed = false;
121  if (transpose) {
122  fTransposed = transpose;
124  }
125 
126  return;
127  }
128 
129  RESTError << "Extension format - " << extension << " - not recognized!" << RESTendl;
130 }
131 
140 std::vector<std::pair<Double_t, Double_t>> TRestResponse::GetResponse(Double_t input) {
141  std::vector<std::pair<Double_t, Double_t>> response;
142 
143  if (fResponseMatrix.empty()) {
144  RESTError << "TRestResponse::GetResponse. Response matrix has not been loaded yet!" << RESTendl;
145  return response;
146  }
147 
148  if (input < GetInputRange().X() || input > GetInputRange().Y()) {
149  RESTError << "TRestResponse::GetResponse. The input value " << input << " is outside range!"
150  << RESTendl;
151  return response;
152  }
153 
154  if (!fInterpolation) {
155  Int_t bin = (Int_t)((input - fOrigin.X()) / fBinSize);
156 
157  for (std::size_t n = 0; n < fResponseMatrix[bin].size(); n++) {
158  Double_t output = fOrigin.Y() + ((double)n + 0.5) * fBinSize;
159  Double_t value = fResponseMatrix[bin][n];
160 
161  std::pair<Double_t, Double_t> outp{output, value};
162 
163  response.push_back(outp);
164  }
165 
166  return response;
167  }
168 
169  Int_t binLeft = (Int_t)((input - fBinSize / 2. - fOrigin.X()) / fBinSize);
170  Int_t binRight = binLeft + 1;
171 
172  Double_t distLeft = (input - fBinSize / 2. + fOrigin.X()) - binLeft * fBinSize;
173 
174  if (input <= GetInputRange().X() + fBinSize / 2. || input >= GetInputRange().Y() - fBinSize / 2.)
175  binRight = binLeft;
176 
177  /*
178  std::cout << "Top : " << GetInputRange().Y() - fBinSize/2. << std::endl;
179  std::cout << "binLeft : " << binLeft << std::endl;
180  std::cout << "binRight : " << binRight << std::endl;
181  std::cout << "dLeft : " << distLeft << std::endl;
182  std::cout << "dLeft/fBinSize : " << distLeft/fBinSize << std::endl;
183  std::cout << "1 - distLeft/fBinSize : " << 1 - distLeft/fBinSize << std::endl;
184  */
185 
186  for (std::size_t n = 0; n < fResponseMatrix[binLeft].size(); n++) {
187  Double_t output = fOrigin.Y() + ((double)n + 0.5) * fBinSize;
188 
189  Double_t value = fResponseMatrix[binLeft][n] * (1 - distLeft / fBinSize) +
190  fResponseMatrix[binRight][n] * distLeft / fBinSize;
191 
192  std::pair<Double_t, Double_t> outp{output, value};
193 
194  response.push_back(outp);
195 
196  /*
197  std::cout << "n: " << n << " output : " << output << std::endl;
198  std::cout << "response: " << response << std::endl;
199  */
200  }
201 
202  return response;
203 }
204 
208 void TRestResponse::PrintResponseMatrix(Int_t fromRow = 0, Int_t toRow = 0) {
209  TRestTools::PrintTable(fResponseMatrix, fromRow, toRow);
210 }
211 
217 
218  RESTMetadata << "Response file : " << fFilename << RESTendl;
219  RESTMetadata << "Variable : " << fVariable << RESTendl;
220  RESTMetadata << "Bin size : " << fBinSize << RESTendl;
221  RESTMetadata << " " << RESTendl;
222 
223  if (!fResponseMatrix.empty()) {
224  RESTMetadata << "Response matrix has been loaded" << RESTendl;
225  RESTMetadata << " - Number of columns: " << fResponseMatrix[0].size() << RESTendl;
226  RESTMetadata << " - Number of rows : " << fResponseMatrix.size() << RESTendl;
227  RESTMetadata << " - Input range : " << GetInputRange().X() << " - " << GetInputRange().Y()
228  << RESTendl;
229  RESTMetadata << " - Output range : " << GetOutputRange().X() << " - " << GetOutputRange().Y()
230  << RESTendl;
231 
232  if (fTransposed) {
233  RESTMetadata << " " << RESTendl;
234  RESTMetadata << "Original matrix was transposed" << RESTendl;
235  }
236  } else {
237  RESTMetadata << "Response matrix has NOT been loaded" << RESTendl;
238  RESTMetadata << "Try calling TRestResponse::LoadResponse()" << RESTendl;
239  }
240  if (fInterpolation) {
241  RESTMetadata << " " << RESTendl;
242  RESTMetadata << "Interpolation is enabled" << RESTendl;
243  } else {
244  RESTMetadata << " " << RESTendl;
245  RESTMetadata << "Interpolation is disabled" << RESTendl;
246  }
247  RESTMetadata << "----" << RESTendl;
248 }
A base class for any REST metadata class.
Definition: TRestMetadata.h:74
virtual void PrintMetadata()
Implemented it in the derived metadata class to print out specific metadata information.
endl_t RESTendl
Termination flag object for TRestStringOutput.
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 SearchFile(std::string filename)
Search files in current directory and directories specified in "searchPath" section.
std::string fConfigFileName
Full name of the rml file.
A response matrix that might be applied to a given component inside a TRestComponent.
Definition: TRestResponse.h:29
Double_t fBinSize
The resolution of the response matrix (binning)
Definition: TRestResponse.h:41
std::vector< std::vector< Float_t > > fResponseMatrix
The response matrix.
Definition: TRestResponse.h:44
Bool_t fTransposed
Determines if the response matrix has been transposed.
Definition: TRestResponse.h:47
void Initialize() override
It will initialize the data frame with the filelist and column names (or observables) that have been ...
void LoadResponse(Bool_t transpose=true)
It loads into the fResponseMatrix data member the response from a file.
~TRestResponse()
Default destructor.
Bool_t fInterpolation
It allows to decide if the returned response should be interpolated (default:false)
Definition: TRestResponse.h:50
std::string fVariable
It defines the variable name for which the response should be applied to.
Definition: TRestResponse.h:35
std::vector< std::pair< Double_t, Double_t > > GetResponse(Double_t input)
This method will return a vector of std::pair, each pair will contain the output energy together with...
TVector2 fOrigin
First element of the response matrix (input/incident, output/detected)
Definition: TRestResponse.h:38
TRestResponse()
Default constructor.
void PrintMetadata() override
Prints on screen the information about the metadata members of TRestAxionSolarFlux.
std::string fFilename
The filename used to import the response matrix.
Definition: TRestResponse.h:32
void PrintResponseMatrix(Int_t fromRow, Int_t toRow)
Prints on screen the information about the metadata members of TRestAxionSolarFlux.
@ REST_Info
+show most of the information for each steps
static void TransposeTable(std::vector< std::vector< T >> &data)
It transposes the std::vector<std::vector> table given in the argument. It will transform rows in col...
Definition: TRestTools.cxx:345
static std::string GetFileNameExtension(const std::string &fullname)
Gets the file extension as the substring found after the latest ".".
Definition: TRestTools.cxx:823
static int PrintTable(std::vector< std::vector< T >> data, Int_t start=0, Int_t end=0)
Prints the contents of the vector table given as argument in screen. Allowed types are Int_t,...
Definition: TRestTools.cxx:163
static int ReadBinaryTable(std::string fName, std::vector< std::vector< T >> &data, Int_t columns=-1)
Reads a binary file containing a fixed-columns table with values.
Definition: TRestTools.cxx:253