ATLAS Offline Software
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 
5 //
6 // Distributed under the Boost Software License, Version 1.0.
7 // (See accompanying file LICENSE_1_0.txt or copy at
8 // http://www.boost.org/LICENSE_1_0.txt)
9 
10 // Please feel free to contact me (krumnack@iastate.edu) for bug
11 // reports, feature suggestions, praise and complaints.
12 
13 
14 //
15 // includes
16 //
17 
18 #include <SampleHandler/Sample.h>
19 
20 #include <RootCoreUtils/Assert.h>
21 #include <RootCoreUtils/PrintMsg.h>
29 #include <TChain.h>
30 #include <TFile.h>
31 #include <memory>
32 #include <iostream>
33 
34 //
35 // method implementations
36 //
37 
39 
40 namespace SH
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  {
81  RCU_DESTROY_INVARIANT (this);
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  {
100  RCU_CHANGE_INVARIANT (this);
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  {
154  RCU_CHANGE_INVARIANT (this);
155  m_tags = tags;
156  }
157 
158 
159 
160  void Sample ::
161  addTag (const std::string& tag)
162  {
163  RCU_CHANGE_INVARIANT (this);
164  m_tags.add (tag);
165  }
166 
167 
168 
169  void Sample ::
170  updateLocation (const std::string& from, const std::string& to)
171  {
172  RCU_CHANGE_INVARIANT (this);
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);
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
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  {
460  RCU_CHANGE_INVARIANT (this);
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 }
ClassImp
ClassImp(SH::Sample) namespace SH
Definition: Sample.cxx:38
SH::Sample::numFiles
std::size_t numFiles() const
the number of files in the sample
SH::MetaObject::CAST_NOCAST_DEFAULT
@ CAST_NOCAST_DEFAULT
cast and return the default value if the input has the wrong type
Definition: MetaObject.h:78
SH::Sample::m_meta
MetaObject * m_meta
the meta-information for this sample
Definition: Sample.h:483
get_generator_info.result
result
Definition: get_generator_info.py:21
SH::Sample::m_tags
TagList m_tags
the tag list we are using
Definition: Sample.h:479
SH::Sample::updateLocation
void updateLocation(const std::string &from, const std::string &to)
update all file references starting with from to to
SH::Sample::doMakeLocal
virtual SamplePtr doMakeLocal() const =0
Make this a local sample, i.e.
SH::Sample::getNumEntries
Long64_t getNumEntries() const
get the number of entries
RootUtils.h
SH::dbg
std::string dbg(const Meta &, unsigned)
Definition: Meta.cxx:28
index
Definition: index.py:1
SH::MetaObject::remove
void remove(const std::string &name)
remove all meta-information with the given name
tree
TChain * tree
Definition: tile_monitor.h:30
CscCalibQuery.fileList
fileList
Definition: CscCalibQuery.py:330
SH::Sample::doReadHist
virtual TObject * doReadHist(const std::string &name) const
read an object from a histogram file
SH::Sample::addTag
void addTag(const std::string &tag)
add a tag to the content of tags()
athena.value
value
Definition: athena.py:124
SH::Sample::readHist
TObject * readHist(const std::string &name) const
read an object from a histogram file
SampleHandler.h
SH::Sample::getMeta
TObject * getMeta(const std::string &name)
the meta-data object with the given name
SH::Sample::print
void print() const
print the debugging output to the screen
Assert.h
MetaNames.h
SH::Sample::getMetaString
std::string getMetaString(const std::string &name, const std::string &def_val="") const
the meta-data string with the given name
SH::MetaObject::fetch
void fetch(const MetaObject &source)
fetch the meta-data from the given sample.
SH::Sample::metaDataList
TCollection * metaDataList()
get the meta-data list
SH::Sample::addReplaceMeta
void addReplaceMeta(TNamed *meta_swallow)
add a meta-data object and remove any existing meta-data with the same name
SH::Sample::setMetaDouble
void setMetaDouble(const std::string &name, double value)
set the meta-data double with the given name
mergePhysValFiles.end
end
Definition: DataQuality/DataQualityUtils/scripts/mergePhysValFiles.py:93
SH::TagList::add
void add(const std::string &tag)
add a tag to the list
Definition: TagList.cxx:136
SH::MetaNames::sampleName
static const std::string & sampleName()
the name of the sample being processed
Definition: MetaNames.cxx:57
SamplePtr.h
RCU_PROVIDE
#define RCU_PROVIDE(x)
Definition: Assert.h:215
tags
std::vector< std::string > tags
Definition: hcg.cxx:102
MetaObject.h
RCU_REQUIRE_SOFT
#define RCU_REQUIRE_SOFT(x)
Definition: Assert.h:153
SH::Sample::fetchMeta
void fetchMeta(const Sample &source)
fetch the meta-data from the given sample.
RCU_REQUIRE2
#define RCU_REQUIRE2(x, y)
Definition: Assert.h:210
SH::Sample::makeFileList
std::vector< std::string > makeFileList() const
make a list of all files, prestaging them if necessary
generateReferenceFile.files
files
Definition: generateReferenceFile.py:12
file
TFile * file
Definition: tile_monitor.h:29
dumpFileToPlots.treeName
string treeName
Definition: dumpFileToPlots.py:20
SH::Sample::getMetaDouble
double getMetaDouble(const std::string &name, double def_val=0) const
the meta-data double with the given name
SH::MetaFields::gridFilter
static const std::string gridFilter
the field containing the file filter for the dataset on the grid
Definition: MetaFields.h:38
SH::Sample::tags
const TagList & tags() const
the tag list we are using
SH::Sample::getContains
virtual bool getContains(const std::string &name) const
whether this sample contains a sample of the given name
SH::MetaObject::castString
std::string castString(const std::string &name, const std::string &def_val="", CastMode mode=CAST_ERROR_THROW) const
the meta-data string with the given name
SH::Sample::getFileName
virtual std::string getFileName(std::size_t index) const =0
the name of the file with the given index
SH::Sample::removeMeta
void removeMeta(const std::string &name)
remove all meta-information with the given name
SH::Sample::meta
MetaObject * meta()
the meta-information for this sample
SH::MetaFields::treeName
static const std::string treeName
the name of the tree in the sample
Definition: MetaFields.h:52
RCU_INVARIANT
#define RCU_INVARIANT(x)
Definition: Assert.h:201
SH::Sample::name
const std::string & name() const
the name of the sample we are using
SH::Sample::testInvariant
void testInvariant() const
test the invariant of this object
SH::Sample::release
void release() const
decrease the reference count by one
SH::MetaObject::addReplace
void addReplace(TNamed *meta_swallow)
add a meta-data object and remove any existing meta-data with the same name
SH::Sample
a base class that manages a set of files belonging to a particular data set and the associated meta-d...
Definition: Sample.h:54
SH::MetaObject::setString
void setString(const std::string &name, const std::string &value)
set the meta-data string with the given name
SH::Sample::dbg
std::string dbg(const Sample &obj, unsigned verbosity=0)
the debugging info of this object
SH::Sample::~Sample
virtual ~Sample()
standard destructor
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:228
SH::Sample::contains
bool contains(const std::string &name) const
whether this sample contains a sample of the given name
RCU::SetDirectory
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
MetaFields.h
SH::Sample::makeLocal
SamplePtr makeLocal() const
Make this a local sample, i.e.
covarianceTool.verbosity
verbosity
Definition: covarianceTool.py:513
SH::Sample::printContent
void printContent() const
print the debugging output to the screen
SH::Sample::m_references
std::atomic< unsigned > m_references
the reference count
Definition: Sample.h:487
SH::Sample::fileName
std::string fileName(std::size_t index) const
the name of the file with the given index
SampleGrid.h
CxxUtils::to
CONT to(RANGE &&r)
Definition: ranges.h:39
SH::MetaObject::get
TObject * get(const std::string &name)
the meta-data object with the given name
SH::Sample::doAddSamples
virtual void doAddSamples(SampleHandler &result)
add all samples this sample corresponds to to the given sample handler
SH::Sample::makeTChain
TChain * makeTChain() const
create a TChain object, containing all these files
SH::MetaFields::gridName
static const std::string gridName
the field containing the name of the dataset on the grid
Definition: MetaFields.h:34
RCU_DESTROY_INVARIANT
#define RCU_DESTROY_INVARIANT(x)
Definition: Assert.h:235
SH::Sample::m_name
std::string m_name
the name of the sample we are using
Definition: Sample.h:475
SH::Sample::Sample
Sample(const std::string &name)
standard constructor
SH::MetaObject::castDouble
double castDouble(const std::string &name, double def_val=0, CastMode mode=CAST_ERROR_THROW) const
the meta-data double with the given name
RCU_CHANGE_INVARIANT
#define RCU_CHANGE_INVARIANT(x)
Definition: Assert.h:231
SH::Sample::doUpdateLocation
virtual void doUpdateLocation(const std::string &from, const std::string &to)
update all file references starting with from to to
SH::Sample::getNumFiles
virtual std::size_t getNumFiles() const =0
the number of files in the sample
pickleTool.object
object
Definition: pickleTool.py:30
copySelective.source
string source
Definition: copySelective.py:32
SH
This module provides a lot of global definitions, forward declarations and includes that are used by ...
Definition: PrunDriver.h:15
RCU_THROW_MSG
#define RCU_THROW_MSG(message)
Definition: PrintMsg.h:58
CaloCondBlobAlgs_fillNoiseFromASCII.tag
string tag
Definition: CaloCondBlobAlgs_fillNoiseFromASCII.py:24
SH::MetaFields::treeName_default
static const std::string treeName_default
the default value of treeName
Definition: MetaFields.h:55
SH::Sample::setMetaString
void setMetaString(const std::string &name, const std::string &value)
set the meta-data string with the given name
SH::Sample::doMakeFileList
virtual std::vector< std::string > doMakeFileList() const =0
make a list of all files, prestaging them if necessary
python.PyAthena.obj
obj
Definition: PyAthena.py:132
RCU_READ_INVARIANT
#define RCU_READ_INVARIANT(x)
Definition: Assert.h:229
Sample.h
SH::Sample::alloc
void alloc() const
increase the reference count by one
PrintMsg.h
SH::MetaObject::setDouble
void setDouble(const std::string &name, double value)
set the meta-data double with the given name
SH::Sample::addSamples
void addSamples(SampleHandler &result)
add all samples this sample corresponds to to the given sample handler
RCU_NEW_INVARIANT
#define RCU_NEW_INVARIANT(x)
Definition: Assert.h:233