ATLAS Offline Software
Loading...
Searching...
No Matches
Sample.cxx File Reference
#include <SampleHandler/Sample.h>
#include <RootCoreUtils/Assert.h>
#include <RootCoreUtils/PrintMsg.h>
#include <RootCoreUtils/RootUtils.h>
#include <SampleHandler/MetaFields.h>
#include <SampleHandler/MetaNames.h>
#include <SampleHandler/MetaObject.h>
#include <SampleHandler/SampleGrid.h>
#include <SampleHandler/SampleHandler.h>
#include <SampleHandler/SamplePtr.h>
#include <TChain.h>
#include <TFile.h>
#include <memory>
#include <iostream>

Go to the source code of this file.

Functions

 ClassImp (SH::Sample) namespace SH

Function Documentation

◆ ClassImp()

ClassImp ( SH::Sample )

Definition at line 38 of file Sample.cxx.

41{
42 std::string dbg (const Sample& obj, unsigned verbosity)
43 {
44 std::string result;
45 result += "Sample:name=" + obj.name();
46 if (dynamic_cast<const SampleGrid*>(&obj))
47 {
48 if (verbosity % 10 > 0)
49 {
50 result += "\n " + MetaFields::gridName + "="+obj.meta()->castString(MetaFields::gridName);
51 result += "\n " + MetaFields::gridFilter + "="+obj.meta()->castString(MetaFields::gridFilter);
52 };
53 } else
54 {
55 if (verbosity % 10 > 0)
56 result += ",tags=" + dbg (obj.tags(), verbosity / 10);
57 if (verbosity % 10 > 1)
58 {
59 result += "\n";
60 for (std::size_t iter = 0, end = obj.numFiles();
61 iter != end; ++ iter)
62 result += obj.fileName (iter) + "\n";
63 };
64 };
65 return result;
66 }
67
68
69
70 void Sample ::
71 testInvariant () const
72 {
73 RCU_INVARIANT (m_meta != 0);
74 }
75
76
77
78 Sample ::
79 ~Sample ()
80 {
82
83 delete m_meta;
84 }
85
86
87
88 const std::string& Sample ::
89 name () const
90 {
91 RCU_READ_INVARIANT (this);
92 return m_name;
93 }
94
95
96
97 void Sample ::
98 name (std::string val_name)
99 {
101 if (m_references > 0)
102 RCU_THROW_MSG ("Sample already owned by SampleHandler");
103 m_meta->setString (MetaNames::sampleName(), val_name);
104 m_name = std::move (val_name);
105
106 }
107
108
109
110 std::size_t Sample ::
111 numFiles () const
112 {
113 RCU_READ_INVARIANT (this);
114 return getNumFiles();
115 }
116
117
118
119 std::string Sample ::
120 fileName (const std::size_t index) const
121 {
122 RCU_READ_INVARIANT (this);
124
125 std::string result = getFileName (index);
126
127 RCU_PROVIDE (!result.empty());
128 return result;
129 }
130
131
132
133 SamplePtr Sample ::
134 makeLocal () const
135 {
136 RCU_READ_INVARIANT (this);
137 return doMakeLocal ();
138 }
139
140
141
142 const TagList& Sample ::
143 tags () const
144 {
145 RCU_READ_INVARIANT (this);
146 return m_tags;
147 }
148
149
150
151 void Sample ::
152 tags (const TagList& tags)
153 {
155 m_tags = tags;
156 }
157
158
159
160 void Sample ::
161 addTag (const std::string& tag)
162 {
164 m_tags.add (tag);
165 }
166
167
168
169 void Sample ::
170 updateLocation (const std::string& from, const std::string& to)
171 {
173 doUpdateLocation (from, to);
174 }
175
176
177
178 MetaObject *Sample ::
179 meta ()
180 {
181 RCU_READ_INVARIANT (this);
182 return m_meta;
183 }
184
185
186
187 const MetaObject *Sample ::
188 meta () const
189 {
190 RCU_READ_INVARIANT (this);
191 return m_meta;
192 }
193
194
195
196 std::vector<std::string> Sample ::
197 makeFileList () const
198 {
199 RCU_READ_INVARIANT (this);
200 return doMakeFileList ();
201 }
202
203
204
205 TChain *Sample ::
206 makeTChain () const
207 {
208 // no invariant used
209
210 std::vector<std::string> files = makeFileList ();
211
212 const std::string treeName (meta()->castString (MetaFields::treeName, MetaFields::treeName_default));
213 if (treeName.empty())
214 RCU_THROW_MSG ("sample " + name() + " does not have a tree name associated");
215 std::unique_ptr<TChain> result (new TChain (treeName.c_str()));
216 for (std::vector<std::string>::const_iterator file = files.begin(),
217 end = files.end(); file != end; ++ file)
218 result->AddFile (file->c_str());
219 return result.release();
220 }
221
222
223
224 void Sample ::
225 doUpdateLocation (const std::string& /*from*/, const std::string& /*to*/)
226 {
227 RCU_READ_INVARIANT (this);
228 }
229
230
231
232 TObject *Sample ::
233 readHist (const std::string& name) const
234 {
235 RCU_READ_INVARIANT (this);
236 return doReadHist (name);
237 }
238
239
240
241 bool Sample ::
242 contains (const std::string& name) const
243 {
244 RCU_READ_INVARIANT (this);
245 if (m_name == name)
246 return true;
247 return false;
248 }
249
250
251
252 void Sample ::
253 addSamples (SampleHandler& result)
254 {
255 RCU_READ_INVARIANT (this);
256 doAddSamples (result);
257 }
258
259
260
261 void Sample ::
262 print () const
263 {
264 RCU_READ_INVARIANT (this);
265 std::cout << dbg (*this, 9999) << std::endl;
266 }
267
268
269
270 void Sample ::
271 printContent () const
272 {
273 // not using invariant
274 print ();
275 }
276
277
278
279 Long64_t Sample ::
280 getNumEntries () const
281 {
282 RCU_READ_INVARIANT (this);
283
284 std::string treeName
285 = meta()->castString (MetaFields::treeName, MetaFields::treeName_default);
286 Long64_t result = 0;
287 std::vector<std::string> fileList = makeFileList();
288 for (std::vector<std::string>::const_iterator fileName = fileList.begin(),
289 end = fileList.end(); fileName != end; ++ fileName)
290 {
291 std::unique_ptr<TFile> file (TFile::Open (fileName->c_str(), "READ"));
292 if (file.get() == 0)
293 RCU_THROW_MSG ("failed to open file: " + *fileName);
294 TTree *tree = dynamic_cast<TTree*>(file->Get (treeName.c_str()));
295 if (tree)
296 result += tree->GetEntries();
297 }
298 return result;
299 }
300
301
302
303 TCollection *Sample ::
304 metaDataList ()
305 {
306 // no invariant used
307 return meta();
308 }
309
310
311
312 const TCollection *Sample ::
313 metaDataList () const
314 {
315 // no invariant used
316 return meta();
317 }
318
319
320
321 void Sample ::
322 removeMeta (const std::string& name)
323 {
324 // no invariant used
325 meta()->remove (name);
326 }
327
328
329
330 void Sample ::
331 addReplaceMeta (TNamed *meta_swallow)
332 {
333 std::unique_ptr<TNamed> mymeta (meta_swallow);
334
335 // no invariant used
336 RCU_REQUIRE_SOFT (meta_swallow != 0);
337
338 meta()->addReplace (mymeta.release());
339 }
340
341
342
343 TObject *Sample ::
344 getMeta (const std::string& name)
345 {
346 // no invariant used
347 return meta()->get (name);
348 }
349
350
351
352 const TObject *Sample ::
353 getMeta (const std::string& name) const
354 {
355 // no invariant used
356 return meta()->get (name);
357 }
358
359
360
361 double Sample ::
362 getMetaDouble (const std::string& name, double def_val) const
363 {
364 // no invariant used
365 return meta()->castDouble (name, def_val, MetaObject::CAST_NOCAST_DEFAULT);
366 }
367
368
369
370 std::string Sample ::
371 getMetaString (const std::string& name, const std::string& def_val) const
372 {
373 // no invariant used
374 return meta()->castString (name, def_val, MetaObject::CAST_NOCAST_DEFAULT);
375 }
376
377
378
379 void Sample ::
380 setMetaDouble (const std::string& name, double value)
381 {
382 // no invariant used
383 meta()->setDouble (name, value);
384 }
385
386
387
388 void Sample ::
389 setMetaString (const std::string& name, const std::string& value)
390 {
391 // no invariant used
392 meta()->setString (name, value);
393 }
394
395
396
397 void Sample ::
398 fetchMeta (const Sample& source)
399 {
400 // no invariant used
401 meta()->fetch (*source.meta());
402 }
403
404
405
406 Sample ::
407 Sample (const std::string& name)
408 : m_name (name), m_meta (new MetaObject),
409 m_references (0)
410 {
411 m_meta->setString (MetaNames::sampleName(), name);
412
413 RCU_NEW_INVARIANT (this);
414 }
415
416
417
418 TObject *Sample ::
419 doReadHist (const std::string& name) const
420 {
421 RCU_READ_INVARIANT (this);
422 std::vector<std::string> fileList (makeFileList());
423 if (fileList.size() > 1)
424 RCU_THROW_MSG ("reading histgrams from samples with multiple files is not (yet) implemented");
425 if (fileList.size() == 0)
426 return nullptr;
427 std::unique_ptr<TFile> file (TFile::Open (fileList[0].c_str(), "READ"));
428 if (file == nullptr)
429 RCU_THROW_MSG ("could not open file " + fileList[0]);
430 //cppcheck-suppress nullPointerRedundantCheck
431 TObject *object = file->Get (name.c_str());
432 if (object != nullptr)
433 RCU::SetDirectory (object, nullptr);
434 return object;
435 }
436
437
438
439 bool Sample ::
440 getContains (const std::string& /*name*/) const
441 {
442 RCU_READ_INVARIANT (this);
443 return false;
444 }
445
446
447
448 void Sample ::
449 doAddSamples (SampleHandler& result)
450 {
451 RCU_READ_INVARIANT (this);
452 result.add (this);
453 }
454
455
456
457 void Sample ::
458 alloc () const
459 {
461 ++ m_references;
462 }
463
464
465
466 void Sample ::
467 release () const
468 {
469 RCU_READ_INVARIANT (this);
470 RCU_REQUIRE2 (m_references > 0, "reference count > 0");
471
472 unsigned refs = -- m_references;
473 if (refs == 0)
474 delete this;
475 }
476}
#define RCU_REQUIRE2(x, y)
Definition Assert.h:210
#define RCU_INVARIANT(x)
Definition Assert.h:201
#define RCU_DESTROY_INVARIANT(x)
Definition Assert.h:235
#define RCU_CHANGE_INVARIANT(x)
Definition Assert.h:231
#define RCU_NEW_INVARIANT(x)
Definition Assert.h:233
#define RCU_PROVIDE(x)
Definition Assert.h:215
#define RCU_REQUIRE_SOFT(x)
Definition Assert.h:153
#define RCU_READ_INVARIANT(x)
Definition Assert.h:229
#define RCU_THROW_MSG(message)
Definition PrintMsg.h:58
void print(char *figname, TCanvas *c1)
std::vector< std::string > files
file names and file pointers
Definition hcg.cxx:50
std::vector< std::string > tags
Definition hcg.cxx:105
bool SetDirectory(TObject *object, TDirectory *directory)
effects: set the directory this object is associated with returns: whether the object type actively k...
Definition RootUtils.cxx:28
Definition index.py:1
-diff
getFileName(name, sep='#')
Definition TrfUtils.py:45
TChain * tree
TFile * file