Project
Loading...
Searching...
No Matches
TreeStream.cxx
Go to the documentation of this file.
1// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
2// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3// All rights not expressly granted are reserved.
4//
5// This software is distributed under the terms of the GNU General Public
6// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7//
8// In applying this license CERN does not waive the privileges and immunities
9// granted to it by virtue of its status as an Intergovernmental Organization
10// or submit itself to any jurisdiction.
11
12// For the functionality of TreeStream see the testTreeStream.cxx
13
15#include <TBranch.h>
16
17using namespace o2::utils;
18
19//_________________________________________________
20TreeStream::TreeStream(const char* treename) : mTree(treename, treename)
21{
22 //
23 // Standard ctor
24}
25
26//_________________________________________________
27int TreeStream::CheckIn(Char_t type, const void* pointer)
28{
29 // Insert object
30
31 if (mCurrentIndex >= static_cast<int>(mElements.size())) {
32 auto& element = mElements.emplace_back();
33 element.type = type;
34 TString name = mNextName;
35 if (name.Length()) {
36 if (mNextNameCounter > 0) {
37 name += mNextNameCounter;
38 }
39 } else {
40 name = TString::Format("B%d.", static_cast<int>(mElements.size()));
41 }
42 element.name = name.Data();
43 element.ptr = pointer;
44 element.arsize = mNextArraySize;
45 mNextArraySize = 1; // reset
46 } else {
47 auto& element = mElements[mCurrentIndex];
48 if (element.type != type) {
49 mStatus++;
50 return 1; // mismatched data element
51 }
52 element.ptr = pointer;
53 }
54 mCurrentIndex++;
55 return 0;
56}
57
58//_________________________________________________
60{
61 // Build the Tree
62
63 int entriesFilled = mTree.GetEntries();
64 if (mBranches.size() < mElements.size()) {
65 mBranches.resize(mElements.size());
66 }
67
68 TString name;
69 TBranch* br = nullptr;
70 for (int i = 0; i < static_cast<int>(mElements.size()); i++) {
71 //
72 auto& element = mElements[i];
73 if (mBranches[i]) {
74 continue;
75 }
76 name = element.name.data();
77 if (name.IsNull()) {
78 name = TString::Format("B%d", i);
79 }
80 if (element.cls) {
81 br = mTree.Branch(name.Data(), element.cls->GetName(), const_cast<void**>(&element.ptr));
82 mBranches[i] = br;
83 if (entriesFilled) {
84 br->SetAddress(nullptr);
85 for (int ientry = 0; ientry < entriesFilled; ientry++) {
86 br->Fill();
87 }
88 br->SetAddress(const_cast<void**>(&element.ptr));
89 }
90 }
91
92 if (element.type > 0) {
93 TString nameC;
94 if (element.arsize > 1) {
95 nameC = TString::Format("%s[%d]/%c", name.Data(), element.arsize,
96 element.type);
97 } else {
98 nameC = TString::Format("%s/%c", name.Data(), element.type);
99 }
100 br = mTree.Branch(name.Data(), const_cast<void*>(element.ptr), nameC.Data());
101 if (entriesFilled) {
102 br->SetAddress(nullptr);
103 for (int ientry = 0; ientry < entriesFilled; ientry++) {
104 br->Fill();
105 }
106 br->SetAddress(const_cast<void*>(element.ptr));
107 }
108 mBranches[i] = br;
109 }
110 }
111}
112
113//_________________________________________________
115{
116 // Fill the tree
117
118 int entries = mElements.size();
119 if (entries > mTree.GetNbranches()) {
120 BuildTree();
121 }
122 for (int i = 0; i < entries; i++) {
123 auto& element = mElements[i];
124 if (!element.type) {
125 continue;
126 }
127 auto br = mBranches[i];
128 if (br) {
129 if (element.type) {
130 br->SetAddress(const_cast<void*>(element.ptr));
131 }
132 }
133 }
134 if (!mStatus) {
135 mTree.Fill(); // fill only in case of non conflicts
136 }
137 mStatus = 0;
138}
139
140//_________________________________________________
142{
143 // Perform pseudo endl operation
144
145 if (mTree.GetNbranches() == 0) {
146 BuildTree();
147 }
148 Fill();
149 mStatus = 0;
150 mCurrentIndex = 0;
151 return *this;
152}
153
154//_________________________________________________
156{
157 // Stream the branch name
158 if (name[0] == '\n') {
159 return Endl();
160 }
161
162 // if tree was already defined ignore
163 if (mTree.GetEntries() > 0) {
164 return *this;
165 }
166
167 int arsize = 1;
168
169 // check branch name if tree was not
170 Int_t last = 0;
171 for (last = 0;; last++) {
172 if (name[last] == 0) {
173 break;
174 }
175 }
176 if (last > 0 && name[last - 1] == '=') {
177 mNextName = name;
178 mNextName[last - 1] = 0; // remove '=' from string
179 mNextNameCounter = 0;
180
181 TString inName{name};
182 auto brkStaPos = inName.Index('[');
183
184 if (brkStaPos != kNPOS) {
185 auto brkEndPos = inName.Index(']');
186 if (brkEndPos != kNPOS && brkEndPos > brkStaPos + 1) {
187 TString size = inName(brkStaPos + 1, brkEndPos - brkStaPos - 1);
188 arsize = size.Atoi();
189 mNextName = inName(0, brkStaPos); // use parsed name
190 }
191 }
192 }
193
194 mNextArraySize = arsize;
195
196 return *this;
197}
int32_t i
TreeStream & Endl()
Int_t CheckIn(Char_t type, const void *pointer)
TreeStream & operator<<(const T &t)
Definition TreeStream.h:139
GLenum void ** pointer
Definition glcorearb.h:805
GLsizeiptr size
Definition glcorearb.h:659
GLuint const GLchar * name
Definition glcorearb.h:781
GLint GLint GLsizei GLint GLenum GLenum type
Definition glcorearb.h:275