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((
"PXD_TCChargeMPV_" + name).data(), (
"PXD TCCharge MPV " + name +
";Switcher;DCD;MPV").data(),
100 6, 0.5, 6.5, 4, 0.5, 4.5);
101 m_cChargeModASIC2d[aVxdID] =
new TCanvas((m_histogramDirectoryName +
"/c_TCCharge_MPV_" + name).data());
104 std::sort(m_PXDModules.begin(), m_PXDModules.end());
108 m_cTrackedClusters =
new TCanvas((m_histogramDirectoryName +
"/c_TrackedClusters").data());
109 m_hTrackedClusters =
new TH1F(
"Tracked_Clusters",
"Tracked Clusters/Event;Module", 40, 0, 40);
110 m_hTrackedClusters->Draw();
111 auto ax = m_hTrackedClusters->GetXaxis();
113 ax->Set(m_PXDModules.size(), 0, m_PXDModules.size());
114 for (
unsigned int i = 0; i < m_PXDModules.size(); i++) {
115 TString ModuleName = (std::string)m_PXDModules[i];
116 ax->SetBinLabel(i + 1, ModuleName);
118 }
else B2ERROR(
"no axis");
120 m_cCharge =
new TCanvas((m_histogramDirectoryName +
"/c_TrackCharge").data());
121 m_monObj->addCanvas(m_cCharge);
123 m_gCharge =
new TGraphErrors();
124 m_gCharge->SetName(
"Track_Cluster_Charge");
125 m_gCharge->SetTitle(
"Track Cluster Charge");
128 m_line_up =
new TLine(0, 10, m_PXDModules.size(), 10);
129 m_line_mean =
new TLine(0, 16, m_PXDModules.size(), 16);
130 m_line_low =
new TLine(0, 3, m_PXDModules.size(), 3);
131 m_line_up->SetHorizontal(
true);
132 m_line_up->SetLineColor(kMagenta);
133 m_line_up->SetLineWidth(3);
134 m_line_up->SetLineStyle(7);
135 m_line_mean->SetHorizontal(
true);
136 m_line_mean->SetLineColor(kGreen);
137 m_line_mean->SetLineWidth(3);
138 m_line_mean->SetLineStyle(4);
139 m_line_low->SetHorizontal(
true);
140 m_line_low->SetLineColor(kMagenta);
141 m_line_low->SetLineWidth(3);
142 m_line_low->SetLineStyle(7);
144 m_fMean =
new TF1(
"f_Mean",
"pol0", 0, m_PXDModules.size());
145 m_fMean->SetParameter(0, 50);
146 m_fMean->SetLineColor(kYellow);
147 m_fMean->SetLineWidth(3);
148 m_fMean->SetLineStyle(7);
149 m_fMean->SetNpx(m_PXDModules.size());
150 m_fMean->SetNumberFitPoints(m_PXDModules.size());
154 if (!ca_current_context()) SEVCHK(ca_context_create(ca_disable_preemptive_callback),
"ca_context_create");
156 SEVCHK(ca_create_channel((m_pvPrefix +
"Mean").data(), NULL, NULL, 10, &mychid[0]),
"ca_create_channel failure");
157 SEVCHK(ca_create_channel((m_pvPrefix +
"Diff").data(), NULL, NULL, 10, &mychid[1]),
"ca_create_channel failure");
158 SEVCHK(ca_create_channel((m_pvPrefix +
"Status").data(), NULL, NULL, 10, &mychid[2]),
"ca_create_channel failure");
159 SEVCHK(ca_pend_io(5.0),
"ca_pend_io failure");
165 void DQMHistAnalysisPXDTrackChargeModule::beginRun()
167 B2DEBUG(99,
"DQMHistAnalysisPXDTrackCharge: beginRun called.");
172 void DQMHistAnalysisPXDTrackChargeModule::event()
174 if (!m_cCharge)
return;
176 gStyle->SetOptStat(0);
177 gStyle->SetStatStyle(1);
178 gStyle->SetOptDate(22);
184 auto model = m_rfws->pdf(
"lxg");
186 auto ml = m_rfws->var(
"ml");
192 m_cTrackedClusters->Clear();
193 m_cTrackedClusters->cd();
194 m_hTrackedClusters->Reset();
196 std::string name =
"Tracked_Clusters";
197 TH1* hh2 = findHist(m_histogramDirectoryName,
"PXD_Tracked_Clusters");
199 auto scale = hh2->GetBinContent(0);
202 for (
int i = 0; i < 64; i++) {
203 auto layer = (((i >> 5) & 0x1) + 1);
204 auto ladder = ((i >> 1) & 0xF);
205 auto sensor = ((i & 0x1) + 1);
210 m_hTrackedClusters->SetBinContent(j, hh2->GetBinContent(i + 1) / scale);
215 m_hTrackedClusters->SetName(name.data());
216 m_hTrackedClusters->SetTitle(
"Tracked Clusters/Event");
217 m_hTrackedClusters->SetFillColor(kWhite);
218 m_hTrackedClusters->SetStats(kFALSE);
219 m_hTrackedClusters->SetLineStyle(1);
220 m_hTrackedClusters->SetLineColor(kBlue);
221 m_hTrackedClusters->Draw(
"hist");
223 TH1* href2 = GetHisto(
"ref/" + m_histogramDirectoryName +
"/" + name);
226 href2->SetLineStyle(3);
227 href2->SetLineColor(kBlack);
228 href2->Draw(
"same,hist");
231 auto tt =
new TLatex(5.5, 0.1,
"1.3.2 Module is excluded, please ignore");
232 tt->SetTextAngle(90);
233 tt->SetTextAlign(12);
240 for (
unsigned int i = 0; i < m_PXDModules.size(); i++) {
241 TCanvas* canvas = m_cChargeMod[m_PXDModules[i]];
242 if (canvas ==
nullptr)
continue;
244 std::string name =
"PXD_Track_Cluster_Charge_" + (std::string)m_PXDModules[i];
245 std::replace(name.begin(), name.end(),
'.',
'_');
250 TH1* hh1 = findHist(m_histogramDirectoryName, name);
253 if (hh1->GetEntries() > 50) {
255 auto hdata =
new RooDataHist(hh1->GetName(), hh1->GetTitle(), *m_x, (
const TH1*) hh1);
256 auto plot = m_x->frame(RooFit::Title(hh1->GetTitle()));
257 model->fitTo(*hdata, RooFit::Range(
"signal"));
259 model->paramOn(plot, RooFit::Format(
"NELU", RooFit::AutoPrecision(2)), RooFit::Layout(0.6, 0.9, 0.9));
260 hdata->plotOn(plot, RooFit::LineColor(kBlue));
261 model->plotOn(plot, RooFit::LineColor(kRed), RooFit::Range(
"signal"), RooFit::NormRange(
"signal"));
273 int p = m_gCharge->GetN();
274 m_gCharge->SetPoint(p, i + 0.49, ml->getValV());
275 m_gCharge->SetPointError(p, 0.1, ml->getError());
276 m_monObj->setVariable((
"trackcharge_" + (std::string)m_PXDModules[i]).c_str(), ml->getValV(), ml->getError());
279 TH1* hist2 = GetHisto(
"ref/" + m_histogramDirectoryName +
"/" + name);
283 hist2->SetLineStyle(3);
284 hist2->SetLineColor(kBlack);
299 TH1* h = (TH1*)hist2->Clone();
300 if (abs(hist2->GetEntries()) > 0) h->Scale(hh1->GetEntries() / hist2->GetEntries());
303 h->Draw(
"same,hist");
307 canvas->Pad()->SetFrameFillColor(10);
309 if (hh1->GetEntries() < 100) {
311 canvas->Pad()->SetFillColor(kGray);
313 canvas->Pad()->SetFillColor(kGreen);
316 canvas->Pad()->SetFillColor(kWhite);
323 if (hh1->GetEntries() >= 1000) enough =
true;
328 for (
unsigned int i = 0; i < m_PXDModules.size(); i++) {
330 VxdID& aVxdID = m_PXDModules[i];
332 if (m_hChargeModASIC2d[aVxdID]) m_hChargeModASIC2d[aVxdID]->Reset();
333 if (m_cChargeModASIC2d[aVxdID]) m_cChargeModASIC2d[aVxdID]->Clear();
335 for (
int s = 1; s <= 6; s++) {
336 for (
int d = 1; d <= 4; d++) {
337 std::string name =
"PXD_Track_Cluster_Charge_" + (std::string)m_PXDModules[i] + Form(
"_sw%d_dcd%d", s, d);
338 std::replace(name.begin(), name.end(),
'.',
'_');
340 if (m_cChargeModASIC[aVxdID][s - 1][d - 1]) {
341 m_cChargeModASIC[aVxdID][s - 1][d - 1]->Clear();
342 m_cChargeModASIC[aVxdID][s - 1][d - 1]->cd();
345 TH1* hh1 = findHist(m_histogramDirectoryName, name);
348 if (hh1->GetEntries() > 50) {
349 auto hdata =
new RooDataHist(hh1->GetName(), hh1->GetTitle(), *m_x, (
const TH1*) hh1);
350 auto plot = m_x->frame(RooFit::Title(hh1->GetTitle()));
351 model->fitTo(*hdata, RooFit::Range(
"signal"));
353 if (m_cChargeModASIC[aVxdID][s - 1][d - 1]) {
354 model->paramOn(plot, RooFit::Format(
"NELU", RooFit::AutoPrecision(2)), RooFit::Layout(0.6, 0.9, 0.9));
355 hdata->plotOn(plot, RooFit::LineColor(kBlue));
356 model->plotOn(plot, RooFit::LineColor(kRed), RooFit::Range(
"signal"), RooFit::NormRange(
"signal"));
363 if (m_hChargeModASIC2d[aVxdID]) {
364 if (mpv > 0.0) m_hChargeModASIC2d[aVxdID]->Fill(s, d, mpv);
371 if (m_hChargeModASIC2d[aVxdID] && m_cChargeModASIC2d[aVxdID]) {
372 m_cChargeModASIC2d[aVxdID]->cd();
373 m_hChargeModASIC2d[aVxdID]->Draw(
"colz");
379 m_gCharge->SetMinimum(0);
380 m_gCharge->SetMaximum(70);
381 auto ax = m_gCharge->GetXaxis();
383 ax->Set(m_PXDModules.size(), 0, m_PXDModules.size());
384 for (
unsigned int i = 0; i < m_PXDModules.size(); i++) {
385 TString ModuleName = (std::string)m_PXDModules[i];
386 ax->SetBinLabel(i + 1, ModuleName);
388 }
else B2ERROR(
"no axis");
390 m_gCharge->SetLineColor(4);
391 m_gCharge->SetLineWidth(2);
392 m_gCharge->SetMarkerStyle(8);
393 m_gCharge->Draw(
"AP");
395 auto tt =
new TLatex(5.5, 0.1,
"1.3.2 Module is excluded, please ignore");
396 tt->SetTextAngle(90);
397 tt->SetTextAlign(12);
401 m_cCharge->Modified();
406 if (m_gCharge && enough) {
408 m_gCharge->Fit(m_fMean,
"R");
409 double mean = m_gCharge->GetMean(2);
410 double maxi = mean + 15;
411 double mini = mean - 15;
412 m_line_up->SetY1(maxi);
413 m_line_up->SetY2(maxi);
414 m_line_mean->SetY1(mean);
415 m_line_mean->SetY2(mean);
416 m_line_low->SetY1(mini);
417 m_line_low->SetY2(mini);
420 diff = m_gCharge->GetRMS(2);
426 m_monObj->setVariable(
"trackcharge", mean, diff);
431 SEVCHK(ca_put(DBR_DOUBLE, mychid[0], (
void*)&data),
"ca_set failure");
432 SEVCHK(ca_put(DBR_DOUBLE, mychid[1], (
void*)&diff),
"ca_set failure");
440 m_cCharge->Pad()->SetFillColor(kGray);
444 if (fabs(data - 30.) > 20. || diff > 12) {
445 m_cCharge->Pad()->SetFillColor(kRed);
447 }
else if (fabs(data - 30) > 15. || diff > 8) {
448 m_cCharge->Pad()->SetFillColor(kYellow);
451 m_cCharge->Pad()->SetFillColor(kGreen);
462 SEVCHK(ca_put(DBR_INT, mychid[2], (
void*)&status),
"ca_set failure");
463 SEVCHK(ca_pend_io(5.0),
"ca_pend_io failure");
469 void DQMHistAnalysisPXDTrackChargeModule::endRun()
471 B2DEBUG(99,
"DQMHistAnalysisPXDTrackCharge : endRun called");
475 void DQMHistAnalysisPXDTrackChargeModule::terminate()
477 B2DEBUG(99,
"DQMHistAnalysisPXDTrackCharge: terminate called");
481 for (
auto m : mychid) SEVCHK(ca_clear_channel(m),
"ca_clear_channel failure");
482 SEVCHK(ca_pend_io(5.0),
"ca_pend_io failure");
485 if (m_refFile)
delete m_refFile;
489 TH1* DQMHistAnalysisPXDTrackChargeModule::GetHisto(TString histoname)
496 B2DEBUG(20,
"findHisto failed " << histoname <<
" not in memfile");
501 if (m_refFile && m_refFile->IsOpen()) {
502 TDirectory* d = m_refFile;
503 TString myl = histoname;
507 while (myl.Tokenize(tok, from,
"/")) {
511 if (myl.Tokenize(dummy, f,
"/")) {
512 auto e = d->GetDirectory(tok);
514 B2DEBUG(20,
"Cd Dir " << tok <<
" from " << d->GetPath());
517 B2DEBUG(20,
"cd failed " << tok <<
" from " << d->GetPath());
523 TObject* obj = d->FindObject(tok);
525 if (obj->IsA()->InheritsFrom(
"TH1")) {
526 B2DEBUG(20,
"Histo " << histoname <<
" found in ref file");
529 B2DEBUG(20,
"Histo " << histoname <<
" found in ref file but wrong type");
533 TIter next(d->GetListOfKeys());
535 while ((key = (TKey*)next())) {
536 TObject* obj2 = key->ReadObj() ;
537 if (obj2->InheritsFrom(
"TH1")) {
538 if (obj2->GetName() == tok) {
540 B2DEBUG(20,
"Histo " << histoname <<
" found as key -> readobj");
545 if (hh1 == NULL) B2DEBUG(20,
"Histo " << histoname <<
" NOT found in ref file " << tok);
550 B2DEBUG(20,
"Histo " << histoname <<
" not in memfile or ref file");
553 TDirectory* d = gROOT;
554 TString myl = histoname;
557 while (myl.Tokenize(tok, from,
"/")) {
561 if (myl.Tokenize(dummy, f,
"/")) {
562 auto e = d->GetDirectory(tok);
564 B2DEBUG(20,
"Cd Dir " << tok);
566 }
else B2DEBUG(20,
"cd failed " << tok);
572 TObject* obj = d->FindObject(tok);
574 if (obj->IsA()->InheritsFrom(
"TH1")) {
575 B2DEBUG(20,
"Histo " << histoname <<
" found in mem");
579 B2DEBUG(20,
"Histo " << histoname <<
" NOT found in mem");
585 B2DEBUG(20,
"Histo " << histoname <<
" not found");