Belle II Software development
CDCInitialT0Determination.cc
1/**************************************************************************
2 * basf2 (Belle II Analysis Software Framework) *
3 * Author: The Belle II Collaboration *
4 * *
5 * See git log for contributors and copyright holders. *
6 * This file is licensed under LGPL-3.0, see LICENSE.md. *
7 **************************************************************************/
8#include "cdc/modules/cdcInitialT0Determination/CDCInitialT0Determination.h"
9#include <cdc/geometry/CDCGeometryPar.h>
10#include <framework/gearbox/Const.h>
11#include "TF1.h"
12#include "TDirectory.h"
13#include "TFile.h"
14#include "TGraphErrors.h"
15#include "vector"
16#include "TROOT.h"
17
18using namespace std;
19using namespace Belle2;
20using namespace CDC;
21REG_MODULE(CDCInitialT0Determination);
22
24{
25 setDescription("Module to determine crude t0");
26 setPropertyFlags(c_ParallelProcessingCertified); // specify this flag if you need parallel processing
27 addParam("OutputFileName", m_outputFileName, "t0 file output file", std::string("t0.dat"));
28 addParam("LowTDC", m_tdcMin, "lower boundary of tdc histogram", m_tdcMin);
29 addParam("UpTDC", m_tdcMax, "Upper boundary of tdc histogram", m_tdcMax);
30 addParam("InitialT0", m_initT0, "initial t0 for fitting", 3579.);
31 addParam("Cosmic", m_cosmic, "true; tof negative for upper part of cdc", true);
32 addParam("Zoffset", m_zOffset, "z offset", 0.);
33 addParam("ADCCut", m_adcMin, "threshold of ADC", m_adcMin);
34 addParam("StoreFittedHisto", m_storeFittedHisto, "Store fitted histogram for each channel or not", true);
35 addParam("HistoFileName", m_histoFileName, "file contain TDC histo", std::string("TDC.root"));
36 addParam("MinEntries", m_minEntries, "minimum entries per channel", m_minEntries);
37}
38
40{
41}
42
43
45{
47 for (int il = 0; il < 56; ++il) {
48 for (unsigned short w = 0; w < cdcgeo.nWiresInLayer(il); ++w) {
49 m_hTDC[il][w] = new TH1D(Form("hLay%d_ch%d", il, w), "tdc", m_tdcMax - m_tdcMin, m_tdcMin, m_tdcMax);
50 }
51 }
52 for (int ib = 0; ib < 300; ++ib) {
53 m_hTDCBoard[ib] = new TH1D(Form("hTDCBoard%d", ib), "", m_tdcMax - m_tdcMin, m_tdcMin, m_tdcMax);
54 }
55 m_hT0All = new TH1D("hT0All", "", 8500, 0, 8500);
56 m_CDCHits.isRequired();
57}
59{
61 for (const auto& hit : m_CDCHits) {
62 WireID wireid(hit.getID());
63 unsigned short lay = wireid.getICLayer();
64 unsigned short w = wireid.getIWire();
65 if (hit.getADCCount() > m_adcMin) {
66 m_hTDC[lay][w]->Fill(hit.getTDCCount());
67 m_hTDCBoard[cdcgeo.getBoardID(WireID(lay, w))]->Fill(hit.getTDCCount());
68 }
69 }
70}
72{
73 gROOT->SetBatch(1);
74 std::vector<double> sb;
75 std::vector<double> dsb;
76 std::vector<double> t0b;
77 std::vector<double> dt0b;
78 std::vector<double> b;
79 std::vector<double> db;
80 TH1D* hs = new TH1D("hs", "sigma", 100, 0, 20);
81
83 TF1* f1 = new TF1("f1", "[0]+[1]*(exp([2]*(x-[3]))/(1+exp(-([4]-x)/[5])))", m_tdcMin, m_tdcMax);
84 f1->SetParLimits(0, 0., 1000.);
85 f1->SetLineColor(kRed);
86 double tdcBinWidth = cdcgeo.getTdcBinWidth();
87 double bflag[300];
88 for (int ib = 1; ib < 300; ++ib) {
89 if (m_hTDCBoard[ib]->GetEntries() < m_minEntries) {
90 B2DEBUG(199, "Warning: this board low statistic: " << m_hTDCBoard[ib]->GetEntries());
91 bflag[ib] = 0;
92 m_t0b[ib] = m_initT0;
93 continue;
94 }
95 double p3 = m_hTDCBoard[ib]->GetXaxis()->GetBinCenter(m_hTDCBoard[ib]->GetMaximumBin());
96 f1->SetParameters(0, m_hTDCBoard[ib]->GetMaximum(), -0.001, p3, m_initT0, 2.5);
97 m_hTDCBoard[ib]->Fit("f1", "QM", "", m_initT0 - 60, m_initT0 + 60);
98
99 if ((fabs(f1->GetParameter(4) - m_initT0) > 100)
100 || (fabs(f1->GetParameter(5)) < 0.01)
101 || (fabs(f1->GetParameter(5)) > 16)) {
102
103 bflag[ib] = 0;
104 m_t0b[ib] = m_initT0;
105 continue;
106 }
107
108 bflag[ib] = 1;
109 m_t0b[ib] = f1->GetParameter(4) * tdcBinWidth;
110
111 sb.push_back(f1->GetParameter(5));
112 dsb.push_back(f1->GetParError(5));
113 t0b.push_back(f1->GetParameter(4));
114 dt0b.push_back(f1->GetParError(4));
115 b.push_back(ib);
116 db.push_back(0);
117 }
118
119 for (int il = 0; il < 56; ++il) {
120 for (unsigned short w = 0; w < cdcgeo.nWiresInLayer(il); ++w) {
121 B2DEBUG(99, "fitting for channel: " << il << " - " << w);
122 B2DEBUG(99, "number of entries" << m_hTDC[il][w]->GetEntries());
123 m_t0[il][w] = m_initT0 * tdcBinWidth;
124 int bid = cdcgeo.getBoardID(WireID(il, w));
125 if (m_hTDC[il][w]->GetEntries() < m_minEntries) {
126 B2DEBUG(99, "Warning: low statistic channel: " << m_hTDC[il][w]->GetEntries());
127 if (bflag[bid] != 0) {
128 m_t0[il][w] = m_t0b[bid];
129 m_flag[il][w] = true;
130 } else {m_flag[il][w] = false;}
131 } else {
132 double p3 = m_hTDC[il][w]->GetXaxis()->GetBinCenter(m_hTDC[il][w]->GetMaximumBin());
133 f1->SetParameters(0, m_hTDC[il][w]->GetMaximum(), -0.001, p3, m_initT0, 2.5);
134 m_hTDC[il][w]->Fit("f1", "QM", "", m_initT0 - 60, m_initT0 + 60);
135 B2DEBUG(99, "prob of fit : " << f1->GetProb());
136 if ((f1->GetProb() < 1E-150) || (fabs(f1->GetParameter(4) - m_initT0) > 100) || (f1->GetParameter(5) < 0.1)
137 || (f1->GetParameter(5) > 20)) {
138 if (bflag[bid] != 0) {
139 m_t0[il][w] = m_t0b[bid];
140 m_flag[il][w] = true;
141 } else {m_flag[il][w] = false;}
142 } else {
143 m_t0[il][w] = f1->GetParameter(4) * tdcBinWidth;
144 hs->Fill(f1->GetParameter(5));
145 m_flag[il][w] = true;
146 }
147 }
148 B2DEBUG(99, "P4 = " << m_t0[il][w]);
149 if (m_cosmic && cdcgeo.wireBackwardPosition(il, w).Y() > 0) {
150 m_t0[il][w] -= cdcgeo.senseWireR(il) / Const::speedOfLight;
151 } else {
152 m_t0[il][w] += cdcgeo.senseWireR(il) / Const::speedOfLight;
153 }
154 m_t0[il][w] += (m_zOffset - cdcgeo.wireBackwardPosition(il, w).Z()) / 27.25;
155 m_t0[il][w] += 6.122;
156 m_hT0All->Fill(m_t0[il][w]);
157 // m_hT0b[cdcgeo.getBoardID(WireID(il, w))]->Fill(m_t0[il][w]);
158 }
159 }
160
161 //check t0, and add t0 for low static channel
162 ofstream ofs(m_outputFileName.c_str());
163 for (int il = 0; il < 56; ++il) {
164 for (unsigned short w = 0; w < cdcgeo.nWiresInLayer(il); ++w) {
165 if (m_flag[il][w] != true) {
166 m_t0[il][w] = m_hT0All->GetMean();
167 }
168 ofs << il << "\t" << w << "\t" << m_t0[il][w] << endl;
169 }
170 }
171 ofs.close();
172 if (m_storeFittedHisto) {
173 TFile* fhist = new TFile(m_histoFileName.c_str(), "recreate");
174 fhist->cd();
175 TDirectory* top = gDirectory;
176 TDirectory* Direct[56];
177 for (int il = 0; il < 56; ++il) {
178 top->cd();
179 Direct[il] = gDirectory->mkdir(Form("lay_%d", il));
180 Direct[il]->cd();
181 for (unsigned short w = 0; w < cdcgeo.nWiresInLayer(il); ++w) {
182 if (m_flag[il][w] == true) {
183 m_hTDC[il][w]->Write();
184 }
185 }
186 }
187 top->cd();
188 TDirectory* board = gDirectory->mkdir("board");
189 board->cd();
190 for (int ib = 0; ib < 300; ++ib) {
191 if (m_hTDCBoard[ib]) {
192 m_hTDCBoard[ib]->Write();
193 }
194 }
195 top->cd();
196 m_hT0All->Write();
197 hs->Write();
198 if (b.size() > 20) {
199 TGraphErrors* gr = new TGraphErrors(b.size(), &b.at(0), &sb.at(0), &db.at(0), &dsb.at(0));
200 gr->SetName("reso");
201 gr->Write();
202 TGraphErrors* grT0b = new TGraphErrors(b.size(), &b.at(0), &t0b.at(0), &db.at(0), &dt0b.at(0));
203 grT0b->SetName("T0Board");
204 grT0b->Write();
205 }
206 fhist->Close();
207 }
208}
209
R E
internal precision of FFTW codelets
DataType Z() const
access variable Z (= .at(2) without boundary check)
Definition: B2Vector3.h:435
DataType Y() const
access variable Y (= .at(1) without boundary check)
Definition: B2Vector3.h:433
TH1D * m_hT0All
T0 distribution of all channel.
unsigned short m_tdcMax
Upper boundary TDC histogram.
void initialize() override
Initializes the Module.
TH1D * m_hTDCBoard[300]
T0 distribution of each board.
bool m_storeFittedHisto
Store fitted histogram or not.
void event() override
Event action (main routine).
bool m_flag[56][400]
flag =1 for good, =0 for low statistic or bad fit
bool m_cosmic
for cosmic case, tof of upper sector will be negative
unsigned short m_adcMin
ADC cut to reject noise.
void terminate() override
Termination action, fit t0 and store histograms.
double m_t0[56][400]
T0 of each channel.
unsigned short m_tdcMin
Lower boundary TDC histogram.
double m_zOffset
z offset for calculate prop time, it is position of trigger counter,
double m_initT0
initial t0, use int fitting
std::string m_histoFileName
output file to store TDC histo after fit
unsigned short m_minEntries
min entries per histo.
TH1D * m_hTDC[56][400]
TDC distribution histo.
std::string m_outputFileName
output file name of t0 file.
The Class for CDC Geometry Parameters.
unsigned short getBoardID(const WireID &wID) const
Returns frontend board id. corresponding to the wire id.
const B2Vector3D wireBackwardPosition(uint layerId, int cellId, EWirePosition set=c_Base) const
Returns the backward position of the input sense wire.
double getTdcBinWidth() const
Return TDC bin width (nsec).
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.
static const double speedOfLight
[cm/ns]
Definition: Const.h:695
Base class for Modules.
Definition: Module.h:72
void setDescription(const std::string &description)
Sets the description of the module.
Definition: Module.cc:214
void setPropertyFlags(unsigned int propertyFlags)
Sets the flags for the module properties.
Definition: Module.cc:208
@ c_ParallelProcessingCertified
This module can be run in parallel processing mode safely (All I/O must be done through the data stor...
Definition: Module.h:80
Class to identify a wire inside the CDC.
Definition: WireID.h:34
void addParam(const std::string &name, T &paramVariable, const std::string &description, const T &defaultValue)
Adds a new parameter to the module.
Definition: Module.h:560
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Definition: Module.h:650
Abstract base class for different kinds of events.
STL namespace.