9#include <cdc/calibration/CDCdEdx/CDCDedxHadBGAlgorithm.h>
22 setDescription(
"A calibration algorithm for CDC dE/dx hadron Beta Gamma curve and resolution fitting");
36 B2FATAL(
"There is no valid payload for Beta-Gamma saturation");
39 std::vector< std::string > particles = {
"muon",
"kaon",
"proton",
"pion",
"electron"};
42 for (
int i = 0; i < int(particles.size()); i++) {
43 std::string p = particles[i];
48 gSystem->Exec(
"mkdir -p plots/HadronPrep");
49 gSystem->Exec(
"mkdir -p plots/HadronCal/BGfits");
50 gSystem->Exec(
"mkdir -p plots/HadronCal/Resofits");
51 gSystem->Exec(
"mkdir -p plots/HadronCal/Monitoring");
66 std::string filename =
" ";
68 for (
int iter = 0; iter <
m_iter; ++iter) {
70 const bool isFirstIteration = (iter == 0);
71 const bool isFinalIteration = (iter ==
m_iter - 1);
73 const bool makeIterationSummary =
m_ismakePlots && (isFirstIteration || isFinalIteration);
75 std::string sfx = Form(
"%s_iter%d",
m_suffix.data(), iter);
76 filename = Form(
"widget_corrected_NewHSpars_1D_%s.root", sfx.data());
93 SigmaFits(particles, sfx,
"nhit", makeIterationSummary);
97 SigmaFits(particles, sfx,
"costh", makeIterationSummary);
102 filename = Form(
"widget_corrected_NewHSpars_1D_%s_final.root",
m_suffix.data());
106 B2INFO(
"Saving calibration for: " <<
m_suffix <<
"");
118 if (cruns == 0)B2INFO(
"CDCDedxHadBGAlgorithm: start exp " << expRun.first <<
" and run " << expRun.second <<
"");
123 int estart = erStart.first;
124 int rstart = erStart.second;
128 m_suffix = Form(
"e%dr%d", estart, rstart);
129 B2INFO(
"tool exp = " << estart <<
", run = " << rstart <<
", m_suffix = " <<
m_suffix <<
"");
136 std::ifstream fins(
"parameters.sigmacos.fit"), fin(
"parameters.bgcurve.fit");
138 if (!fin.good()) B2FATAL(
"\t WARNING: CANNOT FIND parameters.bgcurve.fit!");
139 if (!fins.good()) B2FATAL(
"\tWARNING: CANNOT FIND parameters.sigmacos.fit!");
142 double meanpars, sigmapars;
143 std::vector<double> v_meanpars, v_sigmapars;
145 B2INFO(
"\t --> Curve parameters");
146 for (
int i = 0; i < 15; ++i) {
147 fin >> par >> meanpars;
148 v_meanpars.push_back(meanpars);
149 B2INFO(
"\t\t (" << i <<
")" << v_meanpars[i]);
154 B2INFO(
"\t --> Sigma parameters");
155 for (
int i = 0; i < 17; ++i) {
156 fins >> par >> sigmapars;
157 v_sigmapars.push_back(sigmapars);
158 B2INFO(
"\t\t (" << i <<
")" << v_sigmapars[i]);
162 B2INFO(
"dE/dx Calibration done for " << v_meanpars.size() <<
" CDC Beta Gamma curve");
167 B2INFO(
"dE/dx Calibration done for " << v_sigmapars.size() <<
" CDC Beta Gamma resolution");
173 const std::string& sfx,
const bool makeIterationSummary)
176 TFile* outfile =
new TFile(filename.data(),
"RECREATE");
178 for (
int i = 0; i < int(particles.size()); ++i) {
180 std::string p = particles[i];
193 const bool makeIterationSummary)
198 std::string filename = Form(
"widget_%s_1D_%s.root", svar.data(), sfx.data());
200 TFile* outfile =
new TFile(filename.data(),
"RECREATE");
203 for (
int ip = 0; ip < int(particles.size()); ++ip) {
205 std::string particle = particles[ip];
213 if (mass == 0.0) B2FATAL(
"Mass of particle " << particle.data() <<
" is zero");
223 hadron->SetBranchAddress(
"dedxnosat", &dedxnosat);
224 hadron->SetBranchAddress(
"p", &p);
225 hadron->SetBranchAddress(
"costh", &costh);
226 hadron->SetBranchAddress(
"timereso", &timereso);
227 hadron->SetBranchAddress(
"nhits", &nhits);
232 double nstep = (upper - lower + 1) / nbins;
234 if (svar ==
"costh") {
237 nstep = (upper - lower) / nbins;
241 std::vector<TH1F*> hdedx_var;
244 prep.
defineHisto(hdedx_var,
"chi", svar, particle);
247 std::vector<double> sumvar(nbins);
248 std::vector<int> sumsize(nbins);
249 for (
int i = 0; i < nbins; ++i) {
268 for (
unsigned int index = 0; index < hadron->GetEntries(); ++index) {
270 hadron->GetEntry(index);
271 double bg = std::fabs(p) / mass;
273 if (std::fabs(p) > 8.0)
continue;
275 if (svar ==
"nhit") {
276 if (nhits < lower || nhits > upper)
continue;
277 }
else if (svar ==
"costh") {
278 if (costh > upper || costh < lower)
continue;
281 if (dedxnosat <= 0)
continue;
282 if (!(costh == costh))
continue;
284 if (particle ==
"proton") {
285 if ((dedxnosat - 0.45) * std::abs(p) * std::abs(p) <
m_cut)
continue;
288 if (particle ==
"electron" || particle ==
"muon") {
289 if (std::fabs(p) > 2.0)
continue;
292 double dedx_new = had.
D2I(costh, had.
I2D(costh, 1.0) * dedxnosat);
293 double dedx_cur = mgpar.
getMean(bg);
295 if (svar ==
"nhit") {
297 int nhitBin =
static_cast<int>((std::fabs(nhits) - lower) / nstep);
299 if (nhitBin < 0) nhitBin = 0;
300 else if (nhitBin >= nbins) nhitBin = nbins - 1;
303 hdedx_var[nhitBin]->Fill((dedx_new - dedx_cur) / res_cor);
305 sumvar[nhitBin] += nhits;
306 sumsize[nhitBin] += 1;
309 double denom = (upper - lower);
310 int cosBin =
static_cast<int>((costh - lower) / denom * nbins);
312 if (cosBin < 0) cosBin = 0;
313 else if (cosBin >= nbins) cosBin = nbins - 1;
316 if (res_cor != 0) hdedx_var[cosBin]->Fill((dedx_new - dedx_cur) / res_cor);
317 sumvar[cosBin] += costh;
318 sumsize[cosBin] += 1;
329 TTree* tTree =
new TTree(Form(
"%s_%s", particle.data(), svar.data()),
"chi m_means and m_errors");
330 double avg, mean, mean_err, sigma, sigma_err;
332 tTree->Branch(
"avg", &avg,
"avg/D");
333 tTree->Branch(
"chimean", &mean,
"chimean/D");
334 tTree->Branch(
"chimean_err", &mean_err,
"chimean_err/D");
335 tTree->Branch(
"chisigma", &sigma,
"chisigma/D");
336 tTree->Branch(
"chisigma_err", &sigma_err,
"chisigma_err/D");
338 double avg_sigma = 0.0;
339 std::vector<double> var(nbins), varres(nbins), varreserr(nbins), varmean(nbins), varmeanerr(nbins);
342 for (
int i = 0; i < nbins; ++i) {
344 varres[i] = 0.0, varmean[i] = 0.0;
345 varreserr[i] = 0.0, varmeanerr[i] = 0.0;
346 var[i] = (sumsize[i] > 0) ? (sumvar[i] / sumsize[i]) : 0.0;
349 if (hdedx_var[i]->Integral() > 100) {
351 prep.
fit(hdedx_var[i], particle, stats);
353 varmean[i] = hdedx_var[i]->GetFunction(
"gaus")->GetParameter(1);
354 varmeanerr[i] = hdedx_var[i]->GetFunction(
"gaus")->GetParError(1);
355 varres[i] = hdedx_var[i]->GetFunction(
"gaus")->GetParameter(2);;
356 varreserr[i] = hdedx_var[i]->GetFunction(
"gaus")->GetParError(2);
359 avg_sigma += varres[i];
363 if (count_bins > 0) avg_sigma /= count_bins;
364 for (
int i = 0; i < nbins; ++i) {
367 sigma = varres[i] / avg_sigma;
368 sigma_err = varreserr[i] / avg_sigma;
371 mean_err = varmeanerr[i];
378 if (makeIterationSummary) prep.
plotDist(hdedx_var, Form(
"fits_chi_%s_%s_%s", svar.data(), sfx.data(), particle.data()), nbins);
int m_nhitBins
bins for nhits
DBObjPtr< CDCDedxMeanPars > m_DBMeanPars
db object for dE/dx mean parameters
std::map< std::string, std::array< double, 3 > > m_bgpar
bg bins, min, max for different particles
void getExpRunInfo()
function to get exp/run information (payload object, plotting)
double m_cosMax
max range of cosine
std::map< std::string, double > m_cospar
cos bins for different particles
std::string m_bgcurve
string for mean parameter file names
bool m_ismakePlots
produce plots for monitoring
int m_iter
set number of iteration
std::string m_suffix
string suffix for object names
void prepareSample(const std::vector< std::string > &particles, const std::string &filename, const std::string &sfx, const bool makeIterationSummary)
function to prepare sample for bgcurve fitting, sigma vs ionzation fitting and monitoring plots
CDCDedxHadBGAlgorithm()
Constructor: Sets the description, the properties and the parameters of the algorithm.
double m_nhitMax
max range of nhits
DBObjPtr< CDCDedxSigmaPars > m_DBSigmaPars
db object for dE/dx resolution parameters
std::string m_bgsigma
string for sigma parameter file names
double m_cosMin
min range of cosine
double m_cut
cut to clean protons
virtual EResult calibrate() override
CDC dE/dx Beta Gamma curve and resolution algorithm.
double m_nhitMin
min range of nhits
std::map< std::string, std::array< double, 3 > > m_injpar
injection time bins, min, max for different particles
void createPayload()
function to store payloads after full calibration
void SigmaFits(const std::vector< std::string > &particles, const std::string &sfx, const std::string &svar, const bool makeIterationSummary)
function to do the sigma vs nhit or cos fits and store parameters
Class to hold the hadron saturation functions.
double I2D(double cosTheta, double I) const
hadron saturation parameterization part 2
double D2I(double cosTheta, double D) const
hadron saturation parameterization part 1
void setParameters()
set the parameters
dE/dx mean (curve versus beta-gamma) parameterization constants
Class to hold the prediction of mean as a function of beta-gamma (bg)
void setParameters(const std::string &infile)
set the parameters from file
double getMean(double bg)
Return the predicted mean value as a function of beta-gamma (bg)
void printParameters(const std::string &infile)
write the parameters in file
dE/dx sigma (versus beta-gamma) parameterization constants
Class to hold the prediction of resolution depending dE/dx, nhit, and cos(theta)
double ionzPrediction(double dedx)
Return sigma from the ionization parameterization.
double cosPrediction(double cos)
Return sigma from the cos parameterization.
void setParameters(const std::string &infile)
set the parameters from file
double nhitPrediction(double nhit)
Return sigma from the nhit parameterization.
void printParameters(const std::string &infile)
write the parameters in file
void saveCalibration(TClonesArray *data, const std::string &name)
Store DBArray payload with given name with default IOV.
static void updateDBObjPtrs(const unsigned int event, const int run, const int experiment)
Updates any DBObjPtrs by calling update(event) for DBStore.
void setDescription(const std::string &description)
Set algorithm description (in constructor)
const std::vector< Calibration::ExpRun > & getRunList() const
Get the list of runs for which calibration is called.
EResult
The result of calibration.
@ c_OK
Finished successfully =0 in Python.
@ c_NotEnoughData
Needs more data =2 in Python.
CalibrationAlgorithm(const std::string &collectorModuleName)
Constructor - sets the prefix for collected objects (won't be accesses until execute(....
Class to prepare sample for fitting in beta gamma bins.
static void fit(TH1F *&hist, const std::string &pdg, gstatus &status)
function to fit the histograms
void defineHisto(std::vector< TH1F * > &htemp, const std::string &svar, const std::string &stype, const std::string &pdg)
function to define histograms
static void plotDist(std::map< int, std::vector< TH1F * > > &hist, const std::string &suffix, int bins)
function to plot the map of histograms
static double getParticleMass(const std::string &particle)
function to get the particle mass
static void deleteHistos(std::vector< TH1F * > &htemp)
function to delete the histograms
void prepareSample(std::shared_ptr< TTree > hadron, TFile *&outfile, const std::string &suffix, const std::string &bgcurvefile, const std::string &bgsigmafile, const std::string &pdg, bool ismakePlots)
function to prepare sample for monitoring plots, bg curve fitting and sigma vs ionz fitting
Class to perform the fitting in beta gamma bins.
void plotBGMonitoring(const std::vector< std::string > &particles, const std::string &filename, const std::string &suffix)
plots mean and width after fitting
static void fitSigmaVsNHit(const std::vector< std::string > &particles, const std::string &filename, const std::string ¶msigma, const std::string &suffx, const bool makeIterationSummary)
fit sigma vs.
void fitSigmavsIonz(const std::vector< std::string > &particles, const std::string &filename, const std::string ¶mfile, const std::string &suffix, const bool makeIterationSummary)
fit sigma vs.
void fitBGCurve(const std::vector< std::string > &particles, const std::string &filename, const std::string ¶mfile, const std::string &suffx, const bool makeIterationSummary)
fit the beta-gamma curve
static void fitSigmaVsCos(const std::vector< std::string > &particles, const std::string &filename, const std::string ¶mfile, const std::string &suffx, const bool makeIterationSummary)
fit sigma vs.
std::shared_ptr< T > getObjectPtr(const std::string &name, const std::vector< Calibration::ExpRun > &requestedRuns)
Get calibration data object by name and list of runs, the Merge function will be called to generate t...
Abstract base class for different kinds of events.