-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathProcFileElements.C
239 lines (209 loc) · 6.84 KB
/
ProcFileElements.C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/// \file
/// \ingroup tutorial_ProcFileElements
///
/// Class to hold information about the processed elements of a file
///
/// \macro_code
///
/// \author Gerardo Ganis (gerardo.ganis@cern.ch)
#include "ProcFileElements.h"
#include "TCollection.h"
//_______________________________________________________________________
Int_t ProcFileElements::ProcFileElement::Compare(const TObject *o) const
{
// Compare this element with 'e'.
// Return
// -1 this should come first
// 0 same
// 1 e should come first
const ProcFileElements::ProcFileElement *e =
dynamic_cast<const ProcFileElements::ProcFileElement *>(o);
if (!e) return -1;
if (fFirst == e->fFirst) {
// They start at the same point
return 0;
} else if (fFirst < e->fFirst) {
// This starts first
return -1;
} else {
// e starts first
return 1;
}
}
//_______________________________________________________________________
Int_t ProcFileElements::ProcFileElement::Overlapping(ProcFileElement *e)
{
// Check overlapping status of this element with 'e'.
// Return
// -1 not overlapping
// 0 adjacent
// 1 overlapping
if (!e) return -1;
if (fFirst == 0 && fLast == -1) {
// We cover the full range, so we overlap
return 1;
}
if (fFirst < e->fFirst) {
if (fLast >= 0) {
if (fLast < e->fFirst - 1) {
// Disjoint
return -1;
} else {
// We somehow overlap
if (fLast == e->fFirst - 1) {
// Just adjacent
return 0;
} else {
// Real overlap
return 1;
}
}
} else {
// Always overlapping
return 1;
}
} else if (fFirst == e->fFirst) {
// Overlapping or adjacent
if (fFirst == fLast || e->fFirst == e->fLast) return 0;
return 1;
} else {
// The other way around
if (e->fLast >= 0) {
if (e->fLast < fFirst - 1) {
// Disjoint
return -1;
} else {
// We somehow overlap
if (e->fLast == fFirst - 1) {
// Just adjacent
return 0;
} else {
// Real overlap
return 1;
}
}
} else {
// Always overlapping
return 1;
}
}
// Should never be here
Warning("Overlapping", "should never be here!");
return -1;
}
//_______________________________________________________________________
Int_t ProcFileElements::ProcFileElement::MergeElement(ProcFileElement *e)
{
// Merge this element with element 'e'.
// Return -1 if it cannot be done, i.e. thei are disjoint; 0 otherwise
// Check if it can be done
if (Overlapping(e) < 0) return -1;
// Ok, we can merge: set the lower bound
if (e->fFirst < fFirst) fFirst = e->fFirst;
// Set the upper bound
if (fLast == -1 || e->fLast == -1) {
fLast = -1;
} else {
if (fLast < e->fLast) fLast = e->fLast;
}
// Done
return 0;
}
//_______________________________________________________________________
void ProcFileElements::ProcFileElement::Print(Option_t *) const
{
// Print range of this element
Printf("\tfirst: %lld\t last: %lld", fFirst, fLast);
}
//_______________________________________________________________________
Int_t ProcFileElements::Add(Long64_t fst, Long64_t lst)
{
// Add a new element to the list
// Return 1 if a new element has been added, 0 if it has been merged
// with an existing one, -1 in case of error
if (!fElements) fElements = new TSortedList;
if (!fElements) {
Error("Add", "could not create internal list!");
return -1;
}
// Create (temporary element)
ProcFileElements::ProcFileElement *ne =
new ProcFileElements::ProcFileElement(fst, lst);
// Check if if it is adjacent or overlapping with an existing one
TIter nxe(fElements);
ProcFileElements::ProcFileElement *e = nullptr;
while ((e = (ProcFileElements::ProcFileElement *)nxe())) {
if (e->MergeElement(ne) == 0) break;
}
Int_t rc = 0;
// Remove and re-add the merged element to sort correctly its possibly new position
if (e) {
fElements->Remove(e);
fElements->Add(e);
SafeDelete(ne);
} else {
// Add the new element
fElements->Add(ne);
rc = 1;
}
// Make sure that all what can be merged is merged (because of the order, some elements
// which could be merged are not merged, making the determination of fFirst and fLast below
// to give incorrect values)
ProcFileElements::ProcFileElement *ep = nullptr, *en = nullptr;
TObjLink *olp = fElements->FirstLink(), *oln = nullptr;
while (olp && (ep = (ProcFileElements::ProcFileElement *) olp->GetObject())) {
oln = olp->Next();
while (oln) {
if ((en = (ProcFileElements::ProcFileElement *) oln->GetObject())) {
if (ep->MergeElement(en) == 0) {
fElements->Remove(en);
delete en;
}
}
oln = oln->Next();
}
olp = olp->Next();
}
// New overall ranges
if ((e = (ProcFileElements::ProcFileElement *) fElements->First())) fFirst = e->fFirst;
if ((e = (ProcFileElements::ProcFileElement *) fElements->Last())) fLast = e->fLast;
// Done
return rc;
}
//_______________________________________________________________________
void ProcFileElements::Print(Option_t *) const
{
// Print info about this processed file
Printf("--- ProcFileElements ----------------------------------------");
Printf(" File: %s", fName.Data());
Printf(" # proc elements: %d", fElements ? fElements->GetSize() : 0);
TIter nxe(fElements);
ProcFileElements::ProcFileElement *e = nullptr;
while ((e = (ProcFileElements::ProcFileElement *)nxe())) { e->Print(); }
Printf(" Raw overall range: [%lld, %lld]", fFirst, fLast);
Printf("-------------------------------------------------------------");
}
//_______________________________________________________________________
Int_t ProcFileElements::Merge(TCollection *li)
{
// Merge this object with those in the list
// Return number of elements added
if (!li) return -1;
if (li->GetSize() <= 0) return 0;
Int_t nadd = 0;
TIter nxo(li);
ProcFileElements *pfe = nullptr;
while ((pfe = (ProcFileElements *) nxo())) {
if (strcmp(GetName(), pfe->GetName()))
Warning("Merge", "merging objects of different name! ('%s' != '%s')",
GetName(), pfe->GetName());
TIter nxe(pfe->GetListOfElements());
ProcFileElements::ProcFileElement *e = nullptr;
while ((e = (ProcFileElements::ProcFileElement *)nxe())) {
Int_t rc = Add(e->fFirst, e->fLast);
if (rc == 1) nadd++;
}
}
// Done
return nadd;
}