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 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];
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];
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->GetEntry(index);
264 double bg = std::fabs(p) / mass;
266 if (std::fabs(p) > 8.0)
continue;
268 if (svar ==
"nhit") {
269 if (nhits < lower || nhits > upper)
continue;
270 }
else if (svar ==
"costh") {
271 if (costh > upper || costh < lower)
continue;
274 if (dedxnosat <= 0)
continue;
275 if (!(costh == costh))
continue;
277 if (particle ==
"proton") {
278 if ((dedxnosat - 0.45) * std::abs(p) * std::abs(p) <
m_cut)
continue;
281 if (particle ==
"electron" || particle ==
"muon") {
282 if (std::fabs(p) > 2.0)
continue;
285 double dedx_new = had.
D2I(costh, had.
I2D(costh, 1.0) * dedxnosat);
286 double dedx_cur = mgpar.
getMean(bg);
288 if (svar ==
"nhit") {
290 int nhitBin =
static_cast<int>((std::fabs(nhits) - lower) / nstep);
292 if (nhitBin < 0) nhitBin = 0;
293 else if (nhitBin >= nbins) nhitBin = nbins - 1;
296 hdedx_var[nhitBin]->Fill((dedx_new - dedx_cur) / res_cor);
298 sumvar[nhitBin] += nhits;
299 sumsize[nhitBin] += 1;
302 double denom = (upper - lower);
303 int cosBin =
static_cast<int>((costh - lower) / denom * nbins);
305 if (cosBin < 0) cosBin = 0;
306 else if (cosBin >= nbins) cosBin = nbins - 1;
309 if (res_cor != 0) hdedx_var[cosBin]->Fill((dedx_new - dedx_cur) / res_cor);
310 sumvar[cosBin] += costh;
311 sumsize[cosBin] += 1;
322 TTree* tTree =
new TTree(Form(
"%s_%s", particle.data(), svar.data()),
"chi m_means and m_errors");
323 double avg, mean, mean_err, sigma, sigma_err;
325 tTree->Branch(
"avg", &avg,
"avg/D");
326 tTree->Branch(
"chimean", &mean,
"chimean/D");
327 tTree->Branch(
"chimean_err", &mean_err,
"chimean_err/D");
328 tTree->Branch(
"chisigma", &sigma,
"chisigma/D");
329 tTree->Branch(
"chisigma_err", &sigma_err,
"chisigma_err/D");
331 double avg_sigma = 0.0;
332 std::vector<double> var(nbins), varres(nbins), varreserr(nbins), varmean(nbins), varmeanerr(nbins);
335 for (
int i = 0; i < nbins; ++i) {
337 varres[i] = 0.0, varmean[i] = 0.0;
338 varreserr[i] = 0.0, varmeanerr[i] = 0.0;
339 var[i] = (sumsize[i] > 0) ? (sumvar[i] / sumsize[i]) : 0.0;
342 if (hdedx_var[i]->Integral() > 100) {
344 prep.
fit(hdedx_var[i], particle.data(), stats);
346 varmean[i] = hdedx_var[i]->GetFunction(
"gaus")->GetParameter(1);
347 varmeanerr[i] = hdedx_var[i]->GetFunction(
"gaus")->GetParError(1);
348 varres[i] = hdedx_var[i]->GetFunction(
"gaus")->GetParameter(2);;
349 varreserr[i] = hdedx_var[i]->GetFunction(
"gaus")->GetParError(2);
352 avg_sigma += varres[i];
356 if (count_bins > 0) avg_sigma /= count_bins;
357 for (
int i = 0; i < nbins; ++i) {
360 sigma = varres[i] / avg_sigma;
361 sigma_err = varreserr[i] / avg_sigma;
364 mean_err = varmeanerr[i];
371 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
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.
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.
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
double getParticleMass(const std::string &particle)
function to get the particle mass
void deleteHistos(std::vector< TH1F * > &htemp)
function to delete 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.
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.