BmnRoot
Loading...
Searching...
No Matches
CbmStsDigitizeTb.cxx
Go to the documentation of this file.
1
6// Includes from ROOT
7#include "TClonesArray.h"
8#include "TF1.h"
9#include "TGeoManager.h"
10#include "TGeoNode.h"
11#include "TMath.h"
12#include "TObjArray.h"
13#include "TRandom3.h"
14
15// Includes from base
16#include "CbmDaqBuffer.h"
17#include "CbmMCBuffer.h"
18#include "FairRootManager.h"
19#include "FairRunAna.h"
20#include "FairRuntimeDb.h"
21
22// Includes from STS
23#include "CbmGeoStsPar.h"
24#include "CbmStsDigiLight.h"
25#include "CbmStsDigiMatch.h"
26#include "CbmStsDigiPar.h"
27#include "CbmStsDigiScheme.h"
28#include "CbmStsDigitizeTb.h"
29#include "CbmStsPoint.h"
30#include "CbmStsSector.h"
31#include "CbmStsSensor.h"
32#include "CbmStsStation.h"
33
34#include <iomanip>
35#include <iostream>
36#include <map>
37#include <vector>
38using std::cerr;
39using std::cout;
40using std::endl;
41using std::fixed;
42using std::flush;
43using std::ios_base;
44using std::left;
45using std::map;
46using std::pair;
47using std::right;
48using std::set;
49using std::setprecision;
50using std::setw;
51using std::vector;
52
53// ----- Default constructor ------------------------------------------
55 : FairTask("StsDigitize", 1)
56 , fGeoPar(NULL)
57 , fDigiPar(NULL)
58 , fDigiScheme(NULL)
59 , fPoints(NULL)
60 , fDigis(NULL)
61 , fDigiMatches(NULL)
62 , fNDigis(0)
63 , fNMulti(0)
64 , fNEvents(0)
65 , fNPoints(0)
66 , fNOutside(0)
67 , fNDigisFront(0)
68 , fNDigisBack(0)
69 , fTime(0.)
70 , fStepSize(0.)
71 , fTimer()
72 , fRealistic(kFALSE)
73 , fPairCreationEnergy(0.)
74 , fFNoiseWidth(0.1)
75 , fBNoiseWidth(0.1)
76 , fStripDeadTime(10)
77 , fQMax(0.)
78 , fThreshold(0.)
79 , fNAdcBits(0)
80 , fNAdcChannels(0.)
81 , fFChannelPointsMap()
82 , fBChannelPointsMap()
83{
84 // fDigiScheme = new CbmStsDigiScheme();
85 fDigiScheme = CbmStsDigiScheme::Instance();
86 Reset();
87}
88// -------------------------------------------------------------------------
89
90// ----- Destructor ----------------------------------------------------
92{
93 if (fGeoPar)
94 delete fGeoPar;
95 if (fDigiPar)
96 delete fDigiPar;
97 if (fDigis) {
98 fDigis->Delete();
99 delete fDigis;
100 }
101 if (fDigiMatches) {
102 fDigiMatches->Delete();
103 delete fDigiMatches;
104 }
105 // if ( fDigiScheme ) delete fDigiScheme; // M&SG HEPL: Deleting singleton is safe at the end of a whole program
106 // only!
107 Reset();
108}
109// -------------------------------------------------------------------------
110
111// ===== Perform digitisation of one StsPoint ==========================
112void CbmStsDigitizeTb::DigitizePoint(const CbmStsPoint* point, Int_t& nFront, Int_t& nBack)
113{
114
115 // Reset counters
116 nFront = nBack = 0;
117
118 // Get corresponding node from GeoManager
119 Double_t xPoint = 0.5 * (point->GetXOut() + point->GetXIn());
120 Double_t yPoint = 0.5 * (point->GetYOut() + point->GetYIn());
121 Double_t zPoint = 0.5 * (point->GetZOut() + point->GetZIn());
122 gGeoManager->FindNode(xPoint, yPoint, zPoint);
123 TGeoNode* curNode = gGeoManager->GetCurrentNode();
124
125 // --- Get corresponding sensor from StsDigiScheme
126 CbmStsSensor* sensor = NULL;
127 if (fDigiScheme->IsNewGeometry()) {
128 TString curPath = fDigiScheme->GetCurrentPath();
129 sensor = fDigiScheme->GetSensorByName(curPath);
130 } else
131 sensor = fDigiScheme->GetSensorByName(curNode->GetName());
132 if (!sensor) {
133 LOG(debug) << fName << ": node " << fDigiScheme->GetCurrentPath() << " not found in digi scheme!";
134 LOG(debug2) << "\t" << "MCPoint information:";
135 point->Info();
136 return;
137 }
138
139 // Length of trajectory in the sensor
140 Double_t deltaX = point->GetXOut() - point->GetXIn();
141 Double_t deltaY = point->GetYOut() - point->GetYIn();
142 Double_t deltaZ = point->GetZOut() - point->GetZIn();
143 Double_t lTraj = TMath::Sqrt(deltaX * deltaX + deltaY * deltaY + deltaZ * deltaZ);
144 Int_t nSteps = Int_t(lTraj / fStepSize) + 1;
145
146 // Total and fractional charge
147 Double_t charge = point->GetEnergyLoss() / fPairCreationEnergy;
148 Double_t stepCharge = charge / (nSteps + 1);
149
150 // Arrays for the strip charges
151 map<Int_t, Double_t> frontSignals;
152 map<Int_t, Double_t> backSignals;
153
154 // Produce charge on the sensor strips
155 // The trajectory is decomposed into steps of size fStepSize. The total
156 // energy loss is equally divided into the steps and projected to the
157 // readout strips.
158 Double_t xPair = point->GetXIn();
159 Double_t yPair = point->GetYIn();
160 Double_t zPair = point->GetZIn();
161 for (Int_t iStep = 0; iStep <= nSteps; iStep++) {
162 Int_t iFChan = sensor->GetFrontChannel(xPair, yPair, zPair);
163 frontSignals[iFChan] += stepCharge;
164 Int_t iBChan = sensor->GetBackChannel(xPair, yPair, zPair);
165 backSignals[iBChan] += stepCharge;
166 xPair += deltaX / Double_t(nSteps);
167 yPair += deltaY / Double_t(nSteps);
168 zPair += deltaZ / Double_t(nSteps);
169 }
170
171 // Digitise the charge on the front side strips
172 map<Int_t, Double_t>::iterator it;
173 for (it = frontSignals.begin(); it != frontSignals.end(); it++) {
174 if ((*it).second < fThreshold)
175 continue;
176 Int_t iAdc = -1;
177 if ((*it).second >= fQMax)
178 iAdc = fNAdcChannels - 1;
179 else
180 iAdc = Int_t((*it).second / fQMax) * fNAdcChannels;
181 CbmStsDigiLight* digi = new CbmStsDigiLight(sensor->GetStationNr(), // station number
182 sensor->GetSectorNr(), // sector number
183 0, // front side
184 (*it).first, // channel number
185 iAdc, // ADC channel
186 point->GetTime()); // time
187 CbmDaqBuffer::Instance()->InsertData(digi);
188 nFront++;
189 }
190
191 // Digitise the charge on the back side strips
192 for (it = backSignals.begin(); it != backSignals.end(); it++) {
193 if ((*it).second < fThreshold)
194 continue;
195 Int_t iAdc = -1;
196 if ((*it).second >= fQMax)
197 iAdc = fNAdcChannels - 1;
198 else
199 iAdc = Int_t((*it).second / fQMax) * fNAdcChannels;
200 CbmStsDigiLight* digi = new CbmStsDigiLight(sensor->GetStationNr(), // station number
201 sensor->GetSectorNr(), // sector number
202 1, // back side
203 (*it).first, // channel number
204 iAdc, // ADC channel
205 point->GetTime()); // time
206 CbmDaqBuffer::Instance()->InsertData(digi);
207 nBack++;
208 }
209
210 LOG(debug1) << fName << ": point ( " << fixed << setprecision(4) << xPoint << ", " << yPoint << ", " << zPoint
211 << ") cm, t = " << setprecision(3) << point->GetTime() << " ns, digis: " << nFront << " front, "
212 << nBack << " back";
213}
214// ==========================================================================
215
216// ===== Intialisation (private) =========================================
217InitStatus CbmStsDigitizeTb::Init()
218{
219
220 // Check for presence of MCBuffer and DaqBuffer
221 if (!(CbmMCBuffer::Instance() && CbmDaqBuffer::Instance())) {
222 LOG(fatal) << "No MCBuffer or DaqBuffer present!";
223 return kFATAL;
224 }
225
226 // Pair creation energy in GeV
227 fPairCreationEnergy = 3.68e-9;
228
229 // Number of ADC channels
230 fNAdcChannels = 1 << (fNAdcBits + 1);
231
232 // Step size for ionisation points
233 fStepSize = 0.001;
234
235 // Build digitisation scheme
236 if (!fDigiScheme->Init(fGeoPar, fDigiPar)) {
237 LOG(error) << "Error in building STS digitisation scheme";
238 return kFATAL;
239 }
240
241 if (fVerbose == 1 || fVerbose == 2)
242 fDigiScheme->Print(kFALSE);
243 else if (fVerbose > 2)
244 fDigiScheme->Print(kTRUE);
245 cout << "-I- " << fName << "::Init: "
246 << "STS digitisation scheme succesfully initialised" << endl;
247 cout << " Stations: " << fDigiScheme->GetNStations() << ", Sectors: " << fDigiScheme->GetNSectors()
248 << ", Channels: " << fDigiScheme->GetNChannels() << endl;
249
250 return kSUCCESS;
251}
252// ==========================================================================
253
254// ===== Task execution =================================================
255void CbmStsDigitizeTb::Exec(Option_t* opt)
256{
257
258 fTimer.Start();
259
260 // Counters
261 Int_t nPoints = 0;
262 Int_t nDigiF = 0;
263 Int_t nDigiB = 0;
264 Int_t nDigiAll = 0;
265 Int_t nOut = 0;
266 Double_t tStart = -1.;
267 Double_t tStop = -1.;
268
269 // Loop over StsPoints from MCBuffer
270 const CbmStsPoint* point = dynamic_cast<const CbmStsPoint*>(CbmMCBuffer::Instance()->GetNextPoint(kGEM));
271 while (point) {
272
273 DigitizePoint(point, nDigiF, nDigiB);
274
275 // Increment counters
276 nPoints++;
277 if (!(nDigiF + nDigiB))
278 nOut++; // Points outside active sensors
279 nDigiAll += nDigiF;
280 nDigiAll += nDigiB;
281 fNDigisFront += nDigiF;
282 fNDigisBack += nDigiB;
283 tStop = point->GetTime();
284 if (nPoints == 1)
285 tStart = tStop;
286
287 // Next StsPoint
288 point = dynamic_cast<const CbmStsPoint*>(CbmMCBuffer::Instance()->GetNextPoint(kGEM));
289 }
290
291 fTimer.Stop();
292 LOG(info) << fName << ": " << fixed << setprecision(4) << fTimer.RealTime() << " s, " << nPoints
293 << " points (outside: " << nOut << "), " << nDigiAll << " digis";
294 if (nPoints)
295 LOG(info) << ", time " << setprecision(3) << tStart << " ns to " << tStop << " ns";
296 LOG(info);
297
298 fNEvents += 1.;
299 fTime += fTimer.RealTime();
300 fNPoints += nPoints;
301 fNOutside += nOut;
302}
303// ==========================================================================
304
305// ----- Private method SetParContainers -------------------------------
306void CbmStsDigitizeTb::SetParContainers()
307{
308
309 // Get run and runtime database
310 FairRunAna* run = FairRunAna::Instance();
311 if (!run)
312 Fatal("SetParContainers", "No analysis run");
313
314 FairRuntimeDb* db = run->GetRuntimeDb();
315 if (!db)
316 Fatal("SetParContainers", "No runtime database");
317
318 // Get STS geometry parameter container
319 fGeoPar = (CbmGeoStsPar*)db->getContainer("CbmGeoStsPar");
320
321 // Get STS digitisation parameter container
322 fDigiPar = (CbmStsDigiPar*)db->getContainer("CbmStsDigiPar");
323}
324// -------------------------------------------------------------------------
325
326// ----- Private method ReInit -----------------------------------------
327InitStatus CbmStsDigitizeTb::ReInit()
328{
329
330 // Clear digitisation scheme
331 fDigiScheme->Clear();
332
333 // Build new digitisation scheme
334 if (fDigiScheme->Init(fGeoPar, fDigiPar)) {
335 return kSUCCESS;
336 }
337
338 return kERROR;
339}
340// -------------------------------------------------------------------------
341
342// ----- Private method Reset ------------------------------------------
343void CbmStsDigitizeTb::Reset()
344{
345 // fNPoints = fNOutside = fNDigisFront = fNDigisBack = fTime = 0.;
346 fNDigis = fNMulti = 0;
347 fFChannelPointsMap.clear();
348 fBChannelPointsMap.clear();
349 // if ( fDigis ) fDigis->Clear();
350 // if ( fDigiMatches ) fDigiMatches->Clear();
351 if (fDigis)
352 fDigis->Delete();
353 if (fDigiMatches)
354 fDigiMatches->Delete();
355}
356// -------------------------------------------------------------------------
357
358// ----- Virtual method Finish -----------------------------------------
359void CbmStsDigitizeTb::Finish()
360{
361 Exec(""); // Digitise the remaining points in the MCBuffer
362 fNEvents -= 1; // Correct for extra call to Exec
363 LOG(info);
364 LOG(info) << "============================================================";
365 LOG(info) << "===== " << fName << ": Run summary ";
366 LOG(info) << "===== ";
367 LOG(info) << "===== Events processed : " << setw(8) << fNEvents;
368 // LOG(info).setf(ios_base::fixed, ios_base::floatfield);
369 LOG(info) << "===== Real time per event : " << setw(8) << setprecision(4) << fTime / fNEvents << " s";
370 LOG(info) << "===== StsPoints per event : " << setw(8) << setprecision(2) << fNPoints / fNEvents;
371 LOG(info) << "===== Outside hits per event : " << setw(8) << setprecision(2) << fNOutside / fNEvents << " = "
372 << setw(6) << setprecision(2) << fNOutside / fNPoints * 100. << " %";
373 LOG(info) << "===== Front digis per point : " << setw(8) << setprecision(2)
374 << fNDigisFront / (fNPoints - fNOutside);
375 LOG(info) << "===== Back digis per point : " << setw(8) << setprecision(2)
376 << fNDigisBack / (fNPoints - fNOutside);
377 LOG(info) << "============================================================";
378}
379// -------------------------------------------------------------------------
BmnSsdDigitizeParameters * fDigiPar
Digitisation parameters.
@ kGEM
void Print(Bool_t kLong=kFALSE)
static CbmStsDigiScheme * Instance(int version=1)
Bool_t IsNewGeometry() const
CbmStsSensor * GetSensorByName(TString sensorName)
virtual void Exec(Option_t *opt)
Double_t GetXIn() const
Definition CbmStsPoint.h:69
Double_t GetZOut() const
Definition CbmStsPoint.h:74
Double_t GetYOut() const
Definition CbmStsPoint.h:73
Double_t GetZIn() const
Definition CbmStsPoint.h:71
Double_t GetYIn() const
Definition CbmStsPoint.h:70
Double_t GetXOut() const
Definition CbmStsPoint.h:72
void Info() const
Int_t GetFrontChannel(Double_t x, Double_t y, Double_t z, Double_t &dPitch)
Int_t GetBackChannel(Double_t x, Double_t y, Double_t z, Double_t &dPitch)
Int_t GetSectorNr() const
Int_t GetStationNr() const
-clang-format