HighLAND
SelectionManager.cxx
1 #include "SelectionManager.hxx"
2 
3 
4 //********************************************************************
5 SelectionManager::SelectionManager():HLClonesArray("config","SEL","SelectionBase",NMAXSELECTIONS){
6 //********************************************************************
7 
8  _nMaxCuts=0;
9  _nMaxBranches=0;
10  _nEnabledSelections=0;
11 }
12 
13 //********************************************************************
14 SelectionBase* SelectionManager::GetSelection(const std::string& name, bool print_error) {
15 //********************************************************************
16 
17  for (std::vector<SelectionBase*>::iterator it=_eventSelections.begin();it!=_eventSelections.end();it++){
18  if ((*it)->Name() == name) return *it;
19  }
20 
21  if (print_error)
22  std::cout << "SelectionManager::GetSelection(). Selection '" << name << "' does not exist !!!!" << std::endl;
23 
24  return NULL;
25 }
26 
27 //********************************************************************
28 SelectionBase* SelectionManager::GetSelection(Int_t index, bool print_error) {
29 //********************************************************************
30 
31  if (index<0 || index>=(Int_t)_eventSelections.size()){
32  if (print_error)
33  std::cout << "SelectionManager::GetSelection(). Selection with index " << index << " does not exist !!!!" << std::endl;
34  return NULL;
35  }
36 
37  return _eventSelections[index];
38 }
39 
40 //********************************************************************
41 void SelectionManager::AddSelection(const std::string& name, const std::string& title, SelectionBase* selection, Int_t presel){
42 //********************************************************************
43 
44  // nothing to do if selection already exists
45  if (GetSelection(name,false)) return;
46 
47  // Set name and title
48  selection->SetName(name);
49  selection->SetTitle(title);
50 
51  // Define the steps and the DetectorFV for the selection
52  selection->Initialize();
53 
54  // Get the index as enabled selection
55  selection->SetEnabledIndex(_nEnabledSelections);
56 
57  // Set preselection accum level, but only when it is specified, use otherwise the default for that selection
58  if (presel>=0)
59  selection->SetPreSelectionAccumLevel(presel);
60 
61  // add the selection
62  (*_objects)[_NObjects++] = selection;
63  _eventSelections.push_back(selection);
64 
65  // computes the maximum number of cuts and branches for all added selections
66  if (selection->GetNBranches() >_nMaxBranches) _nMaxBranches = selection->GetNBranches();
67  if (selection->GetNMaxCuts() >_nMaxCuts) _nMaxCuts = selection->GetNMaxCuts();
68 
69  // computes the number of enabled selections
70  _nEnabledSelections++;
71 }
72 
73 //********************************************************************
74 void SelectionManager::DisableSelection(const std::string& name){
75 //********************************************************************
76 
77  SelectionBase* sel = GetSelection(name);
78  if (!sel) return;
79 
80  // nothing to do when the selection is already disabled
81  if (!sel->IsEnabled()) return;
82 
83  // recompute the number of enabled selections
84  _nEnabledSelections--;
85 
86  // Disable the selection
87  sel->Disable();
88 
89  Int_t isel=0;
90  for (std::vector<SelectionBase*>::iterator it=_eventSelections.begin();it!=_eventSelections.end();it++){
91  if ((*it)->IsEnabled()){
92  (*it)->SetEnabledIndex(isel++);
93  }
94  }
95 
96 }
97 
98 //********************************************************************
99 void SelectionManager::EnableSelection(const std::string& name){
100 //********************************************************************
101 
102  SelectionBase* sel = GetSelection(name);
103  if (!sel) return;
104 
105  // nothing to do when the selection is already enabled
106  if (sel->IsEnabled()) return;
107 
108  // enable the selection
109  sel->Enable();
110 
111  // recompute the number of enabled selections
112  _nEnabledSelections++;
113 
114  // recompute and set the new slection indices
115  Int_t isel=0;
116  for (std::vector<SelectionBase*>::iterator it=_eventSelections.begin();it!=_eventSelections.end();it++){
117  if ((*it)->IsEnabled()){
118  (*it)->SetEnabledIndex(isel++);
119  }
120  }
121 
122 }
123 
124 //********************************************************************
125 bool SelectionManager::ApplySelection(const std::string& name, AnaEventC& event, bool& redo) {
126 //********************************************************************
127 
128  SelectionBase* sel = GetSelection(name);
129  if (!sel) return true;
130 
131  // Check if it is enabled
132  if (!sel->IsEnabled()) return true;
133 
134  // Apply the selection
135  return sel->Apply(event, redo);
136 }
137 
138 //********************************************************************
139 void SelectionManager::ReadSelections(const std::string& file){
140 //********************************************************************
141 
142  // Reset the vectors
143  _eventSelections.clear();
144 
145  _nMaxCuts=0;
146  _nMaxBranches=0;
147  _nEnabledSelections=0;
148 
149  // Read the selections from the clones array in the input file
150  ReadClonesArray(file);
151 
152  for (int i=0;i<_NObjects;i++){
153  SelectionBase* sel = (SelectionBase*)(*_objects)[i];
154  _eventSelections.push_back(sel);
155  if (sel->IsEnabled()){
156 
157  // computes the maximum number of cuts and branches for all added selections
158  if (sel->GetNBranches() >_nMaxBranches) _nMaxBranches = sel->GetNBranches();
159  if (sel->GetNMaxCuts() >_nMaxCuts) _nMaxCuts = sel->GetNMaxCuts();
160 
161  // recompute the number of enabled selections
162  _nEnabledSelections++;
163  }
164  }
165 
166  // int nselections[NMAXBRANCHES]={0};
167 
168 }
169 
170 //********************************************************************
172 //********************************************************************
173 
174  std::cout << " -------- List of Selections -----------------------------------------------------------------------------------------------------" << std::endl;
175  char out[256];
176  sprintf(out,"%3s: %-25s %-40s %-10s %-10s %-13s %-20s %-22s", "#", "name", "title", "enabled", "#branches", "force break", "presel accum_level", "index in accum_level");
177  std::cout << out <<"\n" << std::endl;
178 
179  UInt_t index=0;
180  for (int i=0;i<_NObjects;i++){
181  SelectionBase* sel = (SelectionBase*)(*_objects)[i];
182  if (sel->IsEnabled()){
183  sprintf(out,"%3d: %-25s %-40s %-10d %-10d %-13d %-20d %-22d", i, sel->Name().c_str(), sel->Title().c_str(),
184  (int)sel->IsEnabled(), sel->GetNBranches(), (int)sel->GetForceBreak(), (int)sel->GetPreSelectionAccumLevel(), index);
185  index++;
186  }
187  else{
188  sprintf(out,"%3d: %-25s %-40s %-10d %-10d %-13d %-20d %-22s", i, sel->Name().c_str(), sel->Title().c_str(),
189  (int)sel->IsEnabled(), sel->GetNBranches(), (int)sel->GetForceBreak(), (int)sel->GetPreSelectionAccumLevel(), "-");
190 
191  }
192  std::cout << out << std::endl;
193  }
194 
195  std::cout << " ----------------------------------------------------------------------------------------------------------------------------------" << std::endl;
196 }
197 
198 
199 //********************************************************************
201 //********************************************************************
202 
203  /// Loop over selections
204  for (std::vector<SelectionBase*>::iterator it = _eventSelections.begin(); it!=_eventSelections.end(); it++){
205  if ((*it)->IsEnabled())
206  (*it)->PrintStatistics();
207  }
208 }
209 
210 // //********************************************************************
211 // void SelectionManager::CopySteps(const std::string& ssel1, const std::string& sbranch1, UInt_t first, UInt_t last,const std::string& ssel2, const std::string& sbranch2){
212 // //********************************************************************
213 
214 // SelectionBase* sel1 = GetSelection(ssel1);
215 // SelectionBase* sel2 = GetSelection(ssel2);
216 
217 // if (!sel1 || !sel2) return;
218 
219 // sel2->CopySteps(*sel1,sbranch1,first,last,sbranch2);
220 
221 // // computes the maximum number of cuts and branches for all added selections
222 // if (sel2->GetNBranches() >_nMaxBranches) _nMaxBranches = sel2->GetNBranches();
223 // if (sel2->GetNMaxCuts() >_nMaxCuts) _nMaxCuts = sel2->GetNMaxCuts();
224 // }
225 
226 // //********************************************************************
227 // void SelectionManager::CopySteps(const std::string& ssel1, const std::string& sbranch1, const std::string& ssel2, const std::string& sbranch2){
228 // //********************************************************************
229 
230 // // Copy all steps
231 // SelectionBase* sel1 = GetSelection(ssel1);
232 // if (!sel1) return;
233 
234 // std::vector<StepBase*> steps1 = sel1->GetStepsInBranch(sbranch1);
235 // CopySteps(ssel1,sbranch1,0,steps1.size()-1,ssel2,sbranch2);
236 // }
237 
238 // //********************************************************************
239 // void SelectionManager::CopyStep(const std::string& ssel1, const std::string& sbranch1, UInt_t istep, const std::string& ssel2, const std::string& sbranch2){
240 // //********************************************************************
241 
242 // // Copy only one step
243 // CopySteps(ssel1,sbranch1,istep,istep,ssel2,sbranch2);
244 // }
245 
246 //********************************************************************
248 //********************************************************************
249 
250  for (std::vector<SelectionBase*>::iterator it=_eventSelections.begin();it!=_eventSelections.end();it++){
251  (*it)->SetForceFillEventSummary(force);
252  }
253 }
254 
255 //********************************************************************
256 void SelectionManager::SetValidRunPeriods(const std::string& ssel1, const std::string validRunPeriods){
257 //********************************************************************
258  SelectionBase* sel1 = GetSelection(ssel1);
259  sel1->SetValidRunPeriods(validRunPeriods);
260 }
261 
262 //***********************************************************
264 //***********************************************************
265 
266  for (std::vector<SelectionBase*>::iterator it=_eventSelections.begin();it!=_eventSelections.end();it++){
267  if ((*it)->IsEnabled()){
268  if ((*it)->PreSelectionPassed(event)) return true;
269  }
270  }
271  return false;
272 }
273 
274 //********************************************************************
276 //********************************************************************
277 
278  // Loop over selection
279  for (std::vector<SelectionBase*>::iterator it = _eventSelections.begin(); it!=_eventSelections.end(); it++){
280  if ((*it)->IsEnabled()){
281  (*it)->CreateToyBoxArray(nevents);
282  }
283  }
284 
285 }
286 
287 //********************************************************************
289 //********************************************************************
290 
291  // Loop over selection
292  for (std::vector<SelectionBase*>::iterator it = _eventSelections.begin(); it!=_eventSelections.end(); it++){
293  if ((*it)->IsEnabled()){
294  (*it)->FinalizeEvent(event);
295  }
296  }
297 }
298 
299 //********************************************************************
301 //********************************************************************
302 
303  // Loop over selection
304  for (std::vector<SelectionBase*>::iterator it = _eventSelections.begin(); it!=_eventSelections.end(); it++){
305  if ((*it)->IsEnabled()){
306  // Create and Fill the EventBox
307  (*it)->InitializeEvent(event);
308  }
309  }
310 }
void PrintStatistics()
Print #events passing cuts.
void SetTitle(const std::string &title)
Set the title of this selection, which is the "nice" version of the selection name,.
void DumpSelections()
Print out the index, name and title of each selection for a given branch (no argument for all branche...
bool GetForceBreak() const
void Enable()
enable and disable selection
const std::string & Name() const
Return the name of this selection.
void CreateToyBoxArray(Int_t nevents)
Create the array of PreviousToyBox for all enabled selections.
void SetPreSelectionAccumLevel(Int_t presel)
Set the pre-selection accum level.
UInt_t GetNMaxCuts() const
Return the number of steps in a given branch.
bool ApplySelection(const std::string &name, AnaEventC &event, bool &redo)
Apply the selection that was registered with the given name.
void SetValidRunPeriods(std::string runPeriods)
bool PreSelectionPassed(const AnaEventC &event)
Chek if preselection cuts have been passed;.
void SetName(const std::string &name)
Set the name of this selection, which is used internally by the SelectionManager. ...
void SetEnabledIndex(Int_t index)
Set the Selection index.
void ReadSelections(const std::string &file)
bool Apply(AnaEventC &event, bool &redo)
Apply all steps in the selection.
void SetForceFillEventSummary(bool force)
const std::string & Title() const
Return the title of this selection. A nice version of the name.
void AddSelection(const std::string &name, const std::string &title, SelectionBase *sel, Int_t presel=-1)
Add a user selection to the selection manager.
UInt_t GetNBranches() const
Return the number of branches.
void DisableSelection(const std::string &sel)
Disable a selection.
void EnableSelection(const std::string &sel)
Enable a selection.
void FinalizeEvent(const AnaEventC &event)
Delete the PreviousToyBox pointer for the last toy of the event for all enabled selections.
SelectionBase * GetSelection(const std::string &name, bool print_error=true)
Return the selection that was registered with the given name. NULL if it does not exist...
void SetValidRunPeriods(const std::string &ssel1, const std::string validRunPeriods)
Method to set the valid run periods for this selection (e.g. Anti-neutrino selections should only be ...
void Initialize()
Initialize this selection: defines the steps and the detectorFV.
void InitializeEvent(AnaEventC &event)
Initialize the EventBox for all enabled selections.
Int_t GetPreSelectionAccumLevel() const
Set the pre-selection accum level.