ATLAS Offline Software
Loading...
Searching...
No Matches
Sample.cxx File Reference
#include <SampleHandler/Sample.h>
#include <RootCoreUtils/Assert.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/SampleLocal.h>
#include <TChain.h>
#include <TFile.h>
#include <memory>
#include <iostream>
#include <stdexcept>

Go to the source code of this file.

Functions

 ClassImp (SH::Sample) namespace SH

Function Documentation

◆ ClassImp()

ClassImp ( SH::Sample )
Author
Nils Krumnack

Definition at line 31 of file Sample.cxx.

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