RDFAnalysis  0.1.1
Physics analysis with ROOT::RDataFrame
SchedulerBase.h
Go to the documentation of this file.
1 #ifndef RDFAnalysis_SchedulerBase_H
2 #define RDFAnalysis_SchedulerBase_H
3 
5 
6 #include <string>
7 #include <map>
8 #include <set>
9 #include <vector>
10 
11 namespace RDFAnalysis {
18  class SchedulerBase {
19  public:
21  enum ActionType {
31  };
33  static std::string actionTypeToString(ActionType type);
34 
36  struct RegionDef {
38  std::vector<std::string> filterList;
40  std::set<std::string> fills;
42  void addFill(const std::string& fill) {fills.insert(fill); }
43  }; //> end struct RegionDef
44 
54  const std::string& name,
55  const std::vector<std::string>& filterList);
56 
58  std::map<std::string, RegionDef>& regionDefs()
59  { return m_regionDefs;}
61  const std::map<std::string, RegionDef>& regionDefs() const
62  { return m_regionDefs;}
63 
74  void filterSatisfies(
75  const std::string& filter,
76  const std::vector<std::string>& satisfied);
77 
86  struct Action {
88  Action(ActionType type, const std::string& name, float cost = 0) :
89  type(type), name(name), cost(cost) {}
90 
94  std::string name;
96  float cost;
97 
100  bool operator<(const Action& rhs) const {
101  return (type == rhs.type ? name < rhs.name : type < rhs.type);
102  }
104  bool operator==(const Action& rhs) const {
105  return type == rhs.type && name == rhs.name;
106  }
109  struct CostOrdering {
111  bool operator()(const Action& lhs, const Action& rhs) const
112  {
113  return (lhs.cost == rhs.cost ? lhs < rhs : lhs.cost < rhs.cost);
114  }
115  }; //> end struct CostOrdering
116 
126  std::map<Action, std::set<Action>, CostOrdering> expand(
127  const SchedulerBase& scheduler,
128  const std::set<Action>& preExisting = {}) const;
141  std::map<Action, std::set<Action>, CostOrdering> expand(
142  const SchedulerBase& scheduler,
143  const std::set<Action>& preExisting,
144  std::vector<Action>& processing) const;
145 
147  void retrieveCost(const SchedulerBase& scheduler);
148  }; //> end struct Action
149 
157  struct ScheduleNode {
159  ScheduleNode(const Action& action) : action(action) {}
163  std::map<Action, std::set<Action>, Action::CostOrdering> dependencies;
165  std::vector<ScheduleNode> children;
168  std::string region;
169 
177  const Action& next() const;
178 
180  void removeDependency(Action action, const SchedulerBase& scheduler);
181 
183  void expand(
184  const SchedulerBase& scheduler,
185  const std::set<Action>& preExisting = {})
186  { dependencies = action.expand(scheduler, preExisting); }
187  }; //> end struct ScheduleNode
188 
194  static void printSchedule(std::ostream& os, const ScheduleNode& root);
195 
198  ScheduleNode& getSchedule() { return m_schedule; }
201  const ScheduleNode& getSchedule() const { return m_schedule; }
202 
205  const std::vector<std::string>& usedVariables() const { return m_usedVars; }
206 
211  const std::set<Action>& getDependencies(const Action& action) const;
212 
217  float getCost(const Action& action) const;
218 
227  bool isActionSatisfiedBy(
228  const Action& action,
229  const std::set<Action>& candidates,
230  bool considerSelf = true) const;
231 
242  bool isActionSatisfiedBy(
243  const Action& action,
244  const std::set<Action>& candidates,
245  Action& satisfiedBy,
246  bool considerSelf = true) const;
247  protected:
248 
255  void addAction(
256  const Action& action,
257  const std::set<Action>& dependencies);
258 
269  std::map<Action, Action> buildReplacementMap(
270  const std::set<Action>& filters) const;
271 
278  const std::string& name,
279  const std::vector<std::string>& defined);
280 
287  ScheduleNode& schedule(const IBranchNamer& namer);
288 
289 
296  ScheduleNode rawSchedule() const;
297 
298  private:
299  void addChildren(
300  std::vector<ScheduleNode>&& sources,
301  ScheduleNode* target,
302  std::set<Action> preExisting);
304  std::map<std::string, RegionDef> m_regionDefs;
305 
307  std::map<Action, std::set<Action>> m_dependencies;
308 
312  std::map<Action, std::set<Action>> m_satisfiedBy;
313 
315  ScheduleNode m_schedule{{FILTER, "ROOT"}};
316 
318  std::vector<std::string> m_usedVars;
319 
320  void expandSatisfiesRelations(
321  std::map<Action, std::set<Action>>::iterator itr,
322  std::set<Action>& processed);
323  }; //> end namespace SchedulerBase
324 } //> end namespace RDFAnalysis
325 
326 #endif //> !RDFAnalysis_SchedulerBase_H
Helper struct to represent all the information that the scheduler needs to know about an action in or...
Definition: SchedulerBase.h:86
RegionDef & addRegion(const std::string &name, const std::vector< std::string > &filterList)
Add a region to be scheduled.
Definition: SchedulerBase.cxx:76
static void printSchedule(std::ostream &os, const ScheduleNode &root)
Print a schedule to a graphviz file.
Definition: SchedulerBase.cxx:533
bool operator<(const Action &rhs) const
Operator used to construct sets of actions. Order by type first, then name.
Definition: SchedulerBase.h:100
void expand(const SchedulerBase &scheduler, const std::set< Action > &preExisting={})
Expand this node&#39;s dependencies.
Definition: SchedulerBase.h:183
Abstract base class that describes how a Node should name its branches internally.
Definition: IBranchNamer.h:33
static std::string actionTypeToString(ActionType type)
Convert an ActionType to a string.
Definition: SchedulerBase.cxx:62
bool operator()(const Action &lhs, const Action &rhs) const
The actual ordering.
Definition: SchedulerBase.h:111
void addAction(const Action &action, const std::set< Action > &dependencies)
Add a new action to the record.
Definition: SchedulerBase.cxx:252
Action action
The action performed by this node.
Definition: SchedulerBase.h:161
const std::vector< std::string > & usedVariables() const
Get the variables used by this schedule. Will be empty if schedule has not been called.
Definition: SchedulerBase.h:205
Helper struct used to build and express the schedule.
Definition: SchedulerBase.h:157
float getCost(const Action &action) const
Get the cost of an action if the action is unknown.
Definition: SchedulerBase.cxx:283
ScheduleNode & schedule(const IBranchNamer &namer)
Build the schedule.
Definition: SchedulerBase.cxx:359
ActionType type
The type of this action.
Definition: SchedulerBase.h:92
float cost
The cost-estimate of this action.
Definition: SchedulerBase.h:96
void addFill(const std::string &fill)
Add a fill to the region.
Definition: SchedulerBase.h:42
const std::set< Action > & getDependencies(const Action &action) const
Get the dependency corresponding to an action. if the action is unknown.
Definition: SchedulerBase.cxx:271
std::string region
The region, if any, that this node defines (i.e. is the final action listed for that region) ...
Definition: SchedulerBase.h:168
std::map< Action, std::set< Action >, Action::CostOrdering > dependencies
The dependencies of that action.
Definition: SchedulerBase.h:163
Action(ActionType type, const std::string &name, float cost=0)
Create the action.
Definition: SchedulerBase.h:88
std::vector< std::string > filterList
Ordered list of filters defining the region.
Definition: SchedulerBase.h:38
ActionType
Enum to describe the different types of action.
Definition: SchedulerBase.h:21
std::set< std::string > fills
Set of fills to be performed on that region.
Definition: SchedulerBase.h:40
Base class for the scheduler.
Definition: SchedulerBase.h:18
A filter imposes a selection on the events it sees and can also create a branch in the tree structure...
Definition: SchedulerBase.h:24
Definition: CutflowDetail.h:11
std::map< Action, Action > buildReplacementMap(const std::set< Action > &filters) const
Look through a list of filters for any that satisfy each other.
Definition: SchedulerBase.cxx:332
ScheduleNode rawSchedule() const
Build the &#39;raw&#39; schedule.
Definition: SchedulerBase.cxx:383
The branch naming interface.
std::map< std::string, RegionDef > & regionDefs()
Access the current region definitions.
Definition: SchedulerBase.h:58
Helper struct to define a region.
Definition: SchedulerBase.h:36
A fill is required to return a SysResultPtr to a TObject.
Definition: SchedulerBase.h:28
const std::map< std::string, RegionDef > & regionDefs() const
(const) access the current region definitions
Definition: SchedulerBase.h:61
bool isActionSatisfiedBy(const Action &action, const std::set< Action > &candidates, bool considerSelf=true) const
Check whether an action has already been satisfied by one of a list of candidates.
Definition: SchedulerBase.cxx:295
std::string name
The name of this action.
Definition: SchedulerBase.h:94
std::map< Action, std::set< Action >, CostOrdering > expand(const SchedulerBase &scheduler, const std::set< Action > &preExisting={}) const
Expand the dependencies of this action.
Definition: SchedulerBase.cxx:202
const ScheduleNode & getSchedule() const
Get the ROOT of the output filter schedule. Will be empty if schedule has not been called...
Definition: SchedulerBase.h:201
ScheduleNode(const Action &action)
Build the node from the action it performs.
Definition: SchedulerBase.h:159
std::vector< ScheduleNode > children
The children of this node (i.e. the ones that follow it)
Definition: SchedulerBase.h:165
bool operator==(const Action &rhs) const
Equality comparison operator for actions.
Definition: SchedulerBase.h:104
void actionDefinesMultipleVariables(const std::string &name, const std::vector< std::string > &defined)
Tell the scheduler that an action is defining multiple variables.
Definition: SchedulerBase.cxx:350
A variable defines a new variable.
Definition: SchedulerBase.h:26
ScheduleNode & getSchedule()
Get the ROOT of the output filter schedule. Will be empty if schedule has not been called...
Definition: SchedulerBase.h:198
void filterSatisfies(const std::string &filter, const std::vector< std::string > &satisfied)
Tell the scheduler that a filter also satisfies the condition of other filters.
Definition: SchedulerBase.cxx:105
Ordering used in the actual scheduling process. Order by cost first, then type, then name...
Definition: SchedulerBase.h:109
Not a valid action, used to construct a placeholder action.
Definition: SchedulerBase.h:30