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 for (
unsigned int i = 0;
i < newAcc->
nChannels();
i++) {
271 if (
i % 10000 == 0) {
272 cout <<
"Merging channel " <<
i <<
"/" << newAcc->
nChannels() <<
" (current size = " <<
size <<
")" << endl;
278 if (!history || !history->
isValid())
continue;
283 for (
unsigned int j = 0; j < history->
nData(); j++) {
285 std::map<std::pair<int, int>,
int>::const_iterator newIndex
286 = evtMap.find(std::make_pair(history->
data(j)->
run(), history->
data(j)->
event()));
288 newDC->
setEventIndex(newIndex != evtMap.end() ? newIndex->second : -1);
293 cout <<
"Shape not filled for hash = " <<
i <<
", index = " << j <<
", gain = " <<
Data::gainStr(history->
data(j)->
gain()) << endl;
307 cout <<
"Merging done, final size = " <<
size << endl;
323 std::vector<TreeAccessor*>
328 if (filterList.
size() == 0) {
329 cout <<
"No filter categories specified, done! (?)" << endl;
330 return std::vector<TreeAccessor*>();
333 for (
unsigned int f = 0;
f < filterList.
size();
f++) {
334 cout <<
"Skimming to " << filterList.
fileName(
f) << endl;
335 if (!gSystem->AccessPathName(filterList.
fileName(
f))) {
336 cout <<
"File already exists, exiting." << endl;
337 return std::vector<TreeAccessor*>();
341 std::vector<TreeAccessor*> newAccessors;
342 for (
unsigned int f = 0;
f < filterList.
size();
f++)
344 std::map<std::pair<unsigned int, unsigned int>,
unsigned int> eventIndices;
345 std::vector< std::map<unsigned int, unsigned int> > eventsToKeep(filterList.
size());
346 std::vector< std::map<unsigned int, unsigned int> > runsToKeep(filterList.
size());
348 double nTot = 0, nPass = 0;
350 for (
unsigned int i = 0;
i <
accessor.nEvents();
i++) {
355 for (
unsigned int i = 0;
i <
accessor.nChannels();
i++) {
356 if (
i % 25000 == 0) {
357 cout <<
"Filtering " <<
i <<
"/" <<
accessor.nChannels()
358 <<
" (passing so far = " << nPass <<
", total seen = " << nTot <<
")" << endl;
363 const History* history =
nullptr;
364 for (
unsigned int f = 0;
f < filterList.
size();
f++) {
368 for (
unsigned int f = 0;
f < filterList.
size();
f++) {
377 for (
unsigned int k = 0;
k < history->
nData();
k++) {
380 std::map<std::pair<unsigned int, unsigned int>,
unsigned int>::const_iterator findIndex =
382 if (findIndex == eventIndices.end()) {
383 cout <<
"Inconsistent event numbering!!!" << endl;
385 return std::vector<TreeAccessor*>();
387 int oldEvtIndex = findIndex->second;
388 bool isNewEvt = (eventsToKeep[
f].find(oldEvtIndex) == eventsToKeep[
f].end());
389 unsigned int newEvtIndex = (isNewEvt ? eventsToKeep[
f].size() : eventsToKeep[
f][oldEvtIndex]);
390 if (isNewEvt) eventsToKeep[
f][oldEvtIndex] = newEvtIndex;
393 bool isNewRun = (runsToKeep[
f].find(oldRunIndex) == runsToKeep[
f].end());
394 unsigned int newRunIndex = (isNewRun ? runsToKeep[
f].size() : runsToKeep[
f][oldRunIndex]);
395 if (isNewRun) runsToKeep[
f][oldRunIndex] = newRunIndex;
399 cout <<
"Filtering failed on data " <<
k <<
" of cell " <<
i <<
", aborting" << endl;
401 for (
unsigned int f = 0;
f < filterList.
size();
f++)
delete newAccessors[
f];
402 return std::vector<TreeAccessor*>();
412 for (
unsigned int f = 0;
f < filterList.
size();
f++) {
413 cout <<
"Adding runs..." << endl;
414 std::vector<unsigned int> runsToKeep_ordered(runsToKeep[
f].
size());
415 for (
const auto& runIndex : runsToKeep[
f])
416 runsToKeep_ordered[runIndex.second] = runIndex.first;
418 for (
unsigned int runIndex : runsToKeep_ordered) {
420 newAccessors[
f]->addRun(newRun);
423 cout <<
"Adding events..." << endl;
424 std::vector<unsigned int> eventsToKeep_ordered(eventsToKeep[
f].
size());
428 for (
unsigned int eventIndex : eventsToKeep_ordered) {
429 std::map<unsigned int, unsigned int>::const_iterator
idx = runsToKeep[
f].find(
accessor.eventData(
eventIndex)->runIndex());
430 int newRunIndex = (
idx == runsToKeep[
f].end() ? 0 :
idx->second);
432 newAccessors[
f]->addEvent(newEvent);
436 cout <<
"Filtering done! final size = " << nPass << endl;
438 for (
unsigned int f = 0;
f < filterList.
size();
f++) {
439 cout <<
"Saving " << newAccessors[
f]->fileName() << endl;
440 newAccessors[
f]->save();
450 std::vector<short> samples(5, 0);
451 std::vector<float> autoCorrs(4, 0);
454 newAccessor->
addRun(dummyRun);
461 for (
unsigned int i = 0;
i <
accessor.nChannels();
i++) {
463 cout <<
"Templating " <<
i <<
"/" <<
accessor.nChannels() << endl;
465 if (!history || !history->
cellInfo()) {
484 TFile* newFile =
new TFile(
fileName,
"RECREATE");
485 if (newFile && !newFile->IsOpen()) {
delete newFile; newFile =
nullptr; }
486 if (!newFile)
return false;