100#include "TRestDataSetCalibration.h"
102#include "TRestDataSet.h"
103#include "TRestTools.h"
154 TiXmlElement* peakDefinition =
GetElement(
"peak");
155 while (peakDefinition !=
nullptr) {
156 std::string energy =
GetFieldValue(
"energy", peakDefinition);
157 if (energy.empty() || energy ==
"Not defined") {
158 RESTError <<
"< peak variable key does not contain energy!" <<
RESTendl;
168 RESTError <<
"Energy peaks not provided, exiting..." <<
RESTendl;
190 std::unique_ptr<TH1F> spectrum;
191 std::unique_ptr<TGraph> gr;
192 std::unique_ptr<TF1> linearFit;
197 spectrum = std::unique_ptr<TH1F>(
static_cast<TH1F*
>(histo->DrawClone()));
200 const double max = spectrum->GetBinCenter(spectrum->GetMaximumBin());
204 RESTDebug <<
"Max peak position " << max <<
RESTendl;
206 std::vector<TF1*> gauss;
207 gr = std::unique_ptr<TGraph>(
new TGraph());
208 gr->SetName(
"grFit");
212 std::string fName =
"g" + std::to_string(c);
213 double pos = energy * ratio;
214 double start = pos * 0.85;
215 double end = pos * 1.15;
216 TF1* g =
new TF1(fName.c_str(),
"gaus", start, end);
217 RESTDebug <<
"Fitting gaussian " << pos <<
" " << start <<
" " << end <<
RESTendl;
218 spectrum->Fit(g,
"R+");
219 gauss.emplace_back(g);
220 gr->SetPoint(c, g->GetParameter(1), energy);
222 fCalibFWHM.push_back(g->GetParameter(2) * 235. / g->GetParameter(1));
228 gr->SetPoint(c, 0, 0);
231 linearFit = std::unique_ptr<TF1>(
new TF1(
"linearFit",
"pol1"));
232 gr->Fit(
"linearFit",
"S");
240 gr = std::unique_ptr<TGraph>(f->Get<TGraph>(
"grFit"));
241 linearFit = std::unique_ptr<TF1>(f->Get<TF1>(
"linearFit"));
244 fSlope = linearFit->GetParameter(1);
251 auto calibrate = [&linearFit](
double val) {
252 return linearFit->GetParameter(1) * val + linearFit->GetParameter(0);
256 dataSet.SetDataFrame(df);
263 RESTError << file.Error() <<
RESTendl;
268 if (linearFit) linearFit->Write();
271 if (spectrum) spectrum->Write();
272 if (!file.Close()) RESTError << file.Error() <<
RESTendl;
283 RESTMetadata <<
" Energy peaks to calibrate: ";
284 for (
const auto& peak :
fEnergyPeaks) RESTMetadata << peak <<
" keV; ";
286 RESTMetadata <<
" Calibrated peak position: ";
287 for (
const auto& peak :
fCalibPeaks) RESTMetadata << peak <<
" ADC; ";
289 RESTMetadata <<
" Calibrated FWHM: ";
290 for (
const auto& fwhm :
fCalibFWHM) RESTMetadata << fwhm <<
" %; ";
This class is meant to perform the calibration of different runs.
void Calibrate()
Performs the calibration of a given dataSet. If no calibration file is provided, it performs the gaus...
TVector2 fCalibRange
Range to be calibrated.
Double_t fIntercept
Intercept of the calibration fit.
TRestDataSetCalibration()
Default constructor.
Int_t fNBins
Number of bins used in the calibration.
std::string fCalibFile
Name of the calibration file to be used.
void Initialize() override
Function to initialize input/output event members and define the section name.
~TRestDataSetCalibration()
Default destructor.
std::string fDataSetName
Name of the dataSet inside the config file.
void PrintMetadata() override
Prints on screen the information about the metadata members of TRestDataSetCalibration.
Double_t fSlope
Slope from the calibration fit.
std::vector< double > fCalibPeaks
Vector containing calibrated peaks in ADCs.
std::vector< double > fEnergyPeaks
Vector containing expected energy peaks in keV must be sorted.
std::string fOutputFileName
Name of the output file.
std::vector< double > fCalibFWHM
Vector containing calibrated sigma in ADCs.
void InitFromConfigFile() override
Function to initialize some variables from configfile, in case that the variable is not found in the ...
std::string fCalObservable
Calibration variable to be used.
It allows to group a number of runs that satisfy given metadata conditions.
void PrintMetadata() override
Prints on screen the information about the metadata members of TRestDataSet.
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.
void Export(const std::string &filename, std::vector< std::string > excludeColumns={})
It will generate an output file with the dataset compilation. Only the selected branches and the file...
static TRestRootFileHandle Open(const std::string &filename, TRestRootFileMode mode)
@ REST_Info
+show most of the information for each steps
@ REST_Debug
+show the defined debug messages
Int_t GetChar(std::string hint="Press a KEY to continue ...")
Helps to pause the program, printing a message before pausing.
Double_t StringToDouble(std::string in)
Gets a double from a string.