VMC Examples Version 6.8
Loading...
Searching...
No Matches
read_ttree.C
Go to the documentation of this file.
1//------------------------------------------------
2// The Virtual Monte Carlo examples
3// Copyright (C) 2007 - 2014 Ivana Hrivnacova
4// All rights reserved.
5//
6// For the licensing terms see geant4_vmc/LICENSE.
7// Contact: root-vmc@cern.ch
8//-------------------------------------------------
9
10/// \ingroup E03
11/// \file E03/read_ttree.C
12/// \brief Macro for reading the E03d simulated data from Root file
13/// written with kTTree mode
14
16{
17/// Macro for reading the E03d simulated data from Root file
18/// written with kTTree mode.
19/// Note that since Root 6 the libraries have to be loaded first
20/// via load_g4d.C.
21
22 // MC application
23 Int_t threadId = 1;
25 = new Ex03dMCApplication("Example03", "The example03 MC application");
26
27 for (Int_t i=0; i<5; i++) {
28 cout << " Event no " << i+1 << ":" << endl;
29 appl->ReadEvent(i, TMCRootManager::kTTree, threadId);
30 // appl->GetCalorimeterSD()->Print();
32 cout << endl;
33 }
34}
35
36// Reading tree without using the Ex03dMCApplication functions
38{
39 // Open the file and get the TTree
40 TFile* rfile = TFile::Open("TExample03_1.root", "READ");
41 TTree* tree = nullptr;
42 rfile->GetObject("TExample03", tree);
43
44 // Initialize the pointer to nullptr before reading
45 std::vector<Ex03CalorHit>* hits = nullptr;
46
47 // Set the branch address (pass the address of your pointer variable)
48 tree->SetBranchAddress("hits", &hits);
49 // std::cout << "hits: " << hits << std::endl;
50
51 // Loop over entries
52 Long64_t nEntries = tree->GetEntries();
53 std::cout << "3" << std::endl;
54
55 // for (Long64_t i = 0; i < nEntries; ++i) {
56 for (Long64_t i = 0; i < 5; ++i) {
57 tree->GetEntry(i);
58 std::cout << "Entry " << i << " has " << hits->size() << " hits." << std::endl;
60 }
61
62 // Cleanup
63 // ROOT automatically allocated the vector; delete it when completely done
64 delete hits;
65 rfile->Close();
66}
static void PrintTotal(std::vector< Ex03CalorHit > *collection)
Implementation of the TVirtualMCApplication.
Ex03dCalorimeterSD * GetCalorimeterSD() const
void ReadEvent(Int_t i, TMCRootManager::StorageMode storageMode=TMCRootManager::kRNTuple, Int_t threadId=-1)
void read_ttree()
Definition read_ttree.C:15
void read_ttree_raw()
Definition read_ttree.C:37