| 1 |
pam-fi |
1.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 |
pam-fi |
1.3 |
_actionsPositions.push_back(GetSize() - 1); |
| 15 |
pam-fi |
1.1 |
} |
| 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 |
pam-fi |
1.2 |
void SmartCollection::Setup(PamLevel2 *events) { |
| 27 |
pam-fi |
1.1 |
PamCutCollection::Setup(events); |
| 28 |
|
|
|
| 29 |
pam-fi |
1.2 |
for (unsigned int i = 0; i < _actions.size(); i++) { |
| 30 |
pam-fi |
1.1 |
_actions[i]->Setup(events); |
| 31 |
|
|
} |
| 32 |
|
|
} |
| 33 |
|
|
|
| 34 |
pam-fi |
1.2 |
void SmartCollection::Finalize() { |
| 35 |
pam-fi |
1.1 |
PamCutCollection::Finalize(); |
| 36 |
|
|
|
| 37 |
pam-fi |
1.2 |
for (unsigned int i = 0; i < _actions.size(); i++) { |
| 38 |
pam-fi |
1.1 |
_actions[i]->Finalize(); |
| 39 |
|
|
} |
| 40 |
|
|
} |
| 41 |
|
|
|
| 42 |
pam-fi |
1.3 |
int SmartCollection::ApplyCut(PamLevel2 *event) { |
| 43 |
pam-fi |
1.1 |
|
| 44 |
pam-fi |
1.3 |
_nEv++; |
| 45 |
|
|
// Apply the cuts |
| 46 |
|
|
if (_cuts.size() == 0) { |
| 47 |
|
|
_nGood++; |
| 48 |
|
|
OnGood(event); |
| 49 |
|
|
return CUTOK; |
| 50 |
pam-fi |
1.1 |
} |
| 51 |
|
|
|
| 52 |
pam-fi |
1.3 |
unsigned int bunchEnd = -1; |
| 53 |
|
|
unsigned int nActions = 0; |
| 54 |
|
|
unsigned int firstActionOfBunch = -1; |
| 55 |
|
|
if (_actions.size() > 0) { |
| 56 |
|
|
// Set the bunch end to the position of the first action |
| 57 |
|
|
bunchEnd = _actionsPositions[0]; |
| 58 |
|
|
// Set the position of the first action at the end of the bunch |
| 59 |
|
|
firstActionOfBunch = 0; |
| 60 |
|
|
// Set the number of actions at the end of this bunch of cuts |
| 61 |
|
|
nActions = 1; |
| 62 |
|
|
while (_actionsPositions[nActions] == bunchEnd) |
| 63 |
|
|
nActions++; |
| 64 |
|
|
} |
| 65 |
pam-fi |
1.1 |
|
| 66 |
pam-fi |
1.3 |
for (unsigned int icut = 0; icut < _cuts.size(); icut++) { |
| 67 |
|
|
if (_cuts[icut]->ApplyCut(event) != CUTOK) { |
| 68 |
|
|
// Check if there are actions at the end of the current bunch |
| 69 |
|
|
// (last bunch may have no actions at its end) |
| 70 |
|
|
if (bunchEnd >= icut) { |
| 71 |
|
|
for (unsigned int i = 0; i < nActions; i++) { |
| 72 |
|
|
_actions[firstActionOfBunch + i]->OnBad(event, icut); |
| 73 |
|
|
} |
| 74 |
|
|
} |
| 75 |
|
|
OnBad(event, icut); |
| 76 |
|
|
return icut; |
| 77 |
|
|
} |
| 78 |
|
|
else if (bunchEnd == icut) { |
| 79 |
|
|
// We are at the end of the bunch of cuts, so let's execute the actions |
| 80 |
|
|
for (unsigned int i = 0; i < nActions; i++) { |
| 81 |
|
|
_actions[firstActionOfBunch + i]->OnGood(event); |
| 82 |
|
|
} |
| 83 |
|
|
if (firstActionOfBunch + nActions < _actions.size()) { |
| 84 |
|
|
// Current bunch is done, but actions are not finished yet. Set up the next bunch. |
| 85 |
|
|
// Set the end of the new bunch and the number of actions at this end |
| 86 |
|
|
firstActionOfBunch += nActions; |
| 87 |
|
|
bunchEnd = _actionsPositions[firstActionOfBunch]; |
| 88 |
|
|
nActions = 1; |
| 89 |
|
|
while (_actionsPositions[firstActionOfBunch + nActions] == bunchEnd) |
| 90 |
|
|
nActions++; |
| 91 |
|
|
} |
| 92 |
|
|
} |
| 93 |
pam-fi |
1.1 |
} |
| 94 |
pam-fi |
1.3 |
|
| 95 |
|
|
_nGood++; |
| 96 |
|
|
OnGood(event); |
| 97 |
|
|
return CUTOK; |
| 98 |
pam-fi |
1.1 |
} |