HighLAND
SystematicManager.cxx
1 #include "SystematicManager.hxx"
2 #include "MultiThread.hxx"
3 
4 
5 //***********************************************************
6 SystematicManager::SystematicManager(): HLClonesArray("config","SYST","SystematicBase",NMAXSYSTEMATICS) {
7 //***********************************************************
8 
9  _covariance = new SystematicCovariance();
10  _systematics.resize(NMAXSYSTEMATICS);
11  _systematics.assign(_systematics.size(),NULL);
12 
13  _nVariationSystematics = 0;
14  _nWeightSystematics = 0;
15  _nFluxSystematics = 0;
16  _nVariationSystematicsEnabled = 0;
17  _nWeightSystematicsEnabled = 0;
18  _nFluxSystematicsEnabled = 0;
19 
20 }
21 
22 //***********************************************************
23 SystematicManager::~SystematicManager() {
24 //***********************************************************
25 /*
26  for (int i = 0; i < _nVariationSystematics; ++i) {
27  delete _variationSystematics[i];
28  }
29  for (int i = 0; i < _nWeightSystematics; ++i) {
30  delete _weightSystematics[i];
31  }
32 */
33  for (int i = 0; i < _nFluxSystematics; ++i) {
34  delete _fluxSystematics[i];
35  }
36 }
37 
38 //***********************************************************
39 void SystematicManager::Initialize(SelectionManager& sel, Int_t nevents) {
40 //***********************************************************
41 
42  // For variation systematics the SystBox is the same for all selections and branches
43  // Create the SystBox for a single selection and branch, for a given number of events
44  for (int j = 0; j < _nVariationSystematics; ++j){
45  if (_variationSystematics[j]->IsEnabled())
46  _variationSystematics[j]->Initialize(1,0,1, nevents);
47  }
48 
49  //For each systematic, create the SystBox for each selection, with nbranches and for a given number of events
50  for (std::vector<SelectionBase*>::iterator sit= sel.GetSelections().begin();sit!=sel.GetSelections().end();sit++){
51  SelectionBase* selec = *sit;
52  if (!selec->IsEnabled()) continue;
53  for (int j = 0; j < _nWeightSystematics; ++j){
54  if (_weightSystematics[j]->IsEnabled())
55  _weightSystematics[j]->Initialize(sel.GetNEnabledSelections(),selec->GetEnabledIndex(), selec->GetNBranches(), nevents);
56  }
57  }
58  //TODO
59 }
60 
61 //***********************************************************
62 void SystematicManager::FinalizeEventSystematics(AnaEventC& event) {
63 //***********************************************************
64  for (int j = 0; j < _nVariationSystematics; ++j){
65  if (_variationSystematics[j]->IsEnabled())
66  _variationSystematics[j]->FinalizeEvent(event);
67  }
68 
69  for (int j = 0; j < _nWeightSystematics; ++j){
70  if (_weightSystematics[j]->IsEnabled())
71  _weightSystematics[j]->FinalizeEvent(event);
72  }
73  //TODO
74 }
75 
76 //***********************************************************
77 void SystematicManager::InitializeEventSystematics(SelectionManager& sel, AnaEventC& event) {
78 //***********************************************************
79 
80  // Fill the SysteBox for the enabled systematics
81 
82  for (int j = 0; j < _nVariationSystematics; ++j){
83  if (!_variationSystematics[j]->IsEnabled()) continue;
84  for (std::vector<SelectionBase*>::iterator sit= sel.GetSelections().begin();sit!=sel.GetSelections().end();sit++){
85  SelectionBase* selec = *sit;
86  if (!selec->IsEnabled()) continue;
87  _variationSystematics[j]->InitializeEvent(event,*selec);
88  }
89  _variationSystematics[j]->InitializeEvent(event);
90  }
91 
92 #ifdef MULTITHREAD
93  for (int j = 0; j < _nWeightSystematics; ++j){
94  if (!_weightSystematics[j]->IsEnabled()) continue;
95  for (std::vector<SelectionBase*>::iterator sit= sel.GetSelections().begin();sit!=sel.GetSelections().end();sit++){
96  SelectionBase* selec = *sit;
97  if (!selec->IsEnabled()) continue;
98  for (UInt_t ibranch=0;ibranch<selec->GetNBranches();ibranch++){
99  _weightSystematics[j]->InitializeEvent(event,*selec,ibranch);
100  }
101  }
102  }
103 #endif
104 }
105 
106 //********************************************************************
107 std::vector<SystematicBase*> SystematicManager::GetSystematics(const std::vector<Int_t>& indices) const{
108 //********************************************************************
109 
110  std::vector<SystematicBase*> systematics;
111  for (UInt_t i=0;i<indices.size();i++)
112  systematics.push_back(GetSystematic(indices[i]));
113 
114  return systematics;
115 }
116 
117 //********************************************************************
118 SystematicBase* SystematicManager::GetSystematic(Int_t index) const{
119 //********************************************************************
120 
121  if (index<0 || (UInt_t)index> NMAXSYSTEMATICS-1) return NULL;
122  return _systematics[index];
123 }
124 
125 //********************************************************************
126 SystematicBase* SystematicManager::GetSystematic(const std::string& name) const{
127 //********************************************************************
128 
129  for (UInt_t i = 0; i < NMAXSYSTEMATICS; ++i) {
130  if (!_systematics[i]) continue;
131  if (_systematics[i]->Name() == name) return _systematics[i];
132  }
133  return NULL;
134 }
135 
136 //********************************************************************
137 EventWeightBase* SystematicManager::GetSystematicWeight(Int_t index) const{
138 //********************************************************************
139 
140  SystematicBase* sys = GetSystematic(index);
141  if (!sys) return NULL;
142  if (sys->Type() != SystematicBase::kWeight) return NULL;
143  return static_cast<EventWeightBase*>(sys);
144 }
145 
146 
147 
148 //********************************************************************
149 EventWeightBase* SystematicManager::GetSystematicWeight(const std::string& name) const{
150 //********************************************************************
151 
152  for (int i = 0; i < _nWeightSystematics; ++i) {
153  if (_weightSystematics[i]->Name() == name) return _weightSystematics[i];
154  }
155  return NULL;
156 }
157 
158 //********************************************************************
160 //********************************************************************
161 
162  SystematicBase* sys = GetSystematic(index);
163  if (!sys) return NULL;
164  if (sys->Type() != SystematicBase::kVariation) return NULL;
165  return static_cast<EventVariationBase*>(sys);
166 }
167 
168 //********************************************************************
169 EventWeightBase* SystematicManager::GetSystematicFlux(const std::string& name) const{
170 //********************************************************************
171 
172  for (int i = 0; i < _nFluxSystematics; ++i) {
173  if (_fluxSystematics[i]->Name() == name) return _fluxSystematics[i];
174  }
175 
176  return NULL;
177 }
178 
179 //********************************************************************
180 EventWeightBase* SystematicManager::GetSystematicFlux(Int_t index) const{
181 //********************************************************************
182 
183  SystematicBase* sys = GetSystematic(index);
184  if (!sys) return NULL;
185  if (sys->Type() != SystematicBase::kFlux) return NULL;
186  return static_cast<EventWeightBase*>(sys);
187 }
188 
189 
190 
191 //********************************************************************
193 //********************************************************************
194 
195  for (int i = 0; i < _nVariationSystematics; i++) {
196  if (_variationSystematics[i]->Name() == name) return _variationSystematics[i];
197  }
198 
199  return NULL;
200 }
201 
202 //********************************************************************
203 Int_t SystematicManager::GetSystematicWeightIndex(const std::string& name){
204 //********************************************************************
205 
206  for (int i = 0; i < _nWeightSystematics; i++) {
207  if (_weightSystematics[i]->Name() == name) return _weightSystematics[i]->GetIndex();
208  }
209 
210  return -1;
211 }
212 
213 //********************************************************************
214 Int_t SystematicManager::GetSystematicFluxIndex(const std::string& name){
215 //********************************************************************
216 
217  for (int i = 0; i < _nFluxSystematics; i++) {
218  if (_fluxSystematics[i]->Name() == name) return i;
219  }
220 
221  return -1;
222 }
223 
224 //********************************************************************
225 Int_t SystematicManager::GetSystematicVariationIndex(const std::string& name){
226 //********************************************************************
227 
228  for (int i = 0; i < _nVariationSystematics; i++) {
229  if (_variationSystematics[i]->Name() == name) return _variationSystematics[i]->GetIndex();
230  }
231 
232  return -1;
233 }
234 
235 //********************************************************************
236 bool SystematicManager::HasSystematic(const std::string& name){
237 //********************************************************************
238 
239  for (int i = 0; i < _nVariationSystematics; i++) {
240  if (_variationSystematics[i]->Name() == name) return true;
241  }
242  for (int i = 0; i < _nWeightSystematics; i++) {
243  if (_weightSystematics[i]->Name() == name) return true;
244  }
245  for (int i = 0; i < _nFluxSystematics; i++) {
246  if (_fluxSystematics[i]->Name() == name) return true;
247  }
248 
249  return false;
250 }
251 
252 //***********************************************************
254 //***********************************************************
255 
256  for (int j = 0; j < _nVariationSystematicsEnabled; ++j){
257  Int_t isyst = _variationSystematicsEnabled[j];
258  EventVariationBase* var = static_cast<EventVariationBase*>(_systematics[isyst]);
259  // Apply the variation systematic. Modify the input data
260  // It needs as input the variations corresponding to the ith systematic
261  var->Apply(toy, event);
262  }
263 }
264 
265 //***********************************************************
267 //***********************************************************
268  // WeightSystematics are applied at the end of the each toy experiment
269 
270  // reset the weight to 1 before applying any systematic
271  Weight_h totalWeight(1);
272 
273  for (int j = 0; j < _nWeightSystematics; j++) {
274  // only apply enabled systematics
275  if (_weightSystematics[j]->IsEnabled()){
276  // Compute the event normalization weight for this systematic
277  totalWeight *= _weightSystematics[j]->ComputeWeight(toy, event, ToyBox);
278  }
279  }
280  return totalWeight;
281 }
282 
283 //***********************************************************
284 Weight_h SystematicManager::ApplyWeightSystematics(const ToyExperiment& toy, const AnaEventC& event, const ToyBoxB& ToyBox, Weight_h* weights) {
285 //***********************************************************
286 
287  // WeightSystematics are applied at the end of the each toy experiment
288  // This method returns as argument an array of weights, one for each of the weight systematics enabled
289 
290  // reset the weight to 1 before applying any systematic
291  Weight_h totalWeight(1);
292  Weight_h weight;
293 
294  Int_t w=0;
295  for (int j = 0; j < _nWeightSystematics; j++) {
296  // only apply enabled systematics
297  if (_weightSystematics[j]->IsEnabled()){
298  // Compute the event normalization weight for this systematic
299  weight = _weightSystematics[j]->ComputeWeight(toy, event, ToyBox);
300  // Save in into the vector
301  weights[w++]=weight;
302  // Increment the total weight
303  totalWeight*= weight;
304  }
305  }
306 
307  return totalWeight;
308 
309 }
310 
311 //***********************************************************
312 Weight_h SystematicManager::ApplyWeightSystematics(const SelectionBase& sel, const ToyExperiment& toy, const AnaEventC& event, const ToyBoxB& ToyBox, Weight_h* weights) {
313 //***********************************************************
314 
315  // Apply the relevant weight systematics for a given selection
316  // This method returns as argument an array of weights, one for each of the weight systematics enabled
317 
318  // WeightSystematics are applied at the end of the each toy experiment
319 
320  // reset the weight to 1 before applying any systematic
321  Weight_h totalWeight(1);
322  Weight_h weight;
323 
324  Int_t w=0;
325  for (int j = 0; j < _nWeightSystematics; j++) {
326  // only apply enabled systematics
327  if (!_weightSystematics[j]->IsEnabled()) continue;
328 
329  Int_t ibranch = ToyBox.SuccessfulBranch;
330  // Check if the systematic is relevant for this selection and branch. If not the weight is 1
331  // Any of the branches should be succesful. Otherwise ibranch=-1
332  if (ibranch>-1 && sel.IsRelevantSystematic(event, ToyBox, _weightSystematics[j]->GetIndex(), ibranch)){
333 #ifndef MULTITHREAD
334  // We need to initialize the event (Fill the SystBox). We only do that for the first succesful toy in a given selection and branch
335  if (!_weightSystematics[j]->GetSystBox(event,sel.GetEnabledIndex(),ibranch))
336  _weightSystematics[j]->InitializeEvent(event,sel,ibranch);
337 #endif
338  // Compute the event normalization weight for this systematic
339  weight = _weightSystematics[j]->ComputeWeight(toy, event, ToyBox, sel);
340  }
341  else
342  weight=1;
343 
344  // Save it into the double array
345  weights[w++]=weight;
346  // Increment the total weight
347  totalWeight*= weight;
348  }
349 
350  return totalWeight;
351 }
352 
353 //***********************************************************
355 //***********************************************************
356 
357  // Flux Systematics are applied at the end of the each virtual analysis
358 
359  Weight_h weight(1);
360 
361  for (int j = 0; j < _nFluxSystematics; j++) {
362  // only apply enabled systematics
363  if (_fluxSystematics[j]->IsEnabled()){
364  // Compute the event normalization weight for this systematic
365  weight *= _fluxSystematics[j]->ComputeWeight(toy, event, ToyBox);
366  }
367  }
368 
369  return weight;
370 }
371 
372 
373 //***********************************************************
374 Weight_h SystematicManager::ApplyWeightSystematic(Int_t index, const ToyExperiment& toy, const AnaEventC& event, const ToyBoxB& ToyBox) {
375 //***********************************************************
376 
377  // Apply an specific weight systematic, provided its index
378  // WeightSystematics are applied at the end of the each virtual analysis
379 
380  // Compute the event normalization weight for this systematic
381  return _weightSystematics[index]->ComputeWeight(toy, event, ToyBox);
382 }
383 
384 //***********************************************************
386 //***********************************************************
387 
388  // Returns true when any of the systematics returns true. That means that the full Spill will be reseted
389 
390  bool ok=false;
391  for (int i = 0; i < _nVariationSystematics; ++i) {
392  if (_variationSystematics[i]->IsEnabled())
393  if (_variationSystematics[i]->UndoSystematic(event)) ok=true;
394  }
395  return ok;
396 }
397 
398 //***********************************************************
400 //***********************************************************
401 
402  SystematicBase* syst = GetSystematic(index);
403  if (syst){
404  syst->SetEnabled(true);
405  if (syst->Type() == SystematicBase::kWeight){
406  _weightSystematicsEnabled[_nWeightSystematicsEnabled] = index;
407  _nWeightSystematicsEnabled++;
408  }
409  else if (syst->Type() == SystematicBase::kVariation){
410  _variationSystematicsEnabled[_nVariationSystematicsEnabled] = index;
411  _nVariationSystematicsEnabled++;
412  }
413  else if (syst->Type() == SystematicBase::kFlux){
414  _fluxSystematicsEnabled[_nFluxSystematicsEnabled] = index;
415  _nFluxSystematicsEnabled++;
416  }
417  }
418 }
419 
420 //***********************************************************
422 //***********************************************************
423 
424  SystematicBase* syst = GetSystematic(index);
425  if (!syst) return;
426  syst->SetEnabled(false);
427  if (syst->Type() == SystematicBase::kWeight){
428  for (Int_t it = 0; it < _nWeightSystematicsEnabled;it++){
429  if (_weightSystematicsEnabled[it] == index){
430  for(int j = it; j < _nWeightSystematicsEnabled - 1; ++j){
431  _weightSystematicsEnabled[j] = _weightSystematicsEnabled[j+1];
432  }
433  _nWeightSystematicsEnabled--;
434  break;
435  }
436  }
437  }
438  else if (syst->Type() == SystematicBase::kVariation){
439  for (Int_t it = 0; it < _nVariationSystematicsEnabled;it++){
440  if (_variationSystematicsEnabled[it] == index){
441  for(int j = it; j < _nVariationSystematicsEnabled - 1; ++j){
442  _variationSystematicsEnabled[j] = _variationSystematicsEnabled[j+1];
443  }
444  _nVariationSystematicsEnabled--;
445  break;
446  }
447  }
448  }
449  else if (syst->Type() == SystematicBase::kFlux){
450  for (Int_t it = 0; it < _nFluxSystematicsEnabled;it++){
451  if (_fluxSystematicsEnabled[it] == index){
452  for(int j = it; j < _nFluxSystematicsEnabled - 1; ++j){
453  _fluxSystematicsEnabled[j] = _fluxSystematicsEnabled[j+1];
454  }
455  _nFluxSystematicsEnabled--;
456  break;
457  }
458  }
459  }
460 }
461 
462 //***********************************************************
463 void SystematicManager::EnableSystematics(const std::vector<Int_t>& indices) {
464 //***********************************************************
465 
466  for (std::vector<Int_t>::const_iterator it=indices.begin();it!=indices.end();it++){
467  EnableSystematic(*it);
468  }
469 }
470 
471 //***********************************************************
472 void SystematicManager::DisableSystematics(const std::vector<Int_t>& indices) {
473 //***********************************************************
474 
475  for (std::vector<Int_t>::const_iterator it=indices.begin();it!=indices.end();it++){
476  DisableSystematic(*it);
477  }
478 }
479 
480 //***********************************************************
482 //***********************************************************
483 
484  _nVariationSystematicsEnabled = 0;
485  for (int it = 0; it < _nVariationSystematics; it++) {
486  _variationSystematics[it]->SetEnabled(true);
487  _variationSystematicsEnabled[_nVariationSystematicsEnabled] = _variationSystematics[it]->GetIndex();
488  _nVariationSystematicsEnabled++;
489  }
490 }
491 
492 //***********************************************************
494 //***********************************************************
495 
496  _nWeightSystematicsEnabled = 0;
497  for (int it = 0; it < _nWeightSystematics; it++) {
498  _weightSystematics[it]->SetEnabled(true);
499  _weightSystematicsEnabled[_nWeightSystematicsEnabled] = _weightSystematics[it]->GetIndex();
500  _nWeightSystematicsEnabled++;
501  }
502 }
503 
504 
505 //***********************************************************
507 //***********************************************************
508 
509  _nFluxSystematicsEnabled = 0;
510  for (int it = 0; it < _nFluxSystematics; it++) {
511  _fluxSystematics[it]->SetEnabled(true);
512  _fluxSystematicsEnabled[_nFluxSystematicsEnabled] = _fluxSystematics[it]->GetIndex();
513  _nFluxSystematicsEnabled++;
514  }
515 }
516 
517 //***********************************************************
519 //***********************************************************
520 
521  EnableAllWeightSystematics();
522  EnableAllVariationSystematics();
523  EnableAllFluxSystematics();
524 }
525 
526 //***********************************************************
528 //***********************************************************
529 
530  for (int it = 0; it < _nVariationSystematics; it++) {
531  _variationSystematics[it]->SetEnabled(false);
532  }
533  _nVariationSystematicsEnabled = 0;
534 }
535 
536 //***********************************************************
538 //***********************************************************
539 
540  for (int it = 0; it < _nWeightSystematics; it++) {
541  _weightSystematics[it]->SetEnabled(false);
542  }
543  _nWeightSystematicsEnabled = 0;
544 }
545 
546 //***********************************************************
548 //***********************************************************
549 
550  for (int it = 0; it < _nFluxSystematics; it++) {
551  _fluxSystematics[it]->SetEnabled(false);
552  }
553  _nFluxSystematicsEnabled = 0;
554 
555 }
556 
557 
558 //***********************************************************
560 //***********************************************************
561 
562  DisableAllWeightSystematics();
563  DisableAllVariationSystematics();
564  DisableAllFluxSystematics();
565 }
566 
567 //***********************************************************
568 void SystematicManager::AddVariationSystematic(Int_t index, const std::string& name, EventVariationBase* sys) {
569 //***********************************************************
570 
571  sys->SetName(name);
572  AddVariationSystematic(index,sys);
573 }
574 
575 //***********************************************************
577 //***********************************************************
578 
579  if (GetSystematicVariation(index)){
580  std::cout << "SystematicManager::AddVariationSystematic(). '" << index << "' already added !!!" <<std::endl;
581  exit(1);
582  }
583 
584  sys->SetIndex(index);
585  _variationSystematics[_nVariationSystematics] = sys;
586  _nVariationSystematics++;
587  _systematics[index] = sys;
588  (*_objects)[_NObjects++] = sys;
589 }
590 
591 //***********************************************************
593 //***********************************************************
594 
595  std::cout << _NObjects << std::endl;
596  DumpSystematics();
597 
598  for (Int_t i=0;i<_NObjects;i++){
599  SystematicBase* sys2 = (SystematicBase*)(*_objects)[i];
600  std::cout << i << " " << sys2 << std::endl;
601  std::cout << sys2->GetNParameters() << std::endl;
602  std::cout << sys2->GetIndex() << std::endl;
603  }
604 
605 
606  bool found=false;
607  for (int it = 0; it < _nVariationSystematics; it++) {
608  if (_variationSystematics[it]->GetIndex() == index){
609  // sys->SetName(name);
610  _variationSystematics[it] = sys;
611  found=true;
612  break;
613  }
614  }
615  if (!found){
616  std::cout << "SystematicManager::ReplaceVariationSystematic(). '" << index << "' does not exist !!!" <<std::endl;
617  exit(1);
618  }
619 
620  for (Int_t i=0;i<_NObjects;i++){
621  SystematicBase* sys2 = (SystematicBase*)(*_objects)[i];
622  std::cout << i << " " << sys2 << std::endl;
623  std::cout << sys2->GetNParameters() << std::endl;
624  std::cout << sys2->Name() << std::endl;
625  if (sys2->GetIndex() == index){
626  (*_objects)[i] = sys;
627  break;
628  }
629  }
630 }
631 
632 //***********************************************************
633 void SystematicManager::AddWeightSystematic(Int_t index, const std::string& name, EventWeightBase* sys) {
634 //***********************************************************
635 
636  sys->SetName(name);
637  AddWeightSystematic(index,sys);
638 }
639 
640 //***********************************************************
642 //***********************************************************
643 
644  if (GetSystematicWeight(index)){
645  std::cout << "SystematicManager::AddWeightSystematic(). '" << index << "' already added !!!" <<std::endl;
646  exit(1);
647  }
648 
649  // sys->SetName(name);
650  sys->SetIndex(index);
651  _weightSystematics[_nWeightSystematics++] = sys;
652  _systematics[index] = sys;
653  (*_objects)[_NObjects++] = sys;
654 }
655 
656 //***********************************************************
658 //***********************************************************
659 
660  bool found=false;
661  for (int it = 0; it < _nWeightSystematics; it++) {
662  if (_weightSystematics[it]->GetIndex() == index){
663  // sys->SetName(name);
664  _weightSystematics[it] = sys;
665  found=true;
666  break;
667  }
668  }
669  if (!found){
670  std::cout << "SystematicManager::ReplaceWeightSystematic(). '" << index << "' does not exist !!!" <<std::endl;
671  exit(1);
672  }
673 
674  for (Int_t i=0;i<_NObjects;i++){
675  SystematicBase* sys2 = (SystematicBase*)(*_objects)[i];
676  if (sys2->GetIndex() == index){
677  (*_objects)[i] = sys;
678  break;
679  }
680  }
681 }
682 
683 //***********************************************************
685 //***********************************************************
686 
687  if (GetSystematicFlux(index)){
688  std::cout << "SystematicManager::AddFluxSystematic(). '" << index << "' already added !!!" <<std::endl;
689  exit(1);
690  }
691 
692  sys->SetIndex(index);
693  _fluxSystematics[_nFluxSystematics++] = sys;
694  _systematics[index] = sys;
695  (*_objects)[_NObjects++] = sys;
696 }
697 
698 //***********************************************************
699 void SystematicManager::AddFluxSystematic(Int_t index, const std::string& name, EventWeightBase* sys) {
700 //***********************************************************
701  sys->SetName(name);
702  AddFluxSystematic(index,sys);
703 
704 }
705 
706 //***********************************************************
708 //***********************************************************
709 
710  bool found=false;
711  for (int it = 0; it < _nFluxSystematics; it++) {
712  if (_fluxSystematics[it]->GetIndex() == index){
713  // sys->SetName(name);
714  _fluxSystematics[it] = sys;
715  found=true;
716  break;
717  }
718  }
719  if (!found){
720  std::cout << "SystematicManager::ReplaceFluxSystematic(). '" << index << "' does not exist !!!" <<std::endl;
721  exit(1);
722  }
723 
724  for (Int_t i=0;i<_NObjects;i++){
725  SystematicBase* sys2 = (SystematicBase*)(*_objects)[i];
726  if (sys2->GetIndex() == index){
727  (*_objects)[i] = sys;
728  break;
729  }
730  }
731 }
732 
733 
734 //********************************************************************
735 void SystematicManager::ReadSystematics(const std::string& file){
736 //********************************************************************
737 
738  // Reset the vectors
739  _nVariationSystematics = 0;
740  _nWeightSystematics = 0;
741  _nFluxSystematics = 0;
742  _nVariationSystematicsEnabled = 0;
743  _nWeightSystematicsEnabled = 0;
744  _nFluxSystematicsEnabled = 0;
745 
746  _systematics.resize(NMAXSYSTEMATICS);
747  _systematics.assign(_systematics.size(),NULL);
748 
749  ReadClonesArray(file);
750 
751  // Add them to the map of categories
752  for (Int_t i=0;i<_NObjects;i++){
753  SystematicBase* sys = (SystematicBase*)(*_objects)[i];
754  if (sys->Type() == SystematicBase::kVariation){
755  _variationSystematics[_nVariationSystematics++] = (EventVariationBase*)sys;
756  _variationSystematicsEnabled[_nVariationSystematicsEnabled++] = sys->GetIndex();
757  }
758  else if (sys->Type() == SystematicBase::kWeight){
759  _weightSystematics[_nWeightSystematics++] = (EventWeightBase*)sys;
760  _weightSystematicsEnabled[_nWeightSystematicsEnabled++] = sys->GetIndex();
761  }
762  else if (sys->Type() == SystematicBase::kFlux){
763  _fluxSystematics[_nFluxSystematics++] = (EventWeightBase*)sys;
764  _fluxSystematicsEnabled[_nFluxSystematicsEnabled++] = sys->GetIndex();
765  }
766  _systematics[sys->GetIndex()]= sys;
767  }
768 }
769 
770 //********************************************************************
772 //********************************************************************
773 
774  std::cout << " -------- List of Systematics --------------------" << std::endl;
775  char out[256];
776  sprintf(out,"%3s: %-25s %-15s %4s", "#", "name", "type", "NPar");
777  std::cout << out <<"\n" << std::endl;
778 
779  for (UInt_t i=0;i<_systematics.size();i++){
780  SystematicBase* sys = _systematics[i];
781  if (!sys) continue;
782  sprintf(out,"%3d: %-25s %-15s %4d", i, sys->Name().c_str(), sys->ConvertType().c_str(), sys->GetNParameters());
783  std::cout << out << std::endl;
784  }
785 
786  std::cout << " --------------------------------------------------" << std::endl;
787 }
788 
789 
790 //********************************************************************
792 //********************************************************************
793 
794  std::cout << " -------- List of VariationSystematics ------------" << std::endl;
795  char out[256];
796  sprintf(out,"%3s: %-25s %-15s %-4s", "#", "name", "pdf", "NPar");
797  std::cout << out << "\n" << std::endl;
798 
799 
800  Int_t j=0;
801  for (int it = 0; it < _nVariationSystematics; it++) {
802  if (_variationSystematics[it]->IsEnabled()){
803  sprintf(out,"%3d: %-25s %-15s %-4d", j, _variationSystematics[it]->Name().c_str(), _variationSystematics[it]->ConvertPDF().c_str(), _variationSystematics[it]->GetNParameters());
804  std::cout << out << std::endl;
805  j++;
806  }
807  }
808  std::cout << " --------------------------------------------------" << std::endl;
809 }
810 
811 //********************************************************************
813 //********************************************************************
814 
815  std::cout << " -------- List of WeightSystematics ---------------" << std::endl;
816  char out[256];
817  sprintf(out,"%3s: %-25s %-15s %-4s", "#", "name", "pdf", "NPar");
818  std::cout << out << "\n" << std::endl;
819 
820  Int_t j=0;
821  for (int it = 0; it < _nWeightSystematics; it++) {
822  if (_weightSystematics[it]->IsEnabled()){
823  sprintf(out,"%3d: %-25s %-15s %-4d", j, _weightSystematics[it]->Name().c_str(), _weightSystematics[it]->ConvertPDF().c_str(), _weightSystematics[it]->GetNParameters());
824  std::cout << out << std::endl;
825  j++;
826  }
827  }
828  std::cout << " --------------------------------------------------" << std::endl;
829 }
830 
831 //********************************************************************
833 //********************************************************************
834 
835  std::cout << "-------- List of FluxSystematics -------" << std::endl;
836  char out[256];
837  sprintf(out,"%3s: %-25s %-15s %-4s", "#", "name", "pdf", "NPar");
838  std::cout << out << std::endl;
839  std::cout << " --------------------------------------------------" << std::endl;
840 
841  Int_t j=0;
842  for (int it = 0; it < _nFluxSystematics; it++) {
843  if (_fluxSystematics[it]->IsEnabled()){
844  sprintf(out,"%3d: %-25s %-15s %-4d", j, _fluxSystematics[it]->Name().c_str(), _fluxSystematics[it]->ConvertPDF().c_str(), _fluxSystematics[it]->GetNParameters());
845  std::cout << out << std::endl;
846  j++;
847  }
848  }
849  std::cout << " --------------------------------------------------" << std::endl;
850 }
851 
852 //********************************************************************
854 //********************************************************************
855  _covariance->ConstructCovarianceMatrix(this);
856 }
857 //********************************************************************
858 TMatrixT<double>* SystematicManager::GetCovarianceMatrix() const{
859 //********************************************************************
860  return _covariance->GetCovarianceMatrix();
861 }
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.
bool UndoVariationSystematics(AnaEventC &event)
Undo the variation systematic (Undo the variation, that is, go back to the previous values of modifie...
TMatrixT< double > * GetCovarianceMatrix() const
Get the covariance matrix.
void AddWeightSystematic(Int_t index, EventWeightBase *sys)
Register an SystematicBase as a weight systematic.
void DisableAllFluxSystematics()
Disable all fluxSystematics.
Int_t GetIndex() const
Return the index of this systematic.
void DisableAllVariationSystematics()
Disable all variationSystematics.
void DumpFluxSystematics()
Dump all fluxSystematics.
void DisableAllWeightSystematics()
Disable all weightSystematics.
void EnableAllSystematics()
Enable all Systematics.
void ReplaceFluxSystematic(Int_t index, EventWeightBase *sys)
Replace one of the existing EventWeightBase.
TypeEnum Type() const
Return the type of this systematic.
virtual void Apply(const ToyExperiment &toy, AnaEventC &event)=0
UInt_t GetNEnabledSelections()
Returns the number of enabled selections.
void ReplaceVariationSystematic(Int_t index, EventVariationBase *sys)
Replace one of the existing EventVariationBase.
void EnableSystematic(Int_t index)
Enable the systematic registered with the given index.
void DumpVariationSystematics()
Dump all variationSystematics.
void AddFluxSystematic(Int_t index, EventWeightBase *sys)
Register an SystematicBase as a flux systematic.
void ReplaceWeightSystematic(Int_t index, EventWeightBase *sys)
Replace one of the existing EventWeightBase.
Int_t GetSystematicVariationIndex(const std::string &name)
Get the index of a systematic registered with the given name.
void DisableSystematic(Int_t index)
Disable the systematic registered with the given index.
std::vector< SelectionBase * > & GetSelections()
Return the map of selections.
void DumpSystematics()
Dump all systematics.
void SetName(const std::string &name)
Set the name of this systematic.
Weight_h ApplyFluxSystematics(const ToyExperiment &toy, const AnaEventC &event, const ToyBoxB &ToyBox)
Apply all fluxSystematics. Returns the total event Weight.
Weight_h ApplyWeightSystematics(const ToyExperiment &toy, const AnaEventC &event, const ToyBoxB &ToyBox)
Aplpy all weightSystematics. Returns the total event normalization weight.
Weight_h ApplyWeightSystematic(Int_t index, const ToyExperiment &toy, const AnaEventC &event, const ToyBoxB &ToyBox)
Apply weight Systematics with a given index. Returns the event normalization weight.
void ApplyVariationSystematics(const ToyExperiment &toy, AnaEventC &event)
Apply all variationSystematics.
void ConstructCovarianceMatrix()
Make the covariance.
virtual void InitializeEvent(AnaEventC &)
Fill the EventBox with the objects needed by this selection.
Int_t SuccessfulBranch
The branch that is successful for this toy in the selection this ToyBox belongs to.
Definition: ToyBoxB.hxx:46
std::vector< SystematicBase * > & GetSystematics()
Get the vector containing all systematics (contains NULL pointers)
bool HasSystematic(const std::string &name)
Check if a systematic already exists.
void DisableAllSystematics()
Disable all Systematics.
void EnableAllFluxSystematics()
Enable all systematic fluxSystematics.
void SetIndex(Int_t index)
Set the index of this systematic.
const std::string & Name() const
Return the name of this systematic.
void DumpWeightSystematics()
Dump all weightSystematics.
EventVariationBase * GetSystematicVariation(const std::string &name) const
Get the systematic registered with the given name.
UInt_t GetNBranches() const
Return the number of branches.
void DisableSystematics(const std::vector< Int_t > &systs)
Disable the systematics registered with the given indices.
void EnableSystematics(const std::vector< Int_t > &systs)
Enable the systematics registered with the given indices.
std::string ConvertType()
Return the type of this systematic.
void ReadSystematics(const std::string &file)
Read the systematics from a file.
void EnableAllWeightSystematics()
Enable all systematic weightSystematics.
void AddVariationSystematic(Int_t index, EventVariationBase *sys)
Register an EventVariationBase as a variation systematic.
UInt_t GetNParameters() const
Returns the number of systematic parameters associated to this systematic.
void EnableAllVariationSystematics()
Enable all variationSystematics.
Int_t GetEnabledIndex() const
Get the Selection index.