HighLAND
ConfigurationManager.cxx
1 #include "ConfigurationManager.hxx"
2 #include <DocStringManager.hxx>
3 #include <sstream>
4 #include <stdlib.h>
5 
6 
7 //********************************************************************
8 ConfigurationManager::ConfigurationManager():HLClonesArray("config","CONF","ConfigurationBase",NMAXCONFIGURATIONS) {
9 //********************************************************************
10 
11  _current_conf=-1;
12  _toy_index=0;
13  _confs.resize(NMAXCONFIGURATIONS);
14 
15 }
16 
17 //********************************************************************
18 ConfigurationManager::ConfigurationManager(const std::string& file, double norm, const std::string& cut):HLClonesArray("config","CONF","ConfigurationBase",NMAXCONFIGURATIONS) {
19 //********************************************************************
20 
21  (void)norm;
22  (void)cut;
23  _current_conf=-1;
24  _toy_index=0;
25  ReadConfigurations(file);
26 }
27 
28 //***********************************************************
29 ConfigurationManager::~ConfigurationManager() {
30 //***********************************************************
31  for (std::vector<ConfigurationBase*>::iterator it = _confs_nonull.begin(); it != _confs_nonull.end(); it++) {
32  delete *it;
33  }
34 
35  _confs_nonull.clear();
36  _confs.clear();
37 }
38 
39 //**************************************************
40 void ConfigurationManager::AddConfiguration(Int_t index, const std::string& conf_name, UInt_t ntoys, Int_t randomSeed, ToyMaker* toyMaker){
41 //**************************************************
42 
43  if (index >= (Int_t)NMAXCONFIGURATIONS){
44  std::cout << "ERROR in ConfigurationManager::AddConfiguration(). The index for configuration '" << conf_name << "' with value "
45  << index << " exceeds the maximum number of allowed configurations, which is " << NMAXCONFIGURATIONS << std::endl;
46  exit(1);
47  }
48 
49  // Create the new configuration with a given name
50  ConfigurationBase* conf = new ConfigurationBase(index, conf_name, ntoys, randomSeed, toyMaker);
51 
52  // Add it to the vector of configurations
53  _confs_nonull.push_back(conf);
54 
55  // Add it to the vector of configurations
56  _confs[index] = conf;
57 
58  // Add it to the ClonesArray
59  (*_objects)[_NObjects++] = conf;
60 
61  // AddVar(conf,"toy_ref","I");
62 
63  // The reference toy is the first one by default
64  // SetRefToyIndex(conf,0);
65 
66  // syst().AddConfiguration(conf);
67 
68 }
69 
70 //********************************************************************
71 void ConfigurationManager::ReadConfigurations(const std::string& file){
72 //********************************************************************
73 
74  // Reset the vectors
75  _confs_nonull.clear();
76  _confs.clear();
77  _confs.resize(NMAXCONFIGURATIONS);
78 
79  ReadClonesArray(file);
80 
81  // Add them to the map of categories
82  for (int i=0;i<_NObjects;i++){
83  ConfigurationBase* conf = (ConfigurationBase*)(*_objects)[i];
84  _confs_nonull.push_back(conf);
85  if (conf->GetIndex() >= (Int_t)NMAXCONFIGURATIONS){
86  std::cout << "WARNING in ConfigurationManager::ReadConfigurations(). The index for configuration '" << conf->Name() << "' with value "
87  << conf->GetIndex() << " exceeds the maximum number of allowed configurations, which is " << NMAXCONFIGURATIONS << std::endl;
88  std::cout << " ---> Configuration not added !!!" << std::endl;
89  continue;
90  }
91  _confs[conf->GetIndex()] = conf;
92  }
93 }
94 
95 //********************************************************************
96 //void ConfigurationManager::ReadConfigurations(const std::string& file){
97 //********************************************************************
98 /*
99  // Load a tree from a file
100  ReadFile(file);
101 
102  int NTOYS;
103  int toy_ref;
104 
105  std::map< std::string, TTree* >::iterator cit;
106  for (cit= GetTrees().begin();cit!=GetTrees().end();cit++){
107  std::string conf = cit->first;
108 
109  if (!IsSpecialTree(conf)) {
110  // Header and config are special trees, which don't have the same branches
111  // as the actual configuration trees.
112  GetTree(conf)->SetBranchAddress("NTOYS", &NTOYS);
113  GetTree(conf)->SetBranchAddress("toy_ref", &toy_ref);
114 
115  if (GetTree()->GetEntries() > 0) {
116  GetTree(conf)->GetEntry(0);
117  } else {
118  std::cout << "Warning: tree " << conf << " has no entries!" << std::endl;
119  NTOYS = 1;
120  toy_ref = 0;
121  }
122 
123  // 1 single toy by default
124  SetNToys(conf, NTOYS);
125 
126  // The reference toy is the first one by default
127  SetRefToyIndex(conf, toy_ref);
128 
129  // Current configuration is the last one
130  SetCurrentConfiguration("default");
131  }
132  }
133 }
134 */
135 //**************************************************
137 //**************************************************
138 
139  int nmax =1;
140  for (std::vector<ConfigurationBase* >::iterator it= GetConfigurations().begin();it!=GetConfigurations().end();it++){
141  if ((*it)->GetNToys() > nmax) nmax = (*it)->GetNToys();
142  }
143 
144  return nmax;
145 }
146 
147 //**************************************************
148 void ConfigurationManager::EnableAllConfigurations(){
149 //**************************************************
150 
151  for (std::vector<ConfigurationBase* >::iterator it= GetConfigurations().begin();it!=GetConfigurations().end();it++){
152  (*it)->SetEnabled(true);
153  }
154 }
155 
156 //**************************************************
157 void ConfigurationManager::DisableAllConfigurations(){
158 //**************************************************
159 
160  for ( std::vector<ConfigurationBase* >::iterator it= GetConfigurations().begin();it!=GetConfigurations().end();it++){
161  (*it)->SetEnabled(false);
162  }
163 }
164 
165 //*********************************************************
166 //void TreeManager::SetNToys(const std::string& tree, int NTOYS){
167 //*********************************************************
168 /*
169  _conf_ntoys[tree]=NTOYS;
170  _ana_enabled[conf] = std::vector<bool>();
171  _ana_enabled[conf].resize(NTOYS);
172  EnableAllToys(conf);
173 
174  if(HasCounter(tree, "NTOYS"))
175  ResizeCounter(tree, "NTOYS",NTOYS);
176 
177  // Delete the variables that depend on the number of toys
178  DeleteVar(tree,"toy_index");
179  DeleteVar(tree,"toy_weight");
180 
181  // Add the variables again with the correct size
182  AddAnalysisVar(tree,"toy_index","I");
183  AddAnalysisVar(tree,"toy_weight","D");
184 }
185 
186 
187 */
188 
189 //**************************************************
190 Int_t ConfigurationManager::GetConfigurationIndex(const std::string& conf_name){
191 //**************************************************
192 
193  for (std::vector<ConfigurationBase* >::iterator it= GetConfigurations().begin();it!=GetConfigurations().end();it++){
194  if ((*it)->Name() == conf_name) return (*it)->GetIndex();
195  }
196 
197  return -1;
198 }
199 
200 //**************************************************
201 void ConfigurationManager::SetCurrentConfigurationName(const std::string& conf_name){
202 //**************************************************
203 
204  for (std::vector<ConfigurationBase* >::iterator it= GetConfigurations().begin();it!=GetConfigurations().end();it++){
205  if ((*it)->Name() == conf_name) SetCurrentConfigurationIndex((*it)->GetIndex());
206  break;
207  }
208 }
209 
210 //********************************************************************
212 //********************************************************************
213 
214  std::cout << " -------- List of Configurations ----------------------------------" << std::endl;
215  char out[256];
216  sprintf(out,"%3s: %-25s %5s %8s %8s %12s", "#", "name", "NToys", "enabled", "NSyst", "RandomSeed");
217  std::cout << out <<"\n" << std::endl;
218 
219  for (UInt_t i=0;i<GetConfigurations().size();i++){
220  ConfigurationBase* conf = GetConfigurations()[i];
221  sprintf(out,"%3d: %-25s %5d %8d %8d %12d", i, conf->Name().c_str(), conf->GetNToys(), (Int_t)conf->IsEnabled(), (Int_t)conf->GetEnabledSystematics().size(), conf->GetToyRandomSeed());
222  std::cout << out << std::endl;
223  }
224  std::cout << " -------------------------------------------------------------------" << std::endl;
225 
226 
227  if (!syst) return;
228 
229  for (UInt_t i=0;i<GetConfigurations().size();i++){
230  ConfigurationBase* conf = GetConfigurations()[i];
231  conf->Dump(*syst);
232  }
233 }
234 
235 //********************************************************************
237 //********************************************************************
238 
239  for (std::vector<ConfigurationBase* >::iterator it= GetConfigurations().begin();it!=GetConfigurations().end();it++){
240  (*it)->CreateToyExperiments(syst);
241  }
242 
243 }
244 
void DumpConfigurations(SystematicManager *syst=NULL)
Dump summary info about all configurations.
void AddConfiguration(Int_t index, const std::string &conf, UInt_t ntoys=1, Int_t randomSeed=-1, ToyMaker *toyMaker=NULL)
Add a new configuration.
int GetNMaxToys()
Get the maximum number of toys in all configurations.
Int_t GetNToys() const
Get and sets the number of toys.
Int_t GetConfigurationIndex(const std::string &conf_name)
Returs the index of a configuration with name.
void SetCurrentConfigurationName(const std::string &conf)
Set the name of the current configuration.
const std::string & Name() const
Returns the name of this configuration.
Int_t GetToyRandomSeed() const
Get and sets the random seed used to generate the toys.
The maximum number of systematics that is supported.
const std::vector< Int_t > & GetEnabledSystematics()
Get the systematics enabled for this configuration.
void ReadConfigurations(const std::string &file)
Read configurations from a file.
Int_t GetIndex() const
Returns the configuration index (should match the one in the Configuration Manager) ...
void CreateToyExperiments(const SystematicManager &syst)
Create the ToyExperiments using the ToyMaker and the SystematicManager.