AnalysisTree
Loading...
Searching...
No Matches
Chain.hpp
1/* Copyright (C) 2019-2021 GSI, Universität Tübingen
2 SPDX-License-Identifier: GPL-3.0-only
3 Authors: Viktor Klochkov, Ilya Selyuzhenkov */
4#ifndef ANALYSISTREE_INFRA_CHAIN_HPP_
5#define ANALYSISTREE_INFRA_CHAIN_HPP_
6
7#include <fstream>
8#include <map>
9#include <set>
10#include <string>
11#include <utility>
12#include <vector>
13
14#include <TChain.h>
15#include <TFile.h>
16
17#include "Branch.hpp"
18#include "Configuration.hpp"
19#include "DataHeader.hpp"
20#include "Utils.hpp"
21
22namespace AnalysisTree {
23
24class Chain : public TChain {
25
26 public:
27 Chain() : TChain() {}
28
29 Chain(TTree* tree, Configuration* config, DataHeader* data_header) : TChain(tree->GetName()),
30 configuration_(config),
31 data_header_(data_header) {
32 this->AddFriend(tree);
33 }
34
35 Chain(const std::string& filename, const std::string& treename) : TChain(treename.c_str()) {
36 TFile* file = TFile::Open(filename.c_str(), "read");
37 configuration_ = (Configuration*) file->Get("Configuration");
38 data_header_ = (DataHeader*) file->Get("DataHeader");
39 this->Add(filename.c_str());
40 }
41
42 Chain(std::vector<std::string> filelists, std::vector<std::string> treenames) : TChain(treenames.at(0).c_str()),
43 filelists_(std::move(filelists)),
44 treenames_(std::move(treenames)) {
45 InitChain();
46 InitConfiguration();
47 InitDataHeader();
48 }
49
50 ANALYSISTREE_ATTR_NODISCARD Configuration* GetConfiguration() const { return configuration_; }
51 ANALYSISTREE_ATTR_NODISCARD DataHeader* GetDataHeader() const { return data_header_; }
52 ANALYSISTREE_ATTR_NODISCARD const std::map<std::string, BranchPointer>& GetBranchPointers() const { return branches_; }
53 ANALYSISTREE_ATTR_NODISCARD const std::map<std::string, Matching*>& GetMatchPointers() const { return matches_; }
54
55 void SetDataHeader(DataHeader* dh) { data_header_ = dh; }
56
57 int CheckBranchExistence(const std::string& branchname);
58
59 BranchPointer GetPointerToBranch(const std::string& name) const {
60 auto br = branches_.find(name);
61 if (br != branches_.end()) {
62 return br->second;
63 } else {
64 throw std::runtime_error("Branch " + name + " is not found!");
65 }
66 }
67
74 void InitPointersToBranches(std::set<std::string> names);
75
76 Long64_t Draw(const char* varexp, const char* selection = nullptr, Option_t* option = "", Long64_t nentries = kMaxEntries, Long64_t firstentry = 0) override;
77 Long64_t Scan(const char* varexp, const char* selection = nullptr, Option_t* option = "", Long64_t nentries = kMaxEntries, Long64_t firstentry = 0) override;
78
79 void Print(Option_t*) const override {
80 this->data_header_->Print();
81 this->configuration_->Print();
82 }
83
84 [[deprecated("Will be removed soon to avoid confusion with TChain::GetBranch. Use GetBranchObject")]] class Branch GetBranch(const std::string& name) const;
85
86 class Branch GetBranchObject(const std::string& name) const;
87
88 Matching* GetMatching(const std::string& br1, const std::string& br2) const {
89 auto match = matches_.find(configuration_->GetMatchName(br1, br2));
90 if (match == matches_.end()) {
91 throw std::runtime_error("No matching found");
92 }
93 return match->second;
94 }
95
99 TTree* CloneChain(int nentries = 0);
100
105
106 protected:
107 void InitChain();
108 void InitConfiguration();
109 void InitDataHeader();
110
111 static TChain* MakeChain(const std::string& filelist, const std::string& treename);
112
120 template<class T>
121 static T* GetObjectFromFileList(const std::string& filelist, const std::string& name);
122
127 std::vector<TChain*> GetTChains();
128
129 std::string LookupAlias(const std::vector<std::string>& names, const std::string& name, size_t copy = 0);
130
131 std::vector<std::string> filelists_{};
132 std::vector<std::string> treenames_{};
133
134 Configuration* configuration_{nullptr};
135 DataHeader* data_header_{nullptr};
136
137 std::map<std::string, BranchPointer> branches_{};
138 std::map<std::string, Matching*> matches_{};
139
140 ClassDefOverride(AnalysisTree::Chain, 1)
141};
142
143}// namespace AnalysisTree
144#endif//ANALYSISTREE_INFRA_CHAIN_HPP_
Definition Branch.hpp:23
Definition Chain.hpp:24
void InitPointersToBranches(std::set< std::string > names)
Loads selected list of branches from TTree.
Definition Chain.cpp:58
std::vector< TChain * > GetTChains()
Definition Chain.cpp:203
TTree * CloneChain(int nentries=0)
Clones tree without friends.
Definition Chain.cpp:240
Configuration * CloneConfiguration() const
Clones Configuration of input Chain without friends.
Definition Chain.cpp:251
static T * GetObjectFromFileList(const std::string &filelist, const std::string &name)
Loads object of type T from one (first) ROOT file specified in filelist.
Definition Chain.cpp:144
A class to store configuration of the whole AnalysisTree object.
Definition Configuration.hpp:58
Definition DataHeader.hpp:19
Definition Matching.hpp:17
Cuts keep list of SimpleCuts. Logical AND is applied for all SimpleCut in the Cuts object.
Definition BranchConfig.cpp:10