ATLAS Offline Software
Loading...
Searching...
No Matches
Interface.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3*/
4
6
11#include "LArSamplesMon/Data.h"
13#include "LArCafJobs/RunData.h"
18
19#include "TFile.h"
20#include "TObjString.h"
21#include "TSystem.h"
22#include "TObjArray.h"
23#include "TObjString.h"
24#include <iomanip>
25#include <fstream>
26#include <map>
27
28#include <iostream>
29using std::cout;
30using std::endl;
31
32using namespace LArSamples;
33
34
35std::unique_ptr<Interface> Interface::open(const TString& fileName)
36{
37 std::unique_ptr<TreeAccessor> accessor = TreeAccessor::open(fileName);
38 if (accessor) {
39 return std::make_unique<Interface> (std::move(accessor));
40 }
41 return nullptr;
42}
43
44std::unique_ptr<Interface> Interface::open(const std::vector<TString>& fileNames)
45{
46 std::unique_ptr<MultiTreeAccessor> accessor = MultiTreeAccessor::open(fileNames);
47 if (accessor) {
48 return std::make_unique<Interface> (std::move(accessor));
49 }
50 return nullptr;
51}
52
53
54std::unique_ptr<Interface> Interface::openList(const TString& fileList)
55{
56 std::unique_ptr<MultiTreeAccessor> accessor = MultiTreeAccessor::openList(fileList);
57 if (accessor) {
58 return std::make_unique<Interface> (std::move(accessor));
59 }
60 return nullptr;
61}
62
63
64std::unique_ptr<Interface> Interface::openWild(const TString& wcName)
65{
66 std::unique_ptr<MultiTreeAccessor> accessor = MultiTreeAccessor::openWild(wcName);
67 if (accessor) {
68 return std::make_unique<Interface> (std::move(accessor));
69 }
70 return nullptr;
71}
72
73
74Interface::Interface(std::unique_ptr<const Accessor> accessor)
76{
77}
78
82
83
89
90
92{
93 if (k == 0) { m_shapeErrorGetter = nullptr; m_ownedShapeErrorGetter.reset(); return; }
94 m_ownedShapeErrorGetter = std::make_unique<UniformShapeErrorGetter>(k);
96}
97
98
99void Interface::setShapeError(const TString& fileName)
100{
102}
103
104
105unsigned int Interface::size() const
106{
107 unsigned int n = 0;
108 for (unsigned int i = 0; i < nChannels(); i++)
109 n += accessor().historySize(i);
110 return n;
111}
112
113
114std::unique_ptr<const History> Interface::getCellHistory(unsigned int i) const
115{
116 std::unique_ptr<const History> history = accessor().getCellHistory(i);
117 if (history) {
118 history->setShapeErrorGetter(m_shapeErrorGetter);
119 history->setInterface(this);
120 }
121 return history;
122}
123
124std::unique_ptr<const History> Interface::getSCHistory(unsigned int i) const
125{
126 std::unique_ptr<const History> history = accessor().getSCHistory(i);
127 if (history) {
128 history->setInterface(this);
129 }
130 return history;
131}
132
133
134const History* Interface::cellHistory(unsigned int i) const
135{
136 const History* history = accessor().cellHistory(i);
137 if (history) {
139 history->setInterface(this);
140 }
141 return history;
142}
143
144
145std::unique_ptr<const CellInfo> Interface::getCellInfo(unsigned int i) const
146{
147 return accessor().getCellInfo(i);
148}
149
150
151unsigned int Interface::nFilledChannels() const
152{
153 unsigned int n = 0;
154 for (unsigned int i = 0; i < nChannels(); i++)
155 n += (accessor().historySize(i) ? 1 : 0);
156 return n;
157}
158
159
160void Interface::printFilledRanges(unsigned int skip) const
161{
162 int i1 = 0, i2 = 0;
163 unsigned int nNull = 0;
164 unsigned int size = accessor().historySize(0);
165 bool ok = (size > 0);
166 bool inInterval = ok;
167
168 for (unsigned int i = 0; i < nChannels(); i++) {
170 if (inInterval && nNull == 0) i2 = i;
171 bool ok = (size > 0);
172 if (!ok && inInterval) {
173 nNull++;
174 if (nNull > skip) {
175 nNull = 0;
176 inInterval = false;
177 cout << i1 << "-" << i2 << endl;
178 }
179 }
180 if (ok) nNull = 0;
181 if (ok && !inInterval) {
182 inInterval = true;
183 i1 = i;
184 }
185 }
186
187 if (inInterval) cout << i1 << "-" << nChannels() << endl;
188}
189
190
191HistoryIterator Interface::begin(unsigned int pos, double eMin, double adcMaxMin) const
192{
193 return HistoryIterator(*this, pos, eMin, adcMaxMin);
194}
195
196
197bool Interface::highEData(double eCut, TArrayI& hashes, TArrayI& indices) const
198{
199 std::vector<unsigned int> hashV, indexV;
200 unsigned int nTot = 0;
201
202 for (unsigned int i = 0; i < nChannels(); i++) {
203 const History* history = cellHistory(i);
204 if (!history) continue;
205 for (unsigned int j = 0; j < history->nData(); j++) {
206 const Data& data = *history->data(j);
207 nTot++;
208 if (data.isDisconnected()) continue;
209 if (nTot % 10000 == 0) cout << nTot << endl;
210 if (data.energy() > eCut) {
211 cout << "E = " << data.energy() << " " << i << " " << j << endl;
212 hashV.push_back(i);
213 indexV.push_back(j);
214 }
215 }
216 }
217
218 hashes.Set(hashV.size());
219 indices.Set(indexV.size());
220
221 for (unsigned int i = 0; i < hashV.size(); i++) {
222 hashes[i] = hashV[i];
223 indices[i] = indexV[i];
224 }
225
226 cout << hashV.size() << "/" << nTot << endl;
227 return true;
228}
229
230
232{
233 for (unsigned int i = 0; i < nChannels(); i++) {
234 const History* history = cellHistory(i);
235 if (!history) continue;
236 if (!history->isValid()) {
237 cout << "Invalid LArSamplesHistory at hash = " << i << endl;
238 return false;
239 }
240 }
241
242 return true;
243}
244
245
246std::unique_ptr<Interface> Interface::merge(const Interface& other, const TString& fileName) const
247{
248 std::vector<const Interface*> interfaces { this, &other };
249 return merge(interfaces, fileName);
250}
251
252
253std::unique_ptr<Interface> Interface::merge(const std::vector<const Interface*>& interfaces, const TString& fileName)
254{
255 std::vector<const Accessor*> accessors;
256 for (unsigned int i = 0; i < interfaces.size(); i++)
257 accessors.push_back(&interfaces[i]->accessor());
258 std::unique_ptr<TreeAccessor> newAccessor = TreeAccessor::merge(accessors, fileName);
259 return std::make_unique<Interface>(std::move(newAccessor));
260}
261
262std::unique_ptr<Interface> Interface::merge(const Interface& other, const TString& fileName, const TString& LBFile) const
263{
264 std::vector<const Interface*> interfaces { this, &other };
265 return merge(interfaces, fileName, LBFile);
266}
267
268
269std::unique_ptr<Interface> Interface::merge(const std::vector<const Interface*>& interfaces, const TString& fileName, const TString& LBFile)
270{
271 std::vector<const Accessor*> accessors;
272 for (unsigned int i = 0; i < interfaces.size(); i++)
273 accessors.push_back(&interfaces[i]->accessor());
274 std::unique_ptr<TreeAccessor> newAccessor = TreeAccessor::merge(accessors, fileName, LBFile);
275 return std::make_unique<Interface>(std::move(newAccessor));
276}
277
278
279std::unique_ptr<Interface> Interface::merge(const TString& listFileName, const TString& fileName)
280{
281 std::unique_ptr<const Interface> multi = openList(listFileName);
282 if (!multi) return nullptr;
283 std::vector<const Interface*> justOne { multi.get() };
284 return merge(justOne, fileName);
285}
286
287std::unique_ptr<Interface> Interface::merge(const TString& listFileName, const TString& fileName, const TString& LBFile)
288{
289 std::unique_ptr<const Interface> multi = openList(listFileName);
290 if (!multi) return nullptr;
291 std::vector<const Interface*> justOne { multi.get() };
292 return merge(justOne, fileName, LBFile);
293}
294
295
296bool Interface::filterAndMerge(const TString& listFileName, const TString& outFile, const TString& filters, const TString& tweaks)
297{
298 FilterList filterList;
299
300 std::unique_ptr<TObjArray> list (filters.Tokenize(",;"));
301 if (list->GetEntries() == 0) {
302 cout << "No filtering specified, exiting.";
303 return 0;
304 }
305
306 for (int k = 0; k < list->GetEntries(); k++) {
307 TObjString* tobs = (TObjString*)(list->At(k));
308 std::unique_ptr<TObjArray> items (tobs->String().Tokenize(":"));
309 if (items->GetEntries() != 2) {
310 cout << "Invalid filter entry " << tobs->String() << ", exiting." << endl;
311 return 0;
312 }
313 TString params = ((TObjString*)(items->At(0)))->String();
314 TString suffix = ((TObjString*)(items->At(1)))->String();
315 FilterParams f;
316 if (!f.set(params)) return 0;
317 cout << "---" << endl;
318 filterList.add(f, addSuffix(outFile, suffix));
319 }
320
321
322 DataTweaker tweak;
323 if (!tweak.set(tweaks)) return 0;
324
325 std::unique_ptr<const Interface> multi = openList(listFileName);
326 if (!multi) return 0;
327 const MultiTreeAccessor* mt = dynamic_cast<const MultiTreeAccessor*>(&multi->accessor());
328 if (!mt){
329 return 0;
330 }
331 std::vector<std::unique_ptr<MultiTreeAccessor> > filtered_mts = mt->filterComponents(filterList, tweak);
332 if (filtered_mts.size() != filterList.size()){
333 return 0;
334 }
335 cout << "Component filtering done!" << endl;
336 // The following line should work, but doesn't... so the block of code below replaces it.
337 //Interface* filtered_multi = new Interface(*filtered_mt);
338 //
339 for (unsigned int f = 0; f < filtered_mts.size(); f++) {
340 std::vector<TString> files;
341 for (unsigned int i = 0; i < filtered_mts[f]->nAccessors(); i++) {
342 files.push_back(((const TreeAccessor*)&filtered_mts[f]->accessor(i))->fileName());
343 cout << "Added " << files.back() << endl;
344 }
345 std::unique_ptr<const Interface> filtered_multi = open(files);
346 //
347 std::vector<const Interface*> justOne { filtered_multi.get() };
348 std::unique_ptr<Interface> interface = merge(justOne, filterList.fileName(f));
349 }
350
351 return true;
352}
353
354
355std::unique_ptr<Interface> Interface::filter(const TString& sel, const TString& fileName, const TString& tweaks) const
356{
357 FilterParams f;
358 if (!f.set(sel)) return nullptr;
359
360 DataTweaker tweak;
361 if (!tweak.set(tweaks)) return nullptr;
362
363 TString thisFN = fileName;
364 if (thisFN.Index(".root") < 0 && dynamic_cast<const TreeAccessor*>(&accessor())) {
365 auto pAccess = dynamic_cast<const TreeAccessor*>(&accessor());
366 if (not pAccess) return nullptr;
367 TString newFN = addSuffix(pAccess->fileName(), fileName);
368 if (newFN != "") thisFN = std::move(newFN);
369 }
370 return filter(f, tweak, thisFN);
371}
372
373
374TString Interface::addSuffix(const TString& fileName, const TString& suffix)
375{
376 TString rootName = fileName;
377 if (fileName(fileName.Length() - 5, 5) == ".root") rootName = fileName(0, fileName.Length() - 5);
378
379 return rootName + "_" + suffix + ".root";
380}
381
382
383std::unique_ptr<Interface> Interface::filter(const FilterParams& filterParams, const DataTweaker& tweaker, const TString& fileName) const
384{
385 std::unique_ptr<TreeAccessor> newAcc = TreeAccessor::filter(accessor(), filterParams, fileName, tweaker);
386 return std::make_unique<Interface>(std::move(newAcc));
387}
388
389
390std::unique_ptr<Interface> Interface::makeTemplate(const TString& fileName) const
391{
392 std::unique_ptr<TreeAccessor> newAcc = TreeAccessor::makeTemplate(accessor(), fileName);
393 return std::make_unique<Interface>(std::move(newAcc));
394}
395
396
397std::unique_ptr<Interface> Interface::refit(const TString& newFileName, Chi2Params pars) const
398{
399 FilterParams f;
400 DataTweaker tw;
401 tw.setRefit(true);
402 tw.setFitParams(pars);
403 return filter(f, tw, newFileName);
404}
405
406
407HistoryIterator Interface::findEtaPhi(CaloId calo, short layer, short iEta, short iPhi, short region) const
408{
409 for (unsigned int i = 0; i < nChannels(); i++) {
410 std::unique_ptr<const CellInfo> info = cellInfo(i);
411 if (!info) continue;
412 if (!Id::matchCalo(info->calo(), calo)) continue;
413 if (info->layer() != layer) continue;
414 if (info->iEta() != iEta) continue;
415 if (info->iPhi() != iPhi) continue;
416 if (info->region() != region) continue;
417 return HistoryIterator(*this, i);
418 }
419
420 return HistoryIterator(*this, end());
421}
422
423
424HistoryIterator Interface::findFebChannel(CaloId calo, short feb, short channel) const
425{
426 for (unsigned int i = 0; i < nChannels(); i++) {
427 std::unique_ptr<const CellInfo> info = cellInfo(i);
428 if (!info) continue;
429 if (!Id::matchCalo(info->calo(), calo)) continue;
430 if (info->feb() != feb) continue;
431 if (info->channel() != channel) continue;
432 return HistoryIterator(*this, i);
433 }
434
435 return HistoryIterator(*this, end());
436}
437
438
439HistoryIterator Interface::findFTSlotChannel(CaloId calo, short ft, short slot, short channel) const
440{
441 for (unsigned int i = 0; i < nChannels(); i++) {
442 std::unique_ptr<const CellInfo> info = cellInfo(i);
443 if (!info) continue;
444 if (!Id::matchCalo(info->calo(), calo)) continue;
445 if (ft >= 0 && info->feedThrough() != ft) continue;
446 if (slot >= 0 && info->slot() != slot) continue;
447 if (channel >= 0 && info->channel() != channel) continue;
448 return HistoryIterator(*this, i);
449 }
450
451 return HistoryIterator(*this, end());
452}
453
454
455TH1D* Interface::Draw(const TString& var, int nBins, double xMin, double xMax, const TString& sel, const TString& opt) const
456{
457 MonitorBase m(*this);
458 FilterParams f;
459 if (!f.set(sel)) return nullptr;
460
461 std::vector<TString> vars;
462 std::vector<DataFuncSet> funcs;
463 std::vector<DataFuncArgs> args;
464 if (!MonitorBase::parseVariables(var, vars, funcs, args) || funcs.size() != 1) {
465 cout << "Invalid variable specification " << var << endl;
466 return nullptr;
467 }
468
469 TString title = vars[0];
470 if (TString(sel) != "") title = title + ", " + sel;
471
472 TH1D* h = m.dist(funcs[0], args[0], vars[0], nBins, xMin, xMax,
473 title, vars[0], "digits", f);
474 if (!h) return nullptr;
475 h->Draw(opt);
476 return h;
477}
478
479
480TH2D* Interface::Draw(const TString& varList, int nBinsX, double xMin, double xMax,
481 int nBinsY, double yMin, double yMax,
482 const TString& sel, const TString& opt) const
483{
484 MonitorBase m(*this);
485 FilterParams f;
486 if (!f.set(sel)) return nullptr;
487
488 std::vector<TString> vars;
489 std::vector<DataFuncSet> funcs;
490 std::vector<DataFuncArgs> args;
491 if (!MonitorBase::parseVariables(varList, vars, funcs, args) || funcs.size() != 2) {
492 cout << "Invalid variable specification " << varList << endl;
493 return nullptr;
494 }
495
496 TString title = vars[1] + " vs. " + vars[0];
497
498 if (TString(sel) != "") title = title + ", " + sel;
499 TH2D* h = m.dist(funcs[0], args[0], funcs[1], args[1], vars[0] + "_" + vars[1],
500 nBinsX, xMin, xMax, nBinsY, yMin, yMax,
501 title, vars[0], vars[1], f);
502 if (!h) return nullptr;
503 h->Draw(opt);
504 return h;
505}
506
507
508TH2D* Interface::DrawPartition(PartitionId partition, const TString& var,
509 const TString& sel, const TString& opt,
510 CombinationType comb) const
511{
512 MonitorBase m(*this);
513 FilterParams f;
514 if (!f.set(sel)) return nullptr;
515
516 std::vector<TString> vars;
517 std::vector<DataFuncSet> funcs;
518 std::vector<DataFuncArgs> args;
519 if (!MonitorBase::parseVariables(var, vars, funcs, args) || funcs.size() != 1) {
520 cout << "Invalid variable specification " << var << endl;
521 return nullptr;
522 }
523
524 TString title = var;
525 title = title + ", " + Id::str(partition);
526 if (TString(sel) != "") title = title + ", " + sel;
527
528 TH2D* h = m.partitionMap(funcs[0], args[0], var, partition, title, comb, f);
529 if (!h) return nullptr;
530 h->Draw(opt);
531 return h;
532}
533
534
535TH2D* Interface::DrawEtaPhi(CaloId calo, short layer, const TString& var,
536 const TString& sel, const TString& opt,
537 CombinationType comb) const
538{
539 MonitorBase m(*this);
540 FilterParams f;
541 if (!f.set(sel)) return nullptr;
542
543 std::vector<TString> vars;
544 std::vector<DataFuncSet> funcs;
545 std::vector<DataFuncArgs> args;
546 if (!MonitorBase::parseVariables(var, vars, funcs, args) || funcs.size() != 1) {
547 cout << "Invalid variable specification " << var << endl;
548 return nullptr;
549 }
550 TString title = var;
551 title += Form(", %s, layer %d", Id::str(calo).Data(), layer);
552 if (TString(sel) != "") title = title + ", " + sel;
553
554 TH2D* h = m.etaPhiMap(funcs[0], args[0], var, calo, layer, title, comb, f);
555 if (!h) return nullptr;
556 h->Draw(opt);
557 return h;
558}
559
560
561bool Interface::Scan(const TString& vars, const TString& sel, unsigned int verbosity) const
562{
563 MonitorBase m(*this);
564 FilterParams f;
565 if (!f.set(sel)) return 0;
566 return m.dump(vars, f, verbosity);
567}
568
569
570bool Interface::Scan(const TString& vars, CombinationType comb, const TString& sel, const TString& ranges, unsigned int verbosity) const
571{
572 MonitorBase m(*this);
573 FilterParams f;
574 if (!f.set(sel)) return 0;
575 return m.dump(vars, comb, f, ranges, verbosity);
576}
577
578
579bool Interface::Show(unsigned int hash, unsigned int verbosity) const
580{
581 const History* history = cellHistory(hash);
582 if (!history) return false;
583 cout << history->description(verbosity) << endl;
584 return true;
585}
586
587bool Interface::Show(const TString& sel, unsigned int verbosity) const
588{
589 FilterParams f;
590 if (!f.set(sel)) return false;
591 for (unsigned int i = 0; i < nChannels(); i++) {
592 const History* history = pass(i, f);
593 if (!history) continue;
594 std::unique_ptr<History> filtered = history->filter(sel);
595 TString hDesc = filtered->description(verbosity);
596 if (hDesc == "") continue;
597 cout << Form("Hash = %-5d : ", i) << hDesc
598 << "-----------------------------------------------------------------------------"
599 << endl;
600 }
601 return true;
602}
603
604
605bool Interface::ShowEvents(const TString& sel, unsigned int verbosity) const
606{
607 FilterParams f;
608 if (!f.set(sel)) return false;
609 std::map< std::pair<unsigned int, unsigned int>, unsigned int > eventCells;
610 std::map< std::pair<unsigned int, unsigned int>, double > eventEnergy;
611
612 if (verbosity & 8) {
613 for (unsigned int i = 0; i < nChannels(); i++) {
614 if ((i+1) % 50000 == 0) cout << "Cell # " << i+1 << "/" << nChannels() << endl;
615 const History* history = pass(i, f);
616 if (!history) continue;
617 for (unsigned int j = 0; j < history->nData(); j++) {
618 std::pair<unsigned int, unsigned int> ev = std::make_pair(history->data(j)->run(), history->data(j)->event());
619 eventCells[ev]++;
620 eventEnergy[ev] += history->data(j)->energy();
621 }
622 }
623 }
624 for (unsigned int i = 0; i < nEvents(); i++) {
625 const EventData* evtData = eventData(i);
626 std::pair<unsigned int, unsigned int> id(evtData->run(), evtData->event());
627 TString printout = Form("%d : ", i) + evtData->description(verbosity);
628 if (verbosity & 8)
629 printout += Form(" : %6d LAr hits, %7.1f MeV", eventCells[id], eventEnergy[id]);
630 cout << printout << endl;
631 }
632
633 return true;
634}
635
636
637bool Interface::ShowRuns(unsigned int verbosity) const
638{
639 std::map<unsigned int, unsigned int> events;
640
641 if (verbosity & 8) {
642 for (unsigned int i = 0; i < nEvents(); i++)
643 events[eventData(i)->run()]++;
644 }
645
646 for (unsigned int i = 0; i < nRuns(); i++) {
647 const RunData* rData = runData(i);
648 TString printout = rData->description(verbosity);
649 if (verbosity & 8)
650 printout += Form(" : %6d events", events[rData->run()]);
651 cout << printout << endl;
652 }
653
654 return true;
655}
656
657
658bool Interface::ShowStats(const TString& varList, const TString& sel, bool withErrors) const
659{
660 MonitorBase m(*this);
661 FilterParams f;
662 if (!f.set(sel)) return 0;
663
664 std::vector<TString> vars;
665 std::vector<DataFuncSet> funcs;
666 std::vector<DataFuncArgs> args;
667 if (!MonitorBase::parseVariables(varList, vars, funcs, args)) {
668 cout << "Invalid variable specification " << varList << endl;
669 return 0;
670 }
671
672 TVectorD mean(vars.size()), meanErr(vars.size());
673 TMatrixD covMatrix(vars.size(), vars.size()), covMatrixErr(vars.size(), vars.size());
674 if (!m.statParams(funcs, args, mean, meanErr, covMatrix, covMatrixErr, f)) return false;
675
676 if (!withErrors) {
677 cout << "---------------------------" << endl;
678 for (unsigned int i = 0; i < vars.size(); i++)
679 cout << Form("| %10s | %-9.4g |", vars[i].Data(), mean(i)) << endl;
680 cout << "---------------------------" << endl << endl;
681
682 cout << "| |";
683 for (unsigned int i = 0; i < vars.size(); i++) cout << " |";
684 cout << endl << "--------------";
685 for (unsigned int i = 0; i < vars.size(); i++) cout << "-------------";
686 cout << endl;
687 for (unsigned int i1 = 0; i1 < vars.size(); i1++) {
688 cout << Form("| %10s |", vars[i1].Data());
689 for (unsigned int i2 = 0; i2 < vars.size(); i2++)
690 cout << Form(" %-9.4g |", covMatrix(i1, i2));
691 cout << endl;
692 }
693 cout << "--------------";
694 for (unsigned int i = 0; i < vars.size(); i++) cout << "-------------";
695 cout << endl;
696 }
697 else {
698 cout << "---------------------------" << endl;
699 for (unsigned int i = 0; i < vars.size(); i++)
700 cout << Form("| %10s | %-9.4g +/- %-9.4g |", vars[i].Data(), mean(i), meanErr(i)) << endl;
701 cout << "---------------------------" << endl << endl;
702
703 cout << "| |";
704 for (unsigned int i = 0; i < vars.size(); i++) cout << " |";
705 cout << endl << "--------------";
706 for (unsigned int i = 0; i < vars.size(); i++) cout << "---------------------------";
707 cout << endl;
708 for (unsigned int i1 = 0; i1 < vars.size(); i1++) {
709 cout << Form("| %10s |", vars[i1].Data());
710 for (unsigned int i2 = 0; i2 < vars.size(); i2++)
711 cout << Form(" %-9.4g +/- %-9.4g |", covMatrix(i1, i2), covMatrixErr(i1, i2));
712 cout << endl;
713 }
714 cout << "--------------";
715 for (unsigned int i = 0; i < vars.size(); i++) cout << "---------------------------";
716 cout << endl;
717 }
718
719 return true;
720}
721
722
723bool Interface::neighbors(const CellInfo& cell, double dRCut, std::vector<unsigned int>& hashes) const
724{
725 for (unsigned int i = 0; i < nChannels(); i++) {
726 std::unique_ptr<const CellInfo> otherCell = cellInfo(i);
727 if (!otherCell) continue;
728 if (cell.position().DeltaR(otherCell->position()) > dRCut) continue;
729 //cout << "Adding hash = " << i << " " << otherCell->location(3) << endl;
730 hashes.push_back(i);
731 }
732 return true;
733}
734
735
736bool Interface::firstNeighbors(unsigned int hash, std::vector<unsigned int>& hashes, short layer) const
737{
738 std::unique_ptr<const CellInfo> cell = cellInfo(hash);
739 if (!cell) return true;
740 if (!Id::matchCalo(cell->calo(), HEC)) return false; // for now!
741 if (layer < 0) return true;
742 std::vector<unsigned int> allHashes;
743 CacheEntry_t& cache = m_neighborCache[hash];
744 if (!cache.first) {
745 if (!neighbors(*cell, 0.15, cache.second)) return false;
746 cache.first = true;
747 }
748 for (unsigned int h : cache.second) {
749 std::unique_ptr<const CellInfo> info = cellInfo(h);
750 if (!info) continue;
751 if (info->layer() == layer) hashes.push_back(h);
752 }
753 return true;
754}
755
756
757bool Interface::data(const std::vector<unsigned int>& hashes,const EventData& event,
758 std::vector<std::unique_ptr<const Data> >& data) const
759{
760 if (hashes != m_neighborHistoryPos) {
761 m_neighborHistories.clear();
762 m_neighborHistoryPos.clear();
763 for (std::vector<unsigned int>::const_iterator hash = hashes.begin(); hash != hashes.end(); ++hash) {
764 std::unique_ptr<const History> history = AbsLArCells::newCellHistory(*hash);// bypasses history caching in order not to invalidate cell
765 m_neighborHistories.emplace_back(std::move(history));
766 m_neighborHistoryPos.push_back(*hash);
767 }
768 }
769
770 for (const std::unique_ptr<const History>& history : m_neighborHistories) {
771 if (!history) continue;
772 const Data* dataForEvent = history->data_for_event(event);
773 if (dataForEvent) data.push_back(std::make_unique<Data>(*dataForEvent));
774 }
775 return true;
776}
777
778
779bool Interface::dumpEventTuple(const TString& variables, const TString& fileName) const
780{
781 std::vector<float> floatVars;
782 std::vector<int> intVars;
783 std::vector<std::vector<float> > floatVects;
784 std::vector<std::vector<int> > intVects;
785 std::map<TString, unsigned int> varIndex;
786
787 std::vector<TString> vars;
788 std::vector<DataFuncSet> funcs;
789 std::vector<DataFuncArgs> args;
790 if (!MonitorBase::parseVariables(variables, vars, funcs, args)) return false;
791
792 cout << "Making trees..." << endl;
793
794 std::unique_ptr<TFile> flatFile (TFile::Open(fileName + "_tmpFlatFile.root", "RECREATE"));
795 TTree flatTree = TTree("flatTree", "Flat tree");
796
797 std::unique_ptr<TFile> eventFile (TFile::Open(fileName, "RECREATE"));
798 TTree eventTree ("eventTree", "Event tree");
799
800 // Ensure that the contents of the vectors won't move.
801 intVars.reserve (vars.size());
802 intVects.reserve (vars.size());
803 floatVars.reserve (vars.size());
804 floatVects.reserve (vars.size());
805
806 for (unsigned int j = 0; j < vars.size(); j++) {
807 unsigned int index = 0;
808 if (funcs[j].isNull()) return false;
809 if (funcs[j].isInt()) {
810 index = intVars.size();
811 intVars.push_back(0);
812 intVects.push_back (std::vector<int>());
813 flatTree.Branch(vars[j], &intVars.back());
814 eventTree.Branch(vars[j], &intVects.back());
815 }
816 else {
817 index = floatVars.size();
818 floatVars.push_back(0);
819 floatVects.push_back(std::vector<float>());
820 flatTree.Branch(vars[j], &floatVars.back());
821 eventTree.Branch(vars[j], &floatVects.back());
822 }
823 varIndex[vars[j]] = index;
824 cout << vars[j] << " -> " << index << endl;
825 }
826 std::map< unsigned int, std::map< unsigned int, std::vector<long long> > > runEventIndices;
827 cout << "Making flat ntuple" << endl;
828 unsigned int count = 0;
829 for (HistoryIterator iter = begin(); iter.isValid(); iter.next()) {
830 const History* hist = iter.history();
831 count++;
832 if (count % 100 == 0) cout << "Processing entry " << count << " (hash = " << iter.pos() << "), size = " << hist->nData() << endl;
833 for (unsigned int k = 0; k < hist->nData(); k++) {
834 for (unsigned int j = 0; j < vars.size(); j++) {
835 if (funcs[j].isInt())
836 intVars[varIndex[vars[j]]] = int(funcs[j].intVal(*hist->data(k), args[j]));
837 else
838 floatVars[varIndex[vars[j]]] = funcs[j].doubleVal(*hist->data(k), args[j]);
839 }
840 runEventIndices[hist->data(k)->run()][hist->data(k)->event()].push_back(flatTree.GetEntries());
841 flatTree.Fill();
842 }
843 }
844
845 cout << "Making event tuple" << endl;
846 unsigned int runCount = 0;
847 for (const auto& run : runEventIndices) {
848 runCount++;
849 cout << "Processing run " << run.first << " (" << runCount << " of " << runEventIndices.size() << ")" << endl;
850 unsigned int eventCount = 0;
851 for (const auto& event : run.second) {
852 eventCount++;
853 if (eventCount % 1000 == 0)
854 cout << " processing event " << event.first << " (" << eventCount << " of " << run.second.size() << "), size = " << event.second.size() << endl;
855 for (std::vector<int>& v : intVects) v.clear();
856 for (std::vector<float>& v : floatVects) v.clear();
857 for (long long index : event.second) {
858 flatTree.GetEntry(index);
859 for (unsigned int j = 0; j < vars.size(); j++) {
860 size_t vi = varIndex[vars[j]];
861 if (funcs[j].isInt())
862 intVects[vi].push_back(intVars[vi]);
863 else
864 floatVects[vi].push_back(floatVars[vi]);
865 }
866 }
867 eventTree.Fill();
868 }
869 }
870
871 cout << "Writing data..." << endl;
872 flatFile->cd();
873 flatTree.Write();
874 eventFile->cd();
875 eventTree.Write();
876
877 cout << "Done!" << endl;
878 return true;
879}
@ Data
Definition BaseObject.h:11
virtual std::unique_ptr< const History > getCellHistory(unsigned int i) const =0
virtual std::unique_ptr< const History > getSCHistory(unsigned int i) const =0
virtual unsigned int nChannels() const
Definition AbsLArCells.h:35
const History * pass(unsigned int i, const FilterParams &f) const
virtual std::unique_ptr< const CellInfo > getCellInfo(unsigned int i) const
virtual const History * cellHistory(unsigned int i) const
virtual std::unique_ptr< const History > newCellHistory(unsigned int i) const
virtual std::unique_ptr< const CellInfo > cellInfo(unsigned int i) const
virtual unsigned int historySize(unsigned int i) const =0
void setRefit(bool refit=true)
Definition DataTweaker.h:39
void setFitParams(Chi2Params params)
Definition DataTweaker.h:40
bool set(const TString &tweaks)
double energy() const
Definition Data.h:110
int event() const
Definition Data.cxx:28
int run() const
Definition Data.cxx:27
TString description(unsigned int verbosity) const
void add(const FilterParams &params, const TString &fileName)
Definition FilterList.h:27
unsigned int size() const
Definition FilterList.h:29
storage of the time histories of all the cells
TString description(unsigned int verbosity=1) const
Definition History.cxx:553
bool isValid() const
Definition History.cxx:149
const Data * data(unsigned int i) const
Definition History.cxx:88
void setShapeErrorGetter(const AbsShapeErrorGetter *err) const
Definition History.h:93
unsigned int nData() const
Definition History.h:54
std::unique_ptr< History > filter(const TString &cuts) const
Definition History.cxx:272
void setInterface(const Interface *interface) const
Definition History.h:113
static TString str(CaloId id)
Definition CaloId.cxx:15
static bool matchCalo(CaloId id, CaloId idSpec)
Definition CaloId.cxx:188
std::pair< bool, std::vector< unsigned int > > CacheEntry_t
Definition Interface.h:142
Interface(std::unique_ptr< const Accessor > accessor)
Constructor.
Definition Interface.cxx:74
const Accessor & accessor() const
Definition Interface.h:96
const AbsShapeErrorGetter * m_shapeErrorGetter
Definition Interface.h:139
static std::unique_ptr< Interface > openWild(const TString &wcName)
Definition Interface.cxx:64
virtual std::unique_ptr< const CellInfo > getCellInfo(unsigned int i) const override
std::unique_ptr< Interface > filter(const TString &sel, const TString &fileName, const TString &tweaks="") const
std::unique_ptr< Interface > refit(const TString &newFileName, Chi2Params pars=DefaultChi2) const
virtual const EventData * eventData(unsigned int i) const override
Definition Interface.h:56
bool ShowRuns(unsigned int verbosity=1) const
bool data(const std::vector< unsigned int > &hashes, const EventData &event, std::vector< std::unique_ptr< const Data > > &data) const
std::vector< unsigned int > m_neighborHistoryPos
Definition Interface.h:144
static std::unique_ptr< Interface > open(const TString &fileName)
Definition Interface.cxx:35
static bool filterAndMerge(const TString &listFileName, const TString &outFile, const TString &filters, const TString &tweaks="")
virtual const RunData * runData(unsigned int i) const override
Definition Interface.h:57
TH2D * DrawPartition(PartitionId partition, const TString &var, const TString &sel="", const TString &opt="", CombinationType comb=TotalValue) const
HistoryIterator findEtaPhi(CaloId calo, short layer, short iEta, short iPhi, short region=0) const
virtual unsigned int nEvents() const override
Definition Interface.h:53
bool highEData(double eCut, TArrayI &hashes, TArrayI &indices) const
bool firstNeighbors(unsigned int hash, std::vector< unsigned int > &hashes, short layer=-2) const
std::vector< std::unique_ptr< const History > > m_neighborHistories
Definition Interface.h:145
bool neighbors(const CellInfo &cell, double dRCut, std::vector< unsigned int > &hashes) const
void printFilledRanges(unsigned int skip=0) const
virtual unsigned int historySize(unsigned int i) const override
Definition Interface.h:59
virtual const History * cellHistory(unsigned int i) const override
bool ShowEvents(const TString &sel="", unsigned int verbosity=1) const
virtual std::unique_ptr< const History > getSCHistory(unsigned int i) const override
std::unique_ptr< const AbsShapeErrorGetter > m_ownedShapeErrorGetter
Definition Interface.h:140
unsigned int nFilledChannels() const
TH2D * DrawEtaPhi(CaloId calo, short layer, const TString &var, const TString &sel="", const TString &opt="", CombinationType comb=TotalValue) const
unsigned int size() const
HistoryIterator begin(unsigned int pos=0, double eMin=-1, double adcMaxMin=-1) const
bool Scan(const TString &vars, const TString &sel="", unsigned int verbosity=1) const
bool ShowStats(const TString &varList, const TString &sel="", bool withErrors=false) const
virtual unsigned int nRuns() const override
Definition Interface.h:54
void setShapeError(double k)
Definition Interface.cxx:91
unsigned int end() const
Definition Interface.h:63
std::unique_ptr< Interface > merge(const Interface &other, const TString &fileName) const
std::vector< CacheEntry_t > m_neighborCache
Definition Interface.h:143
std::unique_ptr< Interface > makeTemplate(const TString &fileName) const
static TString addSuffix(const TString &fileName, const TString &suffix)
HistoryIterator findFTSlotChannel(CaloId calo, short ft, short slot, short channel) const
HistoryIterator findFebChannel(CaloId calo, short feb, short channel) const
TH1D * Draw(const TString &var, int nBins, double xMin, double xMax, const TString &sel="", const TString &opt="") const
std::unique_ptr< const Accessor > m_accessor
Definition Interface.h:138
static std::unique_ptr< Interface > openList(const TString &fileList)
Definition Interface.cxx:54
void setShapeErrorGetter(const AbsShapeErrorGetter *err)
Definition Interface.cxx:84
bool dumpEventTuple(const TString &variables, const TString &fileName) const
virtual std::unique_ptr< const History > getCellHistory(unsigned int i) const override
bool Show(unsigned int hash, unsigned int verbosity=1) const
static bool parseVariables(TString varStr, std::vector< TString > &vars, std::vector< DataFuncSet > &funcs, std::vector< DataFuncArgs > &args)
static std::unique_ptr< MultiTreeAccessor > open(const std::vector< TString > &files)
std::vector< std::unique_ptr< MultiTreeAccessor > > filterComponents(const FilterList &filterList, const DataTweaker &tweaker) const
static std::unique_ptr< MultiTreeAccessor > openList(const TString &fileList)
static std::unique_ptr< MultiTreeAccessor > openWild(const TString &wcName)
int run() const
Definition RunData.h:36
TString description(unsigned int verbosity) const
Definition RunData.cxx:56
static std::unique_ptr< TreeAccessor > merge(const std::vector< const Accessor * > &accessors, const TString &fileName="")
static std::unique_ptr< TreeAccessor > open(const TString &fileName)
static std::unique_ptr< TreeAccessor > makeTemplate(const Accessor &accessor, const TString &fileName)
static std::unique_ptr< TreeAccessor > filter(const Accessor &accessor, const FilterParams &filterParams, const TString &fileName, const DataTweaker &tweaker)
void mean(std::vector< double > &bins, std::vector< double > &values, const std::vector< std::string > &files, const std::string &histname, const std::string &tplotname, const std::string &label="")
int ev
Definition globals.cxx:25
std::vector< std::string > files
file names and file pointers
Definition hcg.cxx:50
int count(std::string s, const std::string &regx)
count how many occurances of a regx are in a string
Definition hcg.cxx:146
Definition index.py:1
Definition merge.py:1
STL namespace.
bool inInterval(const cool::ValidityKey n, const cool::ValidityKey from, const cool::ValidityKey to)
int run(int argc, char *argv[])