365{
366 if (filterList.
size() == 0) {
367 cout << "No filter categories specified, done! (?)" << endl;
368 return std::vector<TreeAccessor*>();
369 }
370
371 for (
unsigned int f = 0;
f < filterList.
size();
f++) {
372 cout <<
"Skimming to " << filterList.
fileName(f) << endl;
373 if (!gSystem->AccessPathName(filterList.
fileName(f))) {
374 cout << "File already exists, exiting." << endl;
375 return std::vector<TreeAccessor*>();
376 }
377 }
378
379 std::vector<TreeAccessor*> newAccessors;
380 for (
unsigned int f = 0;
f < filterList.
size();
f++)
382 std::map<std::pair<unsigned int, unsigned int>, unsigned int> eventIndices;
383 std::vector< std::map<unsigned int, unsigned int> > eventsToKeep(filterList.
size());
384 std::vector< std::map<unsigned int, unsigned int> > runsToKeep(filterList.
size());
385
386 double nTot = 0, nPass = 0;
387
388 for (
unsigned int i = 0;
i <
accessor.nEvents();
i++) {
390 eventIndices[std::pair<unsigned int, unsigned int>(
eventData->run(),
eventData->event())] =
i;
391 }
392
393 for (
unsigned int i = 0;
i <
accessor.nChannels();
i++) {
394 if (i % 25000 == 0) {
395 cout <<
"Filtering " <<
i <<
"/" <<
accessor.nChannels()
396 << " (passing so far = " << nPass << ", total seen = " << nTot << ")" << endl;
397
398 }
400
401 const History* history = nullptr;
402 for (
unsigned int f = 0;
f < filterList.
size();
f++) {
404 if (history) break;
405 }
406 for (
unsigned int f = 0;
f < filterList.
size();
f++) {
408 HistoryContainer*
newHist =
new HistoryContainer();
409 newAccessors[
f]->add(newHist);
411 continue;
412 }
413 if (first) { nTot += history->
nData();
first =
false; }
414 HistoryContainer*
newHist =
new HistoryContainer(
new CellInfo(*history->
cellInfo()));
415 for (
unsigned int k = 0;
k < history->
nData();
k++) {
418 std::map<std::pair<unsigned int, unsigned int>, unsigned int>::const_iterator findIndex =
419 eventIndices.find(std::pair<unsigned int, unsigned int>(
eventData->run(),
eventData->event()));
420 if (findIndex == eventIndices.end()) {
421 cout << "Inconsistent event numbering!!!" << endl;
423 return std::vector<TreeAccessor*>();
424 }
425 int oldEvtIndex = findIndex->second;
426 bool isNewEvt = (eventsToKeep[
f].find(oldEvtIndex) == eventsToKeep[f].
end());
427 unsigned int newEvtIndex = (isNewEvt ? eventsToKeep[
f].size() : eventsToKeep[f][oldEvtIndex]);
428 if (isNewEvt) eventsToKeep[
f][oldEvtIndex] = newEvtIndex;
429
431 bool isNewRun = (runsToKeep[
f].find(oldRunIndex) == runsToKeep[f].
end());
432 unsigned int newRunIndex = (isNewRun ? runsToKeep[
f].size() : runsToKeep[f][oldRunIndex]);
433 if (isNewRun) runsToKeep[
f][oldRunIndex] = newRunIndex;
434
435 Data* newData = tweaker.
tweak(*history->
data(k), newEvtIndex);
436 if (!newData) {
437 cout <<
"Filtering failed on data " <<
k <<
" of cell " <<
i <<
", aborting" << endl;
439 for (
unsigned int f = 0;
f < filterList.
size();
f++)
delete newAccessors[f];
440 return std::vector<TreeAccessor*>();
441 }
442 nPass++;
444 }
445 newAccessors[
f]->add(newHist);
447 }
448 }
449
450 for (
unsigned int f = 0;
f < filterList.
size();
f++) {
451 cout << "Adding runs..." << endl;
452 std::vector<unsigned int> runsToKeep_ordered(runsToKeep[f].
size());
453 for (const auto& runIndex : runsToKeep[f])
454 runsToKeep_ordered[runIndex.second] = runIndex.first;
455
456 for (unsigned int runIndex : runsToKeep_ordered) {
457 RunData* newRun =
new RunData(*
accessor.runData(runIndex));
458 newAccessors[
f]->addRun(newRun);
459 delete newRun;
460 }
461 cout << "Adding events..." << endl;
462 std::vector<unsigned int> eventsToKeep_ordered(eventsToKeep[f].
size());
463 for (const auto& eventIndex : eventsToKeep[f])
465
466 for (unsigned int eventIndex : eventsToKeep_ordered) {
467 std::map<unsigned int, unsigned int>::const_iterator
idx = runsToKeep[
f].find(
accessor.eventData(eventIndex)->runIndex());
468 int newRunIndex = (
idx == runsToKeep[
f].end() ? 0 :
idx->second);
470 newAccessors[
f]->addEvent(newEvent);
471 delete newEvent;
472 }
473 }
474 cout << "Filtering done! final size = " << nPass << endl;
475
476 for (
unsigned int f = 0;
f < filterList.
size();
f++) {
477 cout <<
"Saving " << newAccessors[
f]->fileName() << endl;
478 newAccessors[
f]->save();
479 }
480 return newAccessors;
481}
Data * tweak(const Data &data, int evtIndex=-1) const
const EventData * eventData() const
const DataContainer * dissolve()
unsigned int size() const
const FilterParams & filterParams(unsigned int i) const
const TString & fileName(unsigned int i) const
bool passCell(const CellInfo &info) const
bool passEvent(const Data &data) const
const CellInfo * cellInfo() const
const Data * data(unsigned int i) const
unsigned int nData() const
const EventData * eventData(unsigned int i) const
TreeAccessor(TTree &cellTree, TTree &scTree, TTree &eventTree, TTree *runTree, TFile *file)
Constructor.
thread_local event_number_t eventIndex
const AccessorWrapper< T > * accessor(xAOD::JetAttribute::AttributeID id)
Returns an attribute accessor corresponding to an AttributeID.