REST-for-Physics  v2.3
Rare Event Searches ToolKit for Physics
Loading...
Searching...
No Matches
TRestDataSetGainMap.h
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
23#ifndef REST_TRestDataSetGainMap
24#define REST_TRestDataSetGainMap
25
26#include <TCanvas.h>
27#include <TFile.h>
28#include <TGraph.h>
29#include <TH1.h>
30#include <TH2.h>
31#include <TRestStringOutput.h>
32#include <TSpectrum.h>
33#include <TTree.h>
34#include <TVector2.h>
35
36#include "TRestCut.h"
37#include "TRestDataSet.h"
38#include "TRestMetadata.h"
39
42 public:
43 class Module;
44
45 private:
47 std::string fObservable = ""; //<
48
50 std::string fSpatialObservableX = ""; //<
51
53 std::string fSpatialObservableY = ""; //<
54
56 std::string fSpatialObservableXSecondary = ""; //<
57
59 std::string fSpatialObservableYSecondary = ""; //<
60
62 std::vector<Module> fModulesCal = {}; //<
63
65 std::string fCalibFileName = ""; //<
66
68 std::string fOutputFileName = ""; //<
69
71 TRestCut* fCut = nullptr; //<
72
73 void Initialize() override;
74 void InitFromConfigFile() override;
75
76 public:
77 std::set<int> GetPlaneIDs() const;
78 std::set<int> GetModuleIDs(const int planeId) const;
79 std::map<int, std::set<int>> GetModuleIDs() const;
80 Int_t GetNumberOfPlanes() const { return GetPlaneIDs().size(); }
81 Int_t GetNumberOfModules() const {
82 int sum = 0;
83 for (auto pID : GetPlaneIDs()) sum += GetModuleIDs(pID).size();
84 return sum;
85 }
86
87 std::string GetCalibrationFileName() const { return fCalibFileName; }
88 std::string GetOutputFileName() const { return fOutputFileName; }
89 std::string GetObservable() const { return fObservable; }
90 std::string GetSpatialObservableX() const { return fSpatialObservableX; }
91 std::string GetSpatialObservableY() const { return fSpatialObservableY; }
92 std::string GetSpatialObservableXSecondary() const { return fSpatialObservableXSecondary; }
93 std::string GetSpatialObservableYSecondary() const { return fSpatialObservableYSecondary; }
94 TRestCut* GetCut() const { return fCut; }
95
96 Module* GetModule(const size_t index = 0);
97 Module* GetModule(const int planeID, const int moduleID);
98 double GetSlopeParameter(const int planeID, const int moduleID, const double x, const double y);
99 double GetInterceptParameter(const int planeID, const int moduleID, const double x, const double y);
100 double GetSlopeParameterFullSpc(const int planeID, const int moduleID);
101 double GetInterceptParameterFullSpc(const int planeID, const int moduleID);
102
103 void SetCalibrationFileName(const std::string& fileName) { fCalibFileName = fileName; }
104 void SetOutputFileName(const std::string& fileName) { fOutputFileName = fileName; }
105 void SetModule(const Module& moduleCal);
106 void SetObservable(const std::string& observable) { fObservable = observable; }
107 void SetSpatialObservableX(const std::string& spatialObservableX) {
108 fSpatialObservableX = spatialObservableX;
109 }
110 void SetSpatialObservableY(const std::string& spatialObservableY) {
111 fSpatialObservableY = spatialObservableY;
112 }
113 void SetCut(TRestCut* cut) { fCut = cut; }
114
115 void Import(const std::string& fileName);
116 void Export(const std::string& fileName = "");
117
119
120 void PrintMetadata() override;
121
122 void GenerateGainMap();
123 void CalibrateDataSet(const std::string& dataSetFileName, std::string outputFileName = "",
124 std::vector<std::string> excludeColumns = {});
125
127 TRestDataSetGainMap(const char* configFilename, std::string name = "");
129
130 ClassDefOverride(TRestDataSetGainMap, 3);
131
132 class Module {
133 private:
135 const TRestDataSetGainMap* p = nullptr; //<!
136
137 std::pair<double, double> FitPeaks(TH1F* hSeg, TGraph* gr);
138 std::pair<double, double> UpdateCalibrationFits(TH1F* hSeg, TGraph* gr);
139
140 public:
143 Int_t fPlaneId = -1; //<
144
145 // Module ID (unique identifier). Although it is not linked to any TRestDetectorReadout it is
146 // recommended to use the same.
147 Int_t fModuleId = -1; //<
148
150 std::vector<double> fEnergyPeaks = {}; //<
151
153 std::vector<TVector2> fRangePeaks = {}; //<
154
156 TVector2 fCalibRange = TVector2(0, 0); //<
157
159 Int_t fNBins = 100; //<
160
162 std::string fDefinitionCut = "1"; //<
163
165 Int_t fNumberOfSegmentsX = 1; //<
166
168 Int_t fNumberOfSegmentsY = 1; //<
169
171 TVector2 fReadoutRange = TVector2(-1, 246.24); //<
172
174 std::set<double> fSplitX = {}; //<
175
177 std::set<double> fSplitY = {}; //<
178
181 std::string fDataSetFileName = ""; //<
182
184 std::vector<std::vector<double>> fSlope = {}; //<
185
187 double fFullSlope = 0; //<
188
190 double fFullIntercept = 0; //<
191
193 std::vector<std::vector<double>> fIntercept = {}; //<
194
197 bool fZeroPoint = false; //<
198
200 bool fAutoRangePeaks = true; //<
201
203 std::vector<std::vector<TH1F*>> fSegSpectra = {}; //<
204
206 std::vector<std::vector<TGraph*>> fSegLinearFit = {}; //<
207
209 TH1F* fFullSpectrum = nullptr; //<
210
212 TGraph* fFullLinearFit = nullptr; //<
213
214 public:
215 void AddPeak(const double& energyPeak, const TVector2& rangePeak = TVector2(0, 0)) {
216 fEnergyPeaks.push_back(energyPeak);
217 fRangePeaks.push_back(rangePeak);
218 }
219
220 void LoadConfigFromTiXmlElement(const TiXmlElement* module);
221
222 TRestDataSetGainMap* GetParent() const { return const_cast<TRestDataSetGainMap*>(p); }
223 std::pair<int, int> GetIndexMatrix(const double x, const double y) const;
224 double GetSlope(const double x, const double y) const;
225 double GetIntercept(const double x, const double y) const;
226 double GetSlopeFullSpc() const { return fFullSlope; };
227 double GetInterceptFullSpc() const { return fFullIntercept; };
228
229 Int_t GetPlaneId() const { return fPlaneId; }
230 Int_t GetModuleId() const { return fModuleId; }
231 std::string GetObservable() const { return p->fObservable; }
232 std::string GetSpatialObservableX() const { return p->fSpatialObservableX; }
233 std::string GetSpatialObservableY() const { return p->fSpatialObservableY; }
234 std::string GetSpatialObservableXSecondary() const { return p->fSpatialObservableXSecondary; }
235 std::string GetSpatialObservableYSecondary() const { return p->fSpatialObservableYSecondary; }
236 inline std::string GetModuleDefinitionCut() const { return fDefinitionCut; }
237 Int_t GetNumberOfSegmentsX() const { return fNumberOfSegmentsX; }
238 Int_t GetNumberOfSegmentsY() const { return fNumberOfSegmentsY; }
239
240 std::set<double> GetSplitX() const { return fSplitX; }
241 std::set<double> GetSplitY() const { return fSplitY; }
242 std::string GetDataSetFileName() const { return fDataSetFileName; }
243 TVector2 GetReadoutRange() const { return fReadoutRange; }
244
245 void DrawSpectrum(const bool drawFits = true, const int color = -1, TCanvas* c = nullptr);
246 void DrawSpectrum(const TVector2& position, bool drawFits = true, int color = -1,
247 TCanvas* c = nullptr);
248 void DrawSpectrum(const int index_x, const int index_y, bool drawFits = true, int color = -1,
249 TCanvas* c = nullptr);
250 void DrawFullSpectrum(const bool drawFits = true, const int color = -1, TCanvas* c = nullptr);
251
252 void DrawLinearFit(TCanvas* c = nullptr);
253 void DrawLinearFit(const TVector2& position, TCanvas* c = nullptr);
254 void DrawLinearFit(const int index_x, const int index_y, TCanvas* c = nullptr);
255
256 void DrawGainMap(const int peakNumber = 0, const bool fullModuleAsRef = true,
257 const bool showText = true);
258
259 void Refit(const TVector2& position, const double energy, const TVector2& range);
260 void Refit(const size_t x, const size_t y, const size_t peakNumber, const TVector2& range);
261 void RefitFullSpc(const double energy, const TVector2& range);
262 void RefitFullSpc(const size_t peakNumber, const TVector2& range);
263 void UpdateCalibrationFits(const size_t x, const size_t y);
265
266 void SetPlaneId(const Int_t& planeId) { fPlaneId = planeId; }
267 void SetModuleId(const Int_t& moduleId) { fModuleId = moduleId; }
268 void SetModuleDefinitionCut(const std::string& moduleDefinitionCut) {
269 fDefinitionCut = moduleDefinitionCut;
270 }
271 void SetCalibrationRange(const TVector2& calibrationRange) { fCalibRange = calibrationRange; }
272 void SetNBins(const Int_t& nBins) { fNBins = nBins; }
273 void SetSplitX();
274 void SetSplitY();
275 void SetSplitX(const std::set<double>& splitX);
276 void SetSplitY(const std::set<double>& splitY);
277 void SetSplits();
278 void SetSplits(const std::set<double>& splitXandY) {
279 SetSplitX(splitXandY);
280 SetSplitY(splitXandY);
281 }
282
283 void SetNumberOfSegmentsX(const Int_t& numberOfSegmentsX) { fNumberOfSegmentsX = numberOfSegmentsX; }
284 void SetNumberOfSegmentsY(const Int_t& numberOfSegmentsY) { fNumberOfSegmentsY = numberOfSegmentsY; }
285
286 void SetDataSetFileName(const std::string& dataSetFileName) { fDataSetFileName = dataSetFileName; }
287 void SetReadoutRange(const TVector2& readoutRange) { fReadoutRange = readoutRange; }
288 void SetZeroPoint(const bool& ZeroPoint) { fZeroPoint = ZeroPoint; }
289 void SetAutoRangePeaks(const bool& autoRangePeaks) { fAutoRangePeaks = autoRangePeaks; }
290
291 void Print() const;
292
293 void GenerateGainMap();
294 void Initialize();
295
296 Module() {}
297 Module(const TRestDataSetGainMap& parent) : p(&parent){};
298 Module(const TRestDataSetGainMap& parent, const Int_t planeId, const Int_t moduleId) : p(&parent) {
299 SetPlaneId(planeId);
300 SetModuleId(moduleId);
301 };
302 ~Module(){};
303 };
304};
305#endif
A class to help on cuts definitions. To be used with TRestAnalysisTree.
Definition TRestCut.h:31
double fFullIntercept
Intercept of the calibration linear fit of whole module.
std::vector< std::vector< double > > fIntercept
Array containing the intercept of the linear fit for each segment.
Int_t fNumberOfSegmentsY
Number of segments in the y direction.
void SetSplitY()
Function to set the class members for segmentation of the detector plane along the Y axis.
std::vector< std::vector< TGraph * > > fSegLinearFit
Array containing the calibration linear fit for each segment.
void SetSplits()
Function to set the class members for segmentation of the detector plane along the X and Y axis.
void SetSplitX()
Function to set the class members for segmentation of the detector plane along the X axis.
std::vector< std::vector< TH1F * > > fSegSpectra
Array containing the observable spectrum for each segment.
void DrawSpectrum(const bool drawFits=true, const int color=-1, TCanvas *c=nullptr)
Function to draw the spectrum for each segment of the module on the same canvas. The canvas is divide...
void LoadConfigFromTiXmlElement(const TiXmlElement *module)
Function to read the parameters from the RML element (TiXmlElement) and set those class members.
void GenerateGainMap()
Function that calculates the calibration parameters for each segment defined at fSplitX and fSplitY a...
double fFullSlope
Slope of the calibration linear fit of whole module.
void DrawGainMap(const int peakNumber=0, const bool fullModuleAsRef=true, const bool showText=true)
Function to draw the relative gain map for a given energy peak of the module.
double GetSlope(const double x, const double y) const
Function to get the calibration parameter slope for a given x and y position on the detector plane.
std::set< double > fSplitX
Split points in the x direction.
void Print() const
Prints on screen the information about the members of Module.
void Refit(const TVector2 &position, const double energy, const TVector2 &range)
Function to fit again manually a peak for a given segment of the module.
std::set< double > fSplitY
Split points in the y direction.
void RefitFullSpc(const double energy, const TVector2 &range)
Function to fit again manually a peak for the whole module spectrum. The calibration curve is updated...
std::vector< TVector2 > fRangePeaks
Range of the peaks to be used for the calibration. If empty it will be automatically calculated.
const TRestDataSetGainMap * p
Pointer to the parent class.
void UpdateCalibrationFitsFullSpc()
Function to update the calibration curve for the whole module. The calibration curve is cleared and t...
std::vector< std::vector< double > > fSlope
Array containing the slope of the linear fit for each segment.
Int_t fNumberOfSegmentsX
Number of segments in the x direction.
Int_t fNBins
Number of bins for the spectrum histograms.
TVector2 fCalibRange
Calibration range. If fCalibRange.X()>=fCalibRange.Y() the range will be automatically calculated.
std::string fDefinitionCut
Cut that defines which events are from this module. Default is "1" (all events).
TGraph * fFullLinearFit
Calibration linear fit for the whole module.
TH1F * fFullSpectrum
Spectrum of the observable for the whole module.
bool fAutoRangePeaks
Automatic range for the peaks fitting. See GenerateGainMap() for more information of the logic.
double GetIntercept(const double x, const double y) const
Function to get the calibration parameter intercept for a given x and y position on the detector plan...
TVector2 fReadoutRange
Readout dimensions.
std::pair< int, int > GetIndexMatrix(const double x, const double y) const
Function to get the index of the matrix of calibration parameters for a given x and y position on the...
std::vector< double > fEnergyPeaks
Energy of the peaks to be used for the calibration.
Metadata class to calculate,store and apply the gain corrected calibration of a group of detectors.
double GetInterceptParameter(const int planeID, const int moduleID, const double x, const double y)
Function to get the intercept parameter of the module with planeID and moduleID at physical position ...
double GetSlopeParameter(const int planeID, const int moduleID, const double x, const double y)
Function to get the slope parameter of the module with planeID and moduleID at physical position (x,...
std::string fObservable
Observable that will be used to calculate the gain map.
double GetInterceptParameterFullSpc(const int planeID, const int moduleID)
Function to get the intercept parameter of the whole module with planeID and moduleID.
void CalibrateDataSet(const std::string &dataSetFileName, std::string outputFileName="", std::vector< std::string > excludeColumns={})
Function to calibrate a dataset with this gain map.
~TRestDataSetGainMap()
Default destructor.
void GenerateGainMap()
Function to calculate the calibration parameters of all modules.
std::string fSpatialObservableXSecondary
Secondary observable that will be used to segmentize the gain map in the x direction (if needed)
TRestDataSetGainMap()
Default constructor.
void Initialize() override
Making default settings.
std::vector< Module > fModulesCal
List of modules.
void SetModule(const Module &moduleCal)
Function to set a module calibration. If the module calibration already exists (same planeId and modu...
void InitFromConfigFile() override
Initialization of TRestDataSetGainMap members through a RML file.
double GetSlopeParameterFullSpc(const int planeID, const int moduleID)
Function to get the slope parameter of the whole module with planeID and moduleID.
void Export(const std::string &fileName="")
Function to export the calibration to the file fileName.
void PrintMetadata() override
Prints on screen the information about the metadata members.
std::map< int, std::set< int > > GetModuleIDs() const
Function to get the map of the module IDs for each plane ID.
std::string fSpatialObservableY
Observable that will be used to segmentize the gain map in the y direction.
std::string fCalibFileName
Name of the file that contains the calibration data.
TRestCut * fCut
Cut to be applied to the calibration data.
std::string fOutputFileName
Name of the file where the gain map was (or will be) exported.
std::string fSpatialObservableYSecondary
Secondary observable that will be used to segmentize the gain map in the y direction (if needed)
std::set< int > GetPlaneIDs() const
Function to get a list (set) of the plane IDs.
std::string fSpatialObservableX
Observable that will be used to segmentize the gain map in the x direction.
Module * GetModule(const size_t index=0)
Function to retrieve the module calibration by index. Default is 0.
void Import(const std::string &fileName)
Function to import the calibration parameters from the root file fileName.
A base class for any REST metadata class.