Belle II Software  release-05-02-19
TOPChannelT0MCModule.cc
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2017 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Wenlong Yuan *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 
11 // Own include
12 #include <top/modules/TOPChannelT0MC/TOPChannelT0MCModule.h>
13 
14 // framework - DataStore
15 #include <framework/datastore/StoreArray.h>
16 
17 // database classes
18 #include <framework/database/DBStore.h>
19 
20 #include <TFile.h>
21 #include <TH1F.h>
22 #include <TTree.h>
23 #include <sstream>
24 
25 using namespace std;
26 
27 namespace Belle2 {
32  //-----------------------------------------------------------------
33  // Register module
34  //-----------------------------------------------------------------
35 
36  REG_MODULE(TOPChannelT0MC)
37 
38  //-----------------------------------------------------------------
39  // Implementation
40  //-----------------------------------------------------------------
41 
43  {
44  std::vector<double> frange = {100, 0., 1.};
45  // set module description (e.g. insert text)
46  setDescription("TOP channel T0 Calibration of MC extraction");
47  // Add parameters
48  addParam("outputFile", m_outputFile, "Output root file name",
49  string(""));
50  addParam("fitRange", m_fitRange, "fit range[nbins, xmin, xmax]", frange);
51 
52  }
53 
54  TOPChannelT0MCModule::~TOPChannelT0MCModule()
55  {
56  }
57 
58  void TOPChannelT0MCModule::initialize()
59  {
60 
61  m_digits.isRequired();
62 
63  }
64 
65  void TOPChannelT0MCModule::beginRun()
66  {
67  }
68 
69  void TOPChannelT0MCModule::event()
70  {
71 
72  for (auto& digit : m_digits) {
73  unsigned channel = digit.getChannel();
74  if (channel < c_NumChannels) {
75  auto histo = m_histo[channel];
76  if (!histo) {
77  stringstream ss;
78  ss << "chan" << channel ;
79  string name;
80  ss >> name;
81  string title = "Times " + name;
82  histo = new TH1F(name.c_str(), title.c_str(), (int)m_fitRange[0], m_fitRange[1], m_fitRange[2]);
83  m_histo[channel] = histo;
84  }
85  if (digit.getTime() < m_fitRange[1] || digit.getTime() > m_fitRange[2]) continue;
86  histo->Fill(digit.getTime());
87  }
88  }
89  }
90 
91  void TOPChannelT0MCModule::endRun()
92  {
93  }
94 
95  void TOPChannelT0MCModule::terminate()
96  {
97  auto file = new TFile(m_outputFile.c_str(), "RECREATE");
98  auto otree = new TTree("t0MC", "extract channel t0 info. from MC");
99 
100  unsigned channel = 0;
101  double maxpos = 0;
102 
103  otree->Branch("maxpos", &maxpos, "maxpos/D");
104  otree->Branch("channel", &channel, "channel/I");
105 
106  for (int i = 0; i < c_NumChannels; i++) {
107  channel = i;
108  maxpos = m_histo[i]->GetXaxis()->GetBinCenter(m_histo[i]->GetMaximumBin());
109  otree->Fill();
110  }
111  otree->Write();
112  delete otree;
113  file->Close();
114  delete file;
115  }
117 } // end Belle2 namespace
118 
REG_MODULE
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Definition: Module.h:652
Belle2::TOPChannelT0MCModule
TOP Channel T0 MC Extraction module (under development)
Definition: TOPChannelT0MCModule.h:29
Belle2::Module
Base class for Modules.
Definition: Module.h:74
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19