HighLAND
CategoryManager.hxx
1 #ifndef CategoryManager_h
2 #define CategoryManager_h
3 
4 #include <stdio.h>
5 #include <iostream>
6 #include <map>
7 #include <vector>
8 #include <math.h>
9 #include <HLClonesArray.hxx>
10 
11 #include "CategoryClasses.hxx"
12 
13 /// This class manages TrackCategoryDefinition objects. It handles the creation
14 /// and usage of the categories, as well as reading and writing them in the
15 /// "config" header so they can be used in the DrawingTools.
16 ///
17 /// See TrackCategoryDefinition for more details on track categories and their
18 /// usage in the creation of stacked histograms.
19 ///
20 
21 /// Maximum number of categories that can be added
22 const UInt_t NMAXCATEG=100;
23 
24 const UInt_t NMAXCATEGCOUNTERS = 10;
25 
26 
27 // names for common types
28 const std::string NAMESAND = "sand #mu";
29 const std::string NAMEOUTFV = "out FV";
30 const std::string NAMENOTRUTH = "no truth";
31 const std::string NAMEOTHER = "other";
32 const std::string NAME2P2H = "2p2h";
33 const std::string NAMEBKG = "BKG";
34 
35 // codes for common types
36 const int CATSAND = 777;
37 const int CATOUTFV = 7;
38 const int CATNOTRUTH = -1;
39 const int CATOTHER = 999;
40 const int CAT2P2H = 9;
41 const int CATBKG = 888;
42 
43 // colors for common types
44 const int COLSAND = 51;
45 const int COLOUTFV = 1;
46 const int COLNOTRUTH = 92;
47 const int COLOTHER = 48;
48 const int COL2P2H = 874;
49 const int COLBKG = 6;
50 
51 
52 
53 
55 
56  public:
57 
58  /// Private constructor as this is a singleton.
60 
61  virtual ~CategoryManager() {}
62 
63  /// Reset the properties of the current track.
65 
66  /// Add a new track category. This will be automatically saved in the
67  /// "config" tree, so can be used in the DrawingTools.
68  ///
69  /// name: The name of this category (for example, "particle").
70  /// ntypes: The number of types this category permits.
71  /// names: The names of the types this category permits (e.g. "muon"...).
72  /// codes: The codes that associate a track with a given type. See SetCode().
73  /// colors: The colors to be used when drawing the stacked histogram.
74  /// multi: Whether the category should allow multiple types to coexist.
75  /// noWarning: avoid warning when replacing properties,
76  /// used for drawing with % on the legend (with PUR option)
77  /// addNOTRUTH: atutomatically add the NOTRUTH type
78  /// addSAND: atutomatically add the SAND muon type
79  /// The array variables should all be ntypes in length.
80  void AddCategory(const std::string& name, int ntypes, std::string* names, int* codes, int* colors, bool multi = false, bool noWarning = false, bool addNOTRUTH=true, bool addSAND=true);
81 
82  /// Copy an existing Category into another with a different name
83  void CopyCategory(const std::string& categ_name, const std::string& categ_name2);
84 
85  /// Has this category been added?
86  bool HasCategory(const std::string& categ);
87 
88  /// Build the categories from a root file.
89  void ReadCategories(const std::string& file);
90 
91  /// Get the map of track categories.
92  std::map<std::string, TrackCategoryDefinition*>& GetCategories() {
93  return _track_category_map;
94  }
95 
96  /// Dump the map of track categories.
97  void DumpCategories();
98 
99  /// Dump the options stored for the given category.
100  void DumpCategory(const std::string& categ);
101 
102  /// Get a specific category.
103  TrackCategoryDefinition& GetCategory(const std::string& categ) {
104  return *_track_category_map[categ];
105  }
106 
107  /// Get the vector of track types in a given category.
108  std::vector<TrackTypeDefinition>& GetCategoryTypes(const std::string& categ) {
109  return _track_category_map[categ]->GetCategoryTypes();
110  }
111 
112  /// Set the actual code for this category. If the specified code
113  /// isn't defined for the category, defaultcode is used instead.
114  void SetCode(const std::string& categ, int code, int defaultcode = 0) {
115  if (HasCategory(categ)) {
116  GetCategory(categ).SetCode(code, defaultcode);
117  } else {
118  std::cout << "Code not set: category '" << categ << "' not found !!!" << std::endl;
119  }
120  }
121 
122  /// A category can fulfill multiple types. Set the different bits.
123  /// For multitype categories only.
124  void SetCategoryType(const std::string& categ, int type, bool ok) {
125  if (HasCategory(categ)) {
126  GetCategory(categ).SetCategoryType(type, ok);
127  }
128  }
129 
130  /// Get the actual code for this category.
131  int GetCode(const std::string& categ);
132 
133  /// Check is the specified type is true for a given category (for multitype
134  /// categories only)
135  bool CheckCategoryType(const std::string& categ, int index);
136 
137  /// Whether the categories are ready.
138  bool IsReady() const {
139  return _ready;
140  }
141 
142  /// Set that the categories are ready.
143  void SetReady(bool ok) {
144  _ready = ok;
145  }
146 
147  protected:
148 
149  /// The internal map of categories and the names they were registered with.
150  std::map<std::string, TrackCategoryDefinition*> _track_category_map;
151 
152  /// Whether the categories are ready.
153  bool _ready;
154 
155 };
156 
157 namespace anaUtils{
158 
159  extern CategoryManager* _categ;
160 
161 }
162 
163 #endif
void SetCategoryType(int index, bool ok)
Set the type for the actual track.
void AddCategory(const std::string &name, int ntypes, std::string *names, int *codes, int *colors, bool multi=false, bool noWarning=false, bool addNOTRUTH=true, bool addSAND=true)
void SetReady(bool ok)
Set that the categories are ready.
int GetCode(const std::string &categ)
Get the actual code for this category.
void SetCode(const std::string &categ, int code, int defaultcode=0)
bool CheckCategoryType(const std::string &categ, int index)
TrackCategoryDefinition & GetCategory(const std::string &categ)
Get a specific category.
void DumpCategories()
Dump the map of track categories.
bool IsReady() const
Whether the categories are ready.
std::map< std::string, TrackCategoryDefinition * > & GetCategories()
Get the map of track categories.
std::vector< TrackTypeDefinition > & GetCategoryTypes(const std::string &categ)
Get the vector of track types in a given category.
void ResetCurrentCategories()
Reset the properties of the current track.
void SetCode(int code, int defaultcode=-999)
bool _ready
Whether the categories are ready.
void CopyCategory(const std::string &categ_name, const std::string &categ_name2)
Copy an existing Category into another with a different name.
CategoryManager()
Private constructor as this is a singleton.
bool HasCategory(const std::string &categ)
Has this category been added?
void DumpCategory(const std::string &categ)
Dump the options stored for the given category.
std::map< std::string, TrackCategoryDefinition * > _track_category_map
The internal map of categories and the names they were registered with.
This namespace contains useful functions for analyses related to kinematics.
void ReadCategories(const std::string &file)
Build the categories from a root file.
void SetCategoryType(const std::string &categ, int type, bool ok)