Loading [MathJax]/extensions/tex2jax.js
ATLAS Offline Software
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
SampleHandler.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2025 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 //protect
20 
21 #include <iostream>
22 #include <sstream>
23 #include <TFile.h>
24 #include <TSystem.h>
25 #include <RootCoreUtils/Assert.h>
28 #include <RootCoreUtils/ThrowMsg.h>
32 #include <SampleHandler/Sample.h>
34 
35 //
36 // method implementations
37 //
38 
40 
41 namespace SH
42 {
43  namespace
44  {
45  /*
46  bool isspace (const std::string& str)
47  {
48  for (std::string::const_iterator iter = str.begin(),
49  end = str.end(); iter != end; ++ iter)
50  {
51  if (!std::isspace (*iter))
52  return false;
53  };
54  return true;
55  }
56  */
57  }
58 
59 
60 
61  std::string dbg (const SampleHandler& obj, unsigned verbosity)
62  {
63  std::ostringstream result;
64  result << "SampleHandler with " << obj.size() << " files";
65  if (verbosity % 10 > 0)
66  {
67  result << "\n";
68  for (SampleHandler::iterator sample = obj.begin(),
69  end = obj.end(); sample != end; ++ sample)
70  {
71  result << dbg (**sample, verbosity / 10) << "\n";
72  };
73  };
74  return result.str();
75  }
76 
77 
78 
79  void swap (SampleHandler& a, SampleHandler& b)
80  {
81  swap (a.m_samples, b.m_samples);
82  swap (a.m_named, b.m_named);
83  }
84 
85 
86 
87  void SampleHandler ::
88  testInvariant () const
89  {
90  }
91 
92 
93 
96  {
98  RCU_NEW_INVARIANT (this);
99  }
100 
101 
102 
104  SampleHandler (const SampleHandler& that)
105  : TObject (that), m_samples (that.m_samples), m_named (that.m_named)
106  {
107  RCU_NEW_INVARIANT (this);
108  }
109 
110 
111 
114  {
115  RCU_DESTROY_INVARIANT (this);
116  }
117 
118 
119 
120  SampleHandler& SampleHandler ::
121  operator = (const SampleHandler& that)
122  {
123  // no invariant used
124  SampleHandler tmp (that);
125  swap (tmp, *this);
126  return *this;
127  }
128 
129 
130 
131  void SampleHandler ::
132  add (Sample *sample)
133  {
134  SamplePtr mysample (sample);
135 
136  // invariant not used
137  RCU_REQUIRE_SOFT (sample != 0);
138 
139  add (mysample);
140  }
141 
142 
143 
144  void SampleHandler ::
145  add (std::unique_ptr<Sample> sample)
146  {
147  // no invariant used
148  add (SamplePtr (std::move (sample)));
149  }
150 
151 
152 
153  void SampleHandler ::
154  add (SamplePtr& sample)
155  {
156  RCU_CHANGE_INVARIANT (this);
157  RCU_REQUIRE_SOFT (!sample.empty());
158  RCU_REQUIRE_SOFT (!sample->name().empty());
159 
160  if (!sample->name().empty() && m_named.find (sample->name()) != m_named.end())
161  RCU_THROW_MSG ("can't add sample of name " + sample->name() + "\na sample with that name already exists\nold sample:\n" + dbg (*m_named.find (sample->name())->second, 9999) + "\nnew sample:\n" + dbg (*sample, 9999));
162 
163  try
164  {
165  m_samples.push_back (sample.get());
166  if (!sample->name().empty())
167  m_named[sample->name()] = sample;
168  } catch (...)
169  {
170  if (m_samples.back() == sample.get())
171  m_samples.pop_back();
172  };
173  }
174 
175 
176 
177  void SampleHandler ::
178  add (SamplePtr&& sample)
179  {
180  add (sample);
181  }
182 
183 
184 
185  void SampleHandler ::
186  add (const SampleHandler& sh)
187  {
188  // invariant not used
189  RCU_REQUIRE_SOFT (this != &sh);
190 
191  for (iterator iter = sh.begin(), end2 = sh.end();
192  iter != end2; ++ iter)
193  {
194  add (*iter);
195  };
196  }
197 
198 
199 
200  void SampleHandler ::
201  addWithPrefix (const SampleHandler& sh, const std::string& prefix)
202  {
203  // invariant not used
204  RCU_REQUIRE_SOFT (this != &sh);
205 
206  for (iterator iter = sh.begin(), end2 = sh.end();
207  iter != end2; ++ iter)
208  {
209  std::unique_ptr<Sample> sample (dynamic_cast<Sample*>((*iter)->Clone ()));
210  RCU_ASSERT (sample != nullptr);
211  sample->name (prefix + (*iter)->name());
212  add (sample.release());
213  };
214  }
215 
216 
217 
218  void SampleHandler ::
219  remove (const std::string& name)
220  {
221  // invariant not used
222  const Sample *sample = get (name);
223  if (sample == 0)
224  RCU_THROW_MSG ("sample " + name + " not found in SampleHandler");
225  remove (sample);
226  }
227 
228 
229 
230  void SampleHandler ::
231  remove (const Sample *sample)
232  {
233  RCU_CHANGE_INVARIANT (this);
234  RCU_REQUIRE_SOFT (sample != 0);
235 
236  NamedMIter nameIter = m_named.find (sample->name());
237  if (nameIter == m_named.end())
238  RCU_THROW_MSG ("sample " + sample->name() + " not found in SampleHandler");
239  if (nameIter->second.get() != sample)
240  RCU_THROW_MSG ("different sample of name " + sample->name() + " found in SampleHandler");
241  std::erase (m_samples, sample);
242  m_named.erase (nameIter);
243  }
244 
245 
246 
247  Sample *SampleHandler ::
248  get (const std::string& name)
249  {
250  RCU_READ_INVARIANT (this);
251 
252  NamedIter iter = m_named.find (name);
253  if (iter != m_named.end())
254  return iter->second.get();
255  return 0;
256  }
257 
258 
259  const Sample *SampleHandler ::
260  get (const std::string& name) const
261  {
262  RCU_READ_INVARIANT (this);
263 
264  auto iter = m_named.find (name);
265  if (iter != m_named.end())
266  return iter->second.get();
267  return 0;
268  }
269 
270 
271 
272  SampleHandler SampleHandler ::
273  find (const std::string& tags) const
274  {
275  // no invariant used
276  return find (TagList (tags, ','));
277  }
278 
279 
280 
281  SampleHandler SampleHandler ::
282  find (const TagList& tags) const
283  {
284  RCU_READ_INVARIANT (this);
285 
287 
288  for (SamplesIter sample = m_samples.begin(),
289  end = m_samples.end(); sample != end; ++ sample)
290  {
291  bool use = false;
292  for (TagList::iterator iter = tags.begin(),
293  end = tags.end(); !use && iter != end; ++ iter)
294  use = (*sample)->tags().has (*iter);
295  if (use)
296  result.add (*sample);
297  };
298  return result;
299  }
300 
301 
302 
303  Sample *SampleHandler ::
304  findBySource (const std::string& name) const
305  {
306  RCU_READ_INVARIANT (this);
307 
308  std::vector<Sample*> result;
309  for (iterator sample = begin(),
310  end2 = end(); sample != end2; ++ sample)
311  {
312  if (name == (*sample)->meta()->castString (MetaFields::sourceSample, (*sample)->name()))
313  result.push_back ((*sample));
314  };
315  if (result.size() > 1)
316  {
317  std::ostringstream message;
318  message << "multiple samples have " << name << " as a source:";
319  for (std::vector<Sample*>::const_iterator sample = result.begin(),
320  end = result.end(); sample != end; ++ sample)
321  message << " " << (*sample)->name();
322  RCU_THROW_MSG (message.str());
323  };
324  if (result.empty())
325  return 0;
326  return result.front();
327  }
328 
329 
330 
331  SampleHandler SampleHandler ::
332  findByName (const std::string& pattern) const
333  {
334  RCU_READ_INVARIANT (this);
336  boost::regex expr (pattern);
337  for (iterator iter = begin(), end = this->end(); iter != end; ++ iter)
338  {
339  if (RCU::match_expr (expr, (*iter)->name()))
340  result.add (*iter);
341  }
342  return result;
343  }
344 
345 
346 
347  void SampleHandler ::
348  print () const
349  {
350  RCU_READ_INVARIANT (this);
351  std::cout << dbg (*this, 9999) << std::endl;
352  }
353 
354 
355 
356  void SampleHandler ::
357  printContent () const
358  {
359  // not using invariant
360  print ();
361  }
362 
363 
364 
365  void SampleHandler ::
366  save (const std::string& directory) const
367  {
368  RCU_READ_INVARIANT (this);
369 
370  // rationale: not checking the return status, since this is just a
371  // courtesy directory creation that is Ok to fail.
372  gSystem->MakeDirectory (directory.c_str());
373  for (iterator iter = this->begin(),
374  end = this->end(); iter != end; ++ iter)
375  {
376  TFile file ((directory + "/" + (*iter)->name() + ".root").c_str(), "RECREATE");
377  (*iter)->Write ("sample");
378  };
379  }
380 
381 
382 
383  void SampleHandler ::
384  load (const std::string& directory)
385  {
386  RCU_CHANGE_INVARIANT (this);
387 
388  DiskListLocal mydir (directory);
389  while (mydir.next())
390  {
391  const std::string file = mydir.fileName();
392 
393  if (file.size() > 5 &&
394  file.rfind (".root") == file.size() - 5)
395  {
396  TFile myfile (mydir.path().c_str(), "READ");
397  Sample *const sample = dynamic_cast<Sample*>(myfile.Get ("sample"));
398  if (sample != 0)
399  add (sample);
400  };
401  };
402  }
403 
404 
405 
406  void SampleHandler ::
407  updateLocation (const std::string& from, const std::string& to)
408  {
409  // no invariant used
410  RCU_REQUIRE_SOFT (!from.empty());
411  RCU_REQUIRE_SOFT (!to.empty());
412  for (iterator sample = begin(),
413  end = this->end(); sample != end; ++ sample)
414  (*sample)->updateLocation (from, to);
415  }
416 
417 
418 
419  void SampleHandler ::
420  fetch (const SampleHandler& source)
421  {
422  // invariant not used
423 
424  for (iterator sample = begin(),
425  end2 = end(); sample != end2; ++ sample)
426  {
427  const std::string name
428  = (*sample)->meta()->castString (MetaFields::sourceSample, (*sample)->name());
429  const Sample *const mysource = source.get (name);
430  if (mysource)
431  (*sample)->meta()->fetch (*mysource->meta());
432  };
433  }
434 
435 
436 
437  void SampleHandler ::
438  fetchDefaults (const SampleHandler& source)
439  {
440  // invariant not used
441 
442  for (iterator sample = begin(),
443  end2 = end(); sample != end2; ++ sample)
444  {
445  const std::string name
446  = (*sample)->meta()->castString (MetaFields::sourceSample, (*sample)->name());
447  const Sample *const mysource = source.get (name);
448  if (mysource)
449  (*sample)->meta()->fetchDefaults (*mysource->meta());
450  };
451  }
452 
453 
454 
455  bool SampleHandler ::
456  check_complete (const SampleHandler& source) const
457  {
458  // invariant not used
459 
460  std::set<std::string> names;
461  for (iterator sample = begin(),
462  end2 = end(); sample != end2; ++ sample)
463  {
464  names.insert ((*sample)->meta()->castString (MetaFields::sourceSample, (*sample)->name()));
465  };
466 
467  for (iterator sample = source.begin(),
468  end2 = source.end(); sample != end2; ++ sample)
469  {
470  if (names.find ((*sample)->name()) == names.end())
471  return false;
472  };
473  return true;
474  }
475 
476 
477 
478  void SampleHandler ::
479  setMetaDouble (const std::string& name, double value)
480  {
481  // no invariant used
482 
483  for (iterator sample = begin(),
484  end2 = end(); sample != end2; ++ sample)
485  {
486  (*sample)->meta()->setDouble (name, value);
487  };
488  }
489 
490 
491 
492  void SampleHandler ::
493  setMetaString (const std::string& name, const std::string& value)
494  {
495  // no invariant used
496 
497  for (iterator sample = begin(),
498  end2 = end(); sample != end2; ++ sample)
499  {
500  (*sample)->meta()->setString (name, value);
501  };
502  }
503 
504 
505 
506  void SampleHandler ::
507  setMetaDouble (const std::string& pattern, const std::string& name,
508  double value)
509  {
510  // no invariant used
511 
512  boost::regex mypattern (pattern);
513 
514  for (iterator sample = begin(),
515  end2 = end(); sample != end2; ++ sample)
516  {
517  if (RCU::match_expr (mypattern, (*sample)->name()))
518  (*sample)->meta()->setDouble (name, value);
519  };
520  }
521 
522 
523 
524  void SampleHandler ::
525  setMetaString (const std::string& pattern, const std::string& name,
526  const std::string& value)
527  {
528  // no invariant used
529 
530  boost::regex mypattern (pattern);
531 
532  for (iterator sample = begin(),
533  end2 = end(); sample != end2; ++ sample)
534  {
535  if (RCU::match_expr (mypattern, (*sample)->name()))
536  (*sample)->meta()->setString (name, value);
537  };
538  }
539 
540 
541 
543  begin () const
544  {
545  RCU_READ_INVARIANT (this);
546  return m_samples.begin();
547  }
548 
549 
550 
552  end () const
553  {
554  RCU_READ_INVARIANT (this);
555  return m_samples.end();
556  }
557 
558 
559 
560  std::size_t SampleHandler ::
561  size () const
562  {
563  RCU_READ_INVARIANT (this);
564  return m_samples.size();
565  }
566 
567 
568 
569  Sample *SampleHandler ::
570  operator [] (std::size_t index) const
571  {
572  // no invariant used
573  return at (index);
574  }
575 
576 
577 
578  Sample *SampleHandler ::
579  at (std::size_t index) const
580  {
581  RCU_READ_INVARIANT (this);
583  return m_samples[index];
584  }
585 
586 
587 
588  void SampleHandler ::
589  Streamer (TBuffer& b)
590  {
591  if (b.IsReading())
592  {
593  RCU_CHANGE_INVARIANT (this);
595  ULong_t count = 0;
596  b.ReadULong (count);
597  for (ULong_t iter = 0; iter != count; ++ iter)
598  {
599  Sample *sample = 0;
600  b >> sample;
601  sh.add (sample);
602  }
603  swap (*this, sh);
604  } else
605  {
606  RCU_READ_INVARIANT (this);
607  ULong_t count = m_samples.size(), count2 = 0;
608  b.WriteULong (count);
609  for (SamplesIter iter = m_samples.begin(),
610  end = m_samples.end(); iter != end; ++ iter, ++ count2)
611  {
612  Sample *sample = *iter;
613  b << sample;
614  }
615  };
616  }
617 }
SH::SampleHandler::addWithPrefix
void addWithPrefix(const SampleHandler &sh, const std::string &prefix)
add all samples from the given SampleHandler to this one, with prefix prepended to their name
mergePhysValFiles.pattern
pattern
Definition: DataQuality/DataQualityUtils/scripts/mergePhysValFiles.py:26
SH::SampleHandler::findBySource
Sample * findBySource(const std::string &name) const
find a sample by the name in the source sample handler
SH::SampleHandler::fetchDefaults
void fetchDefaults(const SampleHandler &source)
fetch the meta-data for all that samples that don't have it in this sample.
SH::SampleHandler::iterator
std::vector< Sample * >::const_iterator iterator
the iterator to use
Definition: SampleHandler.h:475
ClassImp
ClassImp(SH::SampleHandler) namespace SH
Definition: SampleHandler.cxx:39
SH::SampleHandler::NamedIter
std::map< std::string, SH::SamplePtr >::iterator NamedIter
the iterator for m_named
Definition: SampleHandler.h:545
DiskListLocal.h
get_generator_info.result
result
Definition: get_generator_info.py:21
SH::SampleHandler::at
Sample * at(std::size_t index) const
the sample with the given index
SH::SampleHandler::add
void add(Sample *sample)
add a sample to the handler
SH::SampleHandler::~SampleHandler
~SampleHandler()
standard destructor
CheckRootVersion.h
SH::dbg
std::string dbg(const Meta &, unsigned)
Definition: Meta.cxx:28
index
Definition: index.py:1
SH::SampleHandler::updateLocation
void updateLocation(const std::string &from, const std::string &to)
update all file references starting with from to to
SH::SampleHandler::operator=
SampleHandler & operator=(const SampleHandler &that)
standard assignment operator
SH::SampleHandler::find
SampleHandler find(const TagList &tags) const
find all samples which have at least one of the given tags.
SH::SampleHandler::m_samples
std::vector< SH::Sample * > m_samples
the list of samples managed
Definition: SampleHandler.h:529
SH::SampleHandler::end
iterator end() const
the end iterator to use
athena.value
value
Definition: athena.py:124
SampleHandler.h
SH::SampleHandler::printContent
void printContent() const
print the debugging output to the screen
SH::SampleHandler::size
std::size_t size() const
the number of samples contained
Assert.h
SH::SampleHandler::print
void print() const
print the debugging output to the screen
grl_maker.mydir
mydir
Definition: grl_maker.py:4
ReweightUtils.message
message
Definition: ReweightUtils.py:15
StringUtil.h
XMLtoHeader.count
count
Definition: XMLtoHeader.py:85
mergePhysValFiles.end
end
Definition: DataQuality/DataQualityUtils/scripts/mergePhysValFiles.py:93
SH::SampleHandler::save
void save(const std::string &directory) const
save the list of samples to the given directory
SH::SampleHandler::setMetaDouble
void setMetaDouble(const std::string &name, double value)
set the meta-data double with the given name for all samples.
PrepareReferenceFile.regex
regex
Definition: PrepareReferenceFile.py:43
SH::SampleHandler::testInvariant
void testInvariant() const
test the invariant of this object
SamplePtr.h
tags
std::vector< std::string > tags
Definition: hcg.cxx:102
MetaObject.h
FullCPAlgorithmsTest_eljob.sh
sh
Definition: FullCPAlgorithmsTest_eljob.py:114
FullCPAlgorithmsTest_eljob.sample
sample
Definition: FullCPAlgorithmsTest_eljob.py:116
RCU_REQUIRE_SOFT
#define RCU_REQUIRE_SOFT(x)
Definition: Assert.h:153
DeMoScan.directory
string directory
Definition: DeMoScan.py:80
RCU::Shell
Definition: ShellExec.cxx:28
python.subdetectors.mmg.names
names
Definition: mmg.py:8
RCU::match_expr
bool match_expr(const boost::regex &expr, const std::string &str)
returns: whether we can match the entire string with the regular expression guarantee: strong failure...
Definition: StringUtil.cxx:40
checkCorrelInHIST.prefix
dictionary prefix
Definition: checkCorrelInHIST.py:391
file
TFile * file
Definition: tile_monitor.h:29
DeMoUpdate.tmp
string tmp
Definition: DeMoUpdate.py:1167
WriteCalibToCool.swap
swap
Definition: WriteCalibToCool.py:94
SH::SampleHandler::dbg
std::string dbg(const SampleHandler &obj, unsigned verbosity=0)
the debugging info of this object
SH::SampleHandler::SampleHandler
SampleHandler()
standard constructor
SH::SampleHandler::load
void load(const std::string &directory)
load all the samples from the given directory
SH::SampleHandler::check_complete
bool check_complete(const SampleHandler &source) const
whether we have all samples from the source SampleHandler.
dbg
Definition: SGImplSvc.cxx:69
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:240
plotBeamSpotMon.b
b
Definition: plotBeamSpotMon.py:77
SH::SampleHandler::begin
iterator begin() const
the begin iterator to use
MetaFields.h
ThrowMsg.h
SH::TagList::iterator
std::set< std::string >::const_iterator iterator
the iterator to use
Definition: TagList.h:96
covarianceTool.verbosity
verbosity
Definition: covarianceTool.py:513
SH::MetaFields::sourceSample
static const std::string sourceSample
the original sample from which this sample was derived
Definition: MetaFields.h:49
SH::SampleHandler::setMetaString
void setMetaString(const std::string &name, const std::string &value)
set the meta-data string with the given name for all samples.
SH::SampleHandler::m_named
std::map< std::string, SH::SamplePtr > m_named
the list of samples by name
Definition: SampleHandler.h:541
CxxUtils::to
CONT to(RANGE &&r)
Definition: ranges.h:39
SH::SampleHandler::findByName
SampleHandler findByName(const std::string &pattern) const
find samples by pattern on the name
RCU::check_root_version
void check_root_version()
effects: check whether we are using a consistent root version guarantee: strong failures: version mis...
Definition: CheckRootVersion.cxx:31
DeMoScan.index
string index
Definition: DeMoScan.py:364
a
TList * a
Definition: liststreamerinfos.cxx:10
RCU_DESTROY_INVARIANT
#define RCU_DESTROY_INVARIANT(x)
Definition: Assert.h:235
SH::SampleHandler::operator[]
Sample * operator[](std::size_t index) const
the sample with the given index
RCU_CHANGE_INVARIANT
#define RCU_CHANGE_INVARIANT(x)
Definition: Assert.h:231
SH::SampleHandler::swap
friend void swap(SampleHandler &a, SampleHandler &b)
SH::SampleHandler::NamedMIter
std::map< std::string, SH::SamplePtr >::iterator NamedMIter
the mutable iterator for m_named
Definition: SampleHandler.h:549
SH::SampleHandler::remove
void remove(const std::string &name)
remove the given sample
copySelective.source
string source
Definition: copySelective.py:32
SH::SampleHandler
A class that manages a list of Sample objects.
Definition: SampleHandler.h:60
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
SH::SampleHandler::fetch
void fetch(const SampleHandler &source)
fetch the meta-data for all samples that are also in the source sample handler.
SH::SampleHandler::get
Sample * get(const std::string &name)
get the sample with the given name
SH::SampleHandler::SamplesIter
std::vector< SH::Sample * >::const_iterator SamplesIter
the iterator for m_samples
Definition: SampleHandler.h:533
python.PyAthena.obj
obj
Definition: PyAthena.py:132
RCU_ASSERT
#define RCU_ASSERT(x)
Definition: Assert.h:222
RCU_READ_INVARIANT
#define RCU_READ_INVARIANT(x)
Definition: Assert.h:229
Sample.h
RCU_NEW_INVARIANT
#define RCU_NEW_INVARIANT(x)
Definition: Assert.h:233