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;
92 for (
unsigned int i = 0;
i <
accessor->nRuns();
i++) {
94 if (runMap.find(
run) != runMap.end())
continue;
95 runMap[
run] = runIndex;
103 cout <<
"Merging events" << endl;
104 unsigned int nEventsTotal = 0, iEvt = 0;
106 nEventsTotal +=
accessor->nEvents();
108 for (
unsigned int i = 0;
i <
accessor->nEvents();
i++) {
110 if (iEvt % 100000 == 0) cout <<
"Merging event " << iEvt <<
"/" << nEventsTotal << endl;
111 std::pair<int, int> evtId(
accessor->eventData(
i)->run(),
accessor->eventData(
i)->event());
112 if (evtMap.find(evtId) != evtMap.end())
continue;
113 evtMap[evtId] = evtIndex;
114 std::map<int, int>::const_iterator
idx = runMap.find(
accessor->eventData(
i)->run());
115 int newRunIndex = (
idx == runMap.end() ? -999 :
idx->second);
124 for (
unsigned int i = 0;
i < newAcc->
nChannels();
i++) {
125 if (
i % 10000 == 0) {
126 cout <<
"Merging channel " <<
i <<
"/" << newAcc->
nChannels() <<
" (current size = " <<
size <<
")" << endl;
132 if (!history || !history->
isValid())
continue;
137 for (
unsigned int j = 0; j < history->
nData(); j++) {
139 std::map<std::pair<int, int>,
int>::const_iterator newIndex
140 = evtMap.find(std::make_pair(history->
data(j)->
run(), history->
data(j)->
event()));
141 if (newIndex == evtMap.end()) cout <<
"Event not found for cell " <<
i <<
", data " << j <<
"." << endl;
142 newDC->
setEventIndex(newIndex != evtMap.end() ? newIndex->second : -1);
147 cout <<
"Shape not filled for hash = " <<
i <<
", index = " << j <<
", gain = " <<
Data::gainStr(history->
data(j)->
gain()) << endl;
157 cout <<
"Merging done, final size = " <<
size << endl;
169 std::vector<unsigned int> LBList;
170 std::ifstream
infile(LBFile.Data());
176 if(
list->GetEntries() == 0){
177 printf(
"No LB filtering specified, or bad format. Exiting.\n");
182 for(
int k = 0;
k <
list->GetEntries();
k++){
183 TObjString* tobs = (TObjString*)(
list->At(
k));
184 LBList.push_back((
unsigned int)(tobs->String()).Atoi());
187 printf(
"LB List: %d\n",(
int)LBList.size());
192 unsigned int size = 0;
195 int evtIndex = 0, runIndex = 0;
196 std::map<std::pair<int, int>,
int> evtMap;
197 std::map<int, int> runMap;
199 cout <<
"Merging runs" << endl;
202 cout <<
"Cannot merge: one of the inputs is null!" << endl;
206 for (
unsigned int i = 0;
i <
accessor->nRuns();
i++) {
208 if (runMap.find(
run) != runMap.end())
continue;
209 runMap[
run] = runIndex;
217 cout <<
"Merging events" << endl;
218 unsigned int nEventsTotal = 0, iEvt = 0;
220 nEventsTotal +=
accessor->nEvents();
222 for (
unsigned int i = 0;
i <
accessor->nEvents();
i++) {
224 if (iEvt % 100000 == 0) cout <<
"Merging event " << iEvt <<
"/" << nEventsTotal << endl;
229 for(
unsigned int ilb = 0 ;
ilb < LBList.size() ;
ilb++){
230 if(LBList.at(
ilb)==
accessor->eventData(
i)->lumiBlock()){
239 std::pair<int, int> evtId(
accessor->eventData(
i)->run(),
accessor->eventData(
i)->event());
240 if (evtMap.find(evtId) != evtMap.end())
continue;
241 evtMap[evtId] = evtIndex;
242 std::map<int, int>::const_iterator
idx = runMap.find(
accessor->eventData(
i)->run());
243 int newRunIndex = (
idx == runMap.end() ? -999 :
idx->second);
251 for (
unsigned int i = 0;
i < newAcc->
nChannels();
i++) {
252 if (
i % 10000 == 0) {
253 cout <<
"Merging channel " <<
i <<
"/" << newAcc->
nChannels() <<
" (current size = " <<
size <<
")" << endl;
259 if (!history || !history->
isValid())
continue;
264 for (
unsigned int j = 0; j < history->
nData(); j++) {
266 std::map<std::pair<int, int>,
int>::const_iterator newIndex
267 = evtMap.find(std::make_pair(history->
data(j)->
run(), history->
data(j)->
event()));
269 newDC->
setEventIndex(newIndex != evtMap.end() ? newIndex->second : -1);
274 cout <<
"Shape not filled for hash = " <<
i <<
", index = " << j <<
", gain = " <<
Data::gainStr(history->
data(j)->
gain()) << endl;
288 cout <<
"Merging done, final size = " <<
size << endl;
304 std::vector<TreeAccessor*>
309 if (filterList.
size() == 0) {
310 cout <<
"No filter categories specified, done! (?)" << endl;
311 return std::vector<TreeAccessor*>();
314 for (
unsigned int f = 0;
f < filterList.
size();
f++) {
315 cout <<
"Skimming to " << filterList.
fileName(
f) << endl;
316 if (!gSystem->AccessPathName(filterList.
fileName(
f))) {
317 cout <<
"File already exists, exiting." << endl;
318 return std::vector<TreeAccessor*>();
322 std::vector<TreeAccessor*> newAccessors;
323 for (
unsigned int f = 0;
f < filterList.
size();
f++)
325 std::map<std::pair<unsigned int, unsigned int>,
unsigned int> eventIndices;
326 std::vector< std::map<unsigned int, unsigned int> > eventsToKeep(filterList.
size());
327 std::vector< std::map<unsigned int, unsigned int> > runsToKeep(filterList.
size());
329 double nTot = 0, nPass = 0;
331 for (
unsigned int i = 0;
i <
accessor.nEvents();
i++) {
336 for (
unsigned int i = 0;
i <
accessor.nChannels();
i++) {
337 if (
i % 25000 == 0) {
338 cout <<
"Filtering " <<
i <<
"/" <<
accessor.nChannels()
339 <<
" (passing so far = " << nPass <<
", total seen = " << nTot <<
")" << endl;
344 const History* history =
nullptr;
345 for (
unsigned int f = 0;
f < filterList.
size();
f++) {
349 for (
unsigned int f = 0;
f < filterList.
size();
f++) {
358 for (
unsigned int k = 0;
k < history->
nData();
k++) {
361 std::map<std::pair<unsigned int, unsigned int>,
unsigned int>::const_iterator findIndex =
363 if (findIndex == eventIndices.end()) {
364 cout <<
"Inconsistent event numbering!!!" << endl;
366 return std::vector<TreeAccessor*>();
368 int oldEvtIndex = findIndex->second;
369 bool isNewEvt = (eventsToKeep[
f].find(oldEvtIndex) == eventsToKeep[
f].end());
370 unsigned int newEvtIndex = (isNewEvt ? eventsToKeep[
f].size() : eventsToKeep[
f][oldEvtIndex]);
371 if (isNewEvt) eventsToKeep[
f][oldEvtIndex] = newEvtIndex;
374 bool isNewRun = (runsToKeep[
f].find(oldRunIndex) == runsToKeep[
f].end());
375 unsigned int newRunIndex = (isNewRun ? runsToKeep[
f].size() : runsToKeep[
f][oldRunIndex]);
376 if (isNewRun) runsToKeep[
f][oldRunIndex] = newRunIndex;
380 cout <<
"Filtering failed on data " <<
k <<
" of cell " <<
i <<
", aborting" << endl;
382 for (
unsigned int f = 0;
f < filterList.
size();
f++)
delete newAccessors[
f];
383 return std::vector<TreeAccessor*>();
393 for (
unsigned int f = 0;
f < filterList.
size();
f++) {
394 cout <<
"Adding runs..." << endl;
395 std::vector<unsigned int> runsToKeep_ordered(runsToKeep[
f].
size());
396 for (
const auto& runIndex : runsToKeep[
f])
397 runsToKeep_ordered[runIndex.second] = runIndex.first;
399 for (
unsigned int runIndex : runsToKeep_ordered) {
401 newAccessors[
f]->addRun(newRun);
404 cout <<
"Adding events..." << endl;
405 std::vector<unsigned int> eventsToKeep_ordered(eventsToKeep[
f].
size());
409 for (
unsigned int eventIndex : eventsToKeep_ordered) {
410 std::map<unsigned int, unsigned int>::const_iterator
idx = runsToKeep[
f].find(
accessor.eventData(
eventIndex)->runIndex());
411 int newRunIndex = (
idx == runsToKeep[
f].end() ? 0 :
idx->second);
413 newAccessors[
f]->addEvent(newEvent);
417 cout <<
"Filtering done! final size = " << nPass << endl;
419 for (
unsigned int f = 0;
f < filterList.
size();
f++) {
420 cout <<
"Saving " << newAccessors[
f]->fileName() << endl;
421 newAccessors[
f]->save();
431 std::vector<short> samples(5, 0);
432 std::vector<float> autoCorrs(4, 0);
435 newAccessor->
addRun(dummyRun);
442 for (
unsigned int i = 0;
i <
accessor.nChannels();
i++) {
444 cout <<
"Templating " <<
i <<
"/" <<
accessor.nChannels() << endl;
446 if (!history || !history->
cellInfo()) {
465 TFile* newFile =
new TFile(
fileName,
"RECREATE");
466 if (newFile && !newFile->IsOpen()) {
delete newFile; newFile =
nullptr; }
467 if (!newFile)
return false;