Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding method to apply a calibration with gain correction. #484

Merged
merged 22 commits into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
2b7670a
Adding method to apply a calibration with gain correction.
AlvaroEzq Sep 26, 2023
b858a12
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 26, 2023
db546a4
Merge branch 'master' into alvaroez_CalCorr
AlvaroEzq Sep 26, 2023
3d40d87
Fixing compilation issues with release mode
AlvaroEzq Sep 26, 2023
d402ad9
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 26, 2023
65a1f95
Update source/framework/analysis/inc/TRestCalibrationCorrection.h
AlvaroEzq Sep 26, 2023
737eaf8
Clang format
AlvaroEzq Sep 26, 2023
665cd13
Merge branch 'master' into alvaroez_CalCorr
jgalan Sep 27, 2023
14acd61
Update source/framework/analysis/src/TRestCalibrationCorrection.cxx
AlvaroEzq Sep 27, 2023
4eb3497
Update source/framework/analysis/src/TRestCalibrationCorrection.cxx
AlvaroEzq Sep 27, 2023
ac1786e
Improving documentation
AlvaroEzq Sep 27, 2023
a01aefe
Fix GetNumberOfModules()
AlvaroEzq Sep 28, 2023
b1a482a
Renaming TRestCalibrationCorrection to TRestDataSetGainMap
AlvaroEzq Sep 28, 2023
243241d
Changing class name inside the class after renaming
AlvaroEzq Sep 28, 2023
a50485d
Use Redefine for pmID if a column with same name already exists
AlvaroEzq Oct 6, 2023
9b7d6af
Changed automatic naming on CalibrateDataSet() to avoid overwritting …
AlvaroEzq Oct 12, 2023
b006c39
Renaming GetModuleCalibration -> GetModule and cleaning some comments
AlvaroEzq Oct 12, 2023
6c44a6f
Improving DrawSpectrum() method and documentation
AlvaroEzq Oct 12, 2023
bcc8a38
Fixing class name in rml example
AlvaroEzq Oct 21, 2023
e306346
Changing method name Calibrate and CalculateCalibrationParameters to …
AlvaroEzq Oct 21, 2023
2346740
Adding methods to set any split distribution 'by hand'.
AlvaroEzq Oct 21, 2023
cdaa8ef
Merge branch 'master' into alvaroez_CalCorr
jgalan Nov 15, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added doc/doxygen/images/drawGainMap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added doc/doxygen/images/drawSpectrum.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added doc/doxygen/images/gainCorrectionComparison.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 24 additions & 0 deletions examples/calibrationCorrection.rml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0"?>
<TRestDataSetGainMap name="calib" verboseLevel="info">
<parameter name="calibFileName" value="myDataSet.root"/>
<parameter name="outputFileName" value = "myCalibration.root"/>
<parameter name = "observable" value="rawAna_ThresholdIntegral"/>
<parameter name = "spatialObservableX" value="hitsAna_xMean"/>
<parameter name = "spatialObservableY" value="hitsAna_yMean"/>
<module planeId="0" moduleId="0"
moduleDefinitionCut="TREXsides_tagId==1"
numberOfSegmentsX="5"
numberOfSegmentsY="5"
readoutRange="(-1,246.24)">
<peak energy="22.5" range=""/>
<peak energy="8.0" range=""/>
</module>
<module planeId="1" moduleId="0"
moduleDefinitionCut="TREXsides_tagId==2"
numberOfSegmentsX="5"
numberOfSegmentsY="5"
readoutRange="(-1,246.24)">
<peak energy="22.5" range=""/>
<peak energy="8.0" range=""/>
</module>
</TRestDataSetGainMap>
215 changes: 215 additions & 0 deletions source/framework/analysis/inc/TRestDataSetGainMap.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
/*************************************************************************
* This file is part of the REST software framework. *
* *
* Copyright (C) 2016 GIFNA/TREX (University of Zaragoza) *
* For more information see https://gifna.unizar.es/trex *
* *
* REST is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* REST is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have a copy of the GNU General Public License along with *
* REST in $REST_PATH/LICENSE. *
* If not, see https://www.gnu.org/licenses/. *
* For the list of contributors see $REST_PATH/CREDITS. *
*************************************************************************/

