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 |
pam-fi |
1.6.2.1 |
SmartCollection::~SmartCollection() { |
13 |
|
|
if (_owns) { |
14 |
|
|
for (unsigned int i = 0; i < _actions.size(); i++) |
15 |
|
|
if (_actions[i] != NULL) { |
16 |
|
|
delete _actions[i]; |
17 |
|
|
_actions[i] = NULL; |
18 |
|
|
} |
19 |
|
|
} |
20 |
pam-fi |
1.6 |
} |
21 |
|
|
|
22 |
|
|
void SmartCollection::AddAction(CollectionAction *action) { |
23 |
|
|
_actions.push_back(action); |
24 |
pam-fi |
1.3 |
_actionsPositions.push_back(GetSize() - 1); |
25 |
pam-fi |
1.1 |
} |
26 |
|
|
|
27 |
|
|
CollectionAction *SmartCollection::GetAction(unsigned int iAction) { |
28 |
|
|
if (_actions.size() == 0) |
29 |
|
|
return NULL; |
30 |
|
|
if (iAction < 0 || iAction > _actions.size() - 1) |
31 |
|
|
return NULL; |
32 |
|
|
else |
33 |
|
|
return _actions[iAction]; |
34 |
|
|
} |
35 |
|
|
|
36 |
pam-fi |
1.5 |
CollectionAction *SmartCollection::GetAction(const char *actionName) { |
37 |
|
|
if (_actions.size() == 0) |
38 |
|
|
return NULL; |
39 |
pam-fi |
1.6 |
for (unsigned int i = 0; i < _actions.size(); i++) { |
40 |
pam-fi |
1.5 |
if (strcmp(_actions[i]->GetName(), actionName) == 0) |
41 |
|
|
return _actions[i]; |
42 |
|
|
} |
43 |
|
|
|
44 |
|
|
return NULL; |
45 |
|
|
} |
46 |
|
|
|
47 |
pam-fi |
1.2 |
void SmartCollection::Setup(PamLevel2 *events) { |
48 |
pam-fi |
1.1 |
PamCutCollection::Setup(events); |
49 |
pam-fi |
1.6 |
//cout << GetName() << endl; |
50 |
pam-fi |
1.1 |
|
51 |
pam-fi |
1.2 |
for (unsigned int i = 0; i < _actions.size(); i++) { |
52 |
pam-fi |
1.1 |
_actions[i]->Setup(events); |
53 |
|
|
} |
54 |
|
|
} |
55 |
|
|
|
56 |
pam-fi |
1.2 |
void SmartCollection::Finalize() { |
57 |
pam-fi |
1.1 |
PamCutCollection::Finalize(); |
58 |
|
|
|
59 |
pam-fi |
1.2 |
for (unsigned int i = 0; i < _actions.size(); i++) { |
60 |
pam-fi |
1.1 |
_actions[i]->Finalize(); |
61 |
|
|
} |
62 |
|
|
} |
63 |
|
|
|
64 |
pam-fi |
1.3 |
int SmartCollection::ApplyCut(PamLevel2 *event) { |
65 |
pam-fi |
1.1 |
|
66 |
pam-fi |
1.3 |
_nEv++; |
67 |
pam-fi |
1.6 |
|
68 |
|
|
// Execute the actions placed before the cuts |
69 |
|
|
unsigned int iBeforeCuts = 0; |
70 |
|
|
//cout << GetName() << endl; |
71 |
|
|
if (_actions.size() > 0) { |
72 |
|
|
while (_actionsPositions[iBeforeCuts] == -1) { |
73 |
|
|
_actions[iBeforeCuts]->OnGood(event); |
74 |
|
|
iBeforeCuts++; |
75 |
|
|
if (iBeforeCuts == _actions.size()) |
76 |
|
|
break; |
77 |
|
|
} |
78 |
|
|
} |
79 |
|
|
|
80 |
pam-fi |
1.3 |
// Apply the cuts |
81 |
|
|
if (_cuts.size() == 0) { |
82 |
|
|
_nGood++; |
83 |
|
|
OnGood(event); |
84 |
|
|
return CUTOK; |
85 |
pam-fi |
1.1 |
} |
86 |
|
|
|
87 |
pam-fi |
1.4 |
unsigned int firstFailed = _cuts.size(); |
88 |
pam-fi |
1.6.2.1 |
unsigned int iAction = iBeforeCuts; |
89 |
pam-fi |
1.6 |
for (unsigned int iCut = 0; iCut < _cuts.size(); iCut++) { |
90 |
|
|
if (_cuts[iCut]->ApplyCut(event) != CUTOK && firstFailed == _cuts.size()) { |
91 |
|
|
firstFailed = iCut; |
92 |
pam-fi |
1.6.2.1 |
// Apply the bad actions at the end of the bunch |
93 |
|
|
if (iAction < _actions.size()) { |
94 |
|
|
unsigned int lastPosition = _actionsPositions[iAction]; |
95 |
|
|
for (; iAction < _actions.size(); iAction++) { |
96 |
|
|
if (_actionsPositions[iAction] > (int) lastPosition) // Don't do actions at the end of successive bunches |
97 |
|
|
break; |
98 |
|
|
_actions[iAction]->OnBad(event, (int) firstFailed); |
99 |
|
|
|
100 |
|
|
} |
101 |
|
|
} |
102 |
pam-fi |
1.4 |
break; |
103 |
|
|
} |
104 |
pam-fi |
1.6.2.1 |
else if (iAction < _actions.size()) { |
105 |
|
|
// Do the good actions if we are at the end of the bunch |
106 |
|
|
while (_actionsPositions[iAction] == (int) iCut) { |
107 |
|
|
_actions[iAction]->OnGood(event); |
108 |
|
|
iAction++; |
109 |
|
|
if (iAction == _actions.size()) |
110 |
pam-fi |
1.4 |
break; |
111 |
pam-fi |
1.3 |
} |
112 |
|
|
} |
113 |
pam-fi |
1.1 |
} |
114 |
pam-fi |
1.3 |
|
115 |
pam-fi |
1.4 |
if (firstFailed == _cuts.size()) { |
116 |
|
|
_nGood++; |
117 |
|
|
OnGood(event); |
118 |
|
|
return CUTOK; |
119 |
|
|
} |
120 |
|
|
else { |
121 |
|
|
OnBad(event, firstFailed); |
122 |
|
|
return firstFailed; |
123 |
|
|
} |
124 |
pam-fi |
1.1 |
} |