1 #include "SystematicManager.hxx" 2 #include "MultiThread.hxx" 6 SystematicManager::SystematicManager():
HLClonesArray(
"config",
"SYST",
"SystematicBase",NMAXSYSTEMATICS) {
10 _systematics.resize(NMAXSYSTEMATICS);
11 _systematics.assign(_systematics.size(),NULL);
13 _nVariationSystematics = 0;
14 _nWeightSystematics = 0;
15 _nFluxSystematics = 0;
16 _nVariationSystematicsEnabled = 0;
17 _nWeightSystematicsEnabled = 0;
18 _nFluxSystematicsEnabled = 0;
23 SystematicManager::~SystematicManager() {
33 for (
int i = 0; i < _nFluxSystematics; ++i) {
34 delete _fluxSystematics[i];
44 for (
int j = 0; j < _nVariationSystematics; ++j){
45 if (_variationSystematics[j]->IsEnabled())
46 _variationSystematics[j]->Initialize(1,0,1, nevents);
52 if (!selec->IsEnabled())
continue;
53 for (
int j = 0; j < _nWeightSystematics; ++j){
54 if (_weightSystematics[j]->IsEnabled())
62 void SystematicManager::FinalizeEventSystematics(
AnaEventC& event) {
64 for (
int j = 0; j < _nVariationSystematics; ++j){
65 if (_variationSystematics[j]->IsEnabled())
66 _variationSystematics[j]->FinalizeEvent(event);
69 for (
int j = 0; j < _nWeightSystematics; ++j){
70 if (_weightSystematics[j]->IsEnabled())
71 _weightSystematics[j]->FinalizeEvent(event);
82 for (
int j = 0; j < _nVariationSystematics; ++j){
83 if (!_variationSystematics[j]->IsEnabled())
continue;
86 if (!selec->IsEnabled())
continue;
89 _variationSystematics[j]->InitializeEvent(event);
93 for (
int j = 0; j < _nWeightSystematics; ++j){
94 if (!_weightSystematics[j]->IsEnabled())
continue;
97 if (!selec->IsEnabled())
continue;
98 for (UInt_t ibranch=0;ibranch<selec->
GetNBranches();ibranch++){
110 std::vector<SystematicBase*> systematics;
111 for (UInt_t i=0;i<indices.size();i++)
112 systematics.push_back(GetSystematic(indices[i]));
118 SystematicBase* SystematicManager::GetSystematic(Int_t index)
const{
121 if (index<0 || (UInt_t)index> NMAXSYSTEMATICS-1)
return NULL;
122 return _systematics[index];
126 SystematicBase* SystematicManager::GetSystematic(
const std::string& name)
const{
129 for (UInt_t i = 0; i < NMAXSYSTEMATICS; ++i) {
130 if (!_systematics[i])
continue;
131 if (_systematics[i]->Name() == name)
return _systematics[i];
137 EventWeightBase* SystematicManager::GetSystematicWeight(Int_t index)
const{
141 if (!sys)
return NULL;
142 if (sys->
Type() != SystematicBase::kWeight)
return NULL;
149 EventWeightBase* SystematicManager::GetSystematicWeight(
const std::string& name)
const{
152 for (
int i = 0; i < _nWeightSystematics; ++i) {
153 if (_weightSystematics[i]->Name() == name)
return _weightSystematics[i];
163 if (!sys)
return NULL;
164 if (sys->
Type() != SystematicBase::kVariation)
return NULL;
169 EventWeightBase* SystematicManager::GetSystematicFlux(
const std::string& name)
const{
172 for (
int i = 0; i < _nFluxSystematics; ++i) {
173 if (_fluxSystematics[i]->Name() == name)
return _fluxSystematics[i];
180 EventWeightBase* SystematicManager::GetSystematicFlux(Int_t index)
const{
184 if (!sys)
return NULL;
185 if (sys->
Type() != SystematicBase::kFlux)
return NULL;
195 for (
int i = 0; i < _nVariationSystematics; i++) {
196 if (_variationSystematics[i]->Name() == name)
return _variationSystematics[i];
203 Int_t SystematicManager::GetSystematicWeightIndex(
const std::string& name){
206 for (
int i = 0; i < _nWeightSystematics; i++) {
207 if (_weightSystematics[i]->Name() == name)
return _weightSystematics[i]->GetIndex();
214 Int_t SystematicManager::GetSystematicFluxIndex(
const std::string& name){
217 for (
int i = 0; i < _nFluxSystematics; i++) {
218 if (_fluxSystematics[i]->Name() == name)
return i;
228 for (
int i = 0; i < _nVariationSystematics; i++) {
229 if (_variationSystematics[i]->Name() == name)
return _variationSystematics[i]->GetIndex();
239 for (
int i = 0; i < _nVariationSystematics; i++) {
240 if (_variationSystematics[i]->Name() == name)
return true;
242 for (
int i = 0; i < _nWeightSystematics; i++) {
243 if (_weightSystematics[i]->Name() == name)
return true;
245 for (
int i = 0; i < _nFluxSystematics; i++) {
246 if (_fluxSystematics[i]->Name() == name)
return true;
256 for (
int j = 0; j < _nVariationSystematicsEnabled; ++j){
257 Int_t isyst = _variationSystematicsEnabled[j];
261 var->
Apply(toy, event);
273 for (
int j = 0; j < _nWeightSystematics; j++) {
275 if (_weightSystematics[j]->IsEnabled()){
277 totalWeight *= _weightSystematics[j]->ComputeWeight(toy, event, ToyBox);
295 for (
int j = 0; j < _nWeightSystematics; j++) {
297 if (_weightSystematics[j]->IsEnabled()){
299 weight = _weightSystematics[j]->ComputeWeight(toy, event, ToyBox);
303 totalWeight*= weight;
325 for (
int j = 0; j < _nWeightSystematics; j++) {
327 if (!_weightSystematics[j]->IsEnabled())
continue;
332 if (ibranch>-1 && sel.
IsRelevantSystematic(event, ToyBox, _weightSystematics[j]->GetIndex(), ibranch)){
335 if (!_weightSystematics[j]->GetSystBox(event,sel.
GetEnabledIndex(),ibranch))
336 _weightSystematics[j]->InitializeEvent(event,sel,ibranch);
339 weight = _weightSystematics[j]->ComputeWeight(toy, event, ToyBox, sel);
347 totalWeight*= weight;
361 for (
int j = 0; j < _nFluxSystematics; j++) {
363 if (_fluxSystematics[j]->IsEnabled()){
365 weight *= _fluxSystematics[j]->ComputeWeight(toy, event, ToyBox);
381 return _weightSystematics[index]->ComputeWeight(toy, event, ToyBox);
391 for (
int i = 0; i < _nVariationSystematics; ++i) {
392 if (_variationSystematics[i]->IsEnabled())
393 if (_variationSystematics[i]->UndoSystematic(event)) ok=
true;
405 if (syst->
Type() == SystematicBase::kWeight){
406 _weightSystematicsEnabled[_nWeightSystematicsEnabled] = index;
407 _nWeightSystematicsEnabled++;
409 else if (syst->
Type() == SystematicBase::kVariation){
410 _variationSystematicsEnabled[_nVariationSystematicsEnabled] = index;
411 _nVariationSystematicsEnabled++;
413 else if (syst->
Type() == SystematicBase::kFlux){
414 _fluxSystematicsEnabled[_nFluxSystematicsEnabled] = index;
415 _nFluxSystematicsEnabled++;
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];
433 _nWeightSystematicsEnabled--;
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];
444 _nVariationSystematicsEnabled--;
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];
455 _nFluxSystematicsEnabled--;
466 for (std::vector<Int_t>::const_iterator it=indices.begin();it!=indices.end();it++){
467 EnableSystematic(*it);
475 for (std::vector<Int_t>::const_iterator it=indices.begin();it!=indices.end();it++){
476 DisableSystematic(*it);
484 _nVariationSystematicsEnabled = 0;
485 for (
int it = 0; it < _nVariationSystematics; it++) {
486 _variationSystematics[it]->SetEnabled(
true);
487 _variationSystematicsEnabled[_nVariationSystematicsEnabled] = _variationSystematics[it]->GetIndex();
488 _nVariationSystematicsEnabled++;
496 _nWeightSystematicsEnabled = 0;
497 for (
int it = 0; it < _nWeightSystematics; it++) {
498 _weightSystematics[it]->SetEnabled(
true);
499 _weightSystematicsEnabled[_nWeightSystematicsEnabled] = _weightSystematics[it]->GetIndex();
500 _nWeightSystematicsEnabled++;
509 _nFluxSystematicsEnabled = 0;
510 for (
int it = 0; it < _nFluxSystematics; it++) {
511 _fluxSystematics[it]->SetEnabled(
true);
512 _fluxSystematicsEnabled[_nFluxSystematicsEnabled] = _fluxSystematics[it]->GetIndex();
513 _nFluxSystematicsEnabled++;
521 EnableAllWeightSystematics();
522 EnableAllVariationSystematics();
523 EnableAllFluxSystematics();
530 for (
int it = 0; it < _nVariationSystematics; it++) {
531 _variationSystematics[it]->SetEnabled(
false);
533 _nVariationSystematicsEnabled = 0;
540 for (
int it = 0; it < _nWeightSystematics; it++) {
541 _weightSystematics[it]->SetEnabled(
false);
543 _nWeightSystematicsEnabled = 0;
550 for (
int it = 0; it < _nFluxSystematics; it++) {
551 _fluxSystematics[it]->SetEnabled(
false);
553 _nFluxSystematicsEnabled = 0;
562 DisableAllWeightSystematics();
563 DisableAllVariationSystematics();
564 DisableAllFluxSystematics();
572 AddVariationSystematic(index,sys);
579 if (GetSystematicVariation(index)){
580 std::cout <<
"SystematicManager::AddVariationSystematic(). '" << index <<
"' already added !!!" <<std::endl;
585 _variationSystematics[_nVariationSystematics] = sys;
586 _nVariationSystematics++;
587 _systematics[index] = sys;
588 (*_objects)[_NObjects++] = sys;
595 std::cout << _NObjects << std::endl;
598 for (Int_t i=0;i<_NObjects;i++){
600 std::cout << i <<
" " << sys2 << std::endl;
602 std::cout << sys2->
GetIndex() << std::endl;
607 for (
int it = 0; it < _nVariationSystematics; it++) {
608 if (_variationSystematics[it]->GetIndex() == index){
610 _variationSystematics[it] = sys;
616 std::cout <<
"SystematicManager::ReplaceVariationSystematic(). '" << index <<
"' does not exist !!!" <<std::endl;
620 for (Int_t i=0;i<_NObjects;i++){
622 std::cout << i <<
" " << sys2 << std::endl;
624 std::cout << sys2->
Name() << std::endl;
626 (*_objects)[i] = sys;
637 AddWeightSystematic(index,sys);
644 if (GetSystematicWeight(index)){
645 std::cout <<
"SystematicManager::AddWeightSystematic(). '" << index <<
"' already added !!!" <<std::endl;
651 _weightSystematics[_nWeightSystematics++] = sys;
652 _systematics[index] = sys;
653 (*_objects)[_NObjects++] = sys;
661 for (
int it = 0; it < _nWeightSystematics; it++) {
662 if (_weightSystematics[it]->GetIndex() == index){
664 _weightSystematics[it] = sys;
670 std::cout <<
"SystematicManager::ReplaceWeightSystematic(). '" << index <<
"' does not exist !!!" <<std::endl;
674 for (Int_t i=0;i<_NObjects;i++){
677 (*_objects)[i] = sys;
687 if (GetSystematicFlux(index)){
688 std::cout <<
"SystematicManager::AddFluxSystematic(). '" << index <<
"' already added !!!" <<std::endl;
693 _fluxSystematics[_nFluxSystematics++] = sys;
694 _systematics[index] = sys;
695 (*_objects)[_NObjects++] = sys;
702 AddFluxSystematic(index,sys);
711 for (
int it = 0; it < _nFluxSystematics; it++) {
712 if (_fluxSystematics[it]->GetIndex() == index){
714 _fluxSystematics[it] = sys;
720 std::cout <<
"SystematicManager::ReplaceFluxSystematic(). '" << index <<
"' does not exist !!!" <<std::endl;
724 for (Int_t i=0;i<_NObjects;i++){
727 (*_objects)[i] = sys;
739 _nVariationSystematics = 0;
740 _nWeightSystematics = 0;
741 _nFluxSystematics = 0;
742 _nVariationSystematicsEnabled = 0;
743 _nWeightSystematicsEnabled = 0;
744 _nFluxSystematicsEnabled = 0;
746 _systematics.resize(NMAXSYSTEMATICS);
747 _systematics.assign(_systematics.size(),NULL);
749 ReadClonesArray(file);
752 for (Int_t i=0;i<_NObjects;i++){
754 if (sys->
Type() == SystematicBase::kVariation){
756 _variationSystematicsEnabled[_nVariationSystematicsEnabled++] = sys->
GetIndex();
758 else if (sys->
Type() == SystematicBase::kWeight){
760 _weightSystematicsEnabled[_nWeightSystematicsEnabled++] = sys->
GetIndex();
762 else if (sys->
Type() == SystematicBase::kFlux){
764 _fluxSystematicsEnabled[_nFluxSystematicsEnabled++] = sys->
GetIndex();
774 std::cout <<
" -------- List of Systematics --------------------" << std::endl;
776 sprintf(out,
"%3s: %-25s %-15s %4s",
"#",
"name",
"type",
"NPar");
777 std::cout << out <<
"\n" << std::endl;
779 for (UInt_t i=0;i<_systematics.size();i++){
783 std::cout << out << std::endl;
786 std::cout <<
" --------------------------------------------------" << std::endl;
794 std::cout <<
" -------- List of VariationSystematics ------------" << std::endl;
796 sprintf(out,
"%3s: %-25s %-15s %-4s",
"#",
"name",
"pdf",
"NPar");
797 std::cout << out <<
"\n" << std::endl;
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;
808 std::cout <<
" --------------------------------------------------" << std::endl;
815 std::cout <<
" -------- List of WeightSystematics ---------------" << std::endl;
817 sprintf(out,
"%3s: %-25s %-15s %-4s",
"#",
"name",
"pdf",
"NPar");
818 std::cout << out <<
"\n" << std::endl;
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;
828 std::cout <<
" --------------------------------------------------" << std::endl;
835 std::cout <<
"-------- List of FluxSystematics -------" << std::endl;
837 sprintf(out,
"%3s: %-25s %-15s %-4s",
"#",
"name",
"pdf",
"NPar");
838 std::cout << out << std::endl;
839 std::cout <<
" --------------------------------------------------" << std::endl;
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;
849 std::cout <<
" --------------------------------------------------" << std::endl;
855 _covariance->ConstructCovarianceMatrix(
this);
860 return _covariance->GetCovarianceMatrix();
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.
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.