94 cout <<
"Merging to " <<
fileName << endl;
95 auto newAcc = std::make_unique<TreeAccessor>(
fileName);
96 unsigned int size = 0;
98 int evtIndex = 0, runIndex = 0;
99 std::map<std::pair<int, int>,
int> evtMap;
100 std::map<int, int> runMap;
102 cout <<
"Merging runs" << endl;
103 for (
const Accessor* accessor : accessors) {
105 cout <<
"Cannot merge: one of the inputs is null!" << endl;
108 for (
unsigned int i = 0; i < accessor->nRuns(); i++) {
109 int run = accessor->runData(i)->run();
110 if (runMap.find(
run) != runMap.end())
continue;
111 runMap[
run] = runIndex;
112 RunData newRun(*accessor->runData(i));
113 newAcc->addRun(&newRun);
118 cout <<
"Merging events" << endl;
119 unsigned int nEventsTotal = 0, iEvt = 0;
120 for (
const Accessor* accessor : accessors)
121 nEventsTotal += accessor->nEvents();
122 for (
const Accessor* accessor : accessors) {
123 for (
unsigned int i = 0; i < accessor->nEvents(); i++) {
125 if (iEvt % 100000 == 0) cout <<
"Merging event " << iEvt <<
"/" << nEventsTotal << endl;
126 std::pair<int, int> evtId(accessor->eventData(i)->run(), accessor->eventData(i)->event());
127 if (evtMap.find(evtId) != evtMap.end())
continue;
128 evtMap[evtId] = evtIndex;
129 std::map<int, int>::const_iterator idx = runMap.find(accessor->eventData(i)->run());
130 int newRunIndex = (idx == runMap.end() ? -999 : idx->second);
132 EventData newEvent(*accessor->eventData(i), newRunIndex);
133 newAcc->addEvent(&newEvent);
138 for (
unsigned int i = 0; i < newAcc->nChannels(); i++) {
139 if (i % 10000 == 0) {
140 cout <<
"Merging channel " << i <<
"/" << newAcc->nChannels() <<
" (current size = " << size <<
")" << endl;
145 for (
const Accessor* accessor : accessors) {
146 const History* history = accessor->cellHistory(i);
147 if (!history || !history->
isValid())
continue;
152 for (
unsigned int j = 0; j < history->
nData(); j++) {
153 auto newDC = std::make_unique<DataContainer>(history->
data(j)->
container());
155 = evtMap.find(std::make_pair(history->
data(j)->
run(), history->
data(j)->
event()));
156 if (newIndex == evtMap.end()) std::cout <<
"Event not found for cell " << i <<
", data " << j <<
".\n";
157 newDC->setEventIndex(newIndex != evtMap.end() ? newIndex->second : -1);
159 if (not info)
continue;
160 if (!info->shape(history->
data(j)->
gain())) {
163 cout <<
"Shape not filled for hash = " << i <<
", index = " << j <<
", gain = " <<
Data::gainStr(history->
data(j)->
gain()) << endl;
165 info->setShape(history->
data(j)->
gain(), (shape ?
new ShapeInfo(*shape) :
nullptr));
173 cout <<
"Merging done, final size = " << size << endl;
185 std::vector<unsigned int> LBList;
186 std::ifstream infile(LBFile.Data());
189 std::getline(infile,line,
'\n');
190 TString
filter(line.c_str());
191 std::unique_ptr<TObjArray> list (
filter.Tokenize(
", "));
192 if(list->GetEntries() == 0){
193 printf(
"No LB filtering specified, or bad format. Exiting.\n");
197 for(
int k = 0; k < list->GetEntries(); k++){
198 TObjString* tobs = (TObjString*)(list->At(k));
199 LBList.push_back((
unsigned int)(tobs->String()).Atoi());
201 printf(
"LB List: %d\n",(
int)LBList.size());
205 auto newAcc = std::make_unique<TreeAccessor>(
fileName);
206 unsigned int size = 0;
208 int evtIndex = 0, runIndex = 0;
209 std::map<std::pair<int, int>,
int> evtMap;
210 std::map<int, int> runMap;
212 cout <<
"Merging runs" << endl;
213 for (
const Accessor* accessor : accessors) {
215 cout <<
"Cannot merge: one of the inputs is null!" << endl;
218 for (
unsigned int i = 0; i < accessor->nRuns(); i++) {
219 int run = accessor->runData(i)->run();
220 if (runMap.find(
run) != runMap.end())
continue;
221 runMap[
run] = runIndex;
222 RunData newRun(*accessor->runData(i));
223 newAcc->addRun(&newRun);
228 cout <<
"Merging events" << endl;
229 unsigned int nEventsTotal = 0, iEvt = 0;
230 for (
const Accessor* accessor : accessors)
231 nEventsTotal += accessor->nEvents();
232 for (
const Accessor* accessor : accessors) {
233 for (
unsigned int i = 0; i < accessor->nEvents(); i++) {
235 if (iEvt % 100000 == 0) cout <<
"Merging event " << iEvt <<
"/" << nEventsTotal << endl;
240 for(
unsigned int ilb = 0 ; ilb < LBList.size() ; ilb++){
241 if(LBList.at(ilb)==accessor->eventData(i)->lumiBlock()){
250 std::pair<int, int> evtId(accessor->eventData(i)->run(), accessor->eventData(i)->event());
251 if (evtMap.find(evtId) != evtMap.end())
continue;
252 evtMap[evtId] = evtIndex;
253 std::map<int, int>::const_iterator idx = runMap.find(accessor->eventData(i)->run());
254 int newRunIndex = (idx == runMap.end() ? -999 : idx->second);
255 EventData newEvent(*accessor->eventData(i), newRunIndex);
256 newAcc->addEvent(&newEvent);
261 cout <<
"Merging cells" << endl;
262 for (
unsigned int i = 0; i < newAcc->nChannels(); i++) {
263 if (i % 10000 == 0) {
264 cout <<
"Merging channel " << i <<
"/" << newAcc->nChannels() <<
" (current size = " << size <<
")" << endl;
269 for (
const Accessor* accessor : accessors) {
270 const History* history = accessor->cellHistory(i);
271 if (!history || !history->
isValid())
continue;
276 for (
unsigned int j = 0; j < history->
nData(); j++) {
277 auto newDC = std::make_unique<DataContainer>(history->
data(j)->
container());
279 = evtMap.find(std::make_pair(history->
data(j)->
run(), history->
data(j)->
event()));
281 newDC->setEventIndex(newIndex != evtMap.end() ? newIndex->second : -1);
283 if (not info)
continue;
284 if (!info->shape(history->
data(j)->
gain())) {
287 cout <<
"Shape not filled for hash = " << i <<
", index = " << j <<
", gain = " <<
Data::gainStr(history->
data(j)->
gain()) << endl;
288 info->setShape(history->
data(j)->
gain(), (shape ?
new ShapeInfo(*shape) :
nullptr));
299 cout <<
"Merging SC" << endl;
300 for (
unsigned int i = 0; i < newAcc->nChannelsSC(); i++) {
301 if (i % 10000 == 0) {
302 cout <<
"Merging channel " << i <<
"/" << newAcc->nChannelsSC() <<
" (current size = " << size <<
")" << endl;
306 for (
const Accessor* accessor : accessors) {
307 const History* history = accessor->getSCHistory(i);
308 if (!history || !history->
isValid())
continue;
313 for (
unsigned int j = 0; j < history->
nData(); j++) {
314 auto newDC = std::make_unique<DataContainer>(history->
data(j)->
container());
316 = evtMap.find(std::make_pair(history->
data(j)->
run(), history->
data(j)->
event()));
318 newDC->setEventIndex(newIndex != evtMap.end() ? newIndex->second : -1);
320 if (not info)
continue;
321 if (!info->shape(history->
data(j)->
gain())) {
324 cout <<
"Shape not filled for hash = " << i <<
", index = " << j <<
", gain = " <<
Data::gainStr(history->
data(j)->
gain()) << endl;
325 info->setShape(history->
data(j)->
gain(), (shape ?
new ShapeInfo(*shape) :
nullptr));
336 cout <<
"Merging done, final size = " << size << endl;
357 std::vector<std::unique_ptr<TreeAccessor> > newAccessors;
359 if (filterList.
size() == 0) {
360 cout <<
"No filter categories specified, done! (?)" << endl;
364 for (
unsigned int f = 0; f < filterList.
size(); f++) {
365 cout <<
"Skimming to " << filterList.
fileName(f) << endl;
366 if (!gSystem->AccessPathName(filterList.
fileName(f))) {
367 cout <<
"File already exists, exiting." << endl;
372 for (
unsigned int f = 0; f < filterList.
size(); f++)
373 newAccessors.push_back(std::make_unique<TreeAccessor>(filterList.
fileName(f)));
374 std::map<std::pair<unsigned int, unsigned int>,
unsigned int> eventIndices;
375 std::vector< std::map<unsigned int, unsigned int> > eventsToKeep(filterList.
size());
376 std::vector< std::map<unsigned int, unsigned int> > runsToKeep(filterList.
size());
378 double nTot = 0, nPass = 0;
380 for (
unsigned int i = 0; i < accessor.nEvents(); i++) {
382 eventIndices[std::pair<unsigned int, unsigned int>(
eventData->run(),
eventData->event())] = i;
385 for (
unsigned int i = 0; i < accessor.nChannels(); i++) {
386 if (i % 25000 == 0) {
387 cout <<
"Filtering " << i <<
"/" << accessor.nChannels()
388 <<
" (passing so far = " << nPass <<
", total seen = " << nTot <<
")" << endl;
393 const History* history =
nullptr;
394 for (
unsigned int f = 0; f < filterList.
size(); f++) {
398 for (
unsigned int f = 0; f < filterList.
size(); f++) {
401 newAccessors[f]->add(&newHist);
404 if (first) { nTot += history->
nData(); first =
false; }
406 for (
unsigned int k = 0; k < history->
nData(); k++) {
409 std::map<std::pair<unsigned int, unsigned int>,
unsigned int>
::const_iterator findIndex =
410 eventIndices.find(std::pair<unsigned int, unsigned int>(
eventData->run(),
eventData->event()));
411 if (findIndex == eventIndices.end()) {
412 cout <<
"Inconsistent event numbering!!!" << endl;
413 return std::vector<std::unique_ptr<TreeAccessor> >();
415 int oldEvtIndex = findIndex->second;
416 bool isNewEvt = (eventsToKeep[f].find(oldEvtIndex) == eventsToKeep[f].end());
417 unsigned int newEvtIndex = (isNewEvt ? eventsToKeep[f].size() : eventsToKeep[f][oldEvtIndex]);
418 if (isNewEvt) eventsToKeep[f][oldEvtIndex] = newEvtIndex;
421 bool isNewRun = (runsToKeep[f].find(oldRunIndex) == runsToKeep[f].end());
422 unsigned int newRunIndex = (isNewRun ? runsToKeep[f].size() : runsToKeep[f][oldRunIndex]);
423 if (isNewRun) runsToKeep[f][oldRunIndex] = newRunIndex;
425 Data* newData = tweaker.
tweak(*history->
data(k), newEvtIndex);
427 cout <<
"Filtering failed on data " << k <<
" of cell " << i <<
", aborting" << endl;
428 return std::vector<std::unique_ptr<TreeAccessor> >();
433 newAccessors[f]->add(&newHist);
437 for (
unsigned int f = 0; f < filterList.
size(); f++) {
438 cout <<
"Adding runs..." << endl;
439 std::vector<unsigned int> runsToKeep_ordered(runsToKeep[f].size());
440 for (
const auto& runIndex : runsToKeep[f])
441 runsToKeep_ordered[runIndex.second] = runIndex.first;
443 for (
unsigned int runIndex : runsToKeep_ordered) {
444 RunData newRun(*accessor.runData(runIndex));
445 newAccessors[f]->addRun(&newRun);
447 cout <<
"Adding events..." << endl;
448 std::vector<unsigned int> eventsToKeep_ordered(eventsToKeep[f].size());
449 for (
const auto& eventIndex : eventsToKeep[f])
450 eventsToKeep_ordered[eventIndex.second] = eventIndex.first;
452 for (
unsigned int eventIndex : eventsToKeep_ordered) {
453 std::map<unsigned int, unsigned int>::const_iterator idx = runsToKeep[f].find(accessor.eventData(eventIndex)->runIndex());
454 int newRunIndex = (idx == runsToKeep[f].end() ? 0 : idx->second);
455 std::unique_ptr<EventData> newEvent (tweaker.
tweak(*accessor.eventData(eventIndex), newRunIndex));
456 newAccessors[f]->addEvent(newEvent.get());
459 cout <<
"Filtering done! final size = " << nPass << endl;
461 for (
unsigned int f = 0; f < filterList.
size(); f++) {
462 cout <<
"Saving " << newAccessors[f]->fileName() << endl;
463 newAccessors[f]->save();
471 auto newAccessor = std::make_unique<TreeAccessor>(
fileName);
473 std::vector<short> samples(5, 0);
474 std::vector<float> autoCorrs(4, 0);
477 newAccessor->addRun(&dummyRun);
480 newAccessor->addEvent(&dummyEvent);
482 for (
unsigned int i = 0; i < accessor.nChannels(); i++) {
484 cout <<
"Templating " << i <<
"/" << accessor.nChannels() << endl;
485 const History* history = accessor.cellHistory(i);
486 if (!history || !history->
cellInfo()) {
488 newAccessor->add(&newHist);
492 auto dataContainer = std::make_unique<DataContainer>(
CaloGain::LARHIGHGAIN, samples, 0, 0, 0, 0, autoCorrs);
493 newHist.add(dataContainer.release());
494 newAccessor->add(&newHist);
const CellInfo * cellInfo() const
unsigned int nDataContainers() const
const CellInfo * cellInfo() const
const Data * data(unsigned int i) const
unsigned int nData() const