Belle II Software  release-06-00-14
CsIStudyModule.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 #include <framework/core/HistoModule.h>
10 #include <framework/datastore/StoreArray.h>
11 #include <framework/datastore/StoreObjPtr.h>
12 #include <framework/dataobjects/EventMetaData.h>
13 #include <framework/gearbox/Const.h>
14 #include <beast/csi/modules/CsiModule.h>
15 #include <beast/csi/dataobjects/CsiSimHit.h>
16 #include <beast/csi/dataobjects/CsiHit.h>
17 #include <beast/csi/dataobjects/CsiDigiHit.h>
18 #include <beast/csi/modules/CsIStudyModule.h>
19 
20 // ROOT
21 #include <TH1.h>
22 #include <TFile.h>
23 
24 using namespace std;
25 using namespace Belle2;
26 using namespace csi;
27 
28 //-----------------------------------------------------------------
29 // Register the Module
30 //-----------------------------------------------------------------
31 REG_MODULE(CsIStudy)
32 
33 //-----------------------------------------------------------------
34 // Implementation
35 //-----------------------------------------------------------------
36 
38  fWF(NULL),
39  h_CrystalEdep(NULL),
40  h_CrystalSpectrum(NULL),
41  h_CrystalRadDose(NULL),
42  h_CrystalRadDoseSH(NULL),
43  h_NhitCrystal(NULL),
44  h_LightYieldCrystal(NULL),
45  h_Waveform(NULL),
46  h_Gate(NULL),
47  h_Charge(NULL),
48  h_TrueEdep(NULL),
49  h_Height(NULL),
50  h_Weight(NULL)
51 
52 {
53  // Set module properties
54  setDescription("Analyze simulations of CsI readings in BEAST. Requires HistoManager module.");
55 
56  // Parameter definitions
57  addParam("nWFSamples", m_nWFSamples, "Number of samples in the saved waveforms", 8000);
58  addParam("paramTemplate", m_paramTemplate, "Template of an input parameter. Noop for now.", 0.0);
59 
60  string rootfilepath = "output/waveforms.root";
61  addParam("waveformFilename", m_waveformFilename, "Path to the root file to save waveforms", rootfilepath);
62 
63 }
64 
65 CsIStudyModule::~CsIStudyModule()
66 {
67 }
68 
69 void CsIStudyModule::defineHisto()
70 {
71  h_CrystalEdep = new TH1F("Crystal_Edep", "Energy distribution in each crystal;CellID", 16, -0.5, 15.5);
72  h_CrystalSpectrum = new TH1F("Crystal_Edist", "Photon energy distribution in all crystals;", 100, 0, 0.1);
73  h_NhitCrystal = new TH1F("Crystal_HitRate", "Number of hit per crystal;CellID; hit/s", 16, -0.5, 15.5);
74  h_CrystalRadDose = new TH1F("Crystal_RadDose", "Crystal Radiation Dose;CellID;Gy/yr", 16, -0.5, 15.5);
75  h_CrystalRadDoseSH = new TH1F("Crystal_RadDose_SH", "Crystal Radiation Dose from SimHits;CellID;Gy/yr", 16, -0.5, 15.5);
76  h_LightYieldCrystal = new TH1F("Crystal_g_yield", "Light yield each crystal;CellID;gamma/sample", 16, -0.5, 15.5);
77  h_Waveform = new TH1S("Waveform", "Recorded waveform;Time index;ADC bits", m_nWFSamples, 0, m_nWFSamples - 1);
78  h_Gate = new TH1C("Gate", "Recorded gate;Time index;ADC bits", m_nWFSamples, 0, m_nWFSamples - 1);
79  h_Charge = new TH1F("Charge", "log_{10} of integrated Charge", 200, 0, 10);
80  h_TrueEdep = new TH1F("TrueEdep", "log_{10} of true deposited energy/GeV", 200, -6, 0);
81  h_Height = new TH1F("Height", "log10 of pulse height", 200, 0, 6);
82  h_Weight = new TH1F("Weight", "Distribution of the event weights form the generator", 1000, 0, 10);
83 }
84 
85 void CsIStudyModule::initialize()
86 {
87  REG_HISTOGRAM // required to register histograms to HistoManager
88 
89  m_hits.isOptional();
90  m_simhits.isRequired();
91  m_digihits.isOptional();
92 
93  fWF = new TFile(m_waveformFilename.c_str(), "recreate");
94 }
95 
96 void CsIStudyModule::beginRun()
97 {
98 }
99 
100 void CsIStudyModule::endRun()
101 {
102  // h_LightYieldCrystal->Divide( h_CrystalEdep );
103  // h_LightYieldCrystal->Scale( 1e-6 );
104 }
105 
106 
107 void CsIStudyModule::event()
108 {
109 
110  StoreObjPtr<EventMetaData> eventMetaDataPtr;
111  double eventWeight = eventMetaDataPtr->getGeneratedWeight();
112 
113  h_Weight->Fill(eventWeight);
114 
115  //Loop over CsiHits
116  if (m_hits.getEntries() > 0) {
117  int hitNum = m_hits.getEntries();
119  double E_tmp[16] = {0};
121  double Mass = 5;
122  double edeptodose = GeVtoJ / Mass * usInYr / Sampletime;
125  for (int i = 0; i < hitNum; i++) {
126  CsiHit* aCsIHit = m_hits[i];
127  int m_cellID = aCsIHit->getCellId();
128  double edep = aCsIHit->getEnergyDep();
129  //double hitTime = aCsIHit->getTimeAve(); /**< Time of the hit*/
130 
131  E_tmp[m_cellID] += edep;
132 
133  // Fill histograms
134  h_CrystalSpectrum->Fill(edep, eventWeight);
135  h_CrystalEdep->Fill(m_cellID, edep * eventWeight);
136  h_CrystalRadDose->Fill(m_cellID, edep * edeptodose * eventWeight);
137 
138  //Number of hits per second
139  h_NhitCrystal->Fill(m_cellID, eventWeight * 1.0e9 / Sampletime);
140  }
141  }
142 
143  //Loop over CsiSimHits
144  if (m_simhits.getEntries() > 0) {
145  int hitNum = m_simhits.getEntries();
147  double Mass = 5;
148  double edeptodose = GeVtoJ / Mass * usInYr / Sampletime;
151  for (int i = 0; i < hitNum; i++) {
152  CsiSimHit* aCsIHit = m_simhits[i];
153  int m_cellID = aCsIHit->getCellId();
154  double edep = aCsIHit->getEnergyDep();
156  // Fill histograms
157  h_CrystalRadDoseSH->Fill(m_cellID, eventWeight * edep * edeptodose);
158 
159  // To get the Number of photons per GeV (divide by total edep before plotting)
160  if (Const::photon.getPDGCode() == aCsIHit->getPDGCode()) {
161  h_LightYieldCrystal->Fill(m_cellID, eventWeight);
162  }
163  }
164  }
165 
166  //Loop over Digihits
167  if (m_digihits.getEntries() > 0) {
168  int hitNum = m_digihits.getEntries();
171  for (int i = 0; i < hitNum; i++) {
172  CsiDigiHit* aCsIDigiHit = m_digihits[i];
173 
174  int cellID = aCsIDigiHit->getCellId();
175  // cast to double before taking the log
176  double charge = (double) aCsIDigiHit->getCharge();
177  double height = (double) aCsIDigiHit->getMaxVal();
178  double baseline = (double) aCsIDigiHit->getBaseline();
179  double trueEdep = aCsIDigiHit->getTrueEdep();
180  vector<uint16_t>* waveform = aCsIDigiHit->getWaveform();
181  vector<uint8_t>* status = aCsIDigiHit->getStatusBits();
182 
183  h_TrueEdep->Fill(log10(trueEdep), eventWeight);
184  h_Charge->Fill(log10(charge - baseline), eventWeight);
185  h_Height->Fill(log10(height), eventWeight);
186 
187  // Write the wavforms. All have the same number in the TFile, but
188  // we can access them by their cycle number:
189  // root[] TFile *f1 = new TFile("filename.root")
190  // root[] TH1F *Waveform4; f1->GetObject("h_Waveform;4",Waveform4);
191 
192  if (waveform->size()) {
193  h_Gate->Reset();
194  h_Waveform->Reset();
195 
196  char histoTitle[80];
197  sprintf(histoTitle, "Waveform Hit No. %i, Cell No %i", i, cellID);
198  h_Waveform->SetTitle(histoTitle);
199 
200 
201  for (uint iBin = 0; iBin < waveform->size(); iBin++) {
202  bool* statusBits = readDPPStatusBits(status->at(iBin));
203  h_Gate->Fill(iBin + 1, (int) statusBits[1]);
204  h_Waveform->Fill(iBin + 1, waveform->at(iBin));
205  }
206 
207  fWF->cd();
208  h_Gate->Write();
209  h_Waveform->Write();
210  }
211  }
212  }
213 }
214 
215 
216 void CsIStudyModule::terminate()
217 {
218  fWF->Close();
219 }
220 
221 
222 bool* CsIStudyModule::readDPPStatusBits(char data)
223 {
224 
225  bool* bit = new bool[8];
226 
227  // fill data
228  for (int i = 0; i < 8; i++) {
229  bit[i] = ((data >> i) & 0x01);
230  }
231 
232  return bit;
233 }
Analyze simulations of CsI readings in BEAST.
Class to store Csi digitized hits (output of CsIDigitizer) relation to CsiHit filled in beast/csi/mod...
Definition: CsiDigiHit.h:26
double getTrueEdep() const
Get true deposited energy.
Definition: CsiDigiHit.h:77
std::vector< uint8_t > * getStatusBits()
Get Status bits array.
Definition: CsiDigiHit.h:114
uint16_t getBaseline() const
Get Baseline.
Definition: CsiDigiHit.h:87
uint32_t getCharge() const
Get Integrated Charge.
Definition: CsiDigiHit.h:67
std::vector< uint16_t > * getWaveform()
Get Waveform array.
Definition: CsiDigiHit.h:105
uint8_t getCellId() const
Get Cell ID.
Definition: CsiDigiHit.h:61
uint16_t getMaxVal() const
Get Maximal Value.
Definition: CsiDigiHit.h:95
Class to store simulated hits which equate to average of CsiSimHit on crystals Input for digitization...
Definition: CsiHit.h:26
int getCellId() const
Get Cell ID.
Definition: CsiHit.h:73
double getEnergyDep() const
Get deposit energy.
Definition: CsiHit.h:78
ClassCsiSimHit - Geant4 simulated hits in CsI crystals in BEAST.
Definition: CsiSimHit.h:31
int getPDGCode() const
Get Particle PDG (can be one of secondaries)
Definition: CsiSimHit.h:97
int getCellId() const
Get Cell ID.
Definition: CsiSimHit.h:87
double getEnergyDep() const
Get Deposit energy.
Definition: CsiSimHit.h:107
HistoModule.h is supposed to be used instead of Module.h for the modules with histogram definitions t...
Definition: HistoModule.h:29
Type-safe access to single objects in the data store.
Definition: StoreObjPtr.h:95
#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.