PODIO v00-16-03
An Event-Data-Model Toolkit for High Energy Physics Experiments
Loading...
Searching...
No Matches
podio::ROOTReader Class Reference

#include <ROOTReader.h>

+ Inheritance diagram for podio::ROOTReader:

Public Member Functions

 ROOTReader ()=default
 
 ~ROOTReader ()
 
 ROOTReader (const ROOTReader &)=delete
 
ROOTReaderoperator= (const ROOTReader &)=delete
 
void openFile (const std::string &filename) override
 
void openFiles (const std::vector< std::string > &filenames)
 
void closeFile () override
 
void closeFiles ()
 
void readEvent () override
 Read all collections requested.
 
std::shared_ptr< CollectionIDTablegetCollectionIDTable () override
 Read CollectionIDTable from ROOT file.
 
unsigned getEntries () const override
 Returns number of entries in the TTree.
 
void endOfEvent () override
 Preparing to read next event.
 
void goToEvent (unsigned evnum) override
 Preparing to read a given event.
 
podio::version::Version currentFileVersion () const override
 Get the podio version with which the current file has been written.
 
bool isValid () const override
 Check if TFile is valid.
 
- Public Member Functions inherited from podio::IReader
virtual ~IReader ()=default
 
virtual CollectionBasereadCollection (const std::string &name)=0
 
virtual std::shared_ptr< CollectionIDTablegetCollectionIDTable ()=0
 Get CollectionIDTable of read-in data.
 
virtual GenericParametersreadEventMetaData ()=0
 read event meta data from file
 
virtual std::map< int, GenericParameters > * readCollectionMetaData ()=0
 
virtual std::map< int, GenericParameters > * readRunMetaData ()=0
 
virtual unsigned getEntries () const =0
 get the number of events available from this reader
 
virtual void endOfEvent ()=0
 Prepare the reader to read the next event.
 
virtual bool isValid () const =0
 Check if reader is valid.
 
virtual void openFile (const std::string &filename)=0
 
virtual void closeFile ()=0
 
virtual void readEvent ()=0
 
virtual void goToEvent (unsigned iEvent)=0
 
virtual podio::version::Version currentFileVersion () const =0
 Get the podio version with which the current file has been written.
 

Detailed Description

This class has the function to read available data from disk and to prepare collections and buffers.

Definition at line 33 of file ROOTReader.h.

Constructor & Destructor Documentation

◆ ROOTReader() [1/2]

podio::ROOTReader::ROOTReader ( )
default

◆ ~ROOTReader()

podio::ROOTReader::~ROOTReader ( )

Definition at line 20 of file ROOTReader.cc.

20 { // NOLINT(modernize-use-equals-default)
21}

◆ ROOTReader() [2/2]

podio::ROOTReader::ROOTReader ( const ROOTReader )
delete

Member Function Documentation

◆ closeFile()

void podio::ROOTReader::closeFile ( )
overridevirtual

Implements podio::IReader.

Definition at line 188 of file ROOTReader.cc.

188 {
189 closeFiles();
190}

◆ closeFiles()

void podio::ROOTReader::closeFiles ( )

Definition at line 192 of file ROOTReader.cc.

192 {
193 delete m_chain;
194}

Referenced by closeFile().

◆ currentFileVersion()

podio::version::Version podio::ROOTReader::currentFileVersion ( ) const
inlineoverridevirtual

Get the podio version with which the current file has been written.

Implements podio::IReader.

Definition at line 67 of file ROOTReader.h.

67 {
68 return m_fileVersion;
69 }

◆ endOfEvent()

void podio::ROOTReader::endOfEvent ( )
overridevirtual

Preparing to read next event.

Implements podio::IReader.

Definition at line 212 of file ROOTReader.cc.

212 {
213 ++m_eventNumber;
214 m_inputs.clear();
215}

Referenced by testWithIO().

◆ getCollectionIDTable()

std::shared_ptr< CollectionIDTable > podio::ROOTReader::getCollectionIDTable ( )
inlineoverridevirtual

Read CollectionIDTable from ROOT file.

Implements podio::IReader.

Definition at line 54 of file ROOTReader.h.

54 {
55 return m_table;
56 }

◆ getEntries()

unsigned podio::ROOTReader::getEntries ( ) const
overridevirtual

Returns number of entries in the TTree.

Implements podio::IReader.

