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