Belle II Software  release-08-01-10
EventTime.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 //-----------------------------------------------------------------------------
10 // Description : A class to get Event Time information
11 //-----------------------------------------------------------------------------
12 
13 #define TRG_SHORT_NAMES
14 #define TRGCDC_SHORT_NAMES
15 
16 #include "trg/trg/Debug.h"
17 #include "trg/cdc/Segment.h"
18 #include "trg/cdc/TRGCDCTrack.h"
19 #include "trg/cdc/EventTime.h"
20 #include <iostream>
21 #include <iomanip>
22 #include <fstream>
23 #include <bitset>
24 #include <vector>
25 
26 #include "trg/cdc/TRGCDC.h"
27 #include "trg/cdc/Layer.h"
28 #include "trg/cdc/Wire.h"
29 #include "trg/cdc/WireHit.h"
30 #include "trg/cdc/SegmentHit.h"
31 #include "trg/trg/Utilities.h"
32 
33 #include "TH1D.h"
34 
35 using namespace std;
36 
37 namespace Belle2 {
43  TRGCDCEventTime::TRGCDCEventTime(const TRGCDC& TRGCDC, bool makeRootFile)
44  : _cdc(TRGCDC),
45  cnt{{0}},
46  ft{{{0}}},
47  m_foundT0(0), // 2019/07/31 by ytlai
48  m_minusET(0),
49  m_noET(0),
50  threshold(0),
51  m_eventN(0),
52  m_makeRootFile(makeRootFile),
53  m_ver(0)
54  {
55 
56  if (m_makeRootFile) m_fileEvtTime = new TFile("ETF.root", "RECREATE");
57 
58  m_evtOutputTs = new TTree("m_evtOutsTs", "EvtTime Outputs for TS");
59  m_evtOut = new TTree("m_evtOut", "EvtTime Outputs for 20 TS");
60 
61  m_evtOutputTs->Branch("tsFastestTime", &m_fastestT, "m_fastestT/I");
62  m_evtOutputTs->Branch("tsFoundTime", &m_foundT, "m_foundT/I");
63  m_evtOutputTs->Branch("foundT_fastT_diff", &m_whdiff, "m_whdiff/I");
64 
65  m_evtOut->Branch("histTime", &m_histT, "m_histT/I");
66 
67  h = new TH1D("h", "h", 1000, -500, 499);
68 
69  memset(cnt, 0, sizeof cnt);
70  memset(ft, 0, sizeof ft);
71 
72  // move to constructor
73  //m_minusET = 0;
74  //m_noET = 0;
75  //m_eventN = 0;
76  //m_ver = 0;
77  //m_foundT0 = 0;
78  }
79 
80  TRGCDCEventTime::~TRGCDCEventTime()
81  {
82  delete m_evtOutputTs;
83  delete m_evtOut;
84  if (m_makeRootFile) delete m_fileEvtTime;
85  delete h;
86  }
87 
88  void TRGCDCEventTime::initialize(void)
89  {
90  m_minusET = 0;
91  m_noET = 0;
92  m_eventN = 0;
93  }
94 
95  void TRGCDCEventTime::doit(int ver, bool print)
96  {
97  TRGDebug::enterStage("Event Time");
98  m_eventN += 1;
99  m_ver = ver;
100  if (ver == 1) {
101  oldVer();
102  getT0();
103  } else if (ver == 0) {
104  hitcount();
105  hist();
106  getT0();
107  } else if (ver == 2) {
108  m_histT = 0;
109  m_foundT0 = 1;
110  getT0();
111  } else {
112  cout << "verETF error" << endl;
113  cout << "choose 0:hist, 1:old, 2:trueT0" << endl;
114  }
115  if (print) printFirm();
116  TRGDebug::leaveStage("Event Time");
117  }
118 
119  void TRGCDCEventTime::hitcount(void)
120  {
121  TRGDebug::enterStage("Event Time");
122 
123  memset(cnt, 0, sizeof cnt);
124  memset(ft, 0, sizeof ft);
125 
126  const vector<const TCSHit*> tsh = _cdc.segmentHits();
127 
128  for (int iClk = 0; iClk < 64; iClk++) {
129  for (int iTS = 0; iTS < (int)tsh.size(); iTS++) {
130  const TRGCDCSegment& ts = tsh[iTS]->segment();
131  int fndC = ts.foundTime() / 16;
132  m_fastestT = ts.fastestTime();
133  m_foundT = ts.foundTime();
134  m_whdiff = ts.foundTime() - ts.fastestTime();
135  if (m_whdiff <= 256) {
136  if (iClk == fndC + 31) {
137  for (int iSL = 0; iSL < 9; iSL++) {
138  if ((int)ts.layer().id() == iSL) {
139  cnt[iSL][iClk] += 1;
140  if (cnt[iSL][iClk] <= 10) {
141  h->Fill(m_fastestT);
142  m_evtOutputTs->Fill();
143  ft[iSL][iClk][cnt[iSL][iClk] - 1] = ts.fastestTime();
144  }
145  }
146  }
147  }
148  }
149  }
150  }
151  TRGDebug::leaveStage("Event Time");
152  }//end of TRGCEventTime::hitcount
153 
154  void TRGCDCEventTime::hist(void)
155  {
156  TRGDebug::enterStage("Event Time");
157 
158  m_foundT0 = 0;
159  threshold = 3;
160  m_histT = 500;
161 
162  for (int i = 450; i < 600; i++) {
163  if (h->GetBinContent(i) > threshold) {
164  m_foundT0 = 1;
165  m_histT = i - 500;
166  m_evtOut->Fill();
167  if (m_histT < 0) {
168  m_minusET += 1;
169  }
170  break;
171  }
172  }//end of loop over hist bins
173  if (m_histT == 500) {
174  m_noET += 1;
175  }//if no bin has contents over threshold no evt time
176  h->Reset();
177 
178  TRGDebug::leaveStage("Event Time");
179  }//end of TRGCDCEventTime::hist
180 
181  void TRGCDCEventTime::oldVer(void)
182  {
183  TRGDebug::enterStage("Event Time");
184  m_histT = 65535;
185  for (unsigned i = 0; i < _cdc.nSegmentLayers(); i++) {
186  const Belle2::TRGCDCLayer* l = _cdc.segmentLayer(i);
187  const unsigned nWires = l->nCells();
188  for (unsigned j = 0; j < nWires; j++) {
189  const TCSegment& s = (TCSegment&) * (*l)[j];
190  const vector<const TCWire*>& wires = s.wires();
191  const TRGSignal& timing = s.signal();
192  if (timing.active()) {
193  for (unsigned k = 0; k < wires.size(); k++) {
194  if (wires[k]->hit()) {
195  int dt = wires[k]->signal()[0]->time();
196  if (m_histT > dt) {
197  m_histT = dt;
198  m_foundT0 = 1;
199  m_evtOut->Fill();
200  }
201  }
202  }
203  }
204  }
205  }
206  TRGDebug::leaveStage("Event Time");
207  }//end of TRTGCDCEventTime::oldVer
208 
209  void TRGCDCEventTime::printFirm(void)
210  {
211  TRGDebug::enterStage("Event Time");
212 
213  for (int iClk = 0; iClk < 64; iClk++) {
214  for (int iSL = 0; iSL < 9; iSL++) {
215  int hmbw = 160 + 32 * (iSL - 1); //set bit width of hitmap part
216  if (iSL == 0) hmbw = 160;
217  string hs[10];
218  string t[10];
219  char file[30];
220  sprintf(file, "SL%d.coe", iSL);
221  string cnts = string(cnt[iSL][iClk], '1');
222 
223  string clk = bitset<9>(iClk + 64 * (m_eventN - 1)).to_string();
224  string clk2 = bitset<5>(iClk).to_string();
225  for (int i = 0; i < 10; i++) {
226  hs[i] = bitset<4>(ft[iSL][iClk][i]).to_string();
227  if (cnt[iSL][iClk] > i) t[i] = clk2 + hs[i];
228  else t[i] = "00000" + hs[i];
229  }
230  ofstream fout;
231  fout.open(file, ios::app);
232  fout << clk;
233  fout << setfill('0') << setw(hmbw) << cnts;
234  fout << t[9] << t[8] << t[7] << t[6] << t[5] << t[4] << t[3] << t[2] << t[1] << t[0] << endl;
235  fout.close();
236  }
237  }
238  TRGDebug::leaveStage("Event Time");
239  }//end of TRGCDCEventTime::printFirm
240 
241 
242  int TRGCDCEventTime::getT0(void)const
243  {
244  int et = m_histT;
245  if (m_foundT0 == 0) et = 9999;
246  return et;
247  }
248 
249  void TRGCDCEventTime::terminate(void)
250  {
251  delete h;
252  if (m_makeRootFile) {
253  m_fileEvtTime->Write();
254  m_fileEvtTime->Close();
255  }
256  }
258 }
A class to represent a cell layer.
Definition: Layer.h:33
A class to represent a wire in CDC.
Definition: Segment.h:39
The instance of TRGCDC is a singleton.
Definition: TRGCDC.h:69
A class to represent a digitized signal. Unit is nano second.
Definition: Signal.h:23
float fastestTime(void) const
return fastest time in TSHit.
Definition: Segment.cc:387
unsigned id(void) const
returns id.
Definition: Layer.h:159
const TRGCDCLayer & layer(void) const
returns a pointer to a layer.
Definition: Cell.h:232
unsigned nCells(void) const
returns # of cells.
Definition: Layer.h:194
float foundTime(void) const
return found time in TSHit.
Definition: Segment.cc:423
Abstract base class for different kinds of events.