Crombie Tools
Lumi.h
Go to the documentation of this file.
1 #ifndef CROMBIE_LUMI_H
2 #define CROMBIE_LUMI_H
3 
4 #include <ostream>
5 #include <map>
6 #include <utility>
7 #include <list>
8 #include <cmath>
9 
10 #include "crombie/Debug.h"
11 #include "crombie/FileConfig.h"
12 #include "crombie/LoadTree.h"
13 #include "crombie/Misc.h"
14 
15 namespace crombie {
16  namespace Lumi {
17 
18  /// Stores the lumi and run as a map to ranges
19  class LumiSelection {
20  public:
21  /// Add a lumi range to this object
22  void add(unsigned long run, std::pair<unsigned, unsigned> lumi);
23  /// Add a single run, lumi to this object
24  void add(unsigned long run, unsigned lumi);
25  /// Merge other LumiSelection objects into this one
26  void add(const LumiSelection& other);
27  private:
28  /// A store that is kept in order and merges lumis in
29  std::map<unsigned long, std::list<std::pair<unsigned, unsigned>>> store;
30 
31  friend std::ostream& operator<<(std::ostream& os, const LumiSelection& selection);
32  };
33 
34  std::ostream& operator<<(std::ostream& os, const LumiSelection& selection);
35 
36  /// Return the LumiSelection for a single file
38  /// A functional for the FileConfig::runfiles to be happy
40 
41  /// Return the merged LumiSelection
43 
44 
45  // IMPLEMENTATIONS BELOW HERE //
46 
47 
48  namespace {
49  auto runname = Misc::env("runnum", "runNumber");
50  auto luminame = Misc::env("luminum", "lumiNumber");
51  }
52 
54  LoadTree::Tree loaded{info.name, runname, luminame};
55 
56  auto& run = loaded.result(runname);
57  auto& lumi = loaded.result(luminame);
58 
59  LumiSelection output;
60 
61  while (loaded.next())
62  output.add(run, lumi);
63 
64  return output;
65  }
66 
68  LumiSelection output {};
69  // For each possible directory
70  for (auto& info : outputs) {
71  // Add the LumiSelections in the vector
72  for (auto& sel : info.second)
73  output.add(sel);
74  }
75  return output;
76  }
77 
78  void LumiSelection::add(unsigned long run, std::pair<unsigned, unsigned> lumi) {
79  Debug::Debug(__PRETTY_FUNCTION__, "Adding", run, lumi.first, lumi.second);
80 
81  auto& to_insert = store[run];
82 
83  auto before{to_insert.begin()}; // Iterator the beginning of the new pair should go
84  auto after{before}; // Iterator where the end of the new pair should go
85 
86  for (; after != to_insert.end(); ++after) {
87  if ((lumi.second + 1) < after->first) // If would not merge higher, stop here
88  break;
89  if (before == after) { // If still finding, drag that along
90  if (lumi.first > (after->second + 1))
91  ++before;
92  }
93  }
94 
95  // Just need to insert
96  if (before == after)
97  to_insert.insert(before, lumi);
98  else {
99  // Otherwise, need best values
100  auto insertion = std::make_pair(std::min(lumi.first, before->first),
101  std::max(lumi.second, (--after)->second));
102  // Placed in front of "before"
103  to_insert.insert(before, insertion);
104  // Remove the other values
105  // Go forward one for after again, since we went backwards above for range
106  to_insert.erase(before, ++after);
107  }
108  }
109 
110  void LumiSelection::add(unsigned long run, unsigned lumi) {
111  add(run, std::make_pair(lumi, lumi));
112  }
113 
114  void LumiSelection::add(const LumiSelection& other) {
115  for (auto& run : other.store) {
116  for (auto& pair : run.second)
117  add(run.first, pair);
118  }
119  }
120 
121  std::ostream& operator<<(std::ostream& os, const LumiSelection& selection) {
122  os << "{";
123  bool startedrun = false;
124  for (auto& run : selection.store) {
125  bool startedlumi = false;
126  if (startedrun)
127  os << "], ";
128  startedrun = true;
129  os << '"' << run.first << '"' << ": [";
130  for (auto& lumi : run.second) {
131  if (startedlumi)
132  os << ", ";
133  startedlumi = true;
134  os << "[" << lumi.first << ", " << lumi.second << "]";
135  }
136  }
137  os << "]}" << std::endl;
138  return os;
139  }
140 
141  }
142 }
143 
144 #endif