Definition at line 217 of file ROOTReader.cc.

217 {
218 return m_chain->GetEntries();
219}

◆ goToEvent()

void podio::ROOTReader::goToEvent ( unsigned  evnum)
overridevirtual

Preparing to read a given event.

Implements podio::IReader.

Definition at line 221 of file ROOTReader.cc.

221 {
222 m_eventNumber = eventNumber;
223 m_inputs.clear();
224}

◆ isValid()

bool podio::ROOTReader::isValid ( ) const
overridevirtual

Check if TFile is valid.

Implements podio::IReader.

Definition at line 208 of file ROOTReader.cc.

208 {
209 return m_chain->GetFile()->IsOpen() && !m_chain->GetFile()->IsZombie();
210}

◆ openFile()

void podio::ROOTReader::openFile ( const std::string &  filename)
overridevirtual

Implements podio::IReader.

Definition at line 140 of file ROOTReader.cc.

140 {
141 openFiles({filename});
142}
void openFiles(const std::vector< std::string > &filenames)
Definition: ROOTReader.cc:144

Referenced by testWithIO().

◆ openFiles()

void podio::ROOTReader::openFiles ( const std::vector< std::string > &  filenames)

Definition at line 144 of file ROOTReader.cc.

144 {
145 m_chain = new TChain("events");
146 for (const auto& filename : filenames) {
147 //-1 forces the headers to be read so that
148 // the validity of the files can be checked
149 if (!m_chain->Add(filename.c_str(), -1)) {
150 delete m_chain;
151 throw std::runtime_error("File " + filename + " couldn't be found");
152 }
153 }
154
155 // read the meta data and build the collectionBranches cache
156 // NOTE: This is a small pessimization, if we do not read all collections
157 // afterwards, but it makes the handling much easier in general
158 auto metadatatree = static_cast<TTree*>(m_chain->GetFile()->Get("metadata"));
159 m_table = std::make_shared<podio::CollectionIDTable>();
160 auto* table = m_table.get();
161 metadatatree->SetBranchAddress("CollectionIDs", &table);
162
163 podio::version::Version* versionPtr{nullptr};
164 if (auto* versionBranch = root_utils::getBranch(metadatatree, "PodioVersion")) {
165 versionBranch->SetAddress(&versionPtr);
166 }
167
168 // Check if the CollectionTypeInfo branch is there and assume that the file
169 // has been written with with podio pre #197 (<0.13.1) if that is not the case
170 if (auto* collInfoBranch = root_utils::getBranch(metadatatree, "CollectionTypeInfo")) {
171 auto collectionInfo = new std::vector<root_utils::CollectionInfoT>;
172 collInfoBranch->SetAddress(&collectionInfo);
173 metadatatree->GetEntry(0);
174 createCollectionBranches(*collectionInfo);
175 delete collectionInfo;
176 } else {
177 std::cout << "PODIO: Reconstructing CollectionTypeInfo branch from other sources in file: \'"
178 << m_chain->GetFile()->GetName() << "\'" << std::endl;
179 metadatatree->GetEntry(0);
180 const auto collectionInfo = root_utils::reconstructCollectionInfo(m_chain, *m_table);
181 createCollectionBranches(collectionInfo);
182 }
183
184 m_fileVersion = versionPtr ? *versionPtr : podio::version::Version{0, 0, 0};
185 delete versionPtr;
186}
auto reconstructCollectionInfo(TTree *eventTree, podio::CollectionIDTable const &idTable)
Definition: rootUtils.h:130
TBranch * getBranch(Tree *chain, const char *name)
Definition: rootUtils.h:66

Referenced by openFile().

◆ operator=()

ROOTReader & podio::ROOTReader::operator= ( const ROOTReader )
delete

◆ readEvent()

void podio::ROOTReader::readEvent ( )
overridevirtual

Read all collections requested.

Implements podio::IReader.

Definition at line 196 of file ROOTReader.cc.

196 {
197 m_chain->GetEntry(m_eventNumber);
198 // first prepare all collections in memory...
199 for (auto inputs : m_inputs) {
200 inputs.first->prepareAfterRead();
201 }
202 // ...then clean-up the references between them
203 // for(auto inputs : m_inputs){
204 // inputs.first->setReferences(m_registry);
205
206 // }
207}

The documentation for this class was generated from the following files: