/
FWFileEntry.cc
469 lines (398 loc) · 15.1 KB
/
FWFileEntry.cc
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
#include <boost/regex.hpp>
#include "TFile.h"
#include "TTreeCache.h"
#include "TEveTreeTools.h"
#include "TError.h"
#include "TMath.h"
#include "DataFormats/FWLite/interface/Handle.h"
#include "DataFormats/Common/interface/TriggerResults.h"
#include "DataFormats/Provenance/interface/ProcessConfiguration.h"
#include "DataFormats/Provenance/interface/ProcessHistory.h"
#include "DataFormats/Provenance/interface/ReleaseVersion.h"
#include "FWCore/Utilities/interface/WrappedClassName.h"
#include "FWCore/Common/interface/TriggerNames.h"
#define private public
#include "Fireworks/Core/interface/FWEventItem.h"
#undef private
#include "Fireworks/Core/interface/FWFileEntry.h"
#include "Fireworks/Core/interface/FWEventItemsManager.h"
#include "Fireworks/Core/interface/fwLog.h"
#include "Fireworks/Core/interface/fwPaths.h"
FWFileEntry::FWFileEntry(const std::string& name, bool checkVersion) :
m_name(name), m_file(0), m_eventTree(0), m_event(0),
m_needUpdate(true), m_globalEventList(0)
{
openFile(checkVersion);
}
FWFileEntry::~FWFileEntry()
{
for(std::list<Filter*>::iterator i = m_filterEntries.begin(); i != m_filterEntries.end(); ++i)
delete (*i)->m_eventList;
delete m_globalEventList;
}
void FWFileEntry::openFile(bool checkVersion)
{
gErrorIgnoreLevel = 3000; // suppress warnings about missing dictionaries
TFile *newFile = TFile::Open(m_name.c_str());
if (newFile == 0 || newFile->IsZombie() || !newFile->Get("Events")) {
// std::cout << "Invalid file. Ignored." << std::endl;
// return false;
throw std::runtime_error("Invalid file. Ignored.");
}
gErrorIgnoreLevel = -1;
m_file = newFile;
// check CMSSW relese version for compatibility
if (checkVersion) {
typedef std::vector<edm::ProcessHistory> provList;
TTree *metaData = dynamic_cast<TTree*>(m_file->Get("MetaData"));
TBranch *b = metaData->GetBranch("ProcessHistory");
provList *x = 0;
b->SetAddress(&x);
b->GetEntry(0);
const edm::ProcessConfiguration* dd = 0;
int latestVersion =0;
int currentVersionArr[] = {0, 0, 0};
for (auto const& processHistory : *x)
{
for (auto const& processConfiguration : processHistory)
{
// std::cout << processConfiguration.releaseVersion() << " " << processConfiguration.processName() << std::endl;
TString dcv = processConfiguration.releaseVersion();
fireworks::getDecomposedVersion(dcv, currentVersionArr);
int nvv = currentVersionArr[0]*100 + currentVersionArr[1]*10 + currentVersionArr[2];
if (nvv > latestVersion) {
latestVersion = nvv;
dd = &processConfiguration;
}
}
}
if (latestVersion) {
fwLog(fwlog::kInfo) << "Checking process history. " << m_name.c_str() << " latest process \"" << dd->processName() << "\", version " << dd->releaseVersion() << std::endl;
b->SetAddress(0);
TString v = dd->releaseVersion();
if (!fireworks::acceptDataFormatsVersion(v))
{
int* di = (fireworks::supportedDataFormatsVersion());
TString msg = Form("incompatible data: Process version does not mactch major data formats version. File produced with %s. Data formats version \"CMSSW_%d_%d_%d\".\n",
dd->releaseVersion().c_str(), di[0], di[1], di[2]);
msg += "Use --no-version-check option if you still want to view the file.\n";
throw std::runtime_error(msg.Data());
}
}
else {
TString msg = "No process history available\n";
msg += "Use --no-version-check option if you still want to view the file.\n";
throw std::runtime_error(msg.Data());
}
}
// load event
m_event = new fwlite::Event(m_file);
if (m_event->size() == 0)
throw std::runtime_error("fwlite::Event size == 0");
m_eventTree = dynamic_cast<TTree*>(m_file->Get("Events"));
if (m_eventTree == 0)
{
throw std::runtime_error("Cannot find TTree 'Events' in the data file");
}
// This now set in DataHelper
//TTreeCache::SetLearnEntries(2);
//m_eventTree->SetCacheSize(10*1024*1024);
//TTreeCache *tc = (TTreeCache*) m_file->GetCacheRead();
//tc->AddBranch(m_event->auxBranch_,kTRUE);
//tc->StartLearningPhase();
}
void FWFileEntry::closeFile()
{
if (m_file) {
printf("Reading %lld bytes in %d transactions.\n",
m_file->GetBytesRead(), m_file->GetReadCalls());
m_file->Close();
delete m_file;
}
if (m_event) delete m_event;
}
//______________________________________________________________________________
bool FWFileEntry::isEventSelected(int tree_entry)
{
int idx = m_globalEventList->GetIndex(tree_entry);
return idx >= 0;
}
bool FWFileEntry::hasSelectedEvents()
{
return m_globalEventList->GetN() > 0;
}
int FWFileEntry::firstSelectedEvent()
{
if (m_globalEventList->GetN() > 0)
{
return m_globalEventList->GetEntry(0);
}
else
{
return -1;
}
}
int FWFileEntry::lastSelectedEvent()
{
if (m_globalEventList->GetN() > 0)
return m_globalEventList->GetEntry(m_globalEventList->GetN() - 1);
else
return -1;
}
int FWFileEntry::nextSelectedEvent(int tree_entry)
{
// Find next selected event after the current one.
// This returns the index in the selected event list.
// If none exists -1 is returned.
const Long64_t *list = m_globalEventList->GetList();
Long64_t val = tree_entry;
Long64_t idx = TMath::BinarySearch(m_globalEventList->GetN(), list, val);
++idx;
if (idx >= m_globalEventList->GetN() || idx < 0)
return -1;
return list[idx];
}
int FWFileEntry::previousSelectedEvent(int tree_entry)
{
// Find first selected event before current one.
// This returns the index in the selected event list.
// If none exists -1 is returned.
const Long64_t *list = m_globalEventList->GetList();
Long64_t val = tree_entry;
Long64_t idx = TMath::BinarySearch(m_globalEventList->GetN(), list, val);
if (list[idx] == val)
--idx;
if (idx >= 0)
return list[idx];
else
return -1;
}
//______________________________________________________________________________
bool FWFileEntry::hasActiveFilters()
{
for (std::list<Filter*>::iterator it = m_filterEntries.begin(); it != m_filterEntries.end(); ++it)
{
if ((*it)->m_selector->m_enabled)
return true;
}
return false;
}
//______________________________________________________________________________
void FWFileEntry::updateFilters(const FWEventItemsManager* eiMng, bool globalOR)
{
if (!m_needUpdate)
return;
if (m_globalEventList)
m_globalEventList->Reset();
else
m_globalEventList = new FWTEventList;
for (std::list<Filter*>::iterator it = m_filterEntries.begin(); it != m_filterEntries.end(); ++it)
{
if ((*it)->m_selector->m_enabled && (*it)->m_needsUpdate)
{
runFilter(*it, eiMng);
}
// Need to re-check if enabled after filtering as it can be set to false
// in runFilter().
if ((*it)->m_selector->m_enabled)
{
if ((*it)->hasSelectedEvents())
{
if (globalOR || m_globalEventList->GetN() == 0)
{
m_globalEventList->Add((*it)->m_eventList);
}
else
{
m_globalEventList->Intersect((*it)->m_eventList);
}
}
else if (!globalOR)
{
m_globalEventList->Reset();
break;
}
}
}
fwLog(fwlog::kDebug) << "FWFileEntry::updateFilters in [" << m_file->GetName() << "] global selection [" << m_globalEventList->GetN() << "/" << m_eventTree->GetEntries() << "]" << std::endl;
m_needUpdate = false;
}
//_____________________________________________________________________________
void FWFileEntry::runFilter(Filter* filter, const FWEventItemsManager* eiMng)
{
if (!filter->m_selector->m_triggerProcess.empty())
{
filterEventsWithCustomParser(filter);
return;
}
// parse selection for known Fireworks expressions
std::string interpretedSelection = filter->m_selector->m_expression;
for (FWEventItemsManager::const_iterator i = eiMng->begin(),
end = eiMng->end(); i != end; ++i)
{
FWEventItem *item = *i;
if (item == 0)
continue;
// FIXME: hack to get full branch name filled
if (item->m_event == 0)
{
item->m_event = m_event;
item->getPrimaryData();
item->m_event = 0;
}
boost::regex re(std::string("\\$") + (*i)->name());
if (boost::regex_search(interpretedSelection, re))
{
const edm::TypeWithDict elementType(const_cast<TClass*>(item->type()));
const edm::TypeWithDict wrapperType = edm::TypeWithDict::byName(edm::wrappedClassName(elementType.name()));
std::string fullBranchName = m_event->getBranchNameFor(wrapperType.typeInfo(),
item->moduleLabel().c_str(),
item->productInstanceLabel().c_str(),
item->processName().c_str());
interpretedSelection = boost::regex_replace(interpretedSelection, re,
fullBranchName + ".obj");
// printf("selection after applying s/%s/%s/: %s\n",
// (std::string("\\$") + (*i)->name()).c_str(),
// ((*i)->m_fullBranchName + ".obj").c_str(),
// interpretedSelection.c_str());
}
}
m_file->cd();
m_eventTree->SetEventList(0);
// Since ROOT will leave any TBranches used in the filtering at the last event,
// we need to be able to reset them to what fwlite::Event expects them to be
// we do this by holding onto the old buffers and create temporary new ones.
std::map<TBranch*, void*> prevAddrs;
{
TObjArray* branches = m_eventTree->GetListOfBranches();
std::auto_ptr<TIterator> pIt( branches->MakeIterator());
while (TObject* branchObj = pIt->Next())
{
TBranch* b = dynamic_cast<TBranch*> (branchObj);
if (0!=b)
{
const char * name = b->GetName();
unsigned int length = strlen(name);
if (length > 1 && name[length-1] != '.')
{
// This is not a data branch so we should ignore it.
continue;
}
if (0 != b->GetAddress())
{
if (prevAddrs.find(b) != prevAddrs.end())
{
fwLog(fwlog::kWarning) << "FWFileEntry::runFilter branch is already in the map!\n";
}
prevAddrs.insert(std::make_pair(b, b->GetAddress()));
// std::cout <<"Zeroing branch: "<< b->GetName() <<" "<< (void*) b->GetAddress() <<std::endl;
b->SetAddress(0);
}
}
}
}
if (filter->m_eventList)
filter->m_eventList->Reset();
else
filter->m_eventList = new FWTEventList;
TEveSelectorToEventList stoelist(filter->m_eventList, interpretedSelection.c_str());
Long64_t result = m_eventTree->Process(&stoelist);
if (result < 0)
fwLog(fwlog::kWarning) << "FWFileEntry::runFilter in file [" << m_file->GetName() << "] filter [" << filter->m_selector->m_expression << "] is invalid." << std::endl;
else
fwLog(fwlog::kDebug) << "FWFileEntry::runFilter is file [" << m_file->GetName() << "], filter [" << filter->m_selector->m_expression << "] has [" << filter->m_eventList->GetN() << "] events selected" << std::endl;
// Set back the old branch buffers.
{
for (auto i : prevAddrs)
{
// std::cout <<"Resetting branch: "<< i.first->GetName() <<" "<< i.second <<std::endl;
i.first->SetAddress(i.second);
}
}
filter->m_needsUpdate = false;
}
//______________________________________________________________________________
bool
FWFileEntry::filterEventsWithCustomParser(Filter* filterEntry)
{
std::string selection(filterEntry->m_selector->m_expression);
boost::regex re_spaces("\\s+");
selection = boost::regex_replace(selection,re_spaces,"");
if (selection.find("&&") != std::string::npos &&
selection.find("||") != std::string::npos )
{
// Combination of && and || operators not supported.
return false;
}
fwlite::Handle<edm::TriggerResults> hTriggerResults;
edm::TriggerNames const* triggerNames(0);
try
{
hTriggerResults.getByLabel(*m_event,"TriggerResults","", filterEntry->m_selector->m_triggerProcess.c_str());
triggerNames = &(m_event->triggerNames(*hTriggerResults));
}
catch(...)
{
fwLog(fwlog::kWarning) << " failed to get trigger results with process name "<< filterEntry->m_selector->m_triggerProcess << std::endl;
return false;
}
// std::cout << "Number of trigger names: " << triggerNames->size() << std::endl;
// for (unsigned int i=0; i<triggerNames->size(); ++i)
// std::cout << " " << triggerNames->triggerName(i);
//std::cout << std::endl;
bool junction_mode = true; // AND
if (selection.find("||")!=std::string::npos)
junction_mode = false; // OR
boost::regex re("\\&\\&|\\|\\|");
boost::sregex_token_iterator i(selection.begin(), selection.end(), re, -1);
boost::sregex_token_iterator j;
// filters and how they enter in the logical expression
std::vector<std::pair<unsigned int,bool> > filters;
while (i != j)
{
std::string filter = *i++;
bool flag = true;
if (filter[0] == '!')
{
flag = false;
filter.erase(filter.begin());
}
unsigned int index = triggerNames->triggerIndex(filter);
if (index == triggerNames->size())
{
// Trigger name not found.
return false;
}
filters.push_back(std::make_pair(index, flag));
}
if (filters.empty())
return false;
if (filterEntry->m_eventList)
filterEntry->m_eventList->Reset();
else
filterEntry->m_eventList = new FWTEventList();
FWTEventList* list = filterEntry->m_eventList;
// loop over events
edm::EventID currentEvent = m_event->id();
unsigned int iEvent = 0;
for (m_event->toBegin(); !m_event->atEnd(); ++(*m_event))
{
hTriggerResults.getByLabel(*m_event,"TriggerResults","", filterEntry->m_selector->m_triggerProcess.c_str());
std::vector<std::pair<unsigned int,bool> >::const_iterator filter = filters.begin();
bool passed = hTriggerResults->accept(filter->first) == filter->second;
while (++filter != filters.end())
{
if (junction_mode)
passed &= hTriggerResults->accept(filter->first) == filter->second;
else
passed |= hTriggerResults->accept(filter->first) == filter->second;
}
if (passed)
list->Enter(iEvent);
++iEvent;
}
m_event->to(currentEvent);
filterEntry->m_needsUpdate = false;
fwLog(fwlog::kDebug) << "FWFile::filterEventsWithCustomParser file [" << m_file->GetName() << "], filter [" << filterEntry->m_selector->m_expression << "], selected [" << list->GetN() << "]" << std::endl;
return true;
}