CbmRoot
CbmMvdPileupManager.cxx
Go to the documentation of this file.
1 // -------------------------------------------------------------------------
2 // ----- CbmMvdPileupManager source file -----
3 // ----- Created 08/11/06 by V. Friese -----
4 // -------------------------------------------------------------------------
5 #include "CbmMvdPileupManager.h"
6 
7 #include "TClonesArray.h"
8 #include "TFile.h"
9 #include "TObjArray.h"
10 #include "TTree.h"
11 
12 #include <iostream>
13 
14 using std::cout;
15 using std::endl;
16 
17 
18 // ----- Default constructor -------------------------------------------
19 CbmMvdPileupManager::CbmMvdPileupManager() : TObject(), fBuffer(NULL) {}
20 // -------------------------------------------------------------------------
21 
22 
23 // ----- Standard constructor ------------------------------------------
25  TString branchName,
26  Int_t nEvents)
27  : TObject(), fBuffer(new TObjArray(nEvents)) {
28  if (!FillBuffer(fileName, branchName, nEvents))
29  Fatal("CbmMvdPileupManager", "Error in filling buffer");
30 }
31 // -------------------------------------------------------------------------
32 
33 
34 // ----- Destructor ----------------------------------------------------
36  fBuffer->Delete();
37  delete fBuffer;
38 }
39 // -------------------------------------------------------------------------
40 
41 
42 // ----- Public method GetEvent ----------------------------------------
43 TClonesArray* CbmMvdPileupManager::GetEvent(Int_t iEvent) {
44 
45  if (!fBuffer) Fatal("CbmMvdPileupManager::GetEvent", "No event buffer!");
46 
47  if (iEvent > fBuffer->GetEntriesFast()) {
48  cout << "-W- CbmMvdPileupManager::GetEvent: Event " << iEvent
49  << " not present in buffer! " << endl;
50  cout << " Returning NULL pointer! "
51  << endl;
52  return NULL;
53  }
54 
55  TClonesArray* pArray = (TClonesArray*) fBuffer->At(iEvent);
56 
57  if (!pArray) {
58  cout << "-W CbmMvdPileupManager::GetEvent: Returning NULL pointer!" << endl;
59  return NULL;
60  }
61 
62  return pArray;
63 }
64 // -------------------------------------------------------------------------
65 
66 
67 // ----- Private method FillBuffer -------------------------------------
68 Int_t CbmMvdPileupManager::FillBuffer(TString fileName,
69  TString branchName,
70  Int_t nEvents) {
71 
72  if (!fBuffer) Fatal("Fill Buffer", "No event buffer!");
73 
74  fBuffer->Delete();
75 
76  TClonesArray* pointArray = NULL;
77  TFile* saveGFile = gFile;
78 
79  TFile* bgfile = new TFile(fileName);
80  if (!bgfile) {
81  cout << "-W- CbmMvdPileupManager::FillBuffer: Background file " << fileName
82  << " could noy be opened! " << endl;
83  return 0;
84  }
85  cout << "-I- CbmMvdPileupManager::FillBuffer: Opening file " << endl;
86  cout << fileName << endl;
87 
88  TTree* bgtree = (TTree*) bgfile->Get("cbmsim");
89  if (!bgtree) {
90  cout << "-W- CbmMvdPileupManager::FillBuffer: "
91  << "Could not find cbmsim tree in background file " << endl;
92  return 0;
93  }
94 
95  Int_t nEventsInFile = bgtree->GetEntries();
96  cout << "-I- CbmMvdPileupManager::FillBuffer: " << nEventsInFile
97  << " events in file" << endl;
98  Int_t nBuffer = TMath::Min(nEvents, nEventsInFile);
99  cout << "-I- CbmMvdPileupManager::FillBuffer: Buffering " << nBuffer
100  << " events" << endl;
101  if (nBuffer < 100)
102  cout << "-W- CbmMvdPileupManager::FillBuffer: "
103  << "Number of events may not be sufficient for appropriate "
104  << "presentation of background pattern!" << endl;
105 
106  bgtree->SetBranchAddress(branchName, &pointArray);
107 
108  for (Int_t iEvent = 0; iEvent < nBuffer; iEvent++) {
109  bgtree->GetEntry(iEvent);
110  fBuffer->AddAt(pointArray->Clone(), iEvent);
111  }
112 
113  delete bgtree;
114  bgfile->Close();
115  delete bgfile;
116  saveGFile->cd();
117 
118  return nBuffer;
119 }
120 // -------------------------------------------------------------------------
121 
122 
CbmMvdPileupManager
Definition: CbmMvdPileupManager.h:28
CbmMvdPileupManager::FillBuffer
Int_t FillBuffer(TString fileName, TString branchName, Int_t nEvents)
Definition: CbmMvdPileupManager.cxx:68
CbmMvdPileupManager::GetEvent
TClonesArray * GetEvent(Int_t iEvent)
Definition: CbmMvdPileupManager.cxx:43
ClassImp
ClassImp(CbmConverterManager) InitStatus CbmConverterManager
Definition: CbmConverterManager.cxx:12
CbmMvdPileupManager::~CbmMvdPileupManager
virtual ~CbmMvdPileupManager()
Definition: CbmMvdPileupManager.cxx:35
CbmMvdPileupManager::fBuffer
TObjArray * fBuffer
Definition: CbmMvdPileupManager.h:60
CbmMvdPileupManager.h
CbmMvdPileupManager::CbmMvdPileupManager
CbmMvdPileupManager()
Definition: CbmMvdPileupManager.cxx:19