HighLAND
CategoryManager.cxx
1 #include "CategoryManager.hxx"
2 #include <iomanip>
3 #include <sstream>
4 #include <stdlib.h>
5 #include <cstdlib>
6 
7 namespace anaUtils{
8  CategoryManager* _categ=NULL;
9 }
10 
11 //********************************************************************
12 CategoryManager::CategoryManager(): HLClonesArray("config","CATEG","TrackCategoryDefinition",NMAXCATEG){
13 //********************************************************************
14 
15  // is it ready ? Are the actual track properties saved ?
16  _ready = false;
17 }
18 
19 
20 //********************************************************************
21 void CategoryManager::AddCategory(const std::string& categ_name, int ntypes, std::string* names, int* codes, int* colors, bool multi, bool noWarning, bool addNOTRUTH, bool addSAND){
22 //********************************************************************
23 
24  // Fill this track category definition
25 
27  if (HasCategory(categ_name)){
28  if ( ! noWarning)
29  std::cerr << "WARNING: category '" << categ_name << "' already exists !!! Replacing properties..." << std::endl;
30  categ = _track_category_map[categ_name];
31  categ->Reset();
32  }
33  else{
34  categ = new((*_objects)[_NObjects++]) TrackCategoryDefinition(multi);
35  categ->_name = categ_name;
36  // Add it to the map of categories
37  _track_category_map[categ_name] = categ;
38  }
39 
40  for (int i=0;i<ntypes;i++) {
41  if ( ! noWarning && addSAND && codes[i]==CATSAND) {
42  std::cerr << "ERROR in CategoryManager::AddCategory: for category '" << categ_name
43  << "' user type " << i << " '" << names[i] << "' has same code " << codes[i]
44  << " as the automatic SAND muon type. Please use a different code !!!" << std::endl;
45  }
46  if ( ! noWarning && addNOTRUTH && codes[i]==CATNOTRUTH) {
47  std::cerr << "ERROR in CategoryManager::AddCategory: for category '" << categ_name
48  << "' user type " << i << " '" << names[i] << "' has same code " << codes[i]
49  << " as the automatic NO TRUTH type. Please use a different code !!!" << std::endl;
50  }
51 
53  type._name = names[i];
54  type._code = codes[i];
55  type._color = colors[i];
56  type._order = i;
57  type._enabled=true;
58  categ->AddType(type);
59  }
60 
61  // add "notruth" and "sand #mu" type to all categories unless specified otherwise with the input arguments
62 
63  if (addNOTRUTH){
64  // first no truth (since often sand has no truth)
66  type._name = NAMENOTRUTH;
67  type._code = CATNOTRUTH;
68  type._color = COLNOTRUTH;
69  type._order = categ->GetNTypes();
70  type._enabled = true;
71  categ->AddType(type);
72  }
73 
74  if (addSAND){
75  // sand as last type
76  TrackTypeDefinition typeSand;
77  typeSand._name = NAMESAND;
78  typeSand._code = CATSAND;
79  typeSand._color = COLSAND;
80  typeSand._order = categ->GetNTypes();
81  typeSand._enabled = true;
82  categ->AddType(typeSand);
83  }
84 }
85 
86 //********************************************************************
87 void CategoryManager::CopyCategory(const std::string& categ_name, const std::string& categ_name2){
88 //********************************************************************
89 
90  if (!HasCategory(categ_name)){
91  std::cout << "cannot copy category '" << categ_name << "' because it does not exist !!!" << std::endl;
92  return;
93  }
94 
95  // Get properties of the existing category
96  const UInt_t NMAXTYPES=50;
97  TrackCategoryDefinition* categ = _track_category_map[categ_name];
98  Int_t ntypes = categ->GetNTypes();
99  bool multi = categ->IsMultiType();
100  std::string names[NMAXTYPES];
101  Int_t codes[NMAXTYPES];
102  Int_t colors[NMAXTYPES];
103 
104  for (Int_t i=0;i<ntypes;i++){
105  names[i] = categ->GetCategoryTypes()[i]._name;
106  codes[i] = categ->GetCategoryTypes()[i]._code;
107  colors[i] = categ->GetCategoryTypes()[i]._color;
108  }
109 
110  // Add the new category
111  AddCategory(categ_name2, ntypes, names, codes, colors, multi);
112 
113 }
114 
115 //********************************************************************
116 void CategoryManager::ReadCategories(const std::string& file){
117 //********************************************************************
118 
119  // Reset the vectors
120  _track_category_map.clear();
121 
122  ReadClonesArray(file);
123 
124  // Add them to the map of categories
125  for (int i=0;i<_NObjects;i++){
127  _track_category_map[categ->_name] = categ;
128  }
129 }
130 
131 //********************************************************************
132 bool CategoryManager::HasCategory(const std::string& categ) {
133 //********************************************************************
134 
135  if (_track_category_map.find(categ)==_track_category_map.end()){
136  return false;
137  }
138  return true;
139 }
140 
141 //********************************************************************
143 //********************************************************************
144 
145  std::cout << "-------- Available track categories -------" << std::endl;
146  std::map< std::string, TrackCategoryDefinition* >::iterator it;
147  for (it=GetCategories().begin();it!=GetCategories().end();it++ ){
148  std::string categ_name = it->first;
149  std::cout << " - " << categ_name << std::endl;
150  }
151  std::cout << "-------------------------------------------" << std::endl;
152 
153 }
154 
155 //********************************************************************
156 void CategoryManager::DumpCategory(const std::string& categ) {
157 //********************************************************************
158 
159  if (!HasCategory(categ)) {
160  std::cout << "Category " << categ << " hasn't been set!" << std::endl;
161  return;
162  }
163  std::cout << " --------------------------------------------------------" << std::endl;
164  std::cout << " Types for '" << categ << "' category" << std::endl;
166  std::vector<TrackTypeDefinition>& types = def->GetCategoryTypes();
167 
168  for (unsigned int i = 0; i < types.size(); i++) {
169  TrackTypeDefinition type = types[i];
170  std::cout << std::setw(25) << type._name << std::setw(15) << "code " << type._code << std::setw(15) << "color " << type._color << std::endl;
171  }
172 
173  std::cout << "-------------------------------------------" << std::endl;
174  std::cout << std::endl;
175 
176 }
177 
178 //********************************************************************
180 //********************************************************************
181 
182  std::map< std::string, TrackCategoryDefinition* >::iterator it;
183  for (it=GetCategories().begin();it!=GetCategories().end();it++ ){
184  TrackCategoryDefinition& categ = *(it->second);
185  if (categ.IsMultiType()){
186  for (unsigned int i=0;i<categ.GetNTypes();i++)
187  categ.SetCategoryType(i,false);
188  }
189  else{
190  categ.SetCode(-999,-999);
191  }
192  }
193 
194  _ready = false;
195 }
196 
197 //********************************************************************
198 int CategoryManager::GetCode(const std::string& categ) {
199 //********************************************************************
200 
201  if (!HasCategory(categ)) return -1;
202 
203  if (!GetCategory(categ).IsMultiType())
204  return GetCategory(categ).GetCode();
205  else{
206  std::cout << "Category '" << categ << "' is a multi-type category" << std::endl;
207  return -1;
208  }
209 }
210 
211 //********************************************************************
212 bool CategoryManager::CheckCategoryType(const std::string& categ, int index){
213 //********************************************************************
214 
215  if (!HasCategory(categ)) return -1;
216 
217  if (GetCategory(categ).IsMultiType())
218  return GetCategory(categ).CheckCategoryType(index);
219  else{
220  std::cout << "Category '" << categ << "' is a single-type category" << std::endl;
221  return false;
222  }
223 }
224 
bool _enabled
Is this type enabled?
void SetCategoryType(int index, bool ok)
Set the type for the actual track.
bool IsMultiType()
Is this a multi-type category ? (Can several types coexist?)
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)
int GetCode(const std::string &categ)
Get the actual code for this category.
int _code
The unique code for this type ("muon==13", ...).
bool CheckCategoryType(const std::string &categ, int index)
void AddType(TrackTypeDefinition &type)
Add a new type to this category.
int _order
The order in which it should appear when plotting.
TrackCategoryDefinition & GetCategory(const std::string &categ)
Get a specific category.
void DumpCategories()
Dump the map of track categories.
std::map< std::string, TrackCategoryDefinition * > & GetCategories()
Get the map of track categories.
int _color
The color to be displayed for tracks of this type.
void ResetCurrentCategories()
Reset the properties of the current track.
Int_t _NObjects
The number of steps that were added.
int GetCode()
Get the code for the actual track in this category.
std::string _name
The name of this type ("muon", "electron", ...). It will be displayed in the legend.
void SetCode(int code, int defaultcode=-999)
TClonesArray * _objects
unsigned int GetNTypes()
Number of types defined for this category.
std::vector< TrackTypeDefinition > & GetCategoryTypes()
Get the types defined for this category.
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.
std::string _name
The name of this category (e.g. "particle").
void ReadCategories(const std::string &file)
Build the categories from a root file.
bool CheckCategoryType(int index)
Check if the actual track is of this type.