Belle II Software  release-08-01-10
RunRange.h
1 /**************************************************************************
2  * basf2 (Belle II Analysis Software Framework) *
3  * Author: The Belle II Collaboration *
4  * *
5  * See git log for contributors and copyright holders. *
6  * This file is licensed under LGPL-3.0, see LICENSE.md. *
7  **************************************************************************/
8 #pragma once
9 
10 #include <string>
11 #include <vector>
12 #include <set>
13 #include <TNamed.h>
14 #include <TCollection.h>
15 #include <framework/database/IntervalOfValidity.h>
16 #include <framework/logging/Logger.h>
17 #include <calibration/Utilities.h>
18 
19 namespace Belle2 {
25  class RunRange : public TNamed {
26 
27  public:
29  RunRange() : TNamed() {};
30 
32  explicit RunRange(std::vector<Calibration::ExpRun> expRuns) : TNamed()
33  {
34  for (auto expRun : expRuns) {
35  this->add(expRun.first, expRun.second);
36  }
37  };
38 
40  virtual ~RunRange()
41  {
42  m_expRunSet.clear();
43  }
44 
46  virtual void clear()
47  {
48  m_expRunSet.clear();
49  }
50 
52  virtual void merge(const RunRange* other)
53  {
54  m_expRunSet.insert(other->m_expRunSet.begin(), other->m_expRunSet.end());
55  }
56 
58  void add(int exp, int run)
59  {
60  m_expRunSet.insert(std::make_pair(exp, run));
61  }
62 
64  const std::set<Calibration::ExpRun>& getExpRunSet()
65  {
66  return m_expRunSet;
67  }
68 
71  {
72  if (m_expRunSet.empty())
73  return IntervalOfValidity();
74 
75  auto low = m_expRunSet.begin();
76  auto high = m_expRunSet.rbegin();
77 
78  return IntervalOfValidity(low->first, low->second, high->first, high->second);
79  }
80 
82  Long64_t Merge(TCollection* hlist)
83  {
84  B2DEBUG(100, "Running Merge() on " << this->GetName());
85  Long64_t nMerged = 0;
86  if (hlist) {
87  const RunRange* xh = 0;
88  TIter nxh(hlist);
89  while ((xh = dynamic_cast<RunRange*>(nxh()))) {
90  // Add xh to me
91  merge(xh);
92  ++nMerged;
93  }
94  }
95  B2DEBUG(100, "Merged " << nMerged << " objects");
96  return nMerged;
97  }
98 
100  void setGranularity(const std::string& granularity)
101  {
102  if (granularity == "all" || granularity == "run") {
103  m_granularity = granularity;
104  } else {
105  B2WARNING("Tried to set RunRange granularity to something other than 'run' or 'all' -> " << granularity);
106  }
107  }
108 
110  std::string getGranularity() const {return m_granularity;}
112  void Reset() {clear();}
114  void SetDirectory(TDirectory*) {}
115 
117  bool operator<(const RunRange& other) const
118  {
119  if (m_granularity == other.m_granularity) {
120  return m_expRunSet < other.m_expRunSet;
121  }
122  return m_granularity < other.m_granularity;
123  }
124 
125  private:
127  std::set<Calibration::ExpRun> m_expRunSet = {};
128 
130  std::string m_granularity = "run";
131 
132  ClassDef(RunRange, 2)
133  };
135 }
A class that describes the interval of experiments/runs for which an object in the database is valid.
Mergeable object holding (unique) set of (exp,run) pairs.
Definition: RunRange.h:25
virtual void merge(const RunRange *other)
Implementation of merging - other is added to the set (union)
Definition: RunRange.h:52
Long64_t Merge(TCollection *hlist)
Allow merging using TFileMerger if saved directly to a file.
Definition: RunRange.h:82
void add(int exp, int run)
Add an experiment and run number to the set.
Definition: RunRange.h:58
RunRange()
Constructor.
Definition: RunRange.h:29
void Reset()
Root-like Reset function for "template compatibility" with ROOT objects.
Definition: RunRange.h:112
const std::set< Calibration::ExpRun > & getExpRunSet()
Get access to the stored set.
Definition: RunRange.h:64
virtual ~RunRange()
Destructor.
Definition: RunRange.h:40
std::string m_granularity
granularity used by the collector storing the information.
Definition: RunRange.h:130
void setGranularity(const std::string &granularity)
Set the m_granularity to an allowed value.
Definition: RunRange.h:100
RunRange(std::vector< Calibration::ExpRun > expRuns)
Constructor from vector of ExpRun objects.
Definition: RunRange.h:32
std::set< Calibration::ExpRun > m_expRunSet
The set of (exp,run) stored in object.
Definition: RunRange.h:127
virtual void clear()
Implementation of clearing - resets stored run set.
Definition: RunRange.h:46
bool operator<(const RunRange &other) const
Comparison operator so that we can use RunRange in a map as a key.
Definition: RunRange.h:117
std::string getGranularity() const
Gets the m_granularity.
Definition: RunRange.h:110
IntervalOfValidity getIntervalOfValidity()
Make IntervalOfValidity from the set, spanning all runs. Works because sets are sorted by default.
Definition: RunRange.h:70
void SetDirectory(TDirectory *)
Root-like SetDirectory function for "template compatibility" with ROOT objects. Does nothing.
Definition: RunRange.h:114
Abstract base class for different kinds of events.