Belle II Software development
TOPLaserCalibratorModule.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
9// Own header.
10#include <top/modules/TOPLaserCalibrator/TOPLaserCalibratorModule.h>
11
12// TOP headers.
13#include <top/modules/TOPLaserCalibrator/LaserCalibratorFit.h>
14
15// framework - DataStore
16#include <framework/datastore/StoreArray.h>
17
18// framework aux
19#include <framework/logging/Logger.h>
20
21// DataStore classes
22//#include <framework/dataobjects/EventMetaData.h>
23#include <top/dataobjects/TOPDigit.h>
24
25#include <TFile.h>
26#include <TH1F.h>
27#include <TMath.h>
28#include <TTree.h>
29#include <sstream>
30
31
32using namespace std;
33
34namespace Belle2 {
39 //-----------------------------------------------------------------
41 //-----------------------------------------------------------------
42
43 REG_MODULE(TOPLaserCalibrator);
44
45 //-----------------------------------------------------------------
46 // Implementation
47 //-----------------------------------------------------------------
48
50 {
51 setDescription("T0 Calibration using the laser calibration system");
52
53 std::vector<double> frange = {100, 0., 1.};
54 // Add parameters
55 addParam("dataFitOutput", m_dataFitOutput, "Output root file for data",
56 string("")); //output fitting results
57 addParam("mcInput", m_mcInput, "Input root file from MC", string(""));
58 addParam("channelT0constant", m_chT0C, "Output of channel T0 constant", string(""));
59 addParam("fitChannel", m_fitChannel, "set 0 - 511 to a specific channel; set 0 to all channels in the fit",
60 512);
61 addParam("barID", m_barID, "ID of TOP module to calibrate");
62 addParam("refCh", m_refCh, "reference channel of T0 constant");
63 addParam("fitMethod", m_fitMethod, "gauss: single gaussian; cb: single Crystal Ball; cb2: double Crystal Ball", string("gauss"));
64 addParam("fitRange", m_fitRange, "fit range[nbins, xmin, xmax]", frange);
65
66 for (int i = 0; i < c_NumChannels; ++i) {
67 m_histo[i] = 0;
68 }
69 }
70
72 {
73 B2WARNING("You are using an old version of the laser fitter, now deprecated. This module has been superseded by the CAF collector TOPLaserCalibratorCollector and the CAF fitter TOPLocalCalFitter.");
74 m_digits.isRequired();
75 }
76
78 {
79 for (auto& digit : m_digits) {
80 if (m_barID != 0 && digit.getModuleID() != m_barID) continue; //if m_barID == 0, use all 16 slots(for MC test)
81 unsigned channel = digit.getChannel();
82 if (channel < c_NumChannels) {
83 auto histo = m_histo[channel];
84 if (!histo) {
85 stringstream ss;
86 ss << "chan" << channel ;
87 string name;
88 ss >> name;
89 string title = "Times " + name;
90 histo = new TH1F(name.c_str(), title.c_str(), (int)m_fitRange[0], m_fitRange[1], m_fitRange[2]);
91 m_histo[channel] = histo;
92 }
93 if (digit.getHitQuality() != TOPDigit::EHitQuality::c_Good) continue;
94 if (digit.getTime() < m_fitRange[1] || digit.getTime() > m_fitRange[2]) continue;
95 if (!digit.isTimeBaseCalibrated()) continue;
96 histo->Fill(digit.getTime()); //get Time from TOPDigit
97 }
98 }
99 }
100
102 {
103 std::vector<TH1F*> v_histo;
104 for (int i = 0; i < c_NumChannels; ++i) {
105 v_histo.push_back(m_histo[i]);
106 }
107
108 double dataT[c_NumChannels] = {0};
109 double dataTErr[c_NumChannels] = {0};
110 double mcT[c_NumChannels] = {0};
111
113 t0fit.setHist(v_histo); //set time hist vector
115 t0fit.setFitRange(m_fitRange[1], m_fitRange[2]);
116 if (m_fitChannel == c_NumChannels) {
117 for (int i = 0; i < c_NumChannels; ++i) {
118 t0fit.fitChannel(i);
119 dataT[i] = t0fit.getFitT();
120 dataTErr[i] = t0fit.getFitTErr();
121 }
122 } else {
124 dataT[m_fitChannel] = t0fit.getFitT();
125 }
126 //cout<<"chisq=\t"<<t0fit.getFitChisq(6)<<endl;
127 t0fit.writeFile(m_dataFitOutput); //write to root file
128
129 //read laser propagation time from MC (TOPChannelT0MC)
130 auto mcFile = new TFile(m_mcInput.c_str());
131 auto tree = static_cast<TTree*>(mcFile->Get("t0MC"));
132 int channel_mc;
133 double maxpos;
134
135 tree->SetBranchAddress("channel", &channel_mc);
136 tree->SetBranchAddress("maxpos", &maxpos);
137 for (int i = 0; i < tree->GetEntries(); i++) {
138 tree->GetEntry(i);
139 mcT[channel_mc] = maxpos;
140 }
141
142 //w.r.t. ref. channel
143 for (int i = 0; i < c_NumChannels; i++) {
144 if (i == m_refCh) continue;
145 dataT[i] = dataT[i] - dataT[m_refCh];
146 mcT[i] = mcT[i] - mcT[m_refCh];
147 }
148
149 delete tree;
150 mcFile->Close();
151 delete mcFile;
152
153 //calculate T0 Const, write to root file
154 auto file = new TFile(m_chT0C.c_str(), "RECREATE");
155 auto otree = new TTree("chT0", "Channel T0 Const");
156
157 unsigned channel = 0;
158 double t0_const = 0;
159 double t0ConstRaw = 0;
160 double fittedTime = 0;
161 double fittedTimeError = 0;
162 double mcTime = 0;
163 double mcCorrection = 0;
164 double t0ConstError = 0;
165
166 otree->Branch("fittedTime", &fittedTime, "fittedTime/D");
167 otree->Branch("fittedTimeError", &fittedTimeError, "fittedTimeError/D");
168 otree->Branch("mcTime", &mcTime, "mcTime/D");
169 otree->Branch("t0ConstRaw", &t0ConstRaw, "t0ConstRaw/D");
170 otree->Branch("mcCorrection", &mcCorrection, "mcCorrection/D");
171 otree->Branch("t0Const", &t0_const, "t0_const/D");
172 otree->Branch("t0ConstError", &t0ConstError, "t0ConstError/D");
173 otree->Branch("channel", &channel, "channel/I");
174 otree->Branch("slot", &m_barID, "slot/I");
175
176 for (int i = 0; i < c_NumChannels; i++) {
177 if (i == m_refCh) {
178 fittedTime = dataT[i];
179 mcTime = mcT[i];
180 } else {
181 fittedTime = dataT[i] + dataT[m_refCh];
182 mcTime = mcT[i] + mcT[m_refCh];
183 }
184 fittedTimeError = dataTErr[i];
185 t0ConstRaw = dataT[i];
186 mcCorrection = mcT[i];
187 channel = i;
188 t0_const = dataT[i] - mcT[i];
189 // assuming that the MC error is negligible
190 t0ConstError = TMath::Sqrt(dataTErr[i] * dataTErr[i] + dataTErr[m_refCh] * dataTErr[m_refCh]);
191 if (i == m_refCh) {
192 t0_const = 0;
193 t0ConstError = dataTErr[m_refCh];
194 }
195 otree->Fill();
196 }
197 otree->Write();
198 delete otree;
199 file->Close();
200 delete file;
201
202 // ...
203 // write to database; next to do ...
204 // ...
205 }
206
207} // end Belle2 namespace
208
void setDescription(const std::string &description)
Sets the description of the module.
Definition Module.cc:214
Module()
Constructor.
Definition Module.cc:30
std::vector< double > m_fitRange
fit range [nbins, xmin, xmax]
int m_fitChannel
set 0 - 511 to a specific pixelID in the fit; set 512 to fit all pixels in one slot
int m_barID
ID of TOP module to calibrate.
std::string m_mcInput
Input root file from MC.
StoreArray< TOPDigit > m_digits
collection of digits
int m_refCh
reference channel of T0 constant
std::string m_chT0C
Output of channel T0 constant.
TH1F * m_histo[c_NumChannels]
profile histograms
std::string m_fitMethod
gauss: single gaussian; cb: single Crystal Ball; cb2: double Crystal Ball
std::string m_dataFitOutput
output root file for data
A class do laser calibration fit provide different fitting method (under development)
void setHist(const std::vector< TH1F * > &hist)
set time hist of all channels in one moduleID
double getFitT()
get mean position after fit
void setFitMethod(const std::string &method)
set time fit function
void setFitRange(double xmin=-200, double xmax=200)
set x range in the fit
int fitChannel(unsigned channel)
fit for a specific channel
double getFitTErr()
returns the error mean position after fit
void writeFile(const std::string &outfile)
write fit result to a root file
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:559
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Definition Module.h:649
virtual void initialize() override
Initialize the Module.
virtual void event() override
Event processor.
virtual void terminate() override
Termination action.
Abstract base class for different kinds of events.
STL namespace.