CbmRoot
CbmMvdDigitizer.cxx
Go to the documentation of this file.
1 // -------------------------------------------------------------------------
2 // ----- CbmMvdDigitizer source file -----
3 // -------------------------------------------------------------------------
4 
5 // Includes from MVD
6 #include "CbmMvdDigitizer.h"
7 #include "CbmMvdDetector.h"
8 #include "CbmMvdPileupManager.h"
11 #include "tools/CbmMvdGeoHandler.h"
12 
13 #include "CbmMatch.h"
14 #include "CbmMvdDigi.h"
15 #include "CbmMvdPoint.h"
16 
17 // Includes from FAIR
18 #include "FairLogger.h"
19 #include "FairModule.h"
20 #include "FairRootManager.h"
21 
22 // Includes from ROOT
23 #include "TClonesArray.h"
24 #include "TStopwatch.h"
25 
26 // Includes from C++
27 #include <cassert>
28 #include <iomanip>
29 #include <iostream>
30 #include <vector>
31 
32 using std::cout;
33 using std::endl;
34 using std::vector;
35 
36 // ----- Default constructor ------------------------------------------
38  : CbmDigitize<CbmMvdDigi>("MvdDigitize")
39  , fMode(0)
40  , fShowDebugHistos(kFALSE)
41  , fNoiseSensors(kFALSE)
42  , fDetector(nullptr)
43  , fInputPoints(nullptr)
44  , fMcPileUp(nullptr)
45  , fTmpMatch(nullptr)
46  , fTmpDigi(nullptr)
47  , fDigiVect()
48  , fMatchVect()
49  , fPerformanceDigi()
50  , fDigiPluginNr(0)
51  , fFakeRate(-1.)
52  , fNPileup(-1)
53  , fNDeltaElect(-1)
54  , fDeltaBufferSize(-1)
55  , fBgBufferSize(-1)
56  , epsilon()
57  , fInputBranchName("")
58  , fBgFileName("")
59  , fDeltaFileName("")
60  , fTimer()
61  , fPileupManager(nullptr)
62  , fDeltaManager(nullptr) {}
63 // -------------------------------------------------------------------------
64 
65 // ----- Standard constructor ------------------------------------------
67  Int_t iMode,
68  Int_t /*iVerbose*/)
69  : CbmDigitize<CbmMvdDigi>(name)
70  , fMode(iMode)
71  , fShowDebugHistos(kFALSE)
72  , fNoiseSensors(kFALSE)
73  , fDetector(nullptr)
74  , fInputPoints(nullptr)
75  , fMcPileUp(nullptr)
76  , fTmpMatch(nullptr)
77  , fTmpDigi(nullptr)
78  , fDigiVect()
79  , fMatchVect()
80  , fPerformanceDigi()
81  , fDigiPluginNr(0)
82  , fFakeRate(-1.)
83  , fNPileup(0)
84  , fNDeltaElect(0)
85  , fDeltaBufferSize(-1)
86  , fBgBufferSize(-1)
87  , epsilon()
88  , fInputBranchName("MvdPoint")
89  , fBgFileName("")
90  , fDeltaFileName("")
91  , fTimer()
92  , fPileupManager(nullptr)
93  , fDeltaManager(nullptr) {}
94 // -------------------------------------------------------------------------
95 
96 // ----- Destructor ----------------------------------------------------
98 
99  if (fMcPileUp) {
100  fMcPileUp->Delete();
101  delete fMcPileUp;
102  }
103  delete fPileupManager;
104  delete fDeltaManager;
105 }
106 // -----------------------------------------------------------------------------
107 
108 // ----- Exec --------------------------------------------------------------
109 void CbmMvdDigitizer::Exec(Option_t* /*opt*/) {
110  // --- Start timer
111  using namespace std;
112 
113  fTimer.Start();
114  GetEventInfo(); // event number and time
115 
116  BuildEvent();
117  Int_t nPoints = fInputPoints->GetEntriesFast();
118  Int_t nDigis = 0;
119  if (fInputPoints->GetEntriesFast() > 0) {
120  LOG(debug) << "//----------------------------------------//";
121  LOG(debug) << fName << ": Send Input";
123  LOG(debug) << fName << ": Execute DigitizerPlugin Nr. " << fDigiPluginNr;
125  LOG(debug) << fName << ": End Chain";
126 
127  // --- Send produced digis to DAQ
130 
131  Int_t nEntries = fTmpDigi->GetEntriesFast();
132  for (Int_t index = 0; index < nEntries; index++) {
133 
134  CbmMvdDigi* digi = dynamic_cast<CbmMvdDigi*>(fTmpDigi->At(index));
135  CbmMvdDigi* digi1 = new CbmMvdDigi(*digi);
136  assert(digi1);
137  fDigiVect.push_back(digi1);
138 
139  // CbmMatch match{*(dynamic_cast<CbmMatch*>(fTmpMatch->At(index)))};
140  // CbmMatch* match1 = new CbmMatch(match);
141  CbmMatch* match = dynamic_cast<CbmMatch*>(fTmpMatch->At(index));
142  CbmMatch* match1 = new CbmMatch(*match);
143  fMatchVect.push_back(match1);
144 
145  //digi1->SetMatch(match1);
146  SendData(digi1, match1);
147  nDigis++;
148  }
149 
150 
151  /*
152  for (Int_t index = 0; index < fTmpDigi->GetEntriesFast(); index++) {
153 
154  LOG(info) << "Size: " << fTmpDigi->GetEntriesFast() << ", "
155  << fTmpDigi->GetEntries();
156 
157  CbmMvdDigi* digi = dynamic_cast<CbmMvdDigi*>(fTmpDigi->Remove(fTmpDigi->At(index)));
158  digi->Print();
159  fDigiVect.push_back(digi);
160 
161  CbmMatch* match = dynamic_cast<CbmMatch*>(fTmpMatch->Remove(fTmpMatch->At(index)));
162  match->Print();
163  fMatchVect.push_back(match);
164 
165  digi->SetMatch(match);
166  SendDigi(digi);
167  nDigis++;
168  }
169 */
170  // TODO: (VF) There seem to be no entries in the match array, nor matches
171  // attached to the digi object
172  LOG(debug) << fName << ": Sent " << nDigis << " digis to DAQ";
173 
174  //fDigis->AbsorbObjects(fDetector->GetOutputDigis(),0,fDetector->GetOutputArray(fDigiPluginNr)->GetEntriesFast()-1);
175  //LOG(debug) << "Total of " << fDigis->GetEntriesFast() << " digis in this Event";
176  //if(fVerbose) cout << "Start writing DigiMatchs" << endl;
177  //fDigiMatch->AbsorbObjects(fDetector->GetOutputDigiMatchs(),0,fDetector->GetOutputDigiMatchs()->GetEntriesFast()-1);
178  //if(fVerbose) cout << "Total of " << fDigiMatch->GetEntriesFast() << " digisMatch in this Event" << endl;
179  LOG(debug) << "//----------------------------------------//";
180  //LOG(info) << "+ " << setw(20) << GetName() << ": Created: "
181  // << fDigis->GetEntriesFast() << " digis in "
182  // << fixed << setprecision(6) << fTimer.RealTime() << " s";
183  }
184 
185  // --- Event log
186  fTimer.Stop();
187  LOG(info) << "+ " << setw(15) << GetName() << ": Event " << setw(6) << right
188  << fCurrentEvent << " at " << fixed << setprecision(3)
189  << fCurrentEventTime << " ns, points: " << nPoints
190  << ", digis: " << nDigis << ". Exec time " << setprecision(6)
191  << fTimer.RealTime() << " s.";
192 }
193 // -----------------------------------------------------------------------------
194 
195 // ----- Init --------------------------------------------------------------
196 InitStatus CbmMvdDigitizer::Init() {
197  cout << "-I- " << GetName() << ": Initialisation..." << endl;
198  cout << endl;
199  cout << "---------------------------------------------" << endl;
200  cout << "-I- Initialising " << GetName() << " ...." << endl;
201 
202  // ********** RootManager
203  FairRootManager* ioman = FairRootManager::Instance();
204  if (!ioman) {
205  cout << "-E- " << GetName() << "::Init: No FairRootManager!" << endl;
206  return kFATAL;
207  }
208 
209  // ********** Get input arrays
210  fInputPoints = (TClonesArray*) ioman->GetObject("MvdPoint");
211 
212  if (!fInputPoints) {
213  LOG(error) << "No MvdPoint branch found. There was no MVD in the "
214  "simulation. Switch this task off";
215  return kERROR;
216  }
217 
218 
219  // ********** Register output arrays
220  RegisterOutput();
221  fMcPileUp = new TClonesArray("CbmMvdPoint", 10000);
222  ioman->Register("MvdPileUpMC",
223  "Mvd MC Points after Pile Up",
224  fMcPileUp,
225  IsOutputBranchPersistent("MvdPileUpMC"));
226 
228 
229  // ********** Create pileup manager if necessary
230  if (fNPileup >= 1 && !(fPileupManager) && fMode == 0) {
231  if (fBgFileName == "") {
232  cout << "-E- " << GetName() << "::Init: Pileup events needed, but no "
233  << " background file name given! " << endl;
234  return kERROR;
235  }
238  if (fPileupManager->GetNEvents() < 2 * fNPileup) {
239  cout << "-E- " << GetName()
240  << ": The size of your BG-File is insufficient to perform the "
241  "requested pileup"
242  << endl;
243  cout << " You need at least events > 2* fNPileup." << endl;
244  cout << " Detected: fPileUp = " << fNPileup << ", events in file "
245  << fPileupManager->GetNEvents() << endl;
246  Fatal(GetName(), "The size of your BG-File is insufficient");
247  return kERROR;
248  }
249  }
250 
251  // ********** Create delta electron manager if required
252  if (fNDeltaElect >= 1 && !(fDeltaManager) && fMode == 0) {
253  if (fDeltaFileName == "") {
254  cout << "-E- " << GetName() << "::Init: Pileup events needed, but no "
255  << " background file name given! " << endl;
256  return kERROR;
257  }
260  if (fDeltaManager->GetNEvents() < 2 * fNDeltaElect) {
261  cout << "-E- " << GetName()
262  << ": The size of your Delta-File is insufficient to perform the "
263  "requested pileup"
264  << endl;
265  cout << " You need at least events > 2* fNDeltaElect." << endl;
266  cout << " Detected: fNDeltaElect = " << fNDeltaElect
267  << ", events in file " << fDeltaManager->GetNEvents() << endl;
268  Fatal(GetName(), "The size of your Delta-File is insufficient");
269  return kERROR;
270  }
271  }
273 
274  fDetector->AddPlugin(digiTask);
277  fDetector->Init();
278 
280 
281  // Screen output
282  cout << GetName() << " initialised with parameters: " << endl;
283  //PrintParameters();
284  cout << "---------------------------------------------" << endl;
285 
286 
287  return kSUCCESS;
288 }
289 
290 // ----- Virtual public method Reinit ----------------------------------
291 InitStatus CbmMvdDigitizer::ReInit() { return kSUCCESS; }
292 // -------------------------------------------------------------------------
293 
294 
295 // ----- Virtual method Finish -----------------------------------------
297  // cout<< endl << "finishing" << endl;
298  fDetector->Finish();
299  PrintParameters();
300 }
301 // -------------------------------------------------------------------------
302 
303 
304 // ----- Private method Reset ------------------------------------------
306 // -------------------------------------------------------------------------
307 
308 
309 // ----- Reset output arrays -------------------------------------------
311  if (fMcPileUp) fMcPileUp->Delete();
312  if (fTmpMatch) fTmpMatch->Delete();
313  if (fTmpDigi) fTmpDigi->Delete();
314  fDigiVect.clear();
315  fMatchVect.clear();
316 }
317 // -------------------------------------------------------------------------
318 
319 
320 // ----- Private method GetMvdGeometry ---------------------------------
322 // -------------------------------------------------------------------------
323 
324 
325 // ----- Private method PrintParameters --------------------------------
327 
328  using namespace std;
329 
330  cout.setf(ios_base::fixed, ios_base::floatfield);
331  cout << "============================================================"
332  << endl;
333  cout << "============== Parameters MvdDigitizer ====================="
334  << endl;
335  cout << "============================================================"
336  << endl;
337  cout << "=============== End Task ==================================="
338  << endl;
339 }
340 // -------------------------------------------------------------------------
341 
342 // --------------- Build Event ------------------------------------------------------
344 
345  // Some frequently used variables
346  CbmMvdPoint* point = nullptr;
347  Int_t nOrig = 0;
348  Int_t nPile = 0;
349  Int_t nElec = 0;
350 
351  // ----- First treat standard input file
352  nOrig = fInputPoints->GetEntriesFast();
353 
354 
355  // ----- Then treat event pileup
356  if (fNPileup > 0) {
357 
358  // --- Vector of available background events from pile-up.
359  // --- Each event is used only once.
360  Int_t nBuffer = fPileupManager->GetNEvents();
361  vector<Int_t> freeEvents(nBuffer);
362  for (Int_t i = 0; i < nBuffer; i++)
363  freeEvents[i] = i;
364 
365  // --- Loop over pile-up events
366  for (Int_t iBg = 0; iBg < fNPileup; iBg++) {
367 
368  // Select random event from vector and remove it after usage
369  Int_t index = gRandom->Integer(freeEvents.size());
370 
371  Int_t iEvent = freeEvents[index];
372  TClonesArray* points = fPileupManager->GetEvent(iEvent);
373  freeEvents.erase(freeEvents.begin() + index);
374 
375  // Add points from this event to the input arrays
376  for (Int_t iPoint = 0; iPoint < points->GetEntriesFast(); iPoint++) {
377  point = (CbmMvdPoint*) points->At(iPoint);
378  point->SetTrackID(-2);
379  nPile++;
380  new ((*fInputPoints)[fInputPoints->GetEntriesFast()])
381  CbmMvdPoint(*((CbmMvdPoint*) points->At(iPoint)));
382  }
383 
384 
385  } // Pileup event loop
386 
387  } // Usage of pile-up
388 
389 
390  // ----- Finally, treat delta electrons
391  if (fNDeltaElect > 0) {
392 
393  // --- Vector of available delta events.
394  // --- Each event is used only once.
395  Int_t nDeltaBuffer = fDeltaManager->GetNEvents();
396  vector<Int_t> freeDeltaEvents(nDeltaBuffer);
397  for (Int_t i = 0; i < nDeltaBuffer; i++)
398  freeDeltaEvents[i] = i;
399 
400  // --- Loop over delta events
401  for (Int_t it = 0; it < fNDeltaElect; it++) {
402 
403  // Select random event from vector and remove it after usage
404  Int_t indexD = gRandom->Integer(freeDeltaEvents.size());
405  Int_t iEventD = freeDeltaEvents[indexD];
406  TClonesArray* pointsD = fDeltaManager->GetEvent(iEventD);
407  freeDeltaEvents.erase(freeDeltaEvents.begin() + indexD);
408 
409  // Add points from this event to the input arrays
410  for (Int_t iPoint = 0; iPoint < pointsD->GetEntriesFast(); iPoint++) {
411  point = (CbmMvdPoint*) pointsD->At(iPoint);
412  point->SetTrackID(-3); // Mark the points as delta electron
413  new ((*fInputPoints)[fInputPoints->GetEntriesFast()])
414  CbmMvdPoint(*((CbmMvdPoint*) pointsD->At(iPoint)));
415  nElec++;
416  }
417 
418 
419  } // Delta electron event loop
420 
421  } // Usage of delta
422 
423  for (Int_t i = 0; i < fInputPoints->GetEntriesFast(); i++)
424  new ((*fMcPileUp)[i]) CbmMvdPoint(*((CbmMvdPoint*) fInputPoints->At(i)));
425 
426 
427  // ----- At last: Screen output
428  LOG(debug) << "+ " << GetName() << "::BuildEvent: original " << nOrig
429  << ", pileup " << nPile << ", delta " << nElec << ", total "
430  << nOrig + nPile + nElec << " MvdPoints";
431 }
432 // -----------------------------------------------------------------------------
433 
434 
CbmMvdDetector::Instance
static CbmMvdDetector * Instance()
Definition: CbmMvdDetector.cxx:47
CbmMvdDetector.h
CbmMatch
Definition: CbmMatch.h:22
CbmMvdDetector::Exec
void Exec(UInt_t nLevel)
Definition: CbmMvdDetector.cxx:527
CbmMvdDetector::GetPluginArraySize
UInt_t GetPluginArraySize()
Definition: CbmMvdDetector.h:71
CbmMvdDigitizer::fNoiseSensors
Bool_t fNoiseSensors
Definition: CbmMvdDigitizer.h:76
CbmDigitizeBase::GetEventInfo
void GetEventInfo()
Get event information.
Definition: CbmDigitizeBase.cxx:48
CbmMvdPileupManager
Definition: CbmMvdPileupManager.h:28
CbmMvdDigi.h
CbmMvdDigitizer::fPileupManager
CbmMvdPileupManager * fPileupManager
Definition: CbmMvdDigitizer.h:109
CbmMvdDigitizer::PrintParameters
void PrintParameters()
Definition: CbmMvdDigitizer.cxx:326
CbmMvdDigitizer::CbmMvdDigitizer
CbmMvdDigitizer()
Definition: CbmMvdDigitizer.cxx:37
CbmMvdDetector::Init
void Init()
Definition: CbmMvdDetector.cxx:278
CbmMvdDetector::Finish
void Finish()
Definition: CbmMvdDetector.cxx:705
i
int i
Definition: L1/vectors/P4_F32vec4.h:25
CbmMvdDetector::SendInput
void SendInput(TClonesArray *input)
Definition: CbmMvdDetector.cxx:338
CbmMvdDetector::GetOutputDigiMatchs
TClonesArray * GetOutputDigiMatchs()
Definition: CbmMvdDetector.cxx:641
CbmMvdDigitizer::Init
virtual InitStatus Init()
Definition: CbmMvdDigitizer.cxx:196
CbmMvdDigitizer::fShowDebugHistos
Bool_t fShowDebugHistos
Definition: CbmMvdDigitizer.h:75
CbmMvdDigitizer::fMcPileUp
TClonesArray * fMcPileUp
Definition: CbmMvdDigitizer.h:81
CbmMvdDigitizer::Exec
void Exec(Option_t *opt)
Definition: CbmMvdDigitizer.cxx:109
CbmMvdSensorDigitizerTask
Definition: CbmMvdSensorDigitizerTask.h:47
CbmMvdDigitizer::ResetArrays
virtual void ResetArrays()
Clear data arrays.
Definition: CbmMvdDigitizer.cxx:310
CbmMvdDigitizer::fDetector
CbmMvdDetector * fDetector
Definition: CbmMvdDigitizer.h:78
CbmMatch.h
CbmMvdGeoHandler.h
Helper class to extract information from the GeoManager. Addapted from TrdGeoHandler byFlorian Uhlig ...
CbmMvdDetector::ShowDebugHistos
void ShowDebugHistos()
Definition: CbmMvdDetector.cxx:313
CbmMvdDigitizer.h
CbmMvdDigitizer::fTimer
TStopwatch fTimer
ROOT timer.
Definition: CbmMvdDigitizer.h:106
CbmMvdSensorDigitizerTask.h
CbmMvdPoint.h
CbmMvdPoint
Definition: CbmMvdPoint.h:28
CbmMvdDigitizer::fInputBranchName
TString fInputBranchName
Definition: CbmMvdDigitizer.h:101
CbmDigitize
Base class template for CBM digitisation tasks.
Definition: CbmDigitize.h:39
CbmMvdDigitizer::fDeltaBufferSize
Int_t fDeltaBufferSize
Definition: CbmMvdDigitizer.h:97
CbmMvdDigitizer::fNDeltaElect
Int_t fNDeltaElect
Definition: CbmMvdDigitizer.h:96
CbmDigitizeBase::fCurrentEvent
Int_t fCurrentEvent
Number of current input.
Definition: CbmDigitizeBase.h:163
CbmMvdDigitizer::fBgFileName
TString fBgFileName
Definition: CbmMvdDigitizer.h:102
CbmMvdDigitizer::GetMvdGeometry
void GetMvdGeometry()
Definition: CbmMvdDigitizer.cxx:321
CbmMvdDigitizer::BuildEvent
void BuildEvent()
Definition: CbmMvdDigitizer.cxx:343
CbmMvdDetector::SetProduceNoise
void SetProduceNoise()
Definition: CbmMvdDetector.cxx:326
CbmMvdPileupManager::GetEvent
TClonesArray * GetEvent(Int_t iEvent)
Definition: CbmMvdPileupManager.cxx:43
CbmDigitizeBase::fCurrentEventTime
Double_t fCurrentEventTime
Number of current MC entry.
Definition: CbmDigitizeBase.h:165
CbmMvdDigitizer::ReInit
virtual InitStatus ReInit()
Definition: CbmMvdDigitizer.cxx:291
CbmMvdDigitizer::Finish
virtual void Finish()
Definition: CbmMvdDigitizer.cxx:296
CbmDigitize< CbmMvdDigi >::SendData
void SendData(CbmMvdDigi *digi, CbmMatch *match=nullptr)
Send a digi and the corresponding match object to the DAQ.
Definition: CbmDigitize.h:219
CbmMvdDigitizer::fInputPoints
TClonesArray * fInputPoints
Definition: CbmMvdDigitizer.h:80
CbmMvdDigitizer::~CbmMvdDigitizer
~CbmMvdDigitizer()
Definition: CbmMvdDigitizer.cxx:97
CbmMvdPileupManager::GetNEvents
Int_t GetNEvents()
Definition: CbmMvdPileupManager.h:50
points
TClonesArray * points
Definition: Analyze_matching.h:18
CbmMvdDigitizer::fDigiPluginNr
UInt_t fDigiPluginNr
Definition: CbmMvdDigitizer.h:92
CbmMvdDigitizer::fNPileup
Int_t fNPileup
Definition: CbmMvdDigitizer.h:95
CbmMvdDigitizer::fMode
Int_t fMode
Definition: CbmMvdDigitizer.h:73
CbmMvdDigitizer
Definition: CbmMvdDigitizer.h:26
CbmMvdDigitizer::Reset
void Reset()
Definition: CbmMvdDigitizer.cxx:305
CbmMvdPileupManager.h
CbmMvdDigitizer::fMatchVect
std::vector< CbmMatch * > fMatchVect
Temporary storage for CbmDaq.
Definition: CbmMvdDigitizer.h:88
CbmMvdDigi
Definition: CbmMvdDigi.h:21
CbmMvdDigitizer::fDeltaFileName
TString fDeltaFileName
Definition: CbmMvdDigitizer.h:103
CbmMvdDigitizer::fTmpMatch
TClonesArray * fTmpMatch
Definition: CbmMvdDigitizer.h:84
CbmMvdMimosa26AHR.h
CbmDigitize< CbmMvdDigi >::RegisterOutput
void RegisterOutput()
Register the output arrays.
Definition: CbmDigitize.h:175
CbmMvdDigitizer::fDeltaManager
CbmMvdPileupManager * fDeltaManager
Definition: CbmMvdDigitizer.h:110
CbmMvdDetector::AddPlugin
void AddPlugin(CbmMvdSensorPlugin *plugin)
Definition: CbmMvdDetector.cxx:186
CbmMvdDetector::GetOutputDigis
TClonesArray * GetOutputDigis()
Definition: CbmMvdDetector.cxx:619
CbmMvdDigitizer::fBgBufferSize
Int_t fBgBufferSize
Definition: CbmMvdDigitizer.h:98
CbmMvdDigitizer::fTmpDigi
TClonesArray * fTmpDigi
Temporary TClonesArray to absorb from MvdDetector.
Definition: CbmMvdDigitizer.h:85
CbmMvdDigitizer::fDigiVect
std::vector< CbmMvdDigi * > fDigiVect
Temporary TClonesArray to absorb from MvdDetector.
Definition: CbmMvdDigitizer.h:87
ClassImp
ClassImp(CbmMvdDigitizer)