VMC Version 2.0
Loading...
Searching...
No Matches
TMCRootManager.cxx
Go to the documentation of this file.
1//------------------------------------------------
2// The Geant4 Virtual Monte Carlo package
3// Copyright (C) 2013 - 2018 Ivana Hrivnacova
4// All rights reserved.
5//
6// For the licensing terms see geant4_vmc/LICENSE.
7// Contact: root-vmc@cern.ch
8//-------------------------------------------------
9
14
15#include "TMCRootManager.h"
16#include "Riostream.h"
17#include "TError.h"
18#include "TFile.h"
19#include "TMCAutoLock.h"
20#include "TThread.h"
21#include "TTree.h"
22
23#include <cstdio>
24
25namespace {
26// Define mutexes per operation which modify shared data
27TMCMutex createMutex = TMCMUTEX_INITIALIZER;
28TMCMutex deleteMutex = TMCMUTEX_INITIALIZER;
29} // namespace
30
31//
32// static data, methods
33//
34
36Bool_t TMCRootManager::fgDebug = false;
38
39//_____________________________________________________________________________
41{
43
44 return fgInstance;
45}
46
47//
48// ctors, dtor
49//
50
51//_____________________________________________________________________________
52TMCRootManager::TMCRootManager(const char *projectName, TMCRootManager::FileMode fileMode, Int_t threadRank)
53 : fFile(0), fTree(0), fIsClosed(false)
54{
59
60 if (fgDebug)
61 printf("TMCRootManager::TMCRootManager %p \n", this);
62
63 // lock mutex
64 TMCAutoLock lk(&createMutex);
65
66 // Set Id
67 fId = fgCounter;
68
69 // Increment counter
70 ++fgCounter;
71
72 // singleton instance
73 if (fgInstance) {
74 Fatal("TMCRootManager", "Attempt to create two instances of singleton.");
75 return;
76 }
77
78 fgInstance = this;
79
80 // open file and create a tree
81 OpenFile(projectName, fileMode, threadRank);
82
83 // unlock mutex
84 lk.unlock();
85
86 if (fgDebug)
87 printf("Done TMCRootManagerMT::TMCRootManagerMT %p \n", this);
88}
89
90//_____________________________________________________________________________
92{
94
95 if (fgDebug)
96 printf("TMCRootManager::~TMCRootManager %p \n", this);
97
98 // lock mutex
99 TMCAutoLock lk(&deleteMutex);
100
101 if (fFile && !fIsClosed)
102 fFile->Close();
103 delete fFile;
104
105 --fgCounter;
106
107 // unlock mutex
108 lk.unlock();
109
110 if (fgDebug)
111 printf("Done TMCRootManager::~TMCRootManager %p \n", this);
112}
113
114//
115// privatemethods
116//
117
118//_____________________________________________________________________________
119void TMCRootManager::OpenFile(const char *projectName, FileMode fileMode, Int_t threadRank)
120{
121 TString fileName(projectName);
122 if (threadRank >= 0) {
123 fileName += "_";
124 fileName += threadRank;
125 }
126 fileName += ".root";
127
128 TString treeTitle(projectName);
129 treeTitle += " tree";
130
131 switch (fileMode) {
133 fFile = new TFile(fileName);
134 fTree = (TTree *)fFile->Get(projectName);
135 break;
136
138 if (fgDebug)
139 printf("Going to create Root file \n");
140 fFile = new TFile(fileName, "recreate");
141 if (fgDebug)
142 printf("Done: file %p \n", fFile);
143
144 if (fgDebug)
145 printf("Going to create TTree \n");
146 fTree = new TTree(projectName, treeTitle);
147 if (fgDebug)
148 printf("Done: TTree %p \n", fTree);
149 ;
150 ;
151 }
152}
153
154//
155// public methods
156//
157
158//_____________________________________________________________________________
159void TMCRootManager::Register(const char *name, const char *className, void *objAddress)
160{
165
166 fFile->cd();
167 if (!fTree->GetBranch(name))
168 fTree->Branch(name, className, objAddress, 32000, 99);
169 else
170 fTree->GetBranch(name)->SetAddress(objAddress);
171}
172
173//_____________________________________________________________________________
174void TMCRootManager::Register(const char *name, const char *className, const void *objAddress)
175{
180
181 Register(name, className, const_cast<void *>(objAddress));
182}
183
184//_____________________________________________________________________________
186{
188
189 fFile->cd();
190 fTree->Fill();
191}
192
193//_____________________________________________________________________________
195{
197
198 fFile->cd();
199 fFile->Write();
200}
201
202//_____________________________________________________________________________
204{
206
207 if (fIsClosed) {
208 Error("Close", "The file was already closed.");
209 return;
210 }
211
212 fFile->cd();
213 fFile->Close();
214 fIsClosed = true;
215}
216
217//_____________________________________________________________________________
219{
221
222 WriteAll();
223 Close();
224}
225
226//_____________________________________________________________________________
228{
231
232 fTree->GetEntry(i);
233}
std::mutex TMCMutex
Definition: TMCAutoLock.h:310
#define TMCMUTEX_INITIALIZER
Definition: TMCAutoLock.h:319
Definition of the TMCRootManager class.
The Root IO manager for VMC examples for both sequential and multi-threaded applications.
void OpenFile(const char *projectName, FileMode fileMode, Int_t threadRank)
void ReadEvent(Int_t i)
static TMCThreadLocal TMCRootManager * fgInstance
FileMode
Root file mode.
static Bool_t fgDebug
void Register(const char *name, const char *className, void *objAddress)
virtual ~TMCRootManager()
static Int_t fgCounter
static TMCRootManager * Instance()
TMCRootManager(const char *projectName, FileMode fileMode=kWrite, Int_t threadRank=-1)