Belle II Software development
TRGECLQAMModule.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//framework headers
10#include <framework/datastore/StoreObjPtr.h>
11#include <framework/datastore/StoreArray.h>
12#include <framework/dataobjects/EventMetaData.h>
13//trg package headers
14
15#include <trg/ecl/modules/trgeclQAM/TRGECLQAMModule.h>
16
17#include <string.h>
18#include <fstream>
19#include <TFile.h>
20#include <TString.h>
21
22#include <TAxis.h>
23#include <TTree.h>
24
25#include <RooRealVar.h>
26#include <RooDataHist.h>
27#include <RooGaussian.h>
28#include <RooAddModel.h>
29#include <RooPlot.h>
30#include <RooArgList.h>
31#include <RooNovosibirsk.h>
32#include <RooFitResult.h>
33
34using namespace std;
35
36namespace Belle2 {
41 //
42 //
43
45 REG_MODULE(TRGECLQAM);
46 //
47 //
48 //
49 string
51 {
52 return string("TRGECLQAMModule 1.00");
53 }
54 //
55 //
56 //
58 : Module::Module(),
59 m_nevent(1), m_outputfile("")
60 {
61
62 string desc = "TRGECLQAMModule(" + version() + ")";
63 setDescription(desc);
65
66 addParam("outputFileName", m_outputfile, "TRGECL QAM file", m_outputfile);
67
68 m_nRun = 0;
69 m_nevent = 1;
70
71 //Problem TC Check
72 m_FWD = 0;
73 m_BAR = 0;
74 m_BWD = 0;
75 m_ALL = 0;
76 memset(TCID, 0, sizeof(TCID));
77
78
79 //Total Energy
80
81 m_etot = 0;
82 m_etot_mean = 0;
83 m_etot_error = 0;
84 m_etot_sigma = 0;
85
86 // std::vector<int> etot;
87 etot.clear();
88
89 //Caltime
90
94 caltime.clear();
95
96 //Cluster E
97
98 clusterE = 0;
99 cluster.clear();
100
101 B2DEBUG(100, "TRGECLQAMModule ... created");
102 }
103 //
104 //
105 //
107 {
108
109 B2DEBUG(100, "TRGECLQAMModule ... destructed ");
110 }
111 //
112 //
113 //
114 void
116 {
117
118 B2DEBUG(100, "TRGECLQAMModule::initialize ... options");
119
120
121 m_TRGECLUnpackerStore.registerInDataStore();
122 m_TRGECLUnpackerEvtStore.registerInDataStore();
123
124 m_TRGSummary.registerInDataStore();
125 // EvtMeta.registterInDataStore();
126
127
128 }
129 //
130 //
131 //
132 void
134 {
135
136 B2DEBUG(200, "TRGECLQAMModule ... beginRun called ");
137
138 }
139 //
140 //
141 //
142 void
144 {
145
146 B2DEBUG(200, "TRGECLFAMMoudle ... event called");
147
148
149
150 //QAM ECL Hit Map
151
152 // StoreArray<TRGECLUnpackerStore> m_TRGECLUnpackerStore;
153 for (int ii = 0; ii < m_TRGECLUnpackerStore.getEntries(); ii++) {
154 TRGECLUnpackerStore* m_TRGECLUnpacker = m_TRGECLUnpackerStore[ii];
155
156
157 int TCId = (m_TRGECLUnpacker->getTCId());
158 int HitEnergy = (m_TRGECLUnpacker->getTCEnergy());
159
160 m_etot += HitEnergy;
161 if (TCId < 1 || TCId > 576 || HitEnergy == 0) {continue;}
162 TCID[TCId - 1]++;
163 int Caltime = m_TRGECLUnpacker->getTCCALTime();
164 if (Caltime != 0) {
165 caltime.push_back(Caltime);
166 }
167 }
168
169 etot.push_back(m_etot);
170
171 m_etot = 0;
172
173 //Unpacker EvtStore
174 //for ( int ii = 0; ii < m_TRGECLUnpackerSumStore.getEntries(); ii++){
175 TRGECLUnpackerEvtStore* m_TRGECLUnpackerEvt = m_TRGECLUnpackerEvtStore[0];
176
177 if (m_TRGECLUnpackerEvt->get3DBhabhaS() == 1) {
178 //Cluster Energy 2D Check
179 const int* clE = m_TRGECLUnpackerEvt->getCLEnergy();
180
181 int clER[6] = {0};
182
183 for (int i = 0; i < 6; i++) {
184 clER[i] = clE[i];
185 }
186
187 sort(clER, clER + 6);
188
189 clusterE = clER[5] + clER[4]; // Ecl1+Ecl2
190 cluster.push_back(clusterE);
191 }
192
194 m_nRun = bevt->getRun();
195 m_nevent++;
196
197 }
198 //
199 //
200 //
201 void
203 {
204
205 double mean_FWD = 0;
206 double mean_BAR = 0;
207 double mean_BWD = 0;
208
209 //QAM TC Hit Map Check
210 for (int TCId = 1; TCId < 577; TCId++) {
211 if (TCId < 81) { //Forward Endcap
212 mean_FWD += TCID[TCId - 1];
213 } else if (TCId < 513) { //Barrel
214 mean_BAR += TCID[TCId - 1];
215 } else { //Backward Endcap
216 mean_BWD += TCID[TCId - 1];
217 }
218 }
219 mean_FWD /= 80;
220 mean_BAR /= (512 - 80);
221 mean_BWD /= (576 - 512);
222
223 for (int TCId = 1; TCId < 577; TCId++) {
224 if (TCId < 81) { //Forward Endcap
225 if (TCID[TCId - 1] < mean_FWD * 0.1)m_FWD++;
226 } else if (TCId < 513) { //Barrel
227 if (TCID[TCId - 1] < mean_BAR * 0.1)m_BAR++;
228 } else { //Backward Endcap
229 if (TCID[TCId - 1] < mean_BWD * 0.1)m_BWD++;
230 }
231 }
232
233 m_ALL = m_FWD + m_BAR + m_BWD;
234 const int etot_size = etot.size();
235 for (int i = 0; i < etot_size; i++) {
236 h_etot->Fill(etot[i]);
237 }
238 //Caluate E_total peak and width
239
240 RooRealVar* E_tot = new RooRealVar("E_tot", "E_tot", 0, 4000);
241 RooDataHist* data_E_tot = new RooDataHist("data_E_tot", "dataset with E_tot", *E_tot, h_etot);
242 RooRealVar* mean_check = new RooRealVar("mean_check", "Mean for checking", 2000, 1000, 3000);
243 RooRealVar* sigma_check = new RooRealVar("sigma_check", "Sigma for checking", 100, 0, 300);
244 RooGaussian* gauss_check = new RooGaussian("gauss_check", "Gaussian for checking", *E_tot, *mean_check, *sigma_check);
245 gauss_check->fitTo(*data_E_tot);
246 double mean_ch = mean_check->getVal();
247
248 RooRealVar* mean_E_tot = new RooRealVar("mean_E_tot", "Mean of Gaussian", mean_ch, mean_ch - 200, mean_ch + 200);
249 // RooRealVar sigma_E_tot("sigma_E_tot","Width of Gaussian", 10, 0, 50);
250
251 RooRealVar* sigma1_E_tot = new RooRealVar("sigma1_E_tot", "Width of Gaussian", 30, 0, 60);
252 RooGaussian* gauss1_E_tot = new RooGaussian("gauss1_E_tot", "gauss(x,mean,sigma)", *E_tot, *mean_E_tot, *sigma1_E_tot);
253 RooRealVar* sigma2_E_tot = new RooRealVar("sigma2_E_tot", "Width of Gaussian", 100, 30, 250);
254 RooGaussian* gauss2_E_tot = new RooGaussian("gauss2_E_tot", "gauss(x,mean,sigma)", *E_tot, *mean_E_tot, *sigma2_E_tot);
255
256 RooRealVar* frac_core_E_tot = new RooRealVar("frac_core", "core fraction", 0.85, 0.0, 1.0);
257 RooAddModel* gaussm_E_tot = new RooAddModel("gaussm", "core+tail gauss", RooArgList(*gauss1_E_tot, *gauss2_E_tot),
258 RooArgList(*frac_core_E_tot));
259
260 // RooGaussian gauss_E_tot("gauss_E_tot","gauss(x,mean,sigma)",E_tot,mean_E_tot,sigma_E_tot);
261
262 gaussm_E_tot->fitTo(*data_E_tot, RooFit::Range(mean_ch - 300, mean_ch + 300));
263 //RooPlot* xframe_E_tot = E_tot->frame();
264 RooPlot* xframe_E_tot = E_tot->frame(RooFit::Title("energy_tot"));
265 //data_E_tot->plotOn(xframe_E_tot);
266 data_E_tot->plotOn(xframe_E_tot, RooFit::MarkerStyle(7));
267 // gauss_E_tot->plotOn(xframe_E_tot);
268 gaussm_E_tot->plotOn(xframe_E_tot, RooFit::Name("Cal_energy"), RooFit::LineColor(2));
269 double v_m_E_tot = mean_E_tot->getVal();
270 double e_m_E_tot = mean_E_tot->getError();
271
272 double v1_s1_E_tot = sigma1_E_tot->getVal();
273 // double e1_s1_E_tot = sigma1_E_tot->getError();
274 double v2_s1_E_tot = sigma2_E_tot->getVal();
275 // double e2_s1_E_tot = sigma2_E_tot->getError();
276
277 double frac_E_tot = frac_core_E_tot->getVal();
278 double v_s1_E_tot = v1_s1_E_tot * (frac_E_tot) + v2_s1_E_tot * (1 - frac_E_tot);
279
280
281
282 for (long unsigned int ii = 0; ii < caltime.size(); ii++) {
283 h_caltime->Fill(caltime[ii]);
284 }
285
286
287 //Calculate Caltime peak and width
288 RooRealVar T_c("T_c", "T_c", -100, 100);
289 RooDataHist data_T_c("data_T_c", "dataset with T_c", T_c, h_caltime);
290
291 RooRealVar mean_T_c("mean_T_c", "Mean of Gaussian", 0, -100, 100);
292 RooRealVar sigma1_T_c("sigma1_T_c", "Width of Gaussian", 10, 0, 50);
293 RooGaussian gauss1_T_c("gauss1_T_c", "gauss(x,mean,sigma)", T_c, mean_T_c, sigma1_T_c);
294 RooRealVar sigma2_T_c("sigma2_T_c", "Width of Gaussian", 30, 10, 100);
295 RooGaussian gauss2_T_c("gauss2_T_c", "gauss(x,mean,sigma)", T_c, mean_T_c, sigma2_T_c);
296
297 RooRealVar frac_core_T_c("frac_core", "core fraction", 0.85, 0.0, 1.0);
298 RooAddModel gaussm_T_c("gaussm", "core+tail gauss", RooArgList(gauss1_T_c, gauss2_T_c), RooArgList(frac_core_T_c));
299
300 gaussm_T_c.fitTo(data_T_c, RooFit::Range(-45, 45));
301 RooPlot* xframe_T_c = T_c.frame(RooFit::Title("Caltime"));
302 data_T_c.plotOn(xframe_T_c, RooFit::MarkerStyle(7));
303 gaussm_T_c.plotOn(xframe_T_c, RooFit::Name("Caltime"), RooFit::LineColor(2));
304 gaussm_T_c.plotOn(xframe_T_c, RooFit::Components("gauss1_T_c"), RooFit::LineColor(kGreen));
305 gaussm_T_c.plotOn(xframe_T_c, RooFit::Components("gauss2_T_c"), RooFit::LineColor(kMagenta));
306
307 xframe_T_c->GetXaxis()->SetTitle("Caltime[ns]");
308 xframe_T_c->GetYaxis()->SetTitle("Entries");
309
310 double v_m_T_c = mean_T_c.getVal();
311 double e_m_T_c = mean_T_c.getError();
312
313 double v1_s1_T_c = sigma1_T_c.getVal();
314 // double e1_s1_T_c = sigma1_T_c.getError();
315 double v2_s1_T_c = sigma2_T_c.getVal();
316 // double e2_s1_T_c = sigma2_T_c.getError();
317
318 double frac_T_c = frac_core_T_c.getVal();
319 double v_s1_T_c = v1_s1_T_c * (frac_T_c) + v2_s1_T_c * (1 - frac_T_c);
320
321
322
323 //Cluster E sum for Bhabha skim
324 const int cluster_size = cluster.size();
325 for (int ii = 0; ii < cluster_size; ii++) {
326 h_clusterE->Fill(cluster[ii]);
327 }
328
329 RooRealVar x("x", "esum", 0, 4000);
330 RooDataHist dh("dh", "ecltrg", x, h_clusterE);
331
332 RooRealVar mean("mean", "mean", 2000, 1000, 2500);
333 RooRealVar sigma("sigma", "sigma", 150, 0, 300);
334 RooRealVar tail("tail", "tail", 0.45, 0, 1);
335 RooNovosibirsk novo("novo", "", x, mean, sigma, tail);
336 novo.fitTo(dh, RooFit::Extended(0), RooFit::Range(1500, 2200));
337
338
339 double clusterE_vm, clusterE_em, clusterE_vs;
340
341 clusterE_vm = mean.getVal();
342 clusterE_em = mean.getError();
343 clusterE_vs = sigma.getVal();
344
345
346 TString outputfile = m_outputfile;
347 TFile file(outputfile, "RECREATE");
348 TTree* tree = new TTree("tree", "tree");
349 int nevent;
350 double FWD;
351 double BAR;
352 double BWD;
353 double ALL;
354
355 nevent = m_nevent;
356 FWD = m_FWD;
357 BAR = m_BAR;
358 BWD = m_BWD;
359 ALL = m_ALL;
360
361 m_etot_mean = v_m_E_tot;
362 m_etot_error = e_m_E_tot;
363 m_etot_sigma = v_s1_E_tot;
364
365 m_caltime_mean = v_m_T_c;
366 m_caltime_error = e_m_T_c;
367 m_caltime_sigma = v_s1_T_c;
368
369 tree->Branch("m_nRun", &m_nRun, "m_nRun/I");
370 tree->Branch("m_nevent", &nevent, "m_nevent/I");
371 tree->Branch("m_FWD", &FWD, "m_FWD/D");
372 tree->Branch("m_BAR", &BAR, "m_BAR/D");
373 tree->Branch("m_BWD", &BWD, "m_BWD/D");
374 tree->Branch("m_ALL", &ALL, "m_ALL/D");
375 tree->Branch("m_etot_mean", &m_etot_mean, "m_etot_mean/D");
376 tree->Branch("m_etot_error", &m_etot_error, "m_etot_error/D");
377 tree->Branch("m_etot_sigma", &m_etot_sigma, "m_etot_sigma/D");
378 tree->Branch("m_caltime_mean", &m_caltime_mean, "m_caltime_mean/D");
379 tree->Branch("m_caltime_error", &m_caltime_error, "m_caltime_error/D");
380 tree->Branch("m_caltime_sigma", &m_caltime_sigma, "m_caltime_sigma/D");
381 tree->Branch("m_clusterE_mean", &clusterE_vm, "m_clusterE_mean/D");
382 tree->Branch("m_clusterE_error", &clusterE_em, "m_clusterE_error/D");
383 tree->Branch("m_clusterE_sigma", &clusterE_vs, "m_clusterE_sigma/D");
384
385 tree->Fill();
386 tree->Write();
387 file.Close();
388
389
390
391 B2INFO("ECL Trigger QAM result "
392 << LogVar("Run Number", m_nRun)
393 << LogVar("Total Event", nevent)
394 << LogVar("The # of Low Hit TC in Forward Endcap", FWD)
395 << LogVar("The # of Low Hit TC in Barrel", BAR)
396 << LogVar("The # of Low Hit TC in Backward Endcap", BWD)
397 << LogVar("Total Energy Peak", m_etot_mean)
398 << LogVar("Total Energy Peak width", m_etot_sigma)
399 << LogVar("Caltime Peak", m_caltime_mean)
400 << LogVar("Caltime Peak width", m_caltime_sigma)
401 << LogVar("Cluster Energy Peak", clusterE_vm)
402 << LogVar("Cluster Energy Peak width", clusterE_vs));
403
404
405 B2DEBUG(100, "TRGECLQAMModule ... endRun called ");
406
407 }
408 //
409 //
410 //
412 {
413 B2DEBUG(100, "TRGECLQAMModule ... terminate called ");
414
415 }
416 //
417 //
418 //
420} // namespace Belle2
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
Type-safe access to single objects in the data store.
Definition: StoreObjPtr.h:95
TH1F * h_caltime
Caltime Histogram.
double m_ALL
Total proble TC.
double m_etot_sigma
Total Energy sigma.
StoreArray< TRGECLUnpackerEvtStore > m_TRGECLUnpackerEvtStore
ECL Trigger Unpacker Event output.
int TCID[576]
Hit TCID.
std::vector< int > caltime
Caltime check.
double m_BAR
Barrel problem TC.
double m_BWD
BWD problem TC.
TH1F * h_clusterE
Cluster Energy Histogram.
std::string m_outputfile
Output Root File Name.
double m_etot_mean
Total Energy mean.
std::vector< int > etot
tcenergy check
double m_caltime_mean
Caltime mean.
TH1F * h_etot
Total Energy Histogram.
StoreArray< TRGECLUnpackerStore > m_TRGECLUnpackerStore
ECL Trigger Unpacker TC output.
std::vector< int > cluster
Cluster Energy Vector.
double m_etot_error
Total Energy error.
double m_caltime_error
Caltime error.
int m_nevent
The # of Events.
Double_t m_FWD
FWD problem TC.
StoreObjPtr< TRGSummary > m_TRGSummary
Trigger Summary.
int clusterE
Cluster Energy.
double m_caltime_sigma
Caltime sigma.
int m_etot
Total Energy.
int get3DBhabhaS() const
The method to get 3D Bhabha selection bit.
const int * getCLEnergy() const
The method to get Cluster energy.
int getTCCALTime() const
The method to get cal timing.
int getTCEnergy() const
The method to get deposited energy.
int getTCId() const
The method to get cell id.
Class to store variables with their name which were sent to the logging service.
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 ~TRGECLQAMModule()
Destrunctor.
virtual void initialize() override
initialize
virtual void event() override
Event.
virtual void endRun() override
End Run.
virtual void terminate() override
terminate
TRGECLQAMModule()
Constructor.
virtual void beginRun() override
begin Run
std::string version() const
version
Abstract base class for different kinds of events.
STL namespace.