HighLAND
CorrectionManager.cxx
1 #include "CorrectionManager.hxx"
2 #include <cassert>
3 
4 //***********************************************************
5 CorrectionManager::CorrectionManager():HLClonesArray("config","CORR","CorrectionBase", NMAXCORRECTIONS) {
6  //***********************************************************
7 
8  // By default corrections applied in the input file are not applied again
9  _forceApplyCorrections = false;
10 
11  //Reset();
12  _corrections.clear();
13 
14 }
15 
16 //***********************************************************
17 CorrectionManager::~CorrectionManager() {
18  //***********************************************************
19  for (std::vector<CorrectionBase*>::iterator it = _corrections.begin(); it != _corrections.end(); it++) {
20  delete *it;
21  *it = NULL;
22  }
23 
24  _corrections.clear();
25 
26 }
27 
28 //***********************************************************
30  //***********************************************************
31 
32  for (std::vector<CorrectionBase*>::iterator it = _corrections.begin(); it != _corrections.end(); it++) {
33  if (!(*it)) continue;
34  if ((*it)->IsEnabled()){
35  (*it)->Apply(spill);
36  }
37  }
38 }
39 
40 
41 //***********************************************************
43  //***********************************************************
44 
45  for (std::vector<CorrectionBase*>::iterator it = _corrections.begin(); it != _corrections.end(); it++) {
46  if (!(*it)) continue;
47  (*it)->Disable();
48  }
49 }
50 
51 
52 //***********************************************************
53 void CorrectionManager::AddCorrection(Int_t index, const std::string& name, CorrectionBase* corr) {
54  //***********************************************************
55 
56  // When running over a FlatTree corrections may already exist in it.
57  // It that case the correction is already present in the manager
58  // (corrections are first read from the input file) and it has to be set as
59  // "appliedInInput" and "disabled", such that it is not applied twice.
60  // Some corrections may exit in the input file but as disabled. This case
61  // is also taken into account: the correction is not added again but it is stored
62  // with the proper settings
63  corr->SetName(name);
64  corr->SetIndex(index);
65  CorrectionBase* corr2 = NULL;
66  //CorrectionBase* corr2 = GetCorrection(index);
67 
68 #pragma message("CorrectionManager::AddCorrection Name usage should be removed once approved!!!")
69 
70  // TMP: try name for bwd compatibility
71  if (!corr2)
72  corr2 = GetCorrection(name);
73 
74  if (corr2 && !_forceApplyCorrections){
75  if(corr2->IsAppliedInInput())
76  corr2->Disable();
77  else{
78  if (corr->IsEnabled()) corr2->Enable();
79  else corr2->Disable();
80  }
81  }
82  else{
83  _corrections.push_back(corr);
84  CorrectionBase* corr2 = new((*_objects)[_NObjects++]) CorrectionBase(*corr);
85  (void)corr2;
86  }
87 }
88 
89 //***********************************************************
91  //***********************************************************
92 
93  for (std::vector<CorrectionBase*>::iterator it = _corrections.begin(); it != _corrections.end(); it++) {
94  if (!(*it)) continue;
95  if ((*it)->Name() == name) return *it;
96  }
97  return NULL;
98 }
99 
100 
101 //***********************************************************
102 bool CorrectionManager::IsEnabled(const std::string& name) {
103  //***********************************************************
104 
105  CorrectionBase* corr = GetCorrection(name);
106  if (corr) return corr->IsEnabled();
107  else return false;
108 
109 }
110 
111 //***********************************************************
112 void CorrectionManager::EnableCorrection(const std::string& name) {
113  //***********************************************************
114 
115  CorrectionBase* corr = GetCorrection(name);
116  if (corr) corr->Enable();
117 }
118 
119 //***********************************************************
120 void CorrectionManager::DisableCorrection(const std::string& name) {
121  //***********************************************************
122 
123  CorrectionBase* corr = GetCorrection(name);
124  if (corr) corr->Disable();
125 }
126 
127 
128 //***********************************************************
130  //***********************************************************
131 
132  if (index < 0 || (UInt_t)index > NMAXCORRECTIONS - 1) return NULL;
133  return _corrections[index];
134 
135 }
136 
137 //***********************************************************
138 bool CorrectionManager::IsEnabled(Int_t index) {
139  //***********************************************************
140 
141  CorrectionBase* corr = GetCorrection(index);
142  if (corr) return corr->IsEnabled();
143  else return false;
144 }
145 
146 //***********************************************************
148  //***********************************************************
149 
150  CorrectionBase* corr = GetCorrection(index);
151  if (corr) corr->Enable();
152 }
153 
154 //***********************************************************
156  //***********************************************************
157 
158  CorrectionBase* corr = GetCorrection(index);
159  if (corr) corr->Disable();
160 }
161 
162 
163 //********************************************************************
164 void CorrectionManager::ReadCorrections(const std::string& file, bool input){
165  //********************************************************************
166 
167 // Reset();
168  _corrections.clear();
169  ReadClonesArray(file);
170 
171  // Add them to the map of categories
172  for (int i = 0; i < _NObjects; i++){
173  CorrectionBase* corr = (CorrectionBase*)(*_objects)[i];
174  assert(_corrections.size() < NMAXCORRECTIONS);
175  _corrections.push_back(corr);
176 
177  // When this method is called From the AnalysisLoop corrections are read from the input file
178  // In that case the correction has to be disabled and mark as applied in input
179  if (input) {
180  if (corr->IsEnabled()){
181  corr->Disable();
182  corr->SetAppliedInInput(true);
183  }
184  }
185  }
186 }
187 
188 //********************************************************************
190  //********************************************************************
191 
192  // Reset the vectors
193  _corrections.clear();
194 
195  _corrections.resize(NMAXCORRECTIONS);
196  _corrections.assign(_corrections.size(),NULL);
197 
198 }
199 
200 //********************************************************************
202  //********************************************************************
203 
204  std::cout << " -------- List of Corrections -----------------------------------" << std::endl;
205  char out[256];
206  sprintf(out,"%3s: %-3s %-25s %-15s %-20s", "#", "index", "name", "enabled", "applied in input");
207  std::cout << out << "\n" << std::endl;
208 
209  for (int i = 0; i < _NObjects; i++){
210  CorrectionBase* corr = _corrections[i];
211  if (!corr) continue;
212  sprintf(out,"%3d: %-3d %-25s %-15d %-20d", i, corr->GetIndex(), corr->Name().c_str(), (Int_t)corr->IsEnabled(), (Int_t)corr->IsAppliedInInput());
213  std::cout << out << std::endl;
214  }
215  std::cout << " -----------------------------------------------------------------" << std::endl;
216 }
217 
void DisableAllCorrections()
Disable all corrections in a given configuration (if conf=="" for all confs)
void Enable()
Enable the correction.
void SetName(const std::string &name)
Set the name of this correction.
void AddCorrection(Int_t index, const std::string &name, CorrectionBase *corr)
const std::string & Name() const
Return the name of this correction.
void DisableCorrection(Int_t index)
void Reset()
Reset the corrections data.
All corrections should be registered with the CorrectionManager.
CorrectionBase * GetCorrection(Int_t index)
Get the input correction registered with the given index.
void Disable()
Disable the correction.
void SetIndex(Int_t index)
Set the index of this correction.
void SetAppliedInInput(bool ap)
Set the correction as applied in the input file.
Int_t GetIndex() const
Return the index of this correction.
void EnableCorrection(Int_t index)
bool IsEnabled() const
Is the correction enabled.
void ApplyCorrections(AnaSpillC &spill)
Apply all corrections.
bool IsAppliedInInput() const
Is the correction already applied in the input file ?
bool IsEnabled(Int_t index)
Check if a particular correction is enabled in a given configuration.
void DumpCorrections()
Dump all corrections.
void ReadCorrections(const std::string &file, bool input=false)
Readthe corrections from a file.