9#include <cdc/calibration/CDCdEdx/CDCDedxBadWireAlgorithm.h>
11#include <cdc/geometry/CDCGeometryPar.h>
12#include <framework/utilities/MathHelpers.h>
55 B2FATAL(
"There is no valid payload for BadWire and/or Wirgain");
61 vector<int>* wire = 0;
62 ttree->SetBranchAddress(
"wire", &wire);
64 vector<double>* hitvar = 0;
65 if (
m_isADC) ttree->SetBranchAddress(
"adccorr", &hitvar);
66 else ttree->SetBranchAddress(
"dedxhit", &hitvar);
72 array<TH1D*, 2> hvarL;
73 string label[2] = {
"IL",
"OL"};
74 for (
int il = 0; il < 2; il++) {
76 hvarL[il]->SetTitle(Form(
"dist (%s) %s; %s; %s", label[il].data(),
m_suffix.data(),
m_varName.data(),
"entries"));
79 map<int, vector<double>> vhitvar;
83 for (
int i = 0; i < ttree->GetEntries(); ++i) {
85 for (
unsigned int ih = 0; ih < wire->size(); ++ih) {
86 int jwire = wire->at(ih);
87 double ivalue = hitvar->at(ih);
88 vhitvar[wire->at(ih)].push_back(ivalue);
91 else hvarL[1]->Fill(ivalue);
111 map<int, vector<double>> qapars;
112 vector<double> vdefectwires, vbadwires, vdeadwires;
114 for (
unsigned int jw = 0; jw <
c_nwireCDC; ++jw) {
115 int ncount = 0, tcount = 0;
117 for (
unsigned int jh = 0; jh < vhitvar[jw].size(); ++jh) {
118 double jvalue = vhitvar[jw][jh];
125 bool badwire =
false;
127 qapars[0].push_back(0);
128 qapars[1].push_back(0);
129 qapars[2].push_back(0);
132 nmean = nmean / ncount;
139 for (
unsigned int kh = 0; kh < vhitvar[jw].size(); ++kh) {
140 double kvalue = vhitvar[jw][kh];
144 nrms =
sqrt(nrms / ncount);
150 double badfrac = 0.0;
151 if (tcount > 0) badfrac = (1.0 * tcount) / (tcount + ncount);
154 qapars[0].push_back(nmean);
155 qapars[1].push_back(nrms);
156 qapars[2].push_back(badfrac);
160 vdefectwires.push_back(0.0);
161 if (ncount == 0) vdeadwires.push_back(jw);
162 else vbadwires.push_back(jw);
163 }
else vdefectwires.push_back(1.0);
182 B2INFO(
"dE/dx Badwire Calibration done: " << vdefectwires.size() <<
" wires");
197 if (cruns == 0) B2INFO(
"CDCDedxBadWires: start exp " << expRun.first <<
" and run " << expRun.second <<
"");
202 int estart = erStart.first;
203 int rstart = erStart.second;
206 int rend = erEnd.second;
213 else m_suffix = Form(
"e%d_r%dr%d", estart, rstart, rend);
219 map<
int, vector<double>>& vhitvar)
222 TList* bdlist =
new TList();
223 bdlist->SetName(
"badwires");
225 TList* goodlist =
new TList();
226 goodlist->SetName(
"goodwires");
228 TList* hflist =
new TList();
229 hflist->SetName(
"highfracwires");
231 for (
unsigned int jw = 0; jw <
c_nwireCDC; ++jw) {
234 hvar->SetUniqueID(jw);
237 hvarhf->SetUniqueID(jw);
238 hvarhf->SetTitle(Form(
"%s, wire = %d; %s; entries",
m_suffix.data(), jw,
m_varName.data()));
240 int ncount = 0, tcount = 0;
242 for (
unsigned int jh = 0; jh < vhitvar[jw].size(); ++jh) {
243 double jvalue = vhitvar[jw][jh];
249 if (jvalue <
m_varMax * 10.) hvarhf->Fill(jvalue / 10.);
253 double badfrac = 0.0;
254 if (tcount > 0) badfrac = (1.0 * tcount) / (tcount + ncount);
255 hvar->SetTitle(Form(
"%s, wire = %d; %s; %0.01f",
m_suffix.data(), jw,
m_varName.data(), badfrac * 100));
258 if (count(inwires.begin(), inwires.end(), jw)) isbad =
true;
262 hvar->SetLineWidth(2);
263 hvar->SetLineColor(kRed);
270 if (hvar->Integral() > 100) goodlist->Add(hvar);
286 string listname = list->GetName();
287 string sfx = Form(
"%s_%s", listname.data(),
m_suffix.data());
289 TCanvas ctmp(Form(
"cdcdedx_%s", sfx.data()),
"", 1200, 1200);
291 ctmp.SetBatch(kTRUE);
294 psname << Form(
"cdcdedx_bdcal_%s.pdf[", sfx.data());
295 ctmp.Print(psname.str().c_str());
297 psname << Form(
"cdcdedx_bdcal_%s.pdf", sfx.data());
299 for (
int ih = 0; ih < list->GetSize(); ih++) {
301 TH1D* hist = (TH1D*)list->At(ih);
302 int jw = hist->GetUniqueID();
304 double frac = stod(hist->GetYaxis()->GetTitle());
309 TPaveText* pinfo =
new TPaveText(0.40, 0.63, 0.89, 0.89,
"NBNDC");
311 pinfo->AddText(Form(
"#mu: %0.2f(%0.2f#pm%0.2f)", hist->GetMean(), amean,
m_meanThres * amean));
312 pinfo->AddText(Form(
"#sigma: %0.2f(%0.2f#pm%0.2f)", hist->GetRMS(), arms,
m_rmsThres * arms));
313 pinfo->AddText(Form(
"N: %0.00f", hist->Integral()));
314 pinfo->AddText(Form(
"hf: %0.00f%%(%0.00f%%)", frac,
m_fracThres * 100));
317 ctmp.cd(ih % 16 + 1);
318 hist->GetYaxis()->SetTitle(
"entries");
319 hist->SetFillColor(color);
324 if (listname ==
"badwires") {
325 TH1D* histhf = (TH1D*)hflist->At(ih);
326 if (hist->GetMaximum() < histhf->GetMaximum()) hist->SetMaximum(histhf->GetMaximum() * 1.05);
327 histhf->SetFillColor(kGray);
329 histhf->Draw(
"same");
332 if (((ih + 1) % 16 == 0) || ih == (list->GetSize() - 1)) {
333 ctmp.Print(psname.str().c_str());
339 psname << Form(
"cdcdedx_bdcal_%s.pdf]", sfx.data());
340 ctmp.Print(psname.str().c_str());
347 TCanvas cmap(Form(
"cmap_%s",
m_suffix.data()),
"", 800, 800);
348 cmap.SetTitle(
"CDC dE/dx bad wire status");
352 hxyAll->SetTitle(Form(
"wire status map (%s)",
m_suffix.data()));
360 hxyBad->Draw(
"same");
367 hxyDead->Draw(
"same");
370 int ndefect = nbad + ndead;
371 auto leg =
new TLegend(0.68, 0.80, 0.90, 0.92);
372 leg->SetBorderSize(0);
373 leg->SetLineWidth(3);
374 leg->SetHeader(Form(
"total defective: %d (~%0.02f%%)", ndefect, 100.*(ndefect) /
c_nwireCDC));
375 leg->AddEntry(hxyBad, Form(
"bad #rightarrow %d", nbad),
"p");
376 leg->AddEntry(hxyDead, Form(
"dead #rightarrow %d", ndead),
"p");
379 gStyle->SetLegendTextSize(0.025);
380 TPaveText* pt =
new TPaveText(-0.30, -1.47, -0.31, -1.30,
"br");
383 TText* text = pt->AddText(
"CDC-wire map: counter-clockwise and start from +x");
384 text->SetTextColor(kGray + 1);
387 cmap.SaveAs(Form(
"cdcdedx_bdcal_wiremap_%s.pdf",
m_suffix.data()));
398 B2INFO(
"Creating CDCGeometryPar object");
401 TH2F* temp =
new TH2F(Form(
"temp_%s_%s",
m_suffix.data(), suffix.data()),
"", 2400, -1.2, 1.2, 2400, -1.2, 1.2);
405 for (
unsigned int ilay = 0; ilay < c_maxNSenseLayers; ++ilay) {
406 for (
unsigned int iwire = 0; iwire < cdcgeo.
nWiresInLayer(ilay); ++iwire) {
408 double phi = 2.*TMath::Pi() * (float(iwire) / float(cdcgeo.
nWiresInLayer(ilay)));
409 double radius = cdcgeo.
senseWireR(ilay) / 100.;
410 double x = radius * cos(phi);
411 double y = radius * sin(phi);
412 if (suffix ==
"all") {
416 if (count(inwires.begin(), inwires.end(), jwire)) {
430 string qaname[3] = {
"mean",
"rms",
"high_fraction"};
438 for (
int iqa = 0; iqa < 3; iqa++) {
440 TH1D histqa(Form(
"%s_%s", qaname[iqa].data(),
m_suffix.data()),
"",
c_nwireCDC, -0.5, 14335.5);
442 for (
unsigned int jw = 0; jw <
c_nwireCDC; jw++) {
443 if (iqa == 2) histqa.SetBinContent(jw + 1, qapars[iqa][jw] * 100);
444 else histqa.SetBinContent(jw + 1, qapars[iqa][jw]);
447 TCanvas c_pars(Form(
"c_pars_%d", iqa),
"", 800, 600);
451 histqa.SetTitle(Form(
"%s vs wires (%s); wire ; %s", qaname[iqa].data(),
m_suffix.data(), qaname[iqa].data()));
455 TLine* lminIL =
new TLine(-0.5, lineminIL[iqa], 2239.5, lineminIL[iqa]);
456 lminIL->SetLineColor(kRed);
457 lminIL->Draw(
"same");
458 TLine* lmaxIL =
new TLine(-0.5, linemaxIL[iqa], 2239.5, linemaxIL[iqa]);
459 lmaxIL->SetLineColor(kRed);
460 lmaxIL->Draw(
"same");
461 TLine* lminOL =
new TLine(2239.5, lineminOL[iqa], 14335.5, lineminOL[iqa]);
462 lminOL->SetLineColor(kRed);
463 lminOL->Draw(
"same");
464 TLine* lmaxOL =
new TLine(2239.5, linemaxOL[iqa], 14335.5, linemaxOL[iqa]);
465 lmaxOL->SetLineColor(kRed);
466 lmaxOL->Draw(
"same");
468 c_pars.Print(Form(
"cdcdedx_bdcal_%s_%s.root", qaname[iqa].data(),
m_suffix.data()));
469 c_pars.Print(Form(
"cdcdedx_bdcal_%s_%s.pdf", qaname[iqa].data(),
m_suffix.data()));
480 TCanvas cstats(
"cstats",
"cstats", 1000, 500);
481 cstats.SetBatch(kTRUE);
487 hestats->SetName(Form(
"htstats_%s",
m_suffix.data()));
488 hestats->SetStats(0);
489 hestats->DrawCopy(
"");
495 htstats->SetName(Form(
"htstats_%s",
m_suffix.data()));
496 htstats->SetStats(0);
497 htstats->DrawCopy(
"");
500 cstats.Print(Form(
"cdcdedx_bdcal_qastats_%s.pdf",
m_suffix.data()));
void setHistCosmetics(TH2F *hist, Color_t color)
function to change histogram styles
void plotBadWireMap(const std::vector< double > &vbadwires, const std::vector< double > &vdeadwires)
function to plot wire status map (all, bad and dead)
double m_varMax
max range for input variable
double m_rmsThres
rms Threshold accepted for good wire
double m_varMin
min range for input variable
int m_slWireBoundary
Boundary between inner layers: SL0 (<40), SL0+SL1 (>=40)
double m_amean_IL
average mean of dedx for inner wires
std::string m_varName
std::string to set var name (adc or dedx)
void getExpRunInfo()
function to get extract calibration run/exp
unsigned int c_nwireCDC
number of wires in CDC
bool m_isMakePlots
produce plots for status
DBObjPtr< CDCDedxBadWires > m_DBBadWires
Badwire DB object.
std::string m_suffix
suffix std::string for naming plots
void setTextCosmetics(TPaveText *pt, double size)
function to change text styles
CDCDedxBadWireAlgorithm()
Constructor: Sets the description, the properties and the parameters of the algorithm.
DBObjPtr< CDCGeometry > m_cdcGeo
Geometry of CDC.
int m_exp
exp no to set SL boundaries
double m_meanThres
mean Threshold accepted for good wire
DBObjPtr< CDCDedxWireGain > m_DBWireGains
Wiregain DB object.
double m_fracThres
high-frac Threshold accepted for good wire
int m_varBins
number of bins for input variable
void printCanvas(TList *list, TList *hflist, Color_t color)
function to print canvas
void plotQaPars(std::map< int, std::vector< double > > &qapars)
function to plot the QA (decision) parameters
void plotEventStats()
function to draw the stats
virtual EResult calibrate() override
cdcdedx badwire algorithm
void plotWireDist(const std::vector< double > &inwires, std::map< int, std::vector< double > > &vhitvar)
function to draw per wire plots
double m_arms_IL
average rms of dedx for inner wires
double m_arms_OL
average rms of dedx for outer wires
double m_amean_OL
average mean of dedx for outer wires
bool m_isADC
Use adc if(true) else dedx for calibration.
TH2F * getHistoPattern(const std::vector< double > &inwires, const std::string &suffix, int &total)
function to get wire map with input file (all, bad and dead)
dE/dx wire gain calibration constants
The Class for CDC Geometry Parameters.
unsigned nWiresInLayer(int layerId) const
Returns wire numbers in a layer.
static CDCGeometryPar & Instance(const CDCGeometry *=nullptr)
Static method to get a reference to the CDCGeometryPar instance.
double senseWireR(int layerId) const
Returns radius of sense wire in each layer.
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(....
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...
constexpr T square(const T &x)
Calculate the square of the input.
double sqrt(double a)
sqrt for double
Abstract base class for different kinds of events.