NAIA  1.0.2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups
SkimTreeHandle.h
Go to the documentation of this file.
1 
9 #ifndef NAIA_SKIMTREEHANDLE_H
10 #define NAIA_SKIMTREEHANDLE_H
11 
12 #include <memory>
13 
14 #include "TFile.h"
15 
16 namespace NAIA {
17 
25 template <class T> class SkimTreeHandle {
26 public:
35  SkimTreeHandle(std::unique_ptr<TFile> outFile, std::unique_ptr<T> chain, T *originalChain)
36  : m_outFile{std::move(outFile)}, m_chain{std::move(chain)}, m_originalChain{originalChain} {};
37 
43  int Fill() {
44  m_originalChain->m_event.ForceReadAllBranches();
45  return m_chain->Fill();
46  }
47 
53  int Write() { // NOTE: We copy the whole RTI and FileInfo tree in the output chain
54  NAIA::Version versionHeader;
55  m_outFile->WriteTObject(&versionHeader, "VersionHeader");
56 
57  // we need raw pointers cause SetBranchAddress expects the address to a pointer
58 
59  if (!m_originalChain->IsMC()) {
60  auto *rtiInfo = new NAIA::RTIInfo();
61  m_originalChain->GetRTITree()->SetBranchAddress("RTIInfo", &rtiInfo);
62  m_chain->m_rti.tree->Branch("RTIInfo", rtiInfo);
63 
64  for (int ii = 0; ii < m_originalChain->GetRTITree()->GetEntries(); ii++) {
65  m_originalChain->GetRTITree()->GetEntry(ii);
66  m_chain->FillRTI();
67  }
68  m_chain->m_rti.tree->Write();
69 
70  delete rtiInfo;
71  }
72 
73  auto *fileInfo = new NAIA::FileInfo();
74  auto *fileInfoMC = new NAIA::MCFileInfo();
75  m_originalChain->GetFileInfoTree()->SetBranchAddress("FileInfo", &fileInfo);
76  m_chain->m_file.tree->Branch("FileInfo", fileInfo);
77  if (m_originalChain->IsMC()) {
78  m_originalChain->GetFileInfoTree()->SetBranchAddress("MCFileInfo", &fileInfoMC);
79  m_chain->m_file.tree->Branch("MCFileInfo", fileInfoMC);
80  }
81 
82  for (int ii = 0; ii < m_originalChain->GetFileInfoTree()->GetEntries(); ii++) {
83  m_originalChain->GetFileInfoTree()->GetEntry(ii);
84  m_chain->FillFileInfo();
85  }
86  m_chain->m_file.tree->Write();
87 
88  delete fileInfo;
89 
90  return m_chain->Write();
91  }
92 
98  TFile *RootFile() { return m_outFile.get(); }
99 
100 private:
101  std::unique_ptr<TFile> m_outFile;
102  std::unique_ptr<T> m_chain;
104 };
105 } // namespace NAIA
106 
107 #endif // NAIA_SKIMTREEHANDLE_H
Helper class to ease skimming operations.
std::unique_ptr< T > m_chain
std::unique_ptr< TFile > m_outFile
TFile * RootFile()
Get a non-owning pointer to the underlying rootfile.
SkimTreeHandle(std::unique_ptr< TFile > outFile, std::unique_ptr< T > chain, T *originalChain)
Construct a new SkimTreeHandle.
Container class for processed File information.
Definition: FileInfo.h:23
Container class for additional MC File information.
Definition: FileInfo.h:54
int Write()
Writes the skimmed tree on the output rootfile.
int Fill()
Fills the underlying event in the output tree.
Container class for RTI info.
Definition: RTIInfo.h:33