102 cout <<
"Merging to " <<
fileName << endl;
103 auto newAcc = std::make_unique<TreeAccessor>(
fileName);
104 unsigned int size = 0;
106 int evtIndex = 0, runIndex = 0;
107 std::map<std::pair<int, int>,
int> evtMap;
108 std::map<int, int> runMap;
110 cout <<
"Merging runs" << endl;
111 for (
const Accessor* accessor : accessors) {
113 cout <<
"Cannot merge: one of the inputs is null!" << endl;
116 for (
unsigned int i = 0; i < accessor->nRuns(); i++) {
117 int run = accessor->runData(i)->run();
118 if (runMap.find(
run) != runMap.end())
continue;
119 runMap[
run] = runIndex;
120 RunData newRun(*accessor->runData(i));
121 newAcc->addRun(&newRun);
126 cout <<
"Merging events" << endl;
127 unsigned int nEventsTotal = 0, iEvt = 0;
128 for (
const Accessor* accessor : accessors)
129 nEventsTotal += accessor->nEvents();
130 for (
const Accessor* accessor : accessors) {
131 for (
unsigned int i = 0; i < accessor->nEvents(); i++) {
133 if (iEvt % 100000 == 0) cout <<
"Merging event " << iEvt <<
"/" << nEventsTotal << endl;
134 std::pair<int, int> evtId(accessor->eventData(i)->run(), accessor->eventData(i)->event());
135 if (evtMap.find(evtId) != evtMap.end())
continue;
136 evtMap[evtId] = evtIndex;
137 std::map<int, int>::const_iterator idx = runMap.find(accessor->eventData(i)->run());
138 int newRunIndex = (idx == runMap.end() ? -999 : idx->second);
140 EventData newEvent(*accessor->eventData(i), newRunIndex);
141 newAcc->addEvent(&newEvent);
146 for (
unsigned int i = 0; i < newAcc->nChannels(); i++) {
147 if (i % 10000 == 0) {
148 cout <<
"Merging channel " << i <<
"/" << newAcc->nChannels() <<
" (current size = " << size <<
")" << endl;
153 for (
const Accessor* accessor : accessors) {
154 const History* history = accessor->cellHistory(i);
155 if (!history || !history->
isValid())
continue;
160 for (
unsigned int j = 0; j < history->
nData(); j++) {
161 auto newDC = std::make_unique<DataContainer>(history->
data(j)->
container());
163 = evtMap.find(std::make_pair(history->
data(j)->
run(), history->
data(j)->
event()));
164 if (newIndex == evtMap.end()) std::cout <<
"Event not found for cell " << i <<
", data " << j <<
".\n";
165 newDC->setEventIndex(newIndex != evtMap.end() ? newIndex->second : -1);
167 if (not info)
continue;
168 if (!info->shape(history->
data(j)->
gain())) {
171 cout <<
"Shape not filled for hash = " << i <<
", index = " << j <<
", gain = " <<
Data::gainStr(history->
data(j)->
gain()) << endl;
173 info->setShape(history->
data(j)->
gain(), (shape ?
new ShapeInfo(*shape) :
nullptr));
181 cout <<
"Merging done, final size = " << size << endl;
193 std::vector<unsigned int> LBList;
194 std::ifstream infile(LBFile.Data());
197 std::getline(infile,line,
'\n');
198 TString
filter(line.c_str());
199 std::unique_ptr<TObjArray> list (
filter.Tokenize(
", "));
200 if(list->GetEntries() == 0){
201 printf(
"No LB filtering specified, or bad format. Exiting.\n");
205 for(
int k = 0; k < list->GetEntries(); k++){
206 TObjString* tobs = (TObjString*)(list->At(k));
207 LBList.push_back((
unsigned int)(tobs->String()).Atoi());
209 printf(
"LB List: %d\n",(
int)LBList.size());
213 auto newAcc = std::make_unique<TreeAccessor>(
fileName);
214 unsigned int size = 0;
216 int evtIndex = 0, runIndex = 0;
217 std::map<std::pair<int, int>,
int> evtMap;
218 std::map<int, int> runMap;
220 cout <<
"Merging runs" << endl;
221 for (
const Accessor* accessor : accessors) {
223 cout <<
"Cannot merge: one of the inputs is null!" << endl;
226 for (
unsigned int i = 0; i < accessor->nRuns(); i++) {
227 int run = accessor->runData(i)->run();
228 if (runMap.find(
run) != runMap.end())
continue;
229 runMap[
run] = runIndex;
230 RunData newRun(*accessor->runData(i));
231 newAcc->addRun(&newRun);
236 cout <<
"Merging events" << endl;
237 unsigned int nEventsTotal = 0, iEvt = 0;
238 for (
const Accessor* accessor : accessors)
239 nEventsTotal += accessor->nEvents();
240 for (
const Accessor* accessor : accessors) {
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);
263 EventData newEvent(*accessor->eventData(i), newRunIndex);
264 newAcc->addEvent(&newEvent);
269 cout <<
"Merging cells" << endl;
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;
277 for (
const Accessor* accessor : accessors) {
278 const History* history = accessor->cellHistory(i);
279 if (!history || !history->
isValid())
continue;
284 for (
unsigned int j = 0; j < history->
nData(); j++) {
285 auto newDC = std::make_unique<DataContainer>(history->
data(j)->
container());
287 = evtMap.find(std::make_pair(history->
data(j)->
run(), history->
data(j)->
event()));
289 newDC->setEventIndex(newIndex != evtMap.end() ? newIndex->second : -1);
291 if (not info)
continue;
292 if (!info->shape(history->
data(j)->
gain())) {
295 cout <<
"Shape not filled for hash = " << i <<
", index = " << j <<
", gain = " <<
Data::gainStr(history->
data(j)->
gain()) << endl;
296 info->setShape(history->
data(j)->
gain(), (shape ?
new ShapeInfo(*shape) :
nullptr));
307 cout <<
"Merging SC" << endl;
308 for (
unsigned int i = 0; i < newAcc->nChannelsSC(); i++) {
309 if (i % 10000 == 0) {
310 cout <<
"Merging channel " << i <<
"/" << newAcc->nChannelsSC() <<
" (current size = " << size <<
")" << endl;
314 for (
const Accessor* accessor : accessors) {
315 std::unique_ptr<const History> history = accessor->getSCHistory(i);
316 if (!history || !history->isValid())
continue;
318 info =
new CellInfo(*history->cellInfo());
321 for (
unsigned int j = 0; j < history->nData(); j++) {
322 auto newDC = std::make_unique<DataContainer>(history->data(j)->container());
324 = evtMap.find(std::make_pair(history->data(j)->run(), history->data(j)->event()));
326 newDC->setEventIndex(newIndex != evtMap.end() ? newIndex->second : -1);
328 if (not info)
continue;
329 if (!info->shape(history->data(j)->gain())) {
330 const ShapeInfo* shape = history->cellInfo()->shape(history->data(j)->gain());
332 cout <<
"Shape not filled for hash = " << i <<
", index = " << j <<
", gain = " <<
Data::gainStr(history->data(j)->gain()) << endl;
333 info->setShape(history->data(j)->gain(), (shape ?
new ShapeInfo(*shape) :
nullptr));
344 cout <<
"Merging done, final size = " << size << endl;
365 std::vector<std::unique_ptr<TreeAccessor> > newAccessors;
367 if (filterList.
size() == 0) {
368 cout <<
"No filter categories specified, done! (?)" << endl;
372 for (
unsigned int f = 0; f < filterList.
size(); f++) {
373 cout <<
"Skimming to " << filterList.
fileName(f) << endl;
374 if (!gSystem->AccessPathName(filterList.
fileName(f))) {
375 cout <<
"File already exists, exiting." << endl;
380 for (
unsigned int f = 0; f < filterList.
size(); f++)
381 newAccessors.push_back(std::make_unique<TreeAccessor>(filterList.
fileName(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++) {
390 eventIndices[std::pair<unsigned int, unsigned int>(
eventData->run(),
eventData->event())] = 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++) {
409 newAccessors[f]->add(&newHist);
412 if (first) { nTot += history->
nData(); first =
false; }
414 for (
unsigned int k = 0; k < history->
nData(); k++) {
417 std::map<std::pair<unsigned int, unsigned int>,
unsigned int>
::const_iterator findIndex =
418 eventIndices.find(std::pair<unsigned int, unsigned int>(
eventData.run(),
eventData.event()));
419 if (findIndex == eventIndices.end()) {
420 cout <<
"Inconsistent event numbering!!!" << endl;
421 return std::vector<std::unique_ptr<TreeAccessor> >();
423 int oldEvtIndex = findIndex->second;
424 bool isNewEvt = (eventsToKeep[f].find(oldEvtIndex) == eventsToKeep[f].end());
425 unsigned int newEvtIndex = (isNewEvt ? eventsToKeep[f].size() : eventsToKeep[f][oldEvtIndex]);
426 if (isNewEvt) eventsToKeep[f][oldEvtIndex] = newEvtIndex;
429 bool isNewRun = (runsToKeep[f].find(oldRunIndex) == runsToKeep[f].end());
430 unsigned int newRunIndex = (isNewRun ? runsToKeep[f].size() : runsToKeep[f][oldRunIndex]);
431 if (isNewRun) runsToKeep[f][oldRunIndex] = newRunIndex;
433 std::unique_ptr<Data> newData = tweaker.
tweak(*history->
data(k), newEvtIndex);
435 cout <<
"Filtering failed on data " << k <<
" of cell " << i <<
", aborting" << endl;
436 return std::vector<std::unique_ptr<TreeAccessor> >();
439 newHist.add(newData.release()->dissolve());
441 newAccessors[f]->add(&newHist);
445 for (
unsigned int f = 0; f < filterList.
size(); f++) {
446 cout <<
"Adding runs..." << endl;
447 std::vector<unsigned int> runsToKeep_ordered(runsToKeep[f].size());
448 for (
const auto& runIndex : runsToKeep[f])
449 runsToKeep_ordered[runIndex.second] = runIndex.first;
451 for (
unsigned int runIndex : runsToKeep_ordered) {
452 RunData newRun(*accessor.runData(runIndex));
453 newAccessors[f]->addRun(&newRun);
455 cout <<
"Adding events..." << endl;
456 std::vector<unsigned int> eventsToKeep_ordered(eventsToKeep[f].size());
457 for (
const auto& eventIndex : eventsToKeep[f])
458 eventsToKeep_ordered[eventIndex.second] = eventIndex.first;
460 for (
unsigned int eventIndex : eventsToKeep_ordered) {
461 std::map<unsigned int, unsigned int>::const_iterator idx = runsToKeep[f].find(accessor.eventData(eventIndex)->runIndex());
462 int newRunIndex = (idx == runsToKeep[f].end() ? 0 : idx->second);
463 std::unique_ptr<EventData> newEvent (tweaker.
tweak(*accessor.eventData(eventIndex), newRunIndex));
464 newAccessors[f]->addEvent(newEvent.get());
467 cout <<
"Filtering done! final size = " << nPass << endl;
469 for (
unsigned int f = 0; f < filterList.
size(); f++) {
470 cout <<
"Saving " << newAccessors[f]->fileName() << endl;
471 newAccessors[f]->save();
479 auto newAccessor = std::make_unique<TreeAccessor>(
fileName);
481 std::vector<short> samples(5, 0);
482 std::vector<float> autoCorrs(4, 0);
485 newAccessor->addRun(&dummyRun);
488 newAccessor->addEvent(&dummyEvent);
490 for (
unsigned int i = 0; i < accessor.nChannels(); i++) {
492 cout <<
"Templating " << i <<
"/" << accessor.nChannels() << endl;
493 const History* history = accessor.cellHistory(i);
494 if (!history || !history->
cellInfo()) {
496 newAccessor->add(&newHist);
500 auto dataContainer = std::make_unique<DataContainer>(
CaloGain::LARHIGHGAIN, samples, 0, 0, 0, 0, autoCorrs);
501 newHist.add(dataContainer.release());
502 newAccessor->add(&newHist);
const CellInfo * cellInfo() const
const Data * data(unsigned int i) const
unsigned int nData() const