100 #include "TRestDataSetCalibration.h"
102 #include "TRestDataSet.h"
153 TiXmlElement* peakDefinition =
GetElement(
"peak");
154 while (peakDefinition !=
nullptr) {
155 std::string energy =
GetFieldValue(
"energy", peakDefinition);
156 if (energy.empty() || energy ==
"Not defined") {
157 RESTError <<
"< peak variable key does not contain energy!" <<
RESTendl;
167 RESTError <<
"Energy peaks not provided, exiting..." <<
RESTendl;
189 std::unique_ptr<TH1F> spectrum;
190 std::unique_ptr<TGraph> gr;
191 std::unique_ptr<TF1> linearFit;
196 spectrum = std::unique_ptr<TH1F>(
static_cast<TH1F*
>(histo->DrawClone()));
199 const double max = spectrum->GetBinCenter(spectrum->GetMaximumBin());
203 RESTDebug <<
"Max peak position " << max <<
RESTendl;
205 std::vector<TF1*> gauss;
206 gr = std::unique_ptr<TGraph>(
new TGraph());
207 gr->SetName(
"grFit");
211 std::string fName =
"g" + std::to_string(c);
212 double pos = energy * ratio;
213 double start = pos * 0.85;
214 double end = pos * 1.15;
215 TF1* g =
new TF1(fName.c_str(),
"gaus", start, end);
216 RESTDebug <<
"Fitting gaussian " << pos <<
" " << start <<
" " << end <<
RESTendl;
217 spectrum->Fit(g,
"R+");
218 gauss.emplace_back(g);
219 gr->SetPoint(c, g->GetParameter(1), energy);
221 fCalibFWHM.push_back(g->GetParameter(2) * 235. / g->GetParameter(1));
227 gr->SetPoint(c, 0, 0);
230 linearFit = std::unique_ptr<TF1>(
new TF1(
"linearFit",
"pol1"));
231 gr->Fit(
"linearFit",
"S");
239 gr = std::unique_ptr<TGraph>(f->Get<TGraph>(
"grFit"));
240 linearFit = std::unique_ptr<TF1>(f->Get<TF1>(
"linearFit"));
243 fSlope = linearFit->GetParameter(1);
250 auto calibrate = [&linearFit](
double val) {
251 return linearFit->GetParameter(1) * val + linearFit->GetParameter(0);
255 dataSet.SetDataFrame(df);
263 if (linearFit) linearFit->Write();
266 if (spectrum) spectrum->Write();
278 RESTMetadata <<
" Energy peaks to calibrate: ";
279 for (
const auto& peak :
fEnergyPeaks) RESTMetadata << peak <<
" keV; ";
281 RESTMetadata <<
" Calibrated peak position: ";
282 for (
const auto& peak :
fCalibPeaks) RESTMetadata << peak <<
" ADC; ";
284 RESTMetadata <<
" Calibrated FWHM: ";
285 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...
@ 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.