10 #include <dqm/analysis/modules/DQMHistAnalysisPXDTrackCharge.h>
15 #include <vxd/geometry/GeoCache.h>
18 #include <RooDataHist.h>
19 #include <RooAbsPdf.h>
41 addParam(
"histogramDirectoryName", m_histogramDirectoryName,
"Name of Histogram dir", std::string(
"PXDER"));
42 addParam(
"RangeLow", m_rangeLow,
"Lower border for fit", 20.);
43 addParam(
"RangeHigh", m_rangeHigh,
"High border for fit", 80.);
46 addParam(
"PVPrefix", m_pvPrefix,
"PV Prefix", std::string(
"DQM:PXD:TrackCharge:"));
47 addParam(
"useEpics", m_useEpics,
"useEpics",
true);
48 addParam(
"RefHistoFile", m_refFileName,
"Reference histrogram file name", std::string(
"refHisto.root"));
49 addParam(
"ColorAlert", m_color,
"Whether to show the color alert",
true);
51 B2DEBUG(99,
"DQMHistAnalysisPXDTrackCharge: Constructor done.");
54 DQMHistAnalysisPXDTrackChargeModule::~DQMHistAnalysisPXDTrackChargeModule()
58 if (ca_current_context()) ca_context_destroy();
63 void DQMHistAnalysisPXDTrackChargeModule::initialize()
65 B2DEBUG(99,
"DQMHistAnalysisPXDTrackCharge: initialized.");
68 if (m_refFileName !=
"") {
69 m_refFile =
new TFile(m_refFileName.data());
72 m_monObj = getMonitoringObject(
"pxd");
75 m_rfws =
new RooWorkspace(
"w");
76 m_rfws->factory(
"Landau::landau(x[0,100],ml[20,10,50],sl[5,1,30])");
77 m_rfws->factory(
"Gaussian::gauss(x,mg[0],sg[2,0.1,10])");
78 m_rfws->factory(
"FCONV::lxg(x,landau,gauss)");
80 m_x = m_rfws->var(
"x");
81 m_x->setRange(
"signal", m_rangeLow, m_rangeHigh);
85 for (
VxdID& aVxdID : sensors) {
87 if (info.getType() != VXD::SensorInfoBase::PXD)
continue;
88 m_PXDModules.push_back(aVxdID);
90 std::string name =
"PXD_Track_Cluster_Charge_" + (std::string)aVxdID;
91 std::replace(name.begin(), name.end(),
'.',
'_');
92 m_cChargeMod[aVxdID] =
new TCanvas((m_histogramDirectoryName +
"/c_Fit_" + name).data());
93 if (aVxdID ==
VxdID(
"1.5.1")) {
94 for (
int s = 0; s < 6; s++) {
95 for (
int d = 0; d < 4; d++) {
96 m_cChargeModASIC[aVxdID][s][d] =
new TCanvas((m_histogramDirectoryName +
"/c_Fit_" + name + Form(
"_s%d_d%d", s + 1, d + 1)).data());
99 m_hChargeModASIC2d[aVxdID] =
new TH2F((
"hPXD_TCChargeMPV_" + name).data(),
100 (
"PXD TCCharge MPV " + name +
";Switcher;DCD;MPV").data(),
101 6, 0.5, 6.5, 4, 0.5, 4.5);
102 m_cChargeModASIC2d[aVxdID] =
new TCanvas((m_histogramDirectoryName +
"/c_TCCharge_MPV_" + name).data());
105 std::sort(m_PXDModules.begin(), m_PXDModules.end());
109 m_cTrackedClusters =
new TCanvas((m_histogramDirectoryName +
"/c_TrackedClusters").data());
110 m_hTrackedClusters =
new TH1F(
"hPXDTrackedClusters",
"PXD Tracked Clusters/Event;Module", 40, 0, 40);
111 m_hTrackedClusters->Draw();
112 auto ax = m_hTrackedClusters->GetXaxis();
114 ax->Set(m_PXDModules.size(), 0, m_PXDModules.size());
115 for (
unsigned int i = 0; i < m_PXDModules.size(); i++) {
116 TString ModuleName = (std::string)m_PXDModules[i];
117 ax->SetBinLabel(i + 1, ModuleName);
119 }
else B2ERROR(
"no axis");
121 m_cCharge =
new TCanvas((m_histogramDirectoryName +
"/c_TrackCharge").data());
122 m_monObj->addCanvas(m_cCharge);
124 m_gCharge =
new TGraphErrors();
125 m_gCharge->SetName(
"Track_Cluster_Charge");
126 m_gCharge->SetTitle(
"Track Cluster Charge");
129 m_line_up =
new TLine(0, 10, m_PXDModules.size(), 10);
130 m_line_mean =
new TLine(0, 16, m_PXDModules.size(), 16);
131 m_line_low =
new TLine(0, 3, m_PXDModules.size(), 3);
132 m_line_up->SetHorizontal(
true);
133 m_line_up->SetLineColor(kMagenta);
134 m_line_up->SetLineWidth(3);
135 m_line_up->SetLineStyle(7);
136 m_line_mean->SetHorizontal(
true);
137 m_line_mean->SetLineColor(kGreen);
138 m_line_mean->SetLineWidth(3);
139 m_line_mean->SetLineStyle(4);
140 m_line_low->SetHorizontal(
true);
141 m_line_low->SetLineColor(kMagenta);
142 m_line_low->SetLineWidth(3);
143 m_line_low->SetLineStyle(7);
145 m_fMean =
new TF1(
"f_Mean",
"pol0", 0, m_PXDModules.size());
146 m_fMean->SetParameter(0, 50);
147 m_fMean->SetLineColor(kYellow);
148 m_fMean->SetLineWidth(3);
149 m_fMean->SetLineStyle(7);
150 m_fMean->SetNpx(m_PXDModules.size());
151 m_fMean->SetNumberFitPoints(m_PXDModules.size());
155 if (!ca_current_context()) SEVCHK(ca_context_create(ca_disable_preemptive_callback),
"ca_context_create");
157 SEVCHK(ca_create_channel((m_pvPrefix +
"Mean").data(), NULL, NULL, 10, &mychid[0]),
"ca_create_channel failure");
158 SEVCHK(ca_create_channel((m_pvPrefix +
"Diff").data(), NULL, NULL, 10, &mychid[1]),
"ca_create_channel failure");
159 SEVCHK(ca_create_channel((m_pvPrefix +
"Status").data(), NULL, NULL, 10, &mychid[2]),
"ca_create_channel failure");
160 SEVCHK(ca_pend_io(5.0),
"ca_pend_io failure");
166 void DQMHistAnalysisPXDTrackChargeModule::beginRun()
168 B2DEBUG(99,
"DQMHistAnalysisPXDTrackCharge: beginRun called.");
173 void DQMHistAnalysisPXDTrackChargeModule::event()
175 if (!m_cCharge)
return;
177 gStyle->SetOptStat(0);
178 gStyle->SetStatStyle(1);
179 gStyle->SetOptDate(22);
185 auto model = m_rfws->pdf(
"lxg");
187 auto ml = m_rfws->var(
"ml");
193 m_cTrackedClusters->Clear();
194 m_cTrackedClusters->cd();
195 m_hTrackedClusters->Reset();
197 std::string name =
"Tracked_Clusters";
198 TH1* hh2 = findHist(m_histogramDirectoryName,
"PXD_Tracked_Clusters");
200 auto scale = hh2->GetBinContent(0);
203 for (
int i = 0; i < 64; i++) {
204 auto layer = (((i >> 5) & 0x1) + 1);
205 auto ladder = ((i >> 1) & 0xF);
206 auto sensor = ((i & 0x1) + 1);
211 m_hTrackedClusters->SetBinContent(j, hh2->GetBinContent(i + 1) / scale);
216 m_hTrackedClusters->SetName(name.data());
217 m_hTrackedClusters->SetTitle(
"Tracked Clusters/Event");
218 m_hTrackedClusters->SetFillColor(kWhite);
219 m_hTrackedClusters->SetStats(kFALSE);
220 m_hTrackedClusters->SetLineStyle(1);
221 m_hTrackedClusters->SetLineColor(kBlue);
222 m_hTrackedClusters->Draw(
"hist");
224 TH1* href2 = GetHisto(
"ref/" + m_histogramDirectoryName +
"/" + name);
227 href2->SetLineStyle(3);
228 href2->SetLineColor(kBlack);
229 href2->Draw(
"same,hist");
232 auto tt =
new TLatex(5.5, 0,
" 1.3.2 Module is excluded, please ignore");
233 tt->SetTextAngle(90);
234 tt->SetTextAlign(12);
241 for (
unsigned int i = 0; i < m_PXDModules.size(); i++) {
242 TCanvas* canvas = m_cChargeMod[m_PXDModules[i]];
243 if (canvas ==
nullptr)
continue;
245 std::string name =
"PXD_Track_Cluster_Charge_" + (std::string)m_PXDModules[i];
246 std::replace(name.begin(), name.end(),
'.',
'_');
251 TH1* hh1 = findHist(m_histogramDirectoryName, name);
254 if (hh1->GetEntries() > 50) {
256 auto hdata =
new RooDataHist(hh1->GetName(), hh1->GetTitle(), *m_x, (
const TH1*) hh1);
257 auto plot = m_x->frame(RooFit::Title(hh1->GetTitle()));
258 model->fitTo(*hdata, RooFit::Range(
"signal"));
260 model->paramOn(plot, RooFit::Format(
"NELU", RooFit::AutoPrecision(2)), RooFit::Layout(0.6, 0.9, 0.9));
261 hdata->plotOn(plot, RooFit::LineColor(kBlue));
262 model->plotOn(plot, RooFit::LineColor(kRed), RooFit::Range(
"signal"), RooFit::NormRange(
"signal"));
274 int p = m_gCharge->GetN();
275 m_gCharge->SetPoint(p, i + 0.49, ml->getValV());
276 m_gCharge->SetPointError(p, 0.1, ml->getError());
277 m_monObj->setVariable((
"trackcharge_" + (std::string)m_PXDModules[i]).c_str(), ml->getValV(), ml->getError());
280 TH1* hist2 = GetHisto(
"ref/" + m_histogramDirectoryName +
"/" + name);
284 hist2->SetLineStyle(3);
285 hist2->SetLineColor(kBlack);
300 TH1* h = (TH1*)hist2->Clone();
301 if (abs(hist2->GetEntries()) > 0) h->Scale(hh1->GetEntries() / hist2->GetEntries());
304 h->Draw(
"same,hist");
308 canvas->Pad()->SetFrameFillColor(10);
310 if (hh1->GetEntries() < 100) {
312 canvas->Pad()->SetFillColor(kGray);
314 canvas->Pad()->SetFillColor(kGreen);
317 canvas->Pad()->SetFillColor(kWhite);
324 if (hh1->GetEntries() >= 1000) enough =
true;
329 for (
unsigned int i = 0; i < m_PXDModules.size(); i++) {
331 VxdID& aVxdID = m_PXDModules[i];
333 if (m_hChargeModASIC2d[aVxdID]) m_hChargeModASIC2d[aVxdID]->Reset();
334 if (m_cChargeModASIC2d[aVxdID]) m_cChargeModASIC2d[aVxdID]->Clear();
336 for (
int s = 1; s <= 6; s++) {
337 for (
int d = 1; d <= 4; d++) {
338 std::string name =
"PXD_Track_Cluster_Charge_" + (std::string)m_PXDModules[i] + Form(
"_sw%d_dcd%d", s, d);
339 std::replace(name.begin(), name.end(),
'.',
'_');
341 if (m_cChargeModASIC[aVxdID][s - 1][d - 1]) {
342 m_cChargeModASIC[aVxdID][s - 1][d - 1]->Clear();
343 m_cChargeModASIC[aVxdID][s - 1][d - 1]->cd();
346 TH1* hh1 = findHist(m_histogramDirectoryName, name);
349 if (hh1->GetEntries() > 50) {
350 auto hdata =
new RooDataHist(hh1->GetName(), hh1->GetTitle(), *m_x, (
const TH1*) hh1);
351 auto plot = m_x->frame(RooFit::Title(hh1->GetTitle()));
352 model->fitTo(*hdata, RooFit::Range(
"signal"));
354 if (m_cChargeModASIC[aVxdID][s - 1][d - 1]) {
355 model->paramOn(plot, RooFit::Format(
"NELU", RooFit::AutoPrecision(2)), RooFit::Layout(0.6, 0.9, 0.9));
356 hdata->plotOn(plot, RooFit::LineColor(kBlue));
357 model->plotOn(plot, RooFit::LineColor(kRed), RooFit::Range(
"signal"), RooFit::NormRange(
"signal"));
364 if (m_hChargeModASIC2d[aVxdID]) {
365 if (mpv > 0.0) m_hChargeModASIC2d[aVxdID]->Fill(s, d, mpv);
372 if (m_hChargeModASIC2d[aVxdID] && m_cChargeModASIC2d[aVxdID]) {
373 m_cChargeModASIC2d[aVxdID]->cd();
374 m_hChargeModASIC2d[aVxdID]->Draw(
"colz");
380 m_gCharge->SetMinimum(0);
381 m_gCharge->SetMaximum(70);
382 auto ax = m_gCharge->GetXaxis();
384 ax->Set(m_PXDModules.size(), 0, m_PXDModules.size());
385 for (
unsigned int i = 0; i < m_PXDModules.size(); i++) {
386 TString ModuleName = (std::string)m_PXDModules[i];
387 ax->SetBinLabel(i + 1, ModuleName);
389 }
else B2ERROR(
"no axis");
391 m_gCharge->SetLineColor(4);
392 m_gCharge->SetLineWidth(2);
393 m_gCharge->SetMarkerStyle(8);
394 m_gCharge->Draw(
"AP");
396 auto tt =
new TLatex(5.5, 0,
" 1.3.2 Module is excluded, please ignore");
397 tt->SetTextAngle(90);
398 tt->SetTextAlign(12);
402 m_cCharge->Modified();
407 if (m_gCharge && enough) {
409 m_gCharge->Fit(m_fMean,
"R");
410 double mean = m_gCharge->GetMean(2);
411 double maxi = mean + 15;
412 double mini = mean - 15;
413 m_line_up->SetY1(maxi);
414 m_line_up->SetY2(maxi);
415 m_line_mean->SetY1(mean);
416 m_line_mean->SetY2(mean);
417 m_line_low->SetY1(mini);
418 m_line_low->SetY2(mini);
421 diff = m_gCharge->GetRMS(2);
427 m_monObj->setVariable(
"trackcharge", mean, diff);
432 SEVCHK(ca_put(DBR_DOUBLE, mychid[0], (
void*)&data),
"ca_set failure");
433 SEVCHK(ca_put(DBR_DOUBLE, mychid[1], (
void*)&diff),
"ca_set failure");
441 m_cCharge->Pad()->SetFillColor(kGray);
445 if (fabs(data - 30.) > 20. || diff > 12) {
446 m_cCharge->Pad()->SetFillColor(kRed);
448 }
else if (fabs(data - 30) > 15. || diff > 8) {
449 m_cCharge->Pad()->SetFillColor(kYellow);
452 m_cCharge->Pad()->SetFillColor(kGreen);
463 SEVCHK(ca_put(DBR_INT, mychid[2], (
void*)&status),
"ca_set failure");
464 SEVCHK(ca_pend_io(5.0),
"ca_pend_io failure");
470 void DQMHistAnalysisPXDTrackChargeModule::endRun()
472 B2DEBUG(99,
"DQMHistAnalysisPXDTrackCharge : endRun called");
476 void DQMHistAnalysisPXDTrackChargeModule::terminate()
478 B2DEBUG(99,
"DQMHistAnalysisPXDTrackCharge: terminate called");
482 for (
auto m : mychid) SEVCHK(ca_clear_channel(m),
"ca_clear_channel failure");
483 SEVCHK(ca_pend_io(5.0),
"ca_pend_io failure");
486 if (m_refFile)
delete m_refFile;
490 TH1* DQMHistAnalysisPXDTrackChargeModule::GetHisto(TString histoname)
497 B2DEBUG(20,
"findHisto failed " << histoname <<
" not in memfile");
502 if (m_refFile && m_refFile->IsOpen()) {
503 TDirectory* d = m_refFile;
504 TString myl = histoname;
508 while (myl.Tokenize(tok, from,
"/")) {
512 if (myl.Tokenize(dummy, f,
"/")) {
513 auto e = d->GetDirectory(tok);
515 B2DEBUG(20,
"Cd Dir " << tok <<
" from " << d->GetPath());
518 B2DEBUG(20,
"cd failed " << tok <<
" from " << d->GetPath());
524 TObject* obj = d->FindObject(tok);
526 if (obj->IsA()->InheritsFrom(
"TH1")) {
527 B2DEBUG(20,
"Histo " << histoname <<
" found in ref file");
530 B2DEBUG(20,
"Histo " << histoname <<
" found in ref file but wrong type");
534 TIter next(d->GetListOfKeys());
536 while ((key = (TKey*)next())) {
537 TObject* obj2 = key->ReadObj() ;
538 if (obj2->InheritsFrom(
"TH1")) {
539 if (obj2->GetName() == tok) {
541 B2DEBUG(20,
"Histo " << histoname <<
" found as key -> readobj");
546 if (hh1 == NULL) B2DEBUG(20,
"Histo " << histoname <<
" NOT found in ref file " << tok);
551 B2DEBUG(20,
"Histo " << histoname <<
" not in memfile or ref file");
554 TDirectory* d = gROOT;
555 TString myl = histoname;
558 while (myl.Tokenize(tok, from,
"/")) {
562 if (myl.Tokenize(dummy, f,
"/")) {
563 auto e = d->GetDirectory(tok);
565 B2DEBUG(20,
"Cd Dir " << tok);
567 }
else B2DEBUG(20,
"cd failed " << tok);
573 TObject* obj = d->FindObject(tok);
575 if (obj->IsA()->InheritsFrom(
"TH1")) {
576 B2DEBUG(20,
"Histo " << histoname <<
" found in mem");
580 B2DEBUG(20,
"Histo " << histoname <<
" NOT found in mem");
586 B2DEBUG(20,
"Histo " << histoname <<
" not found");