CP3-llbb Framework
Category.h
1 #ifndef CATEGORY_H
2 #define CATEGORY_H
3 
4 #include <string>
5 #include <map>
6 #include <vector>
7 
8 #include <cp3_llbb/Framework/interface/Cut.h>
9 #include <cp3_llbb/Framework/interface/ProducersManager.h>
10 #include <cp3_llbb/Framework/interface/AnalyzersManager.h>
11 
12 #include <cp3_llbb/TreeWrapper/interface/TreeWrapper.h>
13 
14 class CategoryManager;
15 
17  // Empty
18 };
19 
20 class Category {
21  public:
22  virtual void configure(const edm::ParameterSet& config) {};
23 
24  virtual bool event_in_category_pre_analyzers(const ProducersManager& producers) const = 0;
25  virtual bool event_in_category_post_analyzers(const ProducersManager& producers, const AnalyzersManager& analyzers) const = 0;
26 
27  virtual void register_cuts(CutManager& manager) {};
28 
29  virtual void evaluate_cuts_pre_analyzers(CutManager& manager, const ProducersManager& producers) const {};
30  virtual void evaluate_cuts_post_analyzers(CutManager& manager, const ProducersManager& producers, const AnalyzersManager& analyzers) const {};
31 
32  virtual std::shared_ptr<CategoryMetadata> get_metadata() final {
33  return metadata;
34  };
35 
36  protected:
37  std::shared_ptr<CategoryMetadata> metadata;
38 };
39 
40 struct CategoryData {
41  std::string name;
42  std::string description;
43  std::unique_ptr<Category> callback;
44  ROOT::TreeGroup tree;
45  CutManager cut_manager;
46 
47  uint64_t events = 0;
48 
49  // Tree branches
50  bool in_category_pre = false;
51  bool in_category_post = false;
52  bool& in_category;
53 
54  CategoryData(const std::string& name_, const std::string& description_, std::unique_ptr<Category> category, ROOT::TreeWrapper& tree_):
55  name(name_),
56  description(description_),
57  callback(std::move(category)),
58  tree(tree_.group(name_ + "_")),
59  cut_manager(*this),
60  in_category(tree["category"].write<bool>())
61  {
62  callback->register_cuts(cut_manager);
63  }
64 };
65 
67  public:
68  CategoryWrapper(const CategoryData& d): data(d) {
69  // Empty
70  }
71 
72  bool in_category() const {
73  return data.in_category_pre;
74  }
75 
76  bool cut_passed(const std::string& cut_name) const {
77  return data.cut_manager.cut_passed(cut_name);
78  }
79 
80  std::shared_ptr<CategoryMetadata> get_metadata() const {
81  return data.callback->get_metadata();
82  }
83 
84  private:
85  const CategoryData& data;
86 };
87 
89  friend class ExTreeMaker;
90 
91  public:
92  template<class T>
93  void new_category(const std::string& name, const std::string& description, const edm::ParameterSet& config) {
94  std::string internal_name = m_current_prefix + name;
95  if (m_categories.count(internal_name) > 0) {
96  throw edm::Exception(edm::errors::InsertFailure, "A category named '" + internal_name + "' already exists for this analyzer.");
97  }
98 
99  std::unique_ptr<Category> category(new T());
100  category->configure(config);
101  m_categories.emplace(internal_name, CategoryData(internal_name, description, std::move(category), m_tree));
102  }
103 
104  bool in_category(const std::string& name) const {
105 
106  std::string internal_name = m_current_prefix + name;
107  const auto& category = m_categories.find(internal_name);
108  if (category == m_categories.end()) {
109  std::stringstream details;
110  details << "Category '" << name << "' not found.";
111  throw edm::Exception(edm::errors::NotFound, details.str());
112  }
113 
114  return category->second.in_category_pre;
115  }
116 
117  const CategoryWrapper get(const std::string& name) const {
118 
119  std::string internal_name = m_current_prefix + name;
120  const auto& category = m_categories.find(internal_name);
121  if (category == m_categories.end()) {
122  std::stringstream details;
123  details << "Category '" << name << "' not found.";
124  throw edm::Exception(edm::errors::NotFound, details.str());
125  }
126 
127  return CategoryWrapper(category->second);
128  }
129 
130  private:
131  CategoryManager(ROOT::TreeWrapper& tree):
132  m_tree(tree)
133  {
134  // Empty
135  }
136 
137  void set_prefix(const std::string& prefix);
138 
139  bool evaluate_pre_analyzers(const ProducersManager& producers);
140  bool evaluate_post_analyzers(const ProducersManager& producers, const AnalyzersManager& analyzers);
141 
142  void reset();
143 
144  void print_summary();
145 
146  std::unordered_map<std::string, CategoryData> m_categories;
147  ROOT::TreeWrapper& m_tree;
148 
149  uint64_t processed_events = 0;
150  uint64_t selected_events = 0;
151 
152  std::string m_current_prefix;
153 };
154 
155 #endif
Definition: AnalyzersManager.h:10
Definition: Category.h:88
Definition: Category.h:20
Definition: Cut.h:27
Definition: Framework.h:21
Definition: ProducersManager.h:10
Definition: Category.h:40
Definition: Category.h:16
Definition: Category.h:66