HighLAND
EventWeightManager.cxx
1 #include "EventWeightManager.hxx"
2 #include "MultiThread.hxx"
3 
4 
5 //***********************************************************
6 EventWeightManager::EventWeightManager(){
7 //***********************************************************
8 
9  _nEventWeightsEnabled = 0;
10 
11  _eventWeightsWithNull.resize(NMAXEVENTWEIGHTS);
12  _eventWeightsWithNull.assign(_eventWeightsWithNull.size(),NULL);
13 
14  _eventWeights.clear();
15 }
16 
17 //***********************************************************
18 EventWeightManager::~EventWeightManager() {
19 //***********************************************************
20 
21  for (UInt_t i = 0; i < _eventWeights.size(); ++i) {
22  delete _eventWeights[i];
23  }
24 }
25 
26 //***********************************************************
27 void EventWeightManager::Initialize(SelectionManager& sel, Int_t nevents) {
28 //***********************************************************
29 
30  //For each enabled EventWeight, create the SystBox for each selection, with nbranches and for a given number of events
31  for (std::vector<SelectionBase*>::iterator sit= sel.GetSelections().begin();sit!=sel.GetSelections().end();sit++){
32  SelectionBase* selec = *sit;
33  if (!selec->IsEnabled()) continue;
34  for (UInt_t j = 0; j < _eventWeights.size(); ++j){
35  if (_eventWeights[j]->IsEnabled())
36  _eventWeights[j]->Initialize(sel.GetNEnabledSelections(),selec->GetEnabledIndex(), selec->GetNBranches(), nevents);
37  }
38  }
39  //TODO
40 }
41 
42 //***********************************************************
44 //***********************************************************
45 
46  for (UInt_t j = 0; j < _eventWeights.size(); ++j){
47  if (_eventWeights[j]->IsEnabled())
48  _eventWeights[j]->FinalizeEvent(event);
49  }
50  //TODO
51 }
52 
53 //***********************************************************
55 //***********************************************************
56 
57  // Fill the SysteBox for the enabled EventWeights
58 
59 #ifdef MULTITHREAD
60  for (UInt_t j = 0; j < _eventWeights.size(); ++j){
61  if (!_eventWeights[j]->IsEnabled()) continue;
62  for (std::vector<SelectionBase*>::iterator sit= sel.GetSelections().begin();sit!=sel.GetSelections().end();sit++){
63  SelectionBase* selec = *sit;
64  if (!selec->IsEnabled()) continue;
65  for (UInt_t ibranch=0;ibranch<selec->GetNBranches();ibranch++){
66  _eventWeights[j]->InitializeEvent(event,*selec,ibranch);
67  }
68  }
69  }
70 #else
71  (void)sel;
72  (void)event;
73 #endif
74 }
75 
76 //********************************************************************
77 std::vector<EventWeightBase*> EventWeightManager::GetEventWeights(const std::vector<Int_t>& indices) const{
78 //********************************************************************
79 
80  std::vector<EventWeightBase*> weights;
81  for (UInt_t i=0;i<indices.size();i++)
82  weights.push_back(GetEventWeight(indices[i]));
83 
84  return weights;
85 }
86 
87 //********************************************************************
89 //********************************************************************
90 
91  if (index<0 || (UInt_t)index> NMAXEVENTWEIGHTS-1) return NULL;
92  return _eventWeightsWithNull[index];
93 }
94 
95 //********************************************************************
96 EventWeightBase* EventWeightManager::GetEventWeight(const std::string& name) const{
97 //********************************************************************
98 
99  for (UInt_t i = 0; i < _eventWeights.size(); ++i) {
100  if (_eventWeights[i]->Name() == name) return _eventWeights[i];
101  }
102  return NULL;
103 }
104 
105 //********************************************************************
106 Int_t EventWeightManager::GetEventWeightIndex(const std::string& name){
107 //********************************************************************
108 
109  for (UInt_t i = 0; i < _eventWeights.size(); i++) {
110  if (_eventWeights[i]->Name() == name) return _eventWeights[i]->GetIndex();
111  }
112 
113  return -1;
114 }
115 
116 //********************************************************************
117 bool EventWeightManager::HasEventWeight(const std::string& name){
118 //********************************************************************
119 
120  return (GetEventWeight(name)!=NULL);
121 }
122 
123 //***********************************************************
125 //***********************************************************
126  // EventWeights are applied at the end of the each toy experiment
127 
128  // reset the weight to 1 before applying any systematic
129  Weight_h totalWeight(1);
130 
131  for (UInt_t j = 0; j < _eventWeights.size(); j++) {
132  // only apply enabled systematics
133  if (_eventWeights[j]->IsEnabled()){
134  // Compute the event normalization weight for this systematic
135  // totalWeight *= _eventWeights[j]->ComputeWeight(*(toy.GetVariations( _eventWeights[j]->GetIndex())), event, ToyBox);
136  totalWeight *= _eventWeights[j]->ComputeWeight(toy, event, ToyBox);
137  }
138  }
139  return totalWeight;
140 }
141 
142 //***********************************************************
143 Weight_h EventWeightManager::ComputeEventWeights(const ToyExperiment& toy, const AnaEventC& event, const ToyBoxB& ToyBox, Weight_h* weights) {
144 //***********************************************************
145 
146  // EventWeights are applied at the end of the each toy experiment
147  // This method returns as argument an array of weights, one for each of the weight systematics enabled
148 
149  // reset the weight to 1 before applying any systematic
150  Weight_h totalWeight(1);
151  Weight_h weight;
152 
153  Int_t w=0;
154  for (UInt_t j = 0; j < _eventWeights.size(); j++) {
155  // only apply enabled systematics
156  if (_eventWeights[j]->IsEnabled()){
157  // Compute the event normalization weight for this systematic
158  // weight = _eventWeights[j]->ComputeWeight(*(toy.GetVariations(_eventWeights[j]->GetIndex())), event, ToyBox);
159  weight = _eventWeights[j]->ComputeWeight(toy, event, ToyBox);
160  // Save in into the vector
161  weights[w++]=weight;
162  // Increment the total weight
163  totalWeight*= weight;
164  }
165  }
166 
167  return totalWeight;
168 
169 }
170 
171 //***********************************************************
172 Weight_h EventWeightManager::ComputeEventWeights(const SelectionBase& sel, const ToyExperiment& toy, const AnaEventC& event, const ToyBoxB& ToyBox, Weight_h* weights) {
173 //***********************************************************
174 
175  // Compute the relevant weight systematics for a given selection
176  // This method returns as argument an array of weights, one for each of the weight systematics enabled
177 
178  // EventWeights are applied at the end of the each toy experiment
179 
180  // reset the weight to 1 before applying any systematic
181  Weight_h totalWeight(1);
182  Weight_h weight;
183 
184  Int_t w=0;
185  for (UInt_t j = 0; j < _eventWeights.size(); j++) {
186  // only apply enabled systematics
187  if (!_eventWeights[j]->IsEnabled()) continue;
188 
189  Int_t ibranch = ToyBox.SuccessfulBranch;
190  // Check if the systematic is relevant for this selection and branch. If not the weight is 1
191  // Any of the branches should be succesful. Otherwise ibranch=-1
192  if (ibranch>-1 && sel.IsRelevantSystematic(event, ToyBox, _eventWeights[j]->GetIndex(), ibranch)){
193 #ifndef MULTITHREAD
194  // We need to initialize the event (Fill the SystBox). We only do that for the first succesful toy in a given selection and branch
195  if (!_eventWeights[j]->GetSystBox(event,sel.GetEnabledIndex(),ibranch))
196  _eventWeights[j]->InitializeEvent(event,sel,ibranch);
197 #endif
198  // Compute the event normalization weight for this systematic
199  // weight = _eventWeights[j]->ComputeWeight(*(toy.GetVariations(_eventWeights[j]->GetIndex())), event, ToyBox, sel);
200  weight = _eventWeights[j]->ComputeWeight(toy, event, ToyBox, sel);
201  }
202  else
203  weight=1;
204 
205  // Save it into the double array
206  weights[w++]=weight;
207  // Increment the total weight
208  totalWeight*= weight;
209  }
210 
211  return totalWeight;
212 }
213 
214 //***********************************************************
215 Weight_h EventWeightManager::ComputeEventWeight(Int_t index, const ToyExperiment& toy, const AnaEventC& event, const ToyBoxB& ToyBox) {
216 //***********************************************************
217 
218  // Compute an specific weight systematic, provided its index
219  // EventWeights are applied at the end of the each virtual analysis
220 
221  // Compute the event normalization weight for this systematic
222  // return _eventWeights[index]->ComputeWeight(*(toy.GetVariations(index)), event, ToyBox);
223  return _eventWeightsWithNull[index]->ComputeWeight(toy, event, ToyBox);
224 }
225 
226 //***********************************************************
228 //***********************************************************
229 
230  EventWeightBase* syst = GetEventWeight(index);
231  if (syst){
232  syst->SetEnabled(true);
233  if (syst->Type() == EventWeightBase::kWeight){
234  _eventWeightsEnabled[_nEventWeightsEnabled] = index;
235  _nEventWeightsEnabled++;
236  }
237  }
238 }
239 
240 //***********************************************************
242 //***********************************************************
243 
244  EventWeightBase* syst = GetEventWeight(index);
245  if (!syst) return;
246  syst->SetEnabled(false);
247  if (syst->Type() == EventWeightBase::kWeight){
248  for (Int_t it = 0; it < _nEventWeightsEnabled;it++){
249  if (_eventWeightsEnabled[it] == index){
250  for(int j = it; j < _nEventWeightsEnabled - 1; ++j){
252  }
253  _nEventWeightsEnabled--;
254  break;
255  }
256  }
257  }
258 }
259 
260 //***********************************************************
261 void EventWeightManager::EnableEventWeights(const std::vector<Int_t>& indices) {
262 //***********************************************************
263 
264  for (std::vector<Int_t>::const_iterator it=indices.begin();it!=indices.end();it++){
265  EnableEventWeight(*it);
266  }
267 }
268 
269 //***********************************************************
270 void EventWeightManager::DisableEventWeights(const std::vector<Int_t>& indices) {
271 //***********************************************************
272 
273  for (std::vector<Int_t>::const_iterator it=indices.begin();it!=indices.end();it++){
274  DisableEventWeight(*it);
275  }
276 }
277 
278 //***********************************************************
280 //***********************************************************
281 
282  _nEventWeightsEnabled = 0;
283  for (UInt_t it = 0; it < _eventWeights.size(); it++) {
284  _eventWeights[it]->SetEnabled(true);
285  _eventWeightsEnabled[_nEventWeightsEnabled] = _eventWeights[it]->GetIndex();
286  _nEventWeightsEnabled++;
287  }
288 }
289 
290 //***********************************************************
292 //***********************************************************
293 
294  for (UInt_t it = 0; it < _eventWeights.size(); it++) {
295  _eventWeights[it]->SetEnabled(false);
296  }
297  _nEventWeightsEnabled = 0;
298 }
299 
300 //***********************************************************
301 void EventWeightManager::AddEventWeight(Int_t index, const std::string& name, EventWeightBase* sys) {
302 //***********************************************************
303 
304  sys->SetName(name);
305  AddEventWeight(index,sys);
306 }
307 
308 //***********************************************************
310 //***********************************************************
311 
312  if (GetEventWeight(index)){
313  std::cout << "EventWeightManager::AddEventWeight(). '" << index << "' already added !!!" <<std::endl;
314  exit(1);
315  }
316 
317  // sys->SetName(name);
318  sys->SetIndex(index);
319  _eventWeightsWithNull[index] = sys;
320  _eventWeights.push_back(sys);
321 }
322 
323 //***********************************************************
325 //***********************************************************
326 
327  bool found=false;
328  for (UInt_t it = 0; it < _eventWeights.size(); it++) {
329  if (_eventWeights[it]->GetIndex() == index){
330  // sys->SetName(name);
331  _eventWeights[it] = sys;
332  found=true;
333  break;
334  }
335  }
336  if (!found){
337  std::cout << "EventWeightManager::ReplaceEventWeight(). '" << index << "' does not exist !!!" <<std::endl;
338  exit(1);
339  }
340 }
341 
342 //********************************************************************
344 //********************************************************************
345 
346  std::cout << " -------- List of EventWeights ---------------" << std::endl;
347  char out[256];
348  sprintf(out,"%3s: %-25s %-15s %-4s", "#", "name", "pdf", "NPar");
349  std::cout << out << "\n" << std::endl;
350 
351  Int_t j=0;
352  for (UInt_t it = 0; it < _eventWeights.size(); it++) {
353  if (_eventWeights[it]->IsEnabled()){
354  sprintf(out,"%3d: %-25s %-15s %-4d", j, _eventWeights[it]->Name().c_str(), _eventWeights[it]->ConvertPDF().c_str(), _eventWeights[it]->GetNParameters());
355  std::cout << out << std::endl;
356  j++;
357  }
358  }
359  std::cout << " --------------------------------------------------" << std::endl;
360 }
361 
void SetEnabled(bool ena)
Set the enable bit.
virtual bool IsRelevantSystematic(const AnaEventC &, const ToyBoxB &, SystId_h syst_index, Int_t branch=0) const
Is this systematic relevant for this selection.
Int_t GetEventWeightIndex(const std::string &name)
Get the index of a EventWeight registered with the given name.
void DumpEventWeights()
Dump all eventWeights.
TypeEnum Type() const
Return the type of this systematic.
EventWeightBase * GetEventWeight(const std::string &name) const
Get the EventWeight registered with the given name.
Int_t _eventWeightsEnabled[NMAXEVENTWEIGHTS]
The weight systematics that are enabled.
std::vector< EventWeightBase * > _eventWeights
The systematics that were registered as eventWeights.
UInt_t GetNEnabledSelections()
Returns the number of enabled selections.
void InitializeEvent(SelectionManager &sel, AnaEventC &event)
Fill the SystBox for the enabled EventWeights.
std::vector< EventWeightBase * > _eventWeightsWithNull
The systematics that were registered as eventWeights.
void ReplaceEventWeight(Int_t index, EventWeightBase *sys)
Replace one of the existing EventWeightBase.
void FinalizeEvent(AnaEventC &event)
Delete the SystBox for all EventWeights.
std::vector< SelectionBase * > & GetSelections()
Return the map of selections.
void SetName(const std::string &name)
Set the name of this systematic.
void DisableEventWeights(const std::vector< Int_t > &systs)
Disable the EventWeights registered with the given indices.
Int_t SuccessfulBranch
The branch that is successful for this toy in the selection this ToyBox belongs to.
Definition: ToyBoxB.hxx:46
void EnableAllEventWeights()
Enable all EventWeight eventWeights.
void DisableAllEventWeights()
Disable all eventWeights.
void SetIndex(Int_t index)
Set the index of this systematic.
Weight_h ComputeEventWeight(Int_t index, const ToyExperiment &toy, const AnaEventC &event, const ToyBoxB &ToyBox)
Compute EventWeight with a given index. Returns the event normalization weight.
void AddEventWeight(Int_t index, EventWeightBase *sys)
Add a new Event Weight provided its index in the manager and a pointer to it.
UInt_t GetNBranches() const
Return the number of branches.
std::vector< EventWeightBase * > & GetEventWeights()
Get the vector of EventWeights.
void EnableEventWeight(Int_t index)
Enable the EventWeight registered with the given index.
void EnableEventWeights(const std::vector< Int_t > &systs)
Enable the EventWeights registered with the given indices.
Weight_h ComputeEventWeights(const ToyExperiment &toy, const AnaEventC &event, const ToyBoxB &ToyBox)
Compute all eventWeights. Returns the total event normalization weight.
Int_t GetEnabledIndex() const
Get the Selection index.
bool HasEventWeight(const std::string &name)
Check if an EventWeight with a given name already exists in the manager.
void DisableEventWeight(Int_t index)
Disable the EventWeight registered with the given index.