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