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];
44 auto tree = getObjectPtr<TTree>(Form(
"%s", p.data()));
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 std::string sfx = Form(
"%s_iter%d",
m_suffix.data(), iter);
71 filename = Form(
"widget_corrected_NewHSpars_1D_%s.root", sfx.data());
97 filename = Form(
"widget_corrected_NewHSpars_1D_%s_final.root",
m_suffix.data());
101 B2INFO(
"Saving calibration for: " <<
m_suffix <<
"");
113 if (cruns == 0)B2INFO(
"CDCDedxHadBGAlgorithm: start exp " << expRun.first <<
" and run " << expRun.second <<
"");
118 int estart = erStart.first;
119 int rstart = erStart.second;
123 m_suffix = Form(
"e%dr%d", estart, rstart);
124 B2INFO(
"tool exp = " << estart <<
", run = " << rstart <<
", m_suffix = " <<
m_suffix <<
"");
131 std::ifstream fins(
"parameters.sigmacos.fit"), fin(
"parameters.bgcurve.fit");
133 if (!fin.good()) B2FATAL(
"\t WARNING: CANNOT FIND parameters.bgcurve.fit!");
134 if (!fins.good()) B2FATAL(
"\tWARNING: CANNOT FIND parameters.sigmacos.fit!");
137 double meanpars, sigmapars;
138 std::vector<double> v_meanpars, v_sigmapars;
140 B2INFO(
"\t --> Curve parameters");
141 for (
int i = 0; i < 15; ++i) {
142 fin >> par >> meanpars;
143 v_meanpars.push_back(meanpars);
144 B2INFO(
"\t\t (" << i <<
")" << v_meanpars[i]);
149 B2INFO(
"\t --> Sigma parameters");
150 for (
int i = 0; i < 17; ++i) {
151 fins >> par >> sigmapars;
152 v_sigmapars.push_back(sigmapars);
153 B2INFO(
"\t\t (" << i <<
")" << v_sigmapars[i]);
157 B2INFO(
"dE/dx Calibration done for " << v_meanpars.size() <<
" CDC Beta Gamma curve");
162 B2INFO(
"dE/dx Calibration done for " << v_sigmapars.size() <<
" CDC Beta Gamma resolution");
170 TFile* outfile =
new TFile(filename.data(),
"RECREATE");
172 for (
int i = 0; i < int(particles.size()); ++i) {
174 std::string p = particles[i];
175 auto tree = getObjectPtr<TTree>(Form(
"%s", p.data()));
191 std::string filename = Form(
"widget_%s_1D_%s.root", svar.data(), sfx.data());
193 TFile* outfile =
new TFile(filename.data(),
"RECREATE");
196 for (
int ip = 0; ip < int(particles.size()); ++ip) {
198 std::string particle = particles[ip];
199 auto hadron = getObjectPtr<TTree>(Form(
"%s", particle.data()));
206 if (mass == 0.0) B2FATAL(
"Mass of particle " << particle.data() <<
" is zero");
216 hadron->SetBranchAddress(
"dedxnosat", &dedxnosat);
217 hadron->SetBranchAddress(
"p", &p);
218 hadron->SetBranchAddress(
"costh", &costh);
219 hadron->SetBranchAddress(
"timereso", &timereso);
220 hadron->SetBranchAddress(
"nhits", &nhits);
225 double nstep = (upper - lower + 1) / nbins;
227 if (svar ==
"costh") {
230 nstep = (upper - lower) / nbins;
234 std::vector<TH1F*> hdedx_var;
237 prep.
defineHisto(hdedx_var,
"chi", svar, particle.data());
240 std::vector<double> sumvar(nbins);
241 std::vector<int> sumsize(nbins);
242 for (
int i = 0; i < nbins; ++i) {
261 for (
unsigned int index = 0; index < hadron->GetEntries(); ++index) {
263 hadron->GetEvent(index);
267 if (fabs(p) > 8.0)
continue;
270 if (svar ==
"nhit") {
if (nhits < lower || nhits > upper)
continue;}
271 else if (svar ==
"costh") {
if (costh > upper || costh < lower)
continue;}
273 if (dedxnosat <= 0)
continue;
274 if (costh != costh)
continue;
276 if (particle ==
"proton") {
if ((dedxnosat - 0.45)*abs(p)*abs(p) <
m_cut)
continue;}
278 if (particle ==
"electron" || particle ==
"muon") {
if (fabs(p) > 2.0)
continue;}
280 double dedx_new = had.
D2I(costh, had.
I2D(costh, 1.0) * dedxnosat);
282 double dedx_cur = mgpar.
getMean(bg);
284 if (svar ==
"nhit") {
286 int nhitBin = (int)((fabs(nhits) - lower) / nstep);
287 if (res_cor != 0) hdedx_var[nhitBin]->Fill((dedx_new - dedx_cur) / res_cor);
288 sumvar[nhitBin] += nhits;
289 sumsize[nhitBin] += 1;
290 }
else if (svar ==
"costh") {
293 int cosBin = (int)((costh - lower) / (upper - lower) * nbins);
295 if (res_cor != 0) hdedx_var[cosBin]->Fill((dedx_new - dedx_cur) / res_cor);
297 sumvar[cosBin] += costh;
298 sumsize[cosBin] += 1;
309 TTree* tTree =
new TTree(Form(
"%s_%s", particle.data(), svar.data()),
"chi m_means and m_errors");
310 double avg, mean, mean_err, sigma, sigma_err;
312 tTree->Branch(
"avg", &avg,
"avg/D");
313 tTree->Branch(
"chimean", &mean,
"chimean/D");
314 tTree->Branch(
"chimean_err", &mean_err,
"chimean_err/D");
315 tTree->Branch(
"chisigma", &sigma,
"chisigma/D");
316 tTree->Branch(
"chisigma_err", &sigma_err,
"chisigma_err/D");
318 double avg_sigma = 0.0;
319 std::vector<double> var(nbins), varres(nbins), varreserr(nbins);
322 for (
int i = 0; i < nbins; ++i) {
326 var[i] = sumvar[i] / sumsize[i];
329 if (hdedx_var[i]->Integral() > 100) {
330 prep.
fit(hdedx_var[i], particle.data());
331 varres[i] = hdedx_var[i]->GetFunction(
"gaus")->GetParameter(2);;
332 varreserr[i] = hdedx_var[i]->GetFunction(
"gaus")->GetParError(2);
334 avg_sigma += varres[i];
338 if (count_bins > 0) avg_sigma = avg_sigma / count_bins;
339 for (
int i = 0; i < nbins; ++i) {
342 sigma = varres[i] = varres[i] / avg_sigma;
343 sigma_err = varreserr[i] = varreserr[i] / avg_sigma;
345 mean = hdedx_var[i]->GetFunction(
"gaus")->GetParameter(1);
346 mean_err = hdedx_var[i]->GetFunction(
"gaus")->GetParError(1);
353 prep.
plotDist(hdedx_var, Form(
"fits_chi_%s_%s_%s", svar.data(), sfx.data(), particle.data()), nbins);
int m_nhitBins
bins for nhits
void SigmaFits(std::vector< std::string > particles, const std::string &sfx, const std::string &svar)
function to do the sigma vs nhit or cos fits and store parameters
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
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 prepareSample(std::vector< std::string > particles, const std::string &filename, const std::string &sfx)
function to prepare sample for bgcurve fitting, sigma vs ionzation fitting and monitoring plots
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 printParameters(std::string infile)
write the parameters in file
double getMean(double bg)
Return the predicted mean value as a function of beta-gamma (bg)
void setParameters(std::string infile)
set the parameters from 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.
double nhitPrediction(double nhit)
Return sigma from the nhit parameterization.
void printParameters(std::string infile)
write the parameters in file
void setParameters(std::string infile)
set the parameters from file
Base class for calibration algorithms.
void saveCalibration(TClonesArray *data, const std::string &name)
Store DBArray payload with given name with default IOV.
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.
Class to prepare sample for fitting in beta gamma bins.
void defineHisto(std::vector< TH1F * > &htemp, const std::string &svar, const std::string &stype, const std::string &pdg)
function to define histograms
double getParticleMass(const std::string &particle)
function to get the particle mass
void deleteHistos(std::vector< TH1F * > &htemp)
function to delete the histograms
void fit(TH1F *&hist, const std::string &pdg)
function to fit the histograms
void plotDist(std::map< int, std::vector< TH1F * > > &hist, const std::string &suffix, int bins)
function to plot the map of 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 fitSigmaVsCos(std::vector< std::string > particles, const std::string &filename, const std::string ¶mfile, const std::string &suffx)
fit sigma vs.
void fitSigmaVsNHit(std::vector< std::string > particles, const std::string &filename, const std::string ¶msigma, const std::string &suffx)
fit sigma vs.
void fitBGCurve(std::vector< std::string > particles, const std::string &filename, const std::string ¶mfile, const std::string &suffx)
fit the beta-gamma curve
void plotBGMonitoring(std::vector< std::string > particles, const std::string &filename, const std::string &suffix)
plots mean and width after fitting
void fitSigmavsIonz(std::vector< std::string > particles, const std::string &filename, const std::string ¶mfile, const std::string &suffix)
fit sigma vs.
Abstract base class for different kinds of events.