VMC Version 2.2
Loading...
Searching...
No Matches
TMCRootManager.h
Go to the documentation of this file.
1#ifndef ROOT_TMCRootManager
2#define ROOT_TMCRootManager
3
4//------------------------------------------------
5// The Geant4 Virtual Monte Carlo package
6// Copyright (C) 2013 - 2018 Ivana Hrivnacova
7// All rights reserved.
8//
9// For the licensing terms see geant4_vmc/LICENSE.
10// Contact: root-vmc@cern.ch
11//-------------------------------------------------
12
17
18#include "TMCtls.h"
19#include <Rtypes.h>
20
21#include "TFile.h"
22#include "TTree.h"
23
24class TParticle;
25
26#if (ROOT_VERSION_CODE >= ROOT_VERSION(6, 38, 0))
27#include <ROOT/REntry.hxx>
28#include <ROOT/RField.hxx>
29#include <ROOT/RNTuple.hxx>
30#include <ROOT/RNTupleFillStatus.hxx>
31#include <ROOT/RNTupleModel.hxx>
32#include <ROOT/RNTupleParallelWriter.hxx>
33#include <ROOT/RNTupleWriter.hxx>
34
35using ROOT::RNTupleParallelWriter;
36
37using ROOT::RNTupleFillStatus;
38using REntry = ROOT::REntry;
39using RNTupleModel = ROOT::RNTupleModel;
40using RNTupleWriter = ROOT::RNTupleWriter;
41using RNTupleFillContext = ROOT::RNTupleFillContext;
42using RNTParaWriter = ROOT::RNTupleParallelWriter;
43#endif
44
45
51
53public:
55 enum FileMode {
56 kRead, // Read mode
57 kWrite // Write mode
58 };
60 kTTree, // TTree storage
61 kRNTuple // RNTuple storage
62 };
63
64public:
65 // static access method
66 static TMCRootManager *Instance();
67
68 // static method for activating debug mode
69 static void SetDebug(Bool_t debug);
70 static Bool_t GetDebug();
71
72 TMCRootManager(const char *projectName, FileMode fileMode = kWrite, Int_t threadRank = -1);
73 TMCRootManager(const char *projectName, StorageMode storageMode, FileMode fileMode = kWrite, Int_t threadRank = -1);
74#if (ROOT_VERSION_CODE >= ROOT_VERSION(6, 38, 0))
75 TMCRootManager(std::shared_ptr<RNTParaWriter> sharedWriter);
76#endif
77 virtual ~TMCRootManager();
78
79 // methods
80 template <typename T>
81 void Register(const char *name, T *&obj);
82 void Register(const char *name, const char *className, void *objAddress);
83 void Register(const char *name, const char *className, const void *objAddress);
84 void Fill();
85 void WriteAll();
86 void Close();
87 void WriteAndClose();
88 void ReadEvent(Int_t i);
89
90#if (ROOT_VERSION_CODE >= ROOT_VERSION(6, 38, 0))
91 void CreateRNTuple(bool parallelMode = false, bool workerMode = false);
92 std::shared_ptr<RNTParaWriter> GetParallelRNTupleWriter() { return fParaWriter; }
93#endif
94
95private:
96 // not implemented
99
100 // global static data members
101 static Int_t fgCounter; // The counter of instances
102 // static data members
103 static Bool_t fgDebug; // Option to activate debug printings
104
105#if !defined(__CINT__)
106 static TMCThreadLocal TMCRootManager *fgInstance; // singleton instance
107#else
108 static TMCRootManager *fgInstance; // singleton instance
109#endif
110
111 // Methods
112 void OpenFile(const char *projectName, FileMode fileMode, Int_t threadRank);
113
114 // data members
115 Int_t fId; // This manager ID
116 TFile *fFile{nullptr}; // Root output file
117 TTree *fTree{nullptr}; // Root output tree
118
119#if (ROOT_VERSION_CODE >= ROOT_VERSION(6, 38, 0))
120 std::string fStorageName{};
121 std::vector<std::pair<std::string, void *>> fNameAddress;
122 std::unique_ptr<REntry> fEntry;
123 std::unique_ptr<RNTupleModel> fModel;
124 std::unique_ptr<RNTupleWriter> fWriter;
125 std::shared_ptr<RNTParaWriter> fParaWriter;
126 std::shared_ptr<RNTupleFillContext> fFillContext;
127#endif
128
130
131 Bool_t fIsClosed{false}; // Info whether its file was closed
132};
133
134// inline functions
135
136inline void TMCRootManager::SetDebug(Bool_t debug)
137{
138 fgDebug = debug;
139}
140
142{
143 return fgDebug;
144}
145
146template <typename T>
147void TMCRootManager::Register(const char *brname, T *&obj)
148{
149 if (fStorageMode == kTTree) {
150 fFile->cd();
151 if (!fTree->GetBranch(brname))
152 fTree->Branch(brname, &obj, 32000, 99);
153 else
154 fTree->GetBranch(brname)->SetAddress(&obj);
155 }
156#if (ROOT_VERSION_CODE >= ROOT_VERSION(6, 38, 0))
157 if (fStorageMode == kRNTuple) {
158 if (fModel) {
159 fModel->MakeField<T>(brname);
160 }
161 std::string oString;
162 oString = brname;
163 fNameAddress.push_back(std::make_pair(oString, obj));
164 }
165#endif
166}
167
168#endif // ROOT_TMCRootManager
ROOT::REntry REntry
ROOT::RNTupleWriter RNTupleWriter
ROOT::RNTupleParallelWriter RNTParaWriter
ROOT::RNTupleModel RNTupleModel
ROOT::RNTupleFillContext RNTupleFillContext
The Root IO manager for VMC examples for both sequential and multi-threaded applications.
std::unique_ptr< RNTupleModel > fModel
void OpenFile(const char *projectName, FileMode fileMode, Int_t threadRank)
TMCRootManager(const TMCRootManager &rhs)
void ReadEvent(Int_t i)
static TMCThreadLocal TMCRootManager * fgInstance
FileMode
Root file mode.
std::unique_ptr< RNTupleWriter > fWriter
std::shared_ptr< RNTupleFillContext > fFillContext
TMCRootManager & operator=(const TMCRootManager &rhs)
static Bool_t fgDebug
std::vector< std::pair< std::string, void * > > fNameAddress
StorageMode fStorageMode
std::shared_ptr< RNTParaWriter > fParaWriter
std::string fStorageName
virtual ~TMCRootManager()
static Int_t fgCounter
static TMCRootManager * Instance()
static void SetDebug(Bool_t debug)
TMCRootManager(const char *projectName, FileMode fileMode=kWrite, Int_t threadRank=-1)
std::unique_ptr< REntry > fEntry
void CreateRNTuple(bool parallelMode=false, bool workerMode=false)
static Bool_t GetDebug()
void Register(const char *name, T *&obj)
std::shared_ptr< RNTParaWriter > GetParallelRNTupleWriter()