HighLAND
EventVariationManager.cxx
1 #include "EventVariationManager.hxx"
2 #include "MultiThread.hxx"
3 
4 
5 //***********************************************************
6 EventVariationManager::EventVariationManager(){
7 //***********************************************************
8 
9  _nEventVariationsEnabled = 0;
10 
11  _eventVariationsWithNull.resize(NMAXEVENTVARIATIONS);
13 
14  _eventVariations.clear();
15 }
16 
17 //***********************************************************
18 EventVariationManager::~EventVariationManager() {
19 //***********************************************************
20 
21  for (UInt_t i = 0; i < _eventVariations.size(); ++i) {
22  delete _eventVariations[i];
23  }
24 }
25 
26 //***********************************************************
27 void EventVariationManager::Initialize(Int_t nevents) {
28 //***********************************************************
29 
30  // For variation systematics the SystBox is the same for all selections and branches
31  // Create the SystBox for a single selection and branch, for a given number of events
32  for (UInt_t j = 0; j < _eventVariations.size(); ++j){
33  if (_eventVariations[j]->IsEnabled())
34  _eventVariations[j]->Initialize(1,0,1, nevents);
35  }
36 }
37 
38 //***********************************************************
40 //***********************************************************
41 
42  for (UInt_t j = 0; j < _eventVariations.size(); ++j){
43  if (_eventVariations[j]->IsEnabled())
45  }
46  //TODO
47 }
48 
49 //***********************************************************
51 //***********************************************************
52 
53  // Fill the SystBox for the enabled systematics
54 
55 
56  for (UInt_t j = 0; j < _eventVariations.size(); ++j){
57  if (!_eventVariations[j]->IsEnabled()) continue;
58  for (std::vector<SelectionBase*>::iterator sit= sel.GetSelections().begin();sit!=sel.GetSelections().end();sit++){
59  SelectionBase* selec = *sit;
60  if (!selec->IsEnabled()) continue;
61  _eventVariations[j]->InitializeEvent(event,*selec);
62  }
63  _eventVariations[j]->InitializeEvent(event);
64  }
65 }
66 
67 //********************************************************************
68 std::vector<EventVariationBase*> EventVariationManager::GetEventVariations(const std::vector<Int_t>& indices) const{
69 //********************************************************************
70 
71  std::vector<EventVariationBase*> systematics;
72  for (UInt_t i=0;i<indices.size();i++)
73  systematics.push_back(GetEventVariation(indices[i]));
74 
75  return systematics;
76 }
77 
78 //********************************************************************
80 //********************************************************************
81 
82  if (index<0 || (UInt_t)index> NMAXEVENTVARIATIONS-1) return NULL;
83  return _eventVariationsWithNull[index];
84 }
85 
86 //********************************************************************
88 //********************************************************************
89 
90  for (UInt_t i = 0; i < _eventVariations.size(); ++i) {
91  if (_eventVariations[i]->Name() == name) return _eventVariations[i];
92  }
93  return NULL;
94 }
95 
96 //********************************************************************
97 Int_t EventVariationManager::GetEventVariationIndex(const std::string& name){
98 //********************************************************************
99 
100  for (UInt_t i = 0; i < _eventVariations.size(); i++) {
101  if (_eventVariations[i]->Name() == name) return _eventVariations[i]->GetIndex();
102  }
103 
104  return -1;
105 }
106 
107 //********************************************************************
108 bool EventVariationManager::HasEventVariation(const std::string& name){
109 //********************************************************************
110 
111  return (GetEventVariation(name)!=NULL);
112 }
113 
114 //***********************************************************
116 //***********************************************************
117 
118  for (int j = 0; j < _nEventVariationsEnabled; ++j){
119  Int_t isyst = _eventVariationsEnabled[j];
120  _eventVariationsWithNull[isyst]->Apply(toy, event);
121  }
122 }
123 
124 //***********************************************************
126 //***********************************************************
127 
128  // Returns true when any of the systematics returns true. That means that the full Spill will be reseted
129 
130  bool ok=false;
131  for (UInt_t i = 0; i < _eventVariations.size(); ++i) {
132  if (_eventVariations[i]->IsEnabled())
133  if (_eventVariations[i]->UndoSystematic(event)) ok=true;
134  }
135  return ok;
136 }
137 
138 
139 //***********************************************************
141 //***********************************************************
142 
143  EventVariationBase* syst = GetEventVariation(index);
144  if (syst){
145  syst->SetEnabled(true);
146  if (syst->Type() == EventVariationBase::kVariation){
147  _eventVariationsEnabled[_nEventVariationsEnabled] = index;
148  _nEventVariationsEnabled++;
149  }
150  }
151 }
152 
153 //***********************************************************
155 //***********************************************************
156 
157  EventVariationBase* syst = GetEventVariation(index);
158  if (!syst) return;
159  syst->SetEnabled(false);
160  if (syst->Type() == EventVariationBase::kVariation){
161  for (Int_t it = 0; it < _nEventVariationsEnabled;it++){
162  if (_eventVariationsEnabled[it] == index){
163  for(int j = it; j < _nEventVariationsEnabled - 1; ++j){
165  }
166  _nEventVariationsEnabled--;
167  break;
168  }
169  }
170  }
171 }
172 
173 //***********************************************************
174 void EventVariationManager::EnableEventVariations(const std::vector<Int_t>& indices) {
175 //***********************************************************
176 
177  for (std::vector<Int_t>::const_iterator it=indices.begin();it!=indices.end();it++){
179  }
180 }
181 
182 //***********************************************************
183 void EventVariationManager::DisableEventVariations(const std::vector<Int_t>& indices) {
184 //***********************************************************
185 
186  for (std::vector<Int_t>::const_iterator it=indices.begin();it!=indices.end();it++){
188  }
189 }
190 
191 //***********************************************************
193 //***********************************************************
194 
195  _nEventVariationsEnabled = 0;
196  for (UInt_t it = 0; it < _eventVariations.size(); it++) {
197  _eventVariations[it]->SetEnabled(true);
198  _eventVariationsEnabled[_nEventVariationsEnabled] = _eventVariations[it]->GetIndex();
199  _nEventVariationsEnabled++;
200  }
201 }
202 
203 //***********************************************************
205 //***********************************************************
206 
207  for (UInt_t it = 0; it < _eventVariations.size(); it++) {
208  _eventVariations[it]->SetEnabled(false);
209  }
210  _nEventVariationsEnabled = 0;
211 }
212 
213 //***********************************************************
214 void EventVariationManager::AddEventVariation(Int_t index, const std::string& name, EventVariationBase* sys) {
215 //***********************************************************
216 
217  sys->SetName(name);
218  AddEventVariation(index,sys);
219 }
220 
221 //***********************************************************
223 //***********************************************************
224 
225  if (GetEventVariation(index)){
226  std::cout << "EventVariationManager::AddEventVariation(). '" << index << "' already added !!!" <<std::endl;
227  exit(1);
228  }
229 
230  // sys->SetName(name);
231  sys->SetIndex(index);
232  _eventVariationsWithNull[index] = sys;
233  _eventVariations.push_back(sys);
234 }
235 
236 //***********************************************************
238 //***********************************************************
239 
240  bool found=false;
241  for (UInt_t it = 0; it < _eventVariations.size(); it++) {
242  if (_eventVariations[it]->GetIndex() == index){
243  // sys->SetName(name);
244  _eventVariations[it] = sys;
245  found=true;
246  break;
247  }
248  }
249  if (!found){
250  std::cout << "EventVariationManager::ReplaceEventVariation(). '" << index << "' does not exist !!!" <<std::endl;
251  exit(1);
252  }
253 
254 }
255 
256 
257 //********************************************************************
259 //********************************************************************
260 
261  std::cout << " -------- List of EventVariations ---------------" << std::endl;
262  char out[256];
263  sprintf(out,"%3s: %-25s %-15s %-4s", "#", "name", "pdf", "NPar");
264  std::cout << out << "\n" << std::endl;
265 
266  Int_t j=0;
267  for (UInt_t it = 0; it < _eventVariations.size(); it++) {
268  if (_eventVariations[it]->IsEnabled()){
269  sprintf(out,"%3d: %-25s %-15s %-4d", j, _eventVariations[it]->Name().c_str(), _eventVariations[it]->ConvertPDF().c_str(), _eventVariations[it]->GetNParameters());
270  std::cout << out << std::endl;
271  j++;
272  }
273  }
274  std::cout << " --------------------------------------------------" << std::endl;
275 }
276 
void SetEnabled(bool ena)
Set the enable bit.
TypeEnum Type() const
Return the type of this systematic.
std::vector< EventVariationBase * > & GetEventVariations()
Get the vector of EventVariations.
void DumpEventVariations()
Dump all eventVariations.
bool HasEventVariation(const std::string &name)
Check if an EventVariation with a given name already exists in the manager.
Int_t _eventVariationsEnabled[NMAXEVENTVARIATIONS]
The variation systematics that are enabled.
EventVariationBase * GetEventVariation(const std::string &name) const
Get the EventVariation registered with the given name.
std::vector< SelectionBase * > & GetSelections()
Return the map of selections.
void DisableEventVariations(const std::vector< Int_t > &systs)
Disable the EventVariations registered with the given indices.
void DisableAllEventVariations()
Disable all eventVariations.
void ReplaceEventVariation(Int_t index, EventVariationBase *sys)
Replace one of the existing EventVariationBase.
void SetName(const std::string &name)
Set the name of this systematic.
void InitializeEvent(SelectionManager &sel, AnaEventC &event)
Fill the SystBox for the enabled EventVariations.
void EnableAllEventVariations()
Enable all EventVariation eventVariations.
bool UndoEventVariations(AnaEventC &event)
Undo the event variation (Undo the variation, that is, go back to the previous values of modified obs...
void SetIndex(Int_t index)
Set the index of this systematic.
void AddEventVariation(Int_t index, EventVariationBase *sys)
Add a new Event Variation provided its index in the manager and a pointer to it.
std::vector< EventVariationBase * > _eventVariations
The systematics that were registered as eventVariations.
void ApplyEventVariations(const ToyExperiment &toy, AnaEventC &event)
Apply all EventVariations.
void FinalizeEvent(AnaEventC &event)
Delete the SystBox for all EventVariations.
void EnableEventVariations(const std::vector< Int_t > &systs)
Enable the EventVariations registered with the given indices.
std::vector< EventVariationBase * > _eventVariationsWithNull
The systematics that were registered as eventVariations.
void EnableEventVariation(Int_t index)
Enable the EventVariation registered with the given index.
void DisableEventVariation(Int_t index)
Disable the EventVariation registered with the given index.
Int_t GetEventVariationIndex(const std::string &name)
Get the index of a EventVariation registered with the given name.