| 1 |
/* |
| 2 |
* SmartCollection.cpp |
| 3 |
* |
| 4 |
* Created on: 14-mag-2009 |
| 5 |
* Author: Nicola Mori |
| 6 |
*/ |
| 7 |
|
| 8 |
/*! @file SmartCollection.cpp The SmartCollection class implementation file */ |
| 9 |
|
| 10 |
#include "SmartCollection.h" |
| 11 |
|
| 12 |
void SmartCollection::AddAction(CollectionAction& action) { |
| 13 |
_actions.push_back(&action); |
| 14 |
_actionsPositions.push_back(GetSize() - 1); |
| 15 |
} |
| 16 |
|
| 17 |
CollectionAction *SmartCollection::GetAction(unsigned int iAction) { |
| 18 |
if (_actions.size() == 0) |
| 19 |
return NULL; |
| 20 |
if (iAction < 0 || iAction > _actions.size() - 1) |
| 21 |
return NULL; |
| 22 |
else |
| 23 |
return _actions[iAction]; |
| 24 |
} |
| 25 |
|
| 26 |
void SmartCollection::Setup(PamLevel2 *events) { |
| 27 |
PamCutCollection::Setup(events); |
| 28 |
|
| 29 |
for (unsigned int i = 0; i < _actions.size(); i++) { |
| 30 |
_actions[i]->Setup(events); |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
void SmartCollection::Finalize() { |
| 35 |
PamCutCollection::Finalize(); |
| 36 |
|
| 37 |
for (unsigned int i = 0; i < _actions.size(); i++) { |
| 38 |
_actions[i]->Finalize(); |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
int SmartCollection::ApplyCut(PamLevel2 *event) { |
| 43 |
|
| 44 |
_nEv++; |
| 45 |
// Apply the cuts |
| 46 |
if (_cuts.size() == 0) { |
| 47 |
_nGood++; |
| 48 |
OnGood(event); |
| 49 |
return CUTOK; |
| 50 |
} |
| 51 |
|
| 52 |
unsigned int firstFailed = _cuts.size(); |
| 53 |
for (unsigned int icut = 0; icut < _cuts.size(); icut++) { |
| 54 |
if (_cuts[icut]->ApplyCut(event) != CUTOK && firstFailed == _cuts.size()) { |
| 55 |
firstFailed = icut; |
| 56 |
break; |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
//Do actions |
| 61 |
if (_actions.size() > 0) { |
| 62 |
unsigned int lastPosition = _cuts.size(); |
| 63 |
for (unsigned int i = 0; i < _actions.size(); i++) { |
| 64 |
if (_actionsPositions[i] < firstFailed) |
| 65 |
_actions[i]->OnGood(event); |
| 66 |
else { // A cut has failed |
| 67 |
if (lastPosition == _cuts.size()) // Record the position of the end of the bunch |
| 68 |
lastPosition = _actionsPositions[i]; |
| 69 |
if (_actionsPositions[i] > lastPosition) // Don't do actions at the end of successive bunches |
| 70 |
break; |
| 71 |
_actions[i]->OnBad(event, firstFailed); |
| 72 |
} |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
if (firstFailed == _cuts.size()) { |
| 77 |
_nGood++; |
| 78 |
OnGood(event); |
| 79 |
return CUTOK; |
| 80 |
} |
| 81 |
else { |
| 82 |
OnBad(event, firstFailed); |
| 83 |
return firstFailed; |
| 84 |
} |
| 85 |
} |