#ifndef REST_TRestDataSetGainMap
#define REST_TRestDataSetGainMap

#include <TCanvas.h>
#include <TFile.h>
#include <TGraph.h>
#include <TH1.h>
#include <TH2.h>
#include <TRestStringOutput.h>
#include <TSpectrum.h>
#include <TTree.h>
#include <TVector2.h>

#include "TRestDataSet.h"
#include "TRestMetadata.h"

/// Metadata class to calculate,store and apply the gain corrected calibration of a group of detectors.
class TRestDataSetGainMap : public TRestMetadata {
public:
class Module;

private:
std::string fObservable = ""; //"rawAna_ThresholdIntegral"; //<
std::string fSpatialObservableX = ""; //"hitsAna_xMean"; //<
std::string fSpatialObservableY = ""; //"hitsAna_yMean"; //<

std::vector<Module> fModulesCal = {};
std::string fCalibFileName = "";
std::string fOutputFileName = "";

void Initialize() override;
void InitFromConfigFile() override;

public:
std::set<int> GetPlaneIDs() const;
std::set<int> GetModuleIDs(const int planeId) const;
std::map<int, std::set<int>> GetModuleIDs() const;
Int_t GetNumberOfPlanes() const { return GetPlaneIDs().size(); }
Int_t GetNumberOfModules() const {
int sum = 0;
for (auto pID : GetPlaneIDs()) sum += GetModuleIDs(pID).size();
return sum;
}

std::string GetCalibrationFileName() const { return fCalibFileName; }
std::string GetOutputFileName() const { return fOutputFileName; }
std::string GetObservable() const { return fObservable; }
std::string GetSpatialObservableX() const { return fSpatialObservableX; }
std::string GetSpatialObservableY() const { return fSpatialObservableY; }

Module* GetModule(const int planeID, const int moduleID);
double GetSlopeParameter(const int planeID, const int moduleID, const double x, const double y);
double GetInterceptParameter(const int planeID, const int moduleID, const double x, const double y);

void SetCalibrationFileName(const std::string& fileName) { fCalibFileName = fileName; }
void SetOutputFileName(const std::string& fileName) { fOutputFileName = fileName; }
void SetModuleCalibration(const Module& moduleCal);
void SetObservable(const std::string& observable) { fObservable = observable; }
void SetSpatialObservableX(const std::string& spatialObservableX) {
fSpatialObservableX = spatialObservableX;
}
void SetSpatialObservableY(const std::string& spatialObservableY) {
fSpatialObservableY = spatialObservableY;
}

void Import(const std::string& fileName);
void Export(const std::string& fileName = "");

TRestDataSetGainMap& operator=(TRestDataSetGainMap& src);

public:
void PrintMetadata() override;

void GenerateGainMap();
void CalibrateDataSet(const std::string& dataSetFileName, std::string outputFileName = "");

TRestDataSetGainMap();
TRestDataSetGainMap(const char* configFilename, std::string name = "");
~TRestDataSetGainMap();

ClassDefOverride(TRestDataSetGainMap, 1);

class Module {
private:
const TRestDataSetGainMap* p = nullptr; //<! Pointer to the parent class
public: /// Members that will be written to the ROOT file.
Int_t fPlaneId = -1; //< // Plane ID
Int_t fModuleId = -1; //< // Module ID

std::vector<double> fEnergyPeaks = {};
std::vector<TVector2> fRangePeaks = {}; //{TVector2(230000, 650000), TVector2(40000, 230000)};
TVector2 fCalibRange = TVector2(0, 0); //< // Calibration range
Int_t fNBins = 100; //< // Number of bins for the spectrum histograms
std::string fDefinitionCut = ""; //"TREXsides_tagId == 2"; //<

Int_t fNumberOfSegmentsX = 1; //<
Int_t fNumberOfSegmentsY = 1; //<
TVector2 fReadoutRange = TVector2(-1, 246.24); //< // Readout dimensions
std::set<double> fSplitX = {}; //<
std::set<double> fSplitY = {}; //<

std::string fDataSetFileName = ""; //< // File name for the calibration dataset

std::vector<std::vector<double>> fSlope = {}; //<
std::vector<std::vector<double>> fIntercept = {}; //<

bool fZeroPoint = false; //< Zero point will be automatically added if there are less than 2 peaks
bool fAutoRangePeaks = true; //< Automatic range peaks
std::vector<std::vector<TH1F*>> fSegSpectra = {};
std::vector<std::vector<TGraph*>> fSegLinearFit = {};

public:
void AddPeak(const double& energyPeak, const TVector2& rangePeak = TVector2(0, 0)) {
fEnergyPeaks.push_back(energyPeak);
fRangePeaks.push_back(rangePeak);
}

void LoadConfigFromTiXmlElement(const TiXmlElement* module);

std::pair<int, int> GetIndexMatrix(const double x, const double y) const;
double GetSlope(const double x, const double y) const;
double GetIntercept(const double x, const double y) const;

Int_t GetPlaneId() const { return fPlaneId; }
Int_t GetModuleId() const { return fModuleId; }
std::string GetObservable() const { return p->fObservable; }
std::string GetSpatialObservableX() const { return p->fSpatialObservableX; }
std::string GetSpatialObservableY() const { return p->fSpatialObservableY; }
inline std::string GetModuleDefinitionCut() const { return fDefinitionCut; }
Int_t GetNumberOfSegmentsX() const { return fNumberOfSegmentsX; }
Int_t GetNumberOfSegmentsY() const { return fNumberOfSegmentsY; }

std::set<double> GetSplitX() const { return fSplitX; }
std::set<double> GetSplitY() const { return fSplitY; }
std::string GetDataSetFileName() const { return fDataSetFileName; }
TVector2 GetReadoutRangeVar() const { return fReadoutRange; }

void DrawSpectrum(bool drawFits = true, int color = -1, TCanvas* c = nullptr);
void DrawSpectrum(const double x, const double y, bool drawFits = true, int color = -1,
TCanvas* c = nullptr);
void DrawSpectrum(const size_t index_x, const size_t index_y, bool drawFits = true, int color = -1,
TCanvas* c = nullptr);
void DrawFullSpectrum();

void DrawLinearFit();
void DrawLinearFit(const double x, const double y, TCanvas* c = nullptr);
void DrawLinearFit(const size_t index_x, const size_t index_y, TCanvas* c = nullptr);

void DrawGainMap(const int peakNumber = 0);

void Refit(const double x, const double y, const double energy, const TVector2& range);
void Refit(const int x, const int y, const int peakNumber, const TVector2& range);

void SetPlaneId(const Int_t& planeId) { fPlaneId = planeId; }
void SetModuleId(const Int_t& moduleId) { fModuleId = moduleId; }
void SetModuleDefinitionCut(const std::string& moduleDefinitionCut) {
fDefinitionCut = moduleDefinitionCut;
}
void SetCalibrationRange(const TVector2& calibrationRange) { fCalibRange = calibrationRange; }
void SetNBins(const Int_t& nBins) { fNBins = nBins; }
void SetSplitX();
void SetSplitY();
void SetSplitX(const std::set<double>& splitX);
void SetSplitY(const std::set<double>& splitY);
void SetSplits();
void SetSplits(const std::set<double>& splitXandY) {
SetSplitX(splitXandY);
SetSplitY(splitXandY);
}

void SetNumberOfSegmentsX(const Int_t& numberOfSegmentsX) { fNumberOfSegmentsX = numberOfSegmentsX; }
void SetNumberOfSegmentsY(const Int_t& numberOfSegmentsY) { fNumberOfSegmentsY = numberOfSegmentsY; }

void SetDataSetFileName(const std::string& dataSetFileName) { fDataSetFileName = dataSetFileName; }
void SetReadoutRange(const TVector2& readoutRange) { fReadoutRange = readoutRange; }
void SetZeroPoint(const bool& ZeroPoint) { fZeroPoint = ZeroPoint; }
void SetAutoRangePeaks(const bool& autoRangePeaks) { fAutoRangePeaks = autoRangePeaks; }

void Print() const;

void GenerateGainMap();
void Initialize();

Module() {}
Module(const TRestDataSetGainMap& parent) : p(&parent){};
Module(const TRestDataSetGainMap& parent, const Int_t planeId, const Int_t moduleId) : p(&parent) {
SetPlaneId(planeId);
SetModuleId(moduleId);
};
~Module(){};
};
};
#endif
Loading