8#include <cdc/calibration/T0Correction.h>
9#include <cdc/dbobjects/CDCTimeZeros.h>
10#include <cdc/geometry/CDCGeometryPar.h>
11#include <cdc/dataobjects/WireID.h>
15#include <TGraphErrors.h>
20#include <framework/database/IntervalOfValidity.h>
21#include <framework/database/DBImportObjPtr.h>
22#include <framework/logging/Logger.h>
29 m_firstExperiment(0), m_firstRun(0),
30 m_lastExperiment(-1), m_lastRun(-1)
47 B2INFO(
"CreateHisto");
58 TChain* tree =
new TChain(
"tree");
59 tree->Add(m_inputRootFileName.c_str());
60 tree->SetBranchAddress(
"lay", &lay);
61 tree->SetBranchAddress(
"IWire", &IWire);
62 tree->SetBranchAddress(
"x_u", &x);
63 tree->SetBranchAddress(
"t", &t_mea);
64 tree->SetBranchAddress(
"t_fit", &t_fit);
65 tree->SetBranchAddress(
"weight", &w);
66 tree->SetBranchAddress(
"ndf", &ndf);
67 tree->SetBranchAddress(
"Pval", &Pval);
70 std::vector<TString> list_vars = {
"lay",
"IWire",
"x_u",
"t",
"t_fit",
"weight",
"Pval",
"ndf"};
71 tree->SetBranchStatus(
"*", 0);
73 for (TString brname : list_vars) {
74 tree->SetBranchStatus(brname, 1);
80 for (
int i = 0; i < 56; ++i) {
83 halfCSize[i] = M_PI *
R / nW;
86 m_hTotal =
new TH1F(
"hTotal",
"hTotal", 30, -15, 15);
88 for (
int il = 0; il < 56; il++) {
90 for (
int ic = 0; ic < nW; ++ic) {
91 m_h1[il][ic] =
new TH1F(Form(
"h1%d@%d", il, ic), Form(
"L%d_cell%d", il, ic), 30, -15, 15);
95 for (
int ib = 0; ib < 300; ++ib) {
96 m_hT0b[ib] =
new TH1F(Form(
"hT0b%d", ib), Form(
"boardID_%d", ib), 100, -20, 20);
99 const int nEntries = tree->GetEntries();
100 B2INFO(
"Number of entries: " << nEntries);
101 for (
int i = 0; i < nEntries; ++i) {
103 double xmax = halfCSize[lay] - 0.1;
104 if ((fabs(x) < m_xmin) || (fabs(x) > xmax)
106 || (Pval < m_Pvalmin))
continue;
108 m_hTotal->Fill(t_mea - t_fit);
109 m_h1[lay][IWire]->Fill(t_mea - t_fit);
113 B2INFO(
"Finish making histogram for all channels");
118 B2INFO(
"Start calibration");
121 gErrorIgnoreLevel = 3001;
126 TF1* g1 =
new TF1(
"g1",
"gaus", -100, 100);
127 vector<double> b, db, Sb, dSb;
128 vector<double> c[56];
129 vector<double> dc[56];
130 vector<double> s[56];
131 vector<double> ds[56];
133 B2INFO(
"Gaus fitting for whole channel");
135 m_hTotal->SetDirectory(0);
136 double mean = m_hTotal->GetMean();
137 m_hTotal->Fit(
"g1",
"Q",
"", mean - 15, mean + 15);
138 g1->GetParameters(par);
140 B2INFO(
"Gaus fitting for each board");
141 for (
int ib = 1; ib < 300; ++ib) {
142 if (m_hT0b[ib]->GetEntries() < 10)
continue;
143 mean = m_hT0b[ib]->GetMean();
144 m_hT0b[ib]->SetDirectory(0);
145 m_hT0b[ib]->Fit(
"g1",
"Q",
"", mean - 15, mean + 15);
146 g1->GetParameters(par);
149 Sb.push_back(par[1]);
150 dSb.push_back(g1->GetParError(1));
152 err_dtb[ib] = g1->GetParError(1);
154 B2INFO(
"Gaus fitting for each cell");
155 for (
int ilay = 0; ilay < 56; ++ilay) {
156 for (
unsigned int iwire = 0; iwire < cdcgeo.
nWiresInLayer(ilay); ++iwire) {
157 const int n = m_h1[ilay][iwire]->GetEntries();
158 B2DEBUG(99,
"layer " << ilay <<
" wire " << iwire <<
" entries " << n);
159 if (n < 10)
continue;
160 mean = m_h1[ilay][iwire]->GetMean();
161 m_h1[ilay][iwire]->SetDirectory(0);
162 m_h1[ilay][iwire]->Fit(
"g1",
"Q",
"", mean - 15, mean + 15);
163 g1->GetParameters(par);
164 c[ilay].push_back(iwire);
165 dc[ilay].push_back(0.0);
166 s[ilay].push_back(par[1]);
167 ds[ilay].push_back(g1->GetParError(1));
169 dt[ilay][iwire] = par[1];
170 err_dt[ilay][iwire] = g1->GetParError(1);
176 B2INFO(
"Store histo");
177 TFile* fout =
new TFile(
"Correct_T0.root",
"RECREATE");
179 TGraphErrors* gr[56];
180 TDirectory* top = gDirectory;
182 TDirectory* subDir[56];
183 for (
int ilay = 0; ilay < 56; ++ilay) {
184 subDir[ilay] = top ->mkdir(Form(
"lay_%d", ilay));
186 for (
unsigned int iwire = 0; iwire < cdcgeo.
nWiresInLayer(ilay); ++iwire) {
187 m_h1[ilay][iwire]->Write();
191 TDirectory* corrT0 = top->mkdir(
"DeltaT0");
195 TGraphErrors* grb =
new TGraphErrors(b.size(), &b.at(0), &Sb.at(0), &db.at(0), &dSb.at(0));
196 grb->SetMarkerColor(2);
197 grb->SetMarkerSize(1.0);
198 grb->SetTitle(
"#DeltaT0;BoardID;#DeltaT0[ns]");
200 grb->SetMinimum(-10);
201 grb->SetName(
"Board");
204 for (
int sl = 0; sl < 56; ++sl) {
205 if (c[sl].size() < 2)
continue;
206 gr[sl] =
new TGraphErrors(c[sl].size(), &c[sl].at(0), &s[sl].at(0), &dc[sl].at(0), &ds[sl].at(0));
207 gr[sl]->SetMarkerColor(2);
208 gr[sl]->SetMarkerSize(1.0);
209 gr[sl]->SetTitle(Form(
"Layer_%d;IWire;#LT t_{mea}-t_{fit} #GT [ns]", sl));
210 gr[sl]->SetMaximum(10);
211 gr[sl]->SetMinimum(-10);
212 gr[sl]->SetName(Form(
"lay%d", sl));
217 B2INFO(
"Write constants");
224 ofstream ofs(m_outputT0FileName.c_str());
230 for (
int ib = 0; ib < 300; ++ib) {
231 T0B[ib] =
new TH1F(Form(
"T0B%d", ib), Form(
"boardID_%d", ib), 9000, 0, 9000);
234 for (
int ilay = 0; ilay < 56; ++ilay) {
235 for (
unsigned int iwire = 0; iwire < cdcgeo.
nWiresInLayer(ilay); ++iwire) {
236 WireID wireid(ilay, iwire);
237 T0 = cdcgeo.
getT0(wireid);
243 for (
int ilay = 0; ilay < 56; ++ilay) {
244 for (
unsigned int iwire = 0; iwire < cdcgeo.
nWiresInLayer(ilay); ++iwire) {
245 WireID wireid(ilay, iwire);
247 if (abs(err_dt[ilay][iwire]) > 2 || abs(dt[ilay][iwire]) > 1.e3) {
249 T0 = T0B[bID]->GetMean();
250 dt[ilay][iwire] = dtb[bID];
252 T0 = cdcgeo.
getT0(wireid);
254 ofs << ilay <<
"\t" << iwire <<
"\t" << T0 - dt[ilay][iwire] << std::endl;
256 tz->setT0(wireid, T0 - dt[ilay][iwire]);
263 m_lastExperiment, m_lastRun);
265 B2RESULT(
"T0 was calibrated and imported to database.");
The Class for CDC Geometry Parameters.
unsigned short getBoardID(const WireID &wID) const
Returns frontend board id. corresponding to the wire id.
float getT0(const WireID &wireID) const
Returns t0 parameter of the specified sense wire.
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.
T0Correction()
Constructor.
virtual void Write()
write outut or store db
virtual bool calibrate()
Run algo on data.
virtual void CreateHisto()
create histo for each channel
bool import(const IntervalOfValidity &iov)
Import the object to database.
Class for importing a single object to the database.
void construct(Args &&... params)
Construct an object of type T in this DBImportObjPtr using the provided constructor arguments.
A class that describes the interval of experiments/runs for which an object in the database is valid.
Class to identify a wire inside the CDC.
Abstract base class for different kinds of events.