Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
readTree.cxx
Go to the documentation of this file.
1// Author: Alvaro Tolosa-Delgado CERN 07/2023
2// Author: Jorge Agramunt Ros IFIC(Valencia,Spain) 07/2023
3
4/*************************************************************************
5 * Copyright (C) 1995-2023, Rene Brun and Fons Rademakers. *
6 * All rights reserved. *
7 * *
8 * For the licensing terms see $ROOTSYS/LICENSE. *
9 * For the list of contributors see $ROOTSYS/README/CREDITS. *
10 *************************************************************************/
11
12#include <iostream>
13
14#include <TFile.h>
15#include <TTree.h>
16#include <TTreeReader.h>
17#include <TTreeReaderValue.h>
18
19#include "data2Tree.hxx"
20
22{
23
24 std::unique_ptr<TFile> ifile{TFile::Open("testFile.root", "read")};
25 if (!ifile || ifile->IsZombie()) {
26 std::cerr << "Problem opening the file testFile.root" << std::endl;
27 return;
28 }
29
30 // Create a TTreeReader to read the tree named "myTree"
31 TTreeReader aReader("myTree", ifile.get());
32
33 // Create TTreeReaderValues for the branches "branch1" and "branch2"
34 TTreeReaderValue<myDetectorData> branch1(aReader, "branch1.");
35 TTreeReaderValue<myDetectorData> branch2(aReader, "branch2.");
36
37 // Loop over the entries of the tree
38 while (aReader.Next()) {
39 if (branch1->time != 0)
40 std::cout << " -Branch1 : time: " << branch1->time << "\t energy: " << branch1->energy << std::endl;
41 else if (branch2->time != 0)
42 std::cout << " +Branch2 : time: " << branch2->time << "\t energy: " << branch2->energy << std::endl;
43 else
44 std::cerr << "WARNING: entry " << aReader.GetCurrentEntry() << " is empty! " << std::endl;
45 }
46}
static TFile * Open(const char *name, Option_t *option="", const char *ftitle="", Int_t compress=ROOT::RCompressionSetting::EDefaults::kUseCompiledDefault, Int_t netopt=0)
Create / open a file.
Definition TFile.cxx:4075
An interface for reading values stored in ROOT columnar datasets.
A simple, robust and fast interface to read values from ROOT columnar datasets such as TTree,...
Definition TTreeReader.h:44
Bool_t Next()
Move to the next entry (or index of the TEntryList if that is set).
Long64_t GetCurrentEntry() const
Returns the index of the current entry being read.
void readTree()
Definition readTree.cxx:21