13 #include "TObjString.h"
31 if (!
file)
return nullptr;
32 if (!
file->IsOpen()) {
delete file;
return nullptr; }
46 if (!cont || !cont->
cellInfo())
return nullptr;
60 std::vector<const EventData*> eventDatas;
65 eventDatas.push_back(newEvtData);
76 cout <<
"Merging to " <<
fileName << endl;
78 unsigned int size = 0;
81 int evtIndex = 0, runIndex = 0;
82 std::map<std::pair<int, int>,
int> evtMap;
83 std::map<int, int> runMap;
85 cout <<
"Merging runs" << endl;
88 cout <<
"Cannot merge: one of the inputs is null!" << endl;
91 for (
unsigned int i = 0;
i <
accessor->nRuns();
i++) {
93 if (runMap.find(
run) != runMap.end())
continue;
94 runMap[
run] = runIndex;
102 cout <<
"Merging events" << endl;
103 unsigned int nEventsTotal = 0, iEvt = 0;
105 nEventsTotal +=
accessor->nEvents();
107 for (
unsigned int i = 0;
i <
accessor->nEvents();
i++) {
109 if (iEvt % 100000 == 0) cout <<
"Merging event " << iEvt <<
"/" << nEventsTotal << endl;
110 std::pair<int, int> evtId(
accessor->eventData(
i)->run(),
accessor->eventData(
i)->event());
111 if (evtMap.find(evtId) != evtMap.end())
continue;
112 evtMap[evtId] = evtIndex;
113 std::map<int, int>::const_iterator
idx = runMap.find(
accessor->eventData(
i)->run());
114 int newRunIndex = (
idx == runMap.end() ? -999 :
idx->second);
123 for (
unsigned int i = 0;
i < newAcc->
nChannels();
i++) {
124 if (
i % 10000 == 0) {
125 cout <<
"Merging channel " <<
i <<
"/" << newAcc->
nChannels() <<
" (current size = " <<
size <<
")" << endl;
131 if (!history || !history->
isValid())
continue;
136 for (
unsigned int j = 0; j < history->
nData(); j++) {
138 std::map<std::pair<int, int>,
int>::const_iterator newIndex
139 = evtMap.find(std::make_pair(history->
data(j)->
run(), history->
data(j)->
event()));
140 if (newIndex == evtMap.end()) cout <<
"Event not found for cell " <<
i <<
", data " << j <<
"." << endl;
141 newDC->
setEventIndex(newIndex != evtMap.end() ? newIndex->second : -1);
146 cout <<
"Shape not filled for hash = " <<
i <<
", index = " << j <<
", gain = " <<
Data::gainStr(history->
data(j)->
gain()) << endl;
156 cout <<
"Merging done, final size = " <<
size << endl;
168 std::vector<unsigned int> LBList;
169 std::ifstream
infile(LBFile.Data());
175 if(
list->GetEntries() == 0){
176 printf(
"No LB filtering specified, or bad format. Exiting.\n");
181 for(
int k = 0;
k <
list->GetEntries();
k++){
182 TObjString* tobs = (TObjString*)(
list->At(
k));
183 LBList.push_back((
unsigned int)(tobs->String()).Atoi());
186 printf(
"LB List: %d\n",(
int)LBList.size());
191 unsigned int size = 0;
194 int evtIndex = 0, runIndex = 0;
195 std::map<std::pair<int, int>,
int> evtMap;
196 std::map<int, int> runMap;
198 cout <<
"Merging runs" << endl;
201 cout <<
"Cannot merge: one of the inputs is null!" << endl;
204 for (
unsigned int i = 0;
i <
accessor->nRuns();
i++) {
206 if (runMap.find(
run) != runMap.end())
continue;
207 runMap[
run] = runIndex;
215 cout <<
"Merging events" << endl;
216 unsigned int nEventsTotal = 0, iEvt = 0;
218 nEventsTotal +=
accessor->nEvents();
220 for (
unsigned int i = 0;
i <
accessor->nEvents();
i++) {
222 if (iEvt % 100000 == 0) cout <<
"Merging event " << iEvt <<
"/" << nEventsTotal << endl;
227 for(
unsigned int ilb = 0 ;
ilb < LBList.size() ;
ilb++){
228 if(LBList.at(
ilb)==
accessor->eventData(
i)->lumiBlock()){
237 std::pair<int, int> evtId(
accessor->eventData(
i)->run(),
accessor->eventData(
i)->event());
238 if (evtMap.find(evtId) != evtMap.end())
continue;
239 evtMap[evtId] = evtIndex;
240 std::map<int, int>::const_iterator
idx = runMap.find(
accessor->eventData(
i)->run());
241 int newRunIndex = (
idx == runMap.end() ? -999 :
idx->second);
249 for (
unsigned int i = 0;
i < newAcc->
nChannels();
i++) {
250 if (
i % 10000 == 0) {
251 cout <<
"Merging channel " <<
i <<
"/" << newAcc->
nChannels() <<
" (current size = " <<
size <<
")" << endl;
257 if (!history || !history->
isValid())
continue;
262 for (
unsigned int j = 0; j < history->
nData(); j++) {
264 std::map<std::pair<int, int>,
int>::const_iterator newIndex
265 = evtMap.find(std::make_pair(history->
data(j)->
run(), history->
data(j)->
event()));
267 newDC->
setEventIndex(newIndex != evtMap.end() ? newIndex->second : -1);
272 cout <<
"Shape not filled for hash = " <<
i <<
", index = " << j <<
", gain = " <<
Data::gainStr(history->
data(j)->
gain()) << endl;
286 cout <<
"Merging done, final size = " <<
size << endl;
302 std::vector<TreeAccessor*>
307 if (filterList.
size() == 0) {
308 cout <<
"No filter categories specified, done! (?)" << endl;
309 return std::vector<TreeAccessor*>();
312 for (
unsigned int f = 0;
f < filterList.
size();
f++) {
313 cout <<
"Skimming to " << filterList.
fileName(
f) << endl;
314 if (!gSystem->AccessPathName(filterList.
fileName(
f))) {
315 cout <<
"File already exists, exiting." << endl;
316 return std::vector<TreeAccessor*>();
320 std::vector<TreeAccessor*> newAccessors;
321 for (
unsigned int f = 0;
f < filterList.
size();
f++)
323 std::map<std::pair<unsigned int, unsigned int>,
unsigned int> eventIndices;
324 std::vector< std::map<unsigned int, unsigned int> > eventsToKeep(filterList.
size());
325 std::vector< std::map<unsigned int, unsigned int> > runsToKeep(filterList.
size());
327 double nTot = 0, nPass = 0;
329 for (
unsigned int i = 0;
i <
accessor.nEvents();
i++) {
334 for (
unsigned int i = 0;
i <
accessor.nChannels();
i++) {
335 if (
i % 25000 == 0) {
336 cout <<
"Filtering " <<
i <<
"/" <<
accessor.nChannels()
337 <<
" (passing so far = " << nPass <<
", total seen = " << nTot <<
")" << endl;
342 const History* history =
nullptr;
343 for (
unsigned int f = 0;
f < filterList.
size();
f++) {
347 for (
unsigned int f = 0;
f < filterList.
size();
f++) {
356 for (
unsigned int k = 0;
k < history->
nData();
k++) {
359 std::map<std::pair<unsigned int, unsigned int>,
unsigned int>::const_iterator findIndex =
361 if (findIndex == eventIndices.end()) {
362 cout <<
"Inconsistent event numbering!!!" << endl;
364 return std::vector<TreeAccessor*>();
366 int oldEvtIndex = findIndex->second;
367 bool isNewEvt = (eventsToKeep[
f].find(oldEvtIndex) == eventsToKeep[
f].end());
368 unsigned int newEvtIndex = (isNewEvt ? eventsToKeep[
f].size() : eventsToKeep[
f][oldEvtIndex]);
369 if (isNewEvt) eventsToKeep[
f][oldEvtIndex] = newEvtIndex;
372 bool isNewRun = (runsToKeep[
f].find(oldRunIndex) == runsToKeep[
f].end());
373 unsigned int newRunIndex = (isNewRun ? runsToKeep[
f].size() : runsToKeep[
f][oldRunIndex]);
374 if (isNewRun) runsToKeep[
f][oldRunIndex] = newRunIndex;
378 cout <<
"Filtering failed on data " <<
k <<
" of cell " <<
i <<
", aborting" << endl;
380 for (
unsigned int f = 0;
f < filterList.
size();
f++)
delete newAccessors[
f];
381 return std::vector<TreeAccessor*>();
391 for (
unsigned int f = 0;
f < filterList.
size();
f++) {
392 cout <<
"Adding runs..." << endl;
393 std::vector<unsigned int> runsToKeep_ordered(runsToKeep[
f].
size());
394 for (
const auto& runIndex : runsToKeep[
f])
395 runsToKeep_ordered[runIndex.second] = runIndex.first;
397 for (
unsigned int runIndex : runsToKeep_ordered) {
399 newAccessors[
f]->addRun(newRun);
402 cout <<
"Adding events..." << endl;
403 std::vector<unsigned int> eventsToKeep_ordered(eventsToKeep[
f].
size());
407 for (
unsigned int eventIndex : eventsToKeep_ordered) {
408 std::map<unsigned int, unsigned int>::const_iterator
idx = runsToKeep[
f].find(
accessor.eventData(
eventIndex)->runIndex());
409 int newRunIndex = (
idx == runsToKeep[
f].end() ? 0 :
idx->second);
411 newAccessors[
f]->addEvent(newEvent);
415 cout <<
"Filtering done! final size = " << nPass << endl;
417 for (
unsigned int f = 0;
f < filterList.
size();
f++) {
418 cout <<
"Saving " << newAccessors[
f]->fileName() << endl;
419 newAccessors[
f]->save();
429 std::vector<short>
samples(5, 0);
430 std::vector<float> autoCorrs(4, 0);
433 newAccessor->
addRun(dummyRun);
440 for (
unsigned int i = 0;
i <
accessor.nChannels();
i++) {
442 cout <<
"Templating " <<
i <<
"/" <<
accessor.nChannels() << endl;
444 if (!history || !history->
cellInfo()) {
463 TFile* newFile =
new TFile(
fileName,
"RECREATE");
464 if (newFile && !newFile->IsOpen()) {
delete newFile; newFile =
nullptr; }
465 if (!newFile)
return false;