VMC Version 2.2
Loading...
Searching...
No Matches
TMCTTreeWriter.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
17
18#include "TMCTTreeWriter.h"
19
20#include <TError.h>
21
22#include <iostream>
23
24//
25// ctors/dtors
26//
27
28//_____________________________________________________________________________
29TMCTTreeWriter::TMCTTreeWriter(const char *storageName, TFile* file, Bool_t workerMode)
30 : TMCVNtupleWriter(storageName, file, workerMode)
31{
32 if (!fFile) {
33 Error("TMCTTreeWriter", "File not found, cannot create tree.");
34 return;
35 }
36
37 if (fFile->IsWritable()) {
38 // we are in writing mode
39 fTree = new TTree(storageName, fStorageName.c_str());
40 }
41 else {
42 // we are in reading mode
43 fTree = (TTree *)fFile->Get(storageName);
44 if (!fTree) {
45 Error("TMCTTreeWriter", "Failed to get tree %s from the file.", storageName);
46 }
47 }
48}
49
50//
51// public methods
52//
53
54//_____________________________________________________________________________
55void TMCTTreeWriter::Register(const char *name, const char *className, void *objAddress)
56{
57 if (!fTree) {
58 Error("Register", "Tree not found, cannot register data.");
59 return;
60 }
61
62 // Check if we are in READ mode or WRITE mode
63 TBranch* branch = fTree->GetBranch(name);
64
65 if (branch) {
66 // --- READING MODE ---
67 // The branch exists in the file; attach memory address to read into
68 // std::cout << "Going to set branch address: " << name << " " << objAddress << std::endl;
69 branch->SetAddress(objAddress);
70 }
71 else if (fFile && fFile->IsWritable()) {
72 // --- WRITING MODE ---
73 // The branch doesn't exist AND file is open for writing; create new branch
74 // std::cout << "Going to create branch: " << name << " " << objAddress << std::endl;
75 fFile->cd();
76 fTree->Branch(name, className, objAddress, 32000, 99);
77 }
78 else {
79 Error("Register", "Branch %s not found in tree for reading!", name);
80 }
81}
82//_____________________________________________________________________________
83void TMCTTreeWriter::Register(const char *name, const char *className, const void *objAddress)
84{
89
90 Register(name, className, const_cast<void *&>(objAddress));
91}
92
93//_____________________________________________________________________________
95{
97 fFile->cd();
98 fTree->Fill();
99}
100
101//_____________________________________________________________________________
103{
105 fFile->cd();
106 fFile->Write();
107}
108
109//_____________________________________________________________________________
111{
114
115 fTree->GetEntry(i);
116}
Definition of the TMCTTreeWriter class.
virtual void ReadEvent(Int_t i) override
TMCTTreeWriter()=delete
virtual void WriteAll() override
void Register(const char *name, T *&obj)
virtual void Fill() override
TMCVNtupleWriter(const char *storageName, TFile *file, Bool_t workerMode)