ATTPCROOT  0.3.0-alpha
A ROOT-based framework for analyzing data from active target detectors
AtApolloDigitizer.cxx
Go to the documentation of this file.
1 /********************************************************************************
2  * Copyright (C) 2014 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
3  * *
4  * This software is distributed under the terms of the *
5  * GNU Lesser General Public Licence version 3 (LGPL) version 3, *
6  * copied verbatim in the file "LICENSE" *
7  ********************************************************************************/
8 
9 #include "AtApolloDigitizer.h"
10 
11 #include "AtApolloCrystalCalData.h"
12 #include "AtApolloPoint.h"
13 
14 #include <FairLogger.h>
15 #include <FairRootManager.h>
16 #include <FairRuntimeDb.h>
17 #include <FairTask.h>
18 
19 #include <TClonesArray.h>
20 #include <TObject.h>
21 #include <TRandom.h>
22 
23 #include <algorithm>
24 #include <cmath>
25 #include <iostream>
26 #include <vector>
27 
28 using std::cerr;
29 using std::cout;
30 using std::endl;
31 
33  : FairTask("ATTPC APOLLO Digitizer"), fApolloPointDataCA(nullptr), fApolloCryCalDataCA("AtApolloCrystalCalData", 5)
34 {
35 }
36 
38 {
39  LOG(INFO) << "AtApolloDigitizer: Delete instance";
40 
41  if (fApolloPointDataCA) {
42  fApolloPointDataCA->Delete();
43  delete fApolloPointDataCA;
44  }
45  fApolloCryCalDataCA.Delete();
46 }
47 
49 {
50 
51  FairRuntimeDb *rtdb = FairRuntimeDb::instance();
52  if (!rtdb) {
53  LOG(ERROR) << "AtApolloDigitizer:: FairRuntimeDb not opened!";
54  }
55 }
56 
57 void AtApolloDigitizer::SetParameter() {}
58 
60 {
61  LOG(INFO) << "AtApolloDigitizer::Init ";
62 
63  FairRootManager *rootManager = FairRootManager::Instance();
64  if (!rootManager) {
65  LOG(fatal) << "Init: No FairRootManager";
66  return kFATAL;
67  }
68 
69  fApolloPointDataCA = dynamic_cast<TClonesArray *>(rootManager->GetObject("AtApolloPoint"));
70  if (!fApolloPointDataCA) {
71  LOG(fatal) << "Init: No AtApolloPoint CA";
72  return kFATAL;
73  }
74 
75  rootManager->Register("ApolloCrystalCalData", "CALIFA Crystal Cal", &fApolloCryCalDataCA, kTRUE);
76 
77  SetParameter();
78  return kSUCCESS;
79 }
80 
81 void AtApolloDigitizer::Exec(Option_t *option)
82 {
83  // Reset entries in output arrays, local arrays
84  Reset();
85 
86  // Reading the Input -- Point data --
87  Int_t nHits = fApolloPointDataCA->GetEntries();
88  if (!nHits)
89  return;
90 
91  std::vector<AtApolloPoint *> pointData;
92  // AtApolloPoint **pointData = nullptr;
93  // pointData = new AtApolloPoint *[nHits];
94  for (Int_t i = 0; i < nHits; i++)
95  pointData.push_back(dynamic_cast<AtApolloPoint *>(fApolloPointDataCA->At(i)));
96 
97  Int_t crystalId;
98  Double_t time;
99  Double_t energy;
100 
101  for (Int_t i = 0; i < nHits; i++) {
102  crystalId = pointData[i]->GetCrystalID();
103  time = pointData[i]->GetTime();
104  energy = pointData[i]->GetEnergyLoss();
105 
106  Int_t nCrystalCals = fApolloCryCalDataCA.GetEntriesFast();
107  Bool_t existHit = false;
108  if (nCrystalCals == 0)
109  AddCrystalCal(crystalId, NUSmearing(energy), time);
110  else {
111  for (Int_t j = 0; j < nCrystalCals; j++) {
112  if ((dynamic_cast<AtApolloCrystalCalData *>(fApolloCryCalDataCA.At(j)))->GetCrystalId() == crystalId) {
113  (dynamic_cast<AtApolloCrystalCalData *>(fApolloCryCalDataCA.At(j)))->AddMoreEnergy(NUSmearing(energy));
114  if ((dynamic_cast<AtApolloCrystalCalData *>(fApolloCryCalDataCA.At(j)))->GetTime() > time) {
115  (dynamic_cast<AtApolloCrystalCalData *>(fApolloCryCalDataCA.At(j)))->SetTime(time);
116  }
117  existHit = true; // to avoid the creation of a new CrystalHit
118 
119  break;
120  }
121  }
122  if (!existHit)
123  AddCrystalCal(crystalId, NUSmearing(energy), time);
124  }
125  }
126 
127  Int_t nCrystalCals = fApolloCryCalDataCA.GetEntriesFast();
128  if (nCrystalCals == 0)
129  return;
130 
131  Double_t temp = 0;
132  Int_t tempCryID, parThres;
133  Bool_t inUse;
134  Int_t tempIndex;
135  Float_t parReso;
136 
137  for (Int_t i = 0; i < nCrystalCals; i++) {
138 
139  tempCryID = (dynamic_cast<AtApolloCrystalCalData *>(fApolloCryCalDataCA.At(i)))->GetCrystalId();
140 
141  temp = (dynamic_cast<AtApolloCrystalCalData *>(fApolloCryCalDataCA.At(i)))->GetEnergy();
142  if (temp < fThreshold) {
143  fApolloCryCalDataCA.RemoveAt(i);
144  fApolloCryCalDataCA.Compress();
145  nCrystalCals--; // remove from CalData those below threshold
146  i--;
147  continue;
148  }
149 
150  if (isCsI(tempCryID) && fResolutionCsI > 0)
151  (dynamic_cast<AtApolloCrystalCalData *>(fApolloCryCalDataCA.At(i)))->SetEnergy(ExpResSmearingCsI(temp));
152  if (isLaBr(tempCryID) && fResolutionLaBr > 0)
153  (dynamic_cast<AtApolloCrystalCalData *>(fApolloCryCalDataCA.At(i)))->SetEnergy(ExpResSmearingLaBr(temp));
154  }
155 }
156 
157 // ----- Public method EndOfEvent -----------------------------------------
159 {
160  // fApolloPointDataCA->Clear();
161  // fApolloCryCalDataCA.Clear();
162  ResetParameters();
163 }
164 
166 {
167  FairRootManager::Instance()->Register("CrystalCal", GetName(), &fApolloCryCalDataCA, kTRUE);
168 }
169 
171 {
172  // Clear the CA structure
173  LOG(DEBUG) << "Clearing ApolloCrystalCalData Structure";
174  fApolloCryCalDataCA.Clear();
175 
176  ResetParameters();
177 }
178 
180 
181 void AtApolloDigitizer::SetDetectionThreshold(Double_t thresholdEne)
182 {
183  fThreshold = thresholdEne;
184  LOG(INFO) << "AtApolloDigitizer::SetDetectionThreshold to " << fThreshold << " GeV.";
185 }
186 
187 AtApolloCrystalCalData *AtApolloDigitizer::AddCrystalCal(Int_t ident, Double_t energy, ULong64_t time)
188 {
189  TClonesArray &clref = fApolloCryCalDataCA;
190  Int_t size = clref.GetEntriesFast();
191  if (fVerbose > 1)
192  LOG(INFO) << "-I- AtApolloDigitizer: Adding CrystalCalData "
193  << " with unique identifier " << ident << " entering with " << energy * 1e06 << " keV "
194  << " Time=" << time;
195 
196  return new (clref[size]) AtApolloCrystalCalData(ident, energy, time); // NOLINT
197 }
198 
199 void AtApolloDigitizer::SetExpEnergyRes(Double_t crystalResCsI, Double_t crystalResLaBr)
200 {
201  fResolutionCsI = crystalResCsI;
202  fResolutionLaBr = crystalResLaBr;
203  LOG(INFO) << "AtApolloDigitizer::SetExpEnergyRes to " << fResolutionCsI << "% @ 1 MeV for CsI and "
204  << fResolutionLaBr << "% @ 1 MeV for LaBr.";
205 }
206 
207 Double_t AtApolloDigitizer::NUSmearing(Double_t inputEnergy)
208 {
209  // Very simple preliminary scheme where the NU is introduced as a flat random
210  // distribution with limits fNonUniformity (%) of the energy value.
211  //
212  return gRandom->Uniform(inputEnergy - inputEnergy * fNonUniformity / 100,
213  inputEnergy + inputEnergy * fNonUniformity / 100);
214 }
215 
217 {
218  fNonUniformity = nonU;
219  LOG(INFO) << "AtApolloDigitizer::SetNonUniformity to " << fNonUniformity << " %";
220 }
221 
222 Double_t AtApolloDigitizer::ExpResSmearingCsI(Double_t inputEnergy)
223 {
224  // Smears the energy according to some Experimental Resolution distribution
225  // Very simple scheme where the Experimental Resolution
226  // is introduced as a gaus random distribution with a width given by the
227  // parameter fResolution(in % @ MeV). Scales according to 1/sqrt(E)
228  //
229  // The formula is TF1("name","0.058*x/sqrt(x)",0,10) for 3% at 1MeV (3.687 @
230  // 662keV)
231  // ( % * energy ) / sqrt( energy )
232  // and then the % is given at 1 MeV!!
233  //
234  if (fResolutionCsI == 0)
235  return inputEnergy;
236  else {
237  // Energy in MeV, that is the reason for the factor 1000...
238  Double_t randomIs = gRandom->Gaus(0, inputEnergy * fResolutionCsI * 1000 / (235 * sqrt(inputEnergy * 1000)));
239  return inputEnergy + randomIs / 1000;
240  }
241 }
242 Double_t AtApolloDigitizer::ExpResSmearingLaBr(Double_t inputEnergy)
243 {
244  if (fResolutionLaBr == 0)
245  return inputEnergy;
246  else {
247  // Energy in MeV, that is the reason for the factor 1000...
248  Double_t randomIs = gRandom->Gaus(0, inputEnergy * fResolutionLaBr * 1000 / (235 * sqrt(inputEnergy * 1000)));
249  return inputEnergy + randomIs / 1000;
250  }
251 }
252 
253 Bool_t AtApolloDigitizer::isCsI(Int_t id)
254 {
255  if (id > 6 && id < 27)
256  return kTRUE;
257  else
258  return kFALSE;
259 }
260 
261 Bool_t AtApolloDigitizer::isLaBr(Int_t id)
262 {
263  if (id > 0 && id < 7)
264  return kTRUE;
265  else
266  return kFALSE;
267 }
268 
AtApolloDigitizer::SetNonUniformity
void SetNonUniformity(Double_t nonU)
Definition: AtApolloDigitizer.cxx:216
AtApolloDigitizer::ResetParameters
void ResetParameters()
Definition: AtApolloDigitizer.h:82
AtApolloDigitizer::SetParContainers
virtual void SetParContainers()
Definition: AtApolloDigitizer.cxx:48
AtApolloDigitizer::SetDetectionThreshold
void SetDetectionThreshold(Double_t thresholdEne)
Definition: AtApolloDigitizer.cxx:181
AtApolloDigitizer::~AtApolloDigitizer
~AtApolloDigitizer()
Definition: AtApolloDigitizer.cxx:37
AtApolloPoint.h
AtApolloDigitizer::EndOfEvent
virtual void EndOfEvent()
Definition: AtApolloDigitizer.cxx:158
AtApolloPoint
Definition: AtApolloPoint.h:20
ClassImp
ClassImp(AtApolloDigitizer)
AtApolloDigitizer::Reset
virtual void Reset()
Definition: AtApolloDigitizer.cxx:170
AtApolloCrystalCalData.h
AtApolloDigitizer::SetExpEnergyRes
void SetExpEnergyRes(Double_t crystalResCsI, Double_t crystalResLaBr)
Definition: AtApolloDigitizer.cxx:199
AtApolloDigitizer
Definition: AtApolloDigitizer.h:23
AtApolloDigitizer::AddCrystalCal
AtApolloCrystalCalData * AddCrystalCal(Int_t ident, Double_t energy, ULong64_t time)
Definition: AtApolloDigitizer.cxx:187
AtApolloDigitizer::Exec
virtual void Exec(Option_t *opt)
Definition: AtApolloDigitizer.cxx:81
AtApolloCrystalCalData
Definition: AtApolloCrystalCalData.h:20
AtApolloDigitizer::Init
virtual InitStatus Init()
Definition: AtApolloDigitizer.cxx:59
AtApolloDigitizer.h
AtApolloDigitizer::Register
virtual void Register()
Definition: AtApolloDigitizer.cxx:165
AtApolloDigitizer::FinishEvent
virtual void FinishEvent()
Definition: AtApolloDigitizer.cxx:179
AtApolloDigitizer::AtApolloDigitizer
AtApolloDigitizer()
Definition: AtApolloDigitizer.cxx:32