15 #include "TObjString.h"
33 if (!
file)
return nullptr;
34 if (!
file->IsOpen()) {
delete file;
return nullptr; }
37 TTree* scTree = (TTree*)
file->Get(
"SC");
38 if (!scTree)
return nullptr;
50 if (!cont || !cont->
cellInfo())
return nullptr;
57 if (!cont || !cont->
cellInfo())
return nullptr;
67 std::vector<const EventData*> eventDatas;
72 eventDatas.push_back(newEvtData);
82 std::vector<const EventData*> eventDatas;
87 eventDatas.push_back(newEvtData);
95 cout <<
"Merging to " <<
fileName << endl;
97 unsigned int size = 0;
100 int evtIndex = 0, runIndex = 0;
101 std::map<std::pair<int, int>,
int> evtMap;
102 std::map<int, int> runMap;
104 cout <<
"Merging runs" << endl;
107 cout <<
"Cannot merge: one of the inputs is null!" << endl;
111 for (
unsigned int i = 0;
i <
accessor->nRuns();
i++) {
113 if (runMap.find(
run) != runMap.end())
continue;
114 runMap[
run] = runIndex;
122 cout <<
"Merging events" << endl;
123 unsigned int nEventsTotal = 0, iEvt = 0;
125 nEventsTotal +=
accessor->nEvents();
127 for (
unsigned int i = 0;
i <
accessor->nEvents();
i++) {
129 if (iEvt % 100000 == 0) cout <<
"Merging event " << iEvt <<
"/" << nEventsTotal << endl;
130 std::pair<int, int> evtId(
accessor->eventData(
i)->run(),
accessor->eventData(
i)->event());
131 if (evtMap.find(evtId) != evtMap.end())
continue;
132 evtMap[evtId] = evtIndex;
133 std::map<int, int>::const_iterator
idx = runMap.find(
accessor->eventData(
i)->run());
134 int newRunIndex = (
idx == runMap.end() ? -999 :
idx->second);
143 for (
unsigned int i = 0;
i < newAcc->
nChannels();
i++) {
144 if (
i % 10000 == 0) {
145 cout <<
"Merging channel " <<
i <<
"/" << newAcc->
nChannels() <<
" (current size = " <<
size <<
")" << endl;
151 if (!history || !history->
isValid())
continue;
156 for (
unsigned int j = 0; j < history->
nData(); j++) {
158 std::map<std::pair<int, int>,
int>::const_iterator newIndex
159 = evtMap.find(std::make_pair(history->
data(j)->
run(), history->
data(j)->
event()));
160 if (newIndex == evtMap.end()) cout <<
"Event not found for cell " <<
i <<
", data " << j <<
"." << endl;
161 newDC->
setEventIndex(newIndex != evtMap.end() ? newIndex->second : -1);
166 cout <<
"Shape not filled for hash = " <<
i <<
", index = " << j <<
", gain = " <<
Data::gainStr(history->
data(j)->
gain()) << endl;
176 cout <<
"Merging done, final size = " <<
size << endl;
188 std::vector<unsigned int> LBList;
189 std::ifstream
infile(LBFile.Data());
195 if(
list->GetEntries() == 0){
196 printf(
"No LB filtering specified, or bad format. Exiting.\n");
201 for(
int k = 0;
k <
list->GetEntries();
k++){
202 TObjString* tobs = (TObjString*)(
list->At(
k));
203 LBList.push_back((
unsigned int)(tobs->String()).Atoi());
206 printf(
"LB List: %d\n",(
int)LBList.size());
211 unsigned int size = 0;
214 int evtIndex = 0, runIndex = 0;
215 std::map<std::pair<int, int>,
int> evtMap;
216 std::map<int, int> runMap;
218 cout <<
"Merging runs" << endl;
221 cout <<
"Cannot merge: one of the inputs is null!" << endl;
225 for (
unsigned int i = 0;
i <
accessor->nRuns();
i++) {
227 if (runMap.find(
run) != runMap.end())
continue;
228 runMap[
run] = runIndex;
236 cout <<
"Merging events" << endl;
237 unsigned int nEventsTotal = 0, iEvt = 0;
239 nEventsTotal +=
accessor->nEvents();
241 for (
unsigned int i = 0;
i <
accessor->nEvents();
i++) {
243 if (iEvt % 100000 == 0) cout <<
"Merging event " << iEvt <<
"/" << nEventsTotal << endl;
248 for(
unsigned int ilb = 0 ;
ilb < LBList.size() ;
ilb++){
249 if(LBList.at(
ilb)==
accessor->eventData(
i)->lumiBlock()){
258 std::pair<int, int> evtId(
accessor->eventData(
i)->run(),
accessor->eventData(
i)->event());
259 if (evtMap.find(evtId) != evtMap.end())
continue;
260 evtMap[evtId] = evtIndex;
261 std::map<int, int>::const_iterator
idx = runMap.find(
accessor->eventData(
i)->run());
262 int newRunIndex = (
idx == runMap.end() ? -999 :
idx->second);
270 cout <<
"Merging cells" << endl;
271 for (
unsigned int i = 0;
i < newAcc->
nChannels();
i++) {
272 if (
i % 10000 == 0) {
273 cout <<
"Merging channel " <<
i <<
"/" << newAcc->
nChannels() <<
" (current size = " <<
size <<
")" << endl;
279 if (!history || !history->
isValid())
continue;
284 for (
unsigned int j = 0; j < history->
nData(); j++) {
286 std::map<std::pair<int, int>,
int>::const_iterator newIndex
287 = evtMap.find(std::make_pair(history->
data(j)->
run(), history->
data(j)->
event()));
289 newDC->
setEventIndex(newIndex != evtMap.end() ? newIndex->second : -1);
294 cout <<
"Shape not filled for hash = " <<
i <<
", index = " << j <<
", gain = " <<
Data::gainStr(history->
data(j)->
gain()) << endl;
308 cout <<
"Merging SC" << endl;
310 if (
i % 10000 == 0) {
311 cout <<
"Merging channel " <<
i <<
"/" << newAcc->
nChannelsSC() <<
" (current size = " <<
size <<
")" << endl;
316 if (!history || !history->
isValid())
continue;
321 for (
unsigned int j = 0; j < history->
nData(); j++) {
323 std::map<std::pair<int, int>,
int>::const_iterator newIndex
324 = evtMap.find(std::make_pair(history->
data(j)->
run(), history->
data(j)->
event()));
326 newDC->
setEventIndex(newIndex != evtMap.end() ? newIndex->second : -1);
331 cout <<
"Shape not filled for hash = " <<
i <<
", index = " << j <<
", gain = " <<
Data::gainStr(history->
data(j)->
gain()) << endl;
345 cout <<
"Merging done, final size = " <<
size << endl;
361 std::vector<TreeAccessor*>
366 if (filterList.
size() == 0) {
367 cout <<
"No filter categories specified, done! (?)" << endl;
368 return std::vector<TreeAccessor*>();
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*>();
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());
386 double nTot = 0, nPass = 0;
388 for (
unsigned int i = 0;
i <
accessor.nEvents();
i++) {
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;
401 const History* history =
nullptr;
402 for (
unsigned int f = 0;
f < filterList.
size();
f++) {
406 for (
unsigned int f = 0;
f < filterList.
size();
f++) {
415 for (
unsigned int k = 0;
k < history->
nData();
k++) {
418 std::map<std::pair<unsigned int, unsigned int>,
unsigned int>::const_iterator findIndex =
420 if (findIndex == eventIndices.end()) {
421 cout <<
"Inconsistent event numbering!!!" << endl;
423 return std::vector<TreeAccessor*>();
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;
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;
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*>();
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;
456 for (
unsigned int runIndex : runsToKeep_ordered) {
458 newAccessors[
f]->addRun(newRun);
461 cout <<
"Adding events..." << endl;
462 std::vector<unsigned int> eventsToKeep_ordered(eventsToKeep[
f].
size());
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);
474 cout <<
"Filtering done! final size = " << nPass << endl;
476 for (
unsigned int f = 0;
f < filterList.
size();
f++) {
477 cout <<
"Saving " << newAccessors[
f]->fileName() << endl;
478 newAccessors[
f]->save();
488 std::vector<short> samples(5, 0);
489 std::vector<float> autoCorrs(4, 0);
492 newAccessor->
addRun(dummyRun);
499 for (
unsigned int i = 0;
i <
accessor.nChannels();
i++) {
501 cout <<
"Templating " <<
i <<
"/" <<
accessor.nChannels() << endl;
503 if (!history || !history->
cellInfo()) {
522 TFile* newFile =
new TFile(
fileName,
"RECREATE");
523 if (newFile && !newFile->IsOpen()) {
delete newFile; newFile =
nullptr; }
524 if (!newFile)
return false;