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
114const History* Interface::getCellHistory(unsigned int i) const
115{
116 const History* history = accessor().getCellHistory(i);
117 if (history) {
119 history->setInterface(this);
120 }
121 return history;
122}
123
124const History* Interface::getSCHistory(unsigned int i) const
125{
126 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
145const 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<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 delete filtered_mts[f];
346 std::unique_ptr<const Interface> filtered_multi = open(files);
347 //
348 std::vector<const Interface*> justOne { filtered_multi.get() };
349 std::unique_ptr<Interface> interface = merge(justOne, filterList.fileName(f));
350 }
351
352 return true;
353}
354
355
356std::unique_ptr<Interface> Interface::filter(const TString& sel, const TString& fileName, const TString& tweaks) const
357{
358 FilterParams f;
359 if (!f.set(sel)) return nullptr;
360
361 DataTweaker tweak;
362 if (!tweak.set(tweaks)) return nullptr;
363
364 TString thisFN = fileName;
365 if (thisFN.Index(".root") < 0 && dynamic_cast<const TreeAccessor*>(&accessor())) {
366 auto pAccess = dynamic_cast<const TreeAccessor*>(&accessor());
367 if (not pAccess) return nullptr;
368 TString newFN = addSuffix(pAccess->fileName(), fileName);
369 if (newFN != "") thisFN = std::move(newFN);
370 }
371 return filter(f, tweak, thisFN);
372}
373
374
375TString Interface::addSuffix(const TString& fileName, const TString& suffix)
376{
377 TString rootName = fileName;
378 if (fileName(fileName.Length() - 5, 5) == ".root") rootName = fileName(0, fileName.Length() - 5);
379
380 return rootName + "_" + suffix + ".root";
381}
382
383
384std::unique_ptr<Interface> Interface::filter(const FilterParams& filterParams, const DataTweaker& tweaker, const TString& fileName) const
385{
386 std::unique_ptr<TreeAccessor> newAcc = TreeAccessor::filter(accessor(), filterParams, fileName, tweaker);
387 return std::make_unique<Interface>(std::move(newAcc));
388}
389
390
391std::unique_ptr<Interface> Interface::makeTemplate(const TString& fileName) const
392{
393 std::unique_ptr<TreeAccessor> newAcc = TreeAccessor::makeTemplate(accessor(), fileName);
394 return std::make_unique<Interface>(std::move(newAcc));
395}
396
397
398std::unique_ptr<Interface> Interface::refit(const TString& newFileName, Chi2Params pars) const
399{
400 FilterParams f;
401 DataTweaker tw;
402 tw.setRefit(true);
403 tw.setFitParams(pars);
404 return filter(f, tw, newFileName);
405}
406
407
408HistoryIterator Interface::findEtaPhi(CaloId calo, short layer, short iEta, short iPhi, short region) const
409{
410 for (unsigned int i = 0; i < nChannels(); i++) {
411 const CellInfo* info = cellInfo(i);
412 if (!info) continue;
413 if (!Id::matchCalo(info->calo(), calo)) continue;
414 if (info->layer() != layer) continue;
415 if (info->iEta() != iEta) continue;
416 if (info->iPhi() != iPhi) continue;
417 if (info->region() != region) continue;
418 return HistoryIterator(*this, i);
419 }
420
421 return HistoryIterator(*this, end());
422}
423
424
425HistoryIterator Interface::findFebChannel(CaloId calo, short feb, short channel) const
426{
427 for (unsigned int i = 0; i < nChannels(); i++) {
428 const CellInfo* info = cellInfo(i);
429 if (!info) continue;
430 if (!Id::matchCalo(info->calo(), calo)) continue;
431 if (info->feb() != feb) continue;
432 if (info->channel() != channel) continue;
433 return HistoryIterator(*this, i);
434 }
435
436 return HistoryIterator(*this, end());
437}
438
439
440HistoryIterator Interface::findFTSlotChannel(CaloId calo, short ft, short slot, short channel) const
441{
442 for (unsigned int i = 0; i < nChannels(); i++) {
443 const CellInfo* info = cellInfo(i);
444 if (!info) continue;
445 if (!Id::matchCalo(info->calo(), calo)) continue;
446 if (ft >= 0 && info->feedThrough() != ft) continue;
447 if (slot >= 0 && info->slot() != slot) continue;
448 if (channel >= 0 && info->channel() != channel) continue;
449 return HistoryIterator(*this, i);
450 }
451
452 return HistoryIterator(*this, end());
453}
454
455
456TH1D* Interface::Draw(const TString& var, int nBins, double xMin, double xMax, const TString& sel, const TString& opt) const
457{
458 MonitorBase m(*this);
459 FilterParams f;
460 if (!f.set(sel)) return nullptr;
461
462 std::vector<TString> vars;
463 std::vector<DataFuncSet> funcs;
464 std::vector<DataFuncArgs> args;
465 if (!MonitorBase::parseVariables(var, vars, funcs, args) || funcs.size() != 1) {
466 cout << "Invalid variable specification " << var << endl;
467 return nullptr;
468 }
469
470 TString title = vars[0];
471 if (TString(sel) != "") title = title + ", " + sel;
472
473 TH1D* h = m.dist(funcs[0], args[0], vars[0], nBins, xMin, xMax,
474 title, vars[0], "digits", f);
475 if (!h) return nullptr;
476 h->Draw(opt);
477 return h;
478}
479
480
481TH2D* Interface::Draw(const TString& varList, int nBinsX, double xMin, double xMax,
482 int nBinsY, double yMin, double yMax,
483 const TString& sel, const TString& opt) const
484{
485 MonitorBase m(*this);
486 FilterParams f;
487 if (!f.set(sel)) return nullptr;
488
489 std::vector<TString> vars;
490 std::vector<DataFuncSet> funcs;
491 std::vector<DataFuncArgs> args;
492 if (!MonitorBase::parseVariables(varList, vars, funcs, args) || funcs.size() != 2) {
493 cout << "Invalid variable specification " << varList << endl;
494 return nullptr;
495 }
496
497 TString title = vars[1] + " vs. " + vars[0];
498
499 if (TString(sel) != "") title = title + ", " + sel;
500 TH2D* h = m.dist(funcs[0], args[0], funcs[1], args[1], vars[0] + "_" + vars[1],
501 nBinsX, xMin, xMax, nBinsY, yMin, yMax,
502 title, vars[0], vars[1], f);
503 if (!h) return nullptr;
504 h->Draw(opt);
505 return h;
506}
507
508
509TH2D* Interface::DrawPartition(PartitionId partition, const TString& var,
510 const TString& sel, const TString& opt,
511 CombinationType comb) const
512{
513 MonitorBase m(*this);
514 FilterParams f;
515 if (!f.set(sel)) return nullptr;
516
517 std::vector<TString> vars;
518 std::vector<DataFuncSet> funcs;
519 std::vector<DataFuncArgs> args;
520 if (!MonitorBase::parseVariables(var, vars, funcs, args) || funcs.size() != 1) {
521 cout << "Invalid variable specification " << var << endl;
522 return nullptr;
523 }
524
525 TString title = var;
526 title = title + ", " + Id::str(partition);
527 if (TString(sel) != "") title = title + ", " + sel;
528
529 TH2D* h = m.partitionMap(funcs[0], args[0], var, partition, title, comb, f);
530 if (!h) return nullptr;
531 h->Draw(opt);
532 return h;
533}
534
535
536TH2D* Interface::DrawEtaPhi(CaloId calo, short layer, const TString& var,
537 const TString& sel, const TString& opt,
538 CombinationType comb) const
539{
540 MonitorBase m(*this);
541 FilterParams f;
542 if (!f.set(sel)) return nullptr;
543
544 std::vector<TString> vars;
545 std::vector<DataFuncSet> funcs;
546 std::vector<DataFuncArgs> args;
547 if (!MonitorBase::parseVariables(var, vars, funcs, args) || funcs.size() != 1) {
548 cout << "Invalid variable specification " << var << endl;
549 return nullptr;
550 }
551 TString title = var;
552 title += Form(", %s, layer %d", Id::str(calo).Data(), layer);
553 if (TString(sel) != "") title = title + ", " + sel;
554
555 TH2D* h = m.etaPhiMap(funcs[0], args[0], var, calo, layer, title, comb, f);
556 if (!h) return nullptr;
557 h->Draw(opt);
558 return h;
559}
560
561
562bool Interface::Scan(const TString& vars, const TString& sel, unsigned int verbosity) const
563{
564 MonitorBase m(*this);
565 FilterParams f;
566 if (!f.set(sel)) return 0;
567 return m.dump(vars, f, verbosity);
568}
569
570
571bool Interface::Scan(const TString& vars, CombinationType comb, const TString& sel, const TString& ranges, unsigned int verbosity) const
572{
573 MonitorBase m(*this);
574 FilterParams f;
575 if (!f.set(sel)) return 0;
576 return m.dump(vars, comb, f, ranges, verbosity);
577}
578
579
580bool Interface::Show(unsigned int hash, unsigned int verbosity) const
581{
582 const History* history = cellHistory(hash);
583 if (!history) return false;
584 cout << history->description(verbosity) << endl;
585 return true;
586}
587
588bool Interface::Show(const TString& sel, unsigned int verbosity) const
589{
590 FilterParams f;
591 if (!f.set(sel)) return false;
592 for (unsigned int i = 0; i < nChannels(); i++) {
593 const History* history = pass(i, f);
594 if (!history) continue;
595 std::unique_ptr<History> filtered = history->filter(sel);
596 TString hDesc = filtered->description(verbosity);
597 if (hDesc == "") continue;
598 cout << Form("Hash = %-5d : ", i) << hDesc
599 << "-----------------------------------------------------------------------------"
600 << endl;
601 }
602 return true;
603}
604
605
606bool Interface::ShowEvents(const TString& sel, unsigned int verbosity) const
607{
608 FilterParams f;
609 if (!f.set(sel)) return false;
610 std::map< std::pair<unsigned int, unsigned int>, unsigned int > eventCells;
611 std::map< std::pair<unsigned int, unsigned int>, double > eventEnergy;
612
613 if (verbosity & 8) {
614 for (unsigned int i = 0; i < nChannels(); i++) {
615 if ((i+1) % 50000 == 0) cout << "Cell # " << i+1 << "/" << nChannels() << endl;
616 const History* history = pass(i, f);
617 if (!history) continue;
618 for (unsigned int j = 0; j < history->nData(); j++) {
619 std::pair<unsigned int, unsigned int> ev = std::make_pair(history->data(j)->run(), history->data(j)->event());
620 eventCells[ev]++;
621 eventEnergy[ev] += history->data(j)->energy();
622 }
623 }
624 }
625 for (unsigned int i = 0; i < nEvents(); i++) {
626 const EventData* evtData = eventData(i);
627 std::pair<unsigned int, unsigned int> id(evtData->run(), evtData->event());
628 TString printout = Form("%d : ", i) + evtData->description(verbosity);
629 if (verbosity & 8)
630 printout += Form(" : %6d LAr hits, %7.1f MeV", eventCells[id], eventEnergy[id]);
631 cout << printout << endl;
632 }
633
634 return true;
635}
636
637
638bool Interface::ShowRuns(unsigned int verbosity) const
639{
640 std::map<unsigned int, unsigned int> events;
641
642 if (verbosity & 8) {
643 for (unsigned int i = 0; i < nEvents(); i++)
644 events[eventData(i)->run()]++;
645 }
646
647 for (unsigned int i = 0; i < nRuns(); i++) {
648 const RunData* rData = runData(i);
649 TString printout = rData->description(verbosity);
650 if (verbosity & 8)
651 printout += Form(" : %6d events", events[rData->run()]);
652 cout << printout << endl;
653 }
654
655 return true;
656}
657
658
659bool Interface::ShowStats(const TString& varList, const TString& sel, bool withErrors) const
660{
661 MonitorBase m(*this);
662 FilterParams f;
663 if (!f.set(sel)) return 0;
664
665 std::vector<TString> vars;
666 std::vector<DataFuncSet> funcs;
667 std::vector<DataFuncArgs> args;
668 if (!MonitorBase::parseVariables(varList, vars, funcs, args)) {
669 cout << "Invalid variable specification " << varList << endl;
670 return 0;
671 }
672
673 TVectorD mean(vars.size()), meanErr(vars.size());
674 TMatrixD covMatrix(vars.size(), vars.size()), covMatrixErr(vars.size(), vars.size());
675 if (!m.statParams(funcs, args, mean, meanErr, covMatrix, covMatrixErr, f)) return false;
676
677 if (!withErrors) {
678 cout << "---------------------------" << endl;
679 for (unsigned int i = 0; i < vars.size(); i++)
680 cout << Form("| %10s | %-9.4g |", vars[i].Data(), mean(i)) << endl;
681 cout << "---------------------------" << endl << endl;
682
683 cout << "| |";
684 for (unsigned int i = 0; i < vars.size(); i++) cout << " |";
685 cout << endl << "--------------";
686 for (unsigned int i = 0; i < vars.size(); i++) cout << "-------------";
687 cout << endl;
688 for (unsigned int i1 = 0; i1 < vars.size(); i1++) {
689 cout << Form("| %10s |", vars[i1].Data());
690 for (unsigned int i2 = 0; i2 < vars.size(); i2++)
691 cout << Form(" %-9.4g |", covMatrix(i1, i2));
692 cout << endl;
693 }
694 cout << "--------------";
695 for (unsigned int i = 0; i < vars.size(); i++) cout << "-------------";
696 cout << endl;
697 }
698 else {
699 cout << "---------------------------" << endl;
700 for (unsigned int i = 0; i < vars.size(); i++)
701 cout << Form("| %10s | %-9.4g +/- %-9.4g |", vars[i].Data(), mean(i), meanErr(i)) << endl;
702 cout << "---------------------------" << endl << endl;
703
704 cout << "| |";
705 for (unsigned int i = 0; i < vars.size(); i++) cout << " |";
706 cout << endl << "--------------";
707 for (unsigned int i = 0; i < vars.size(); i++) cout << "---------------------------";
708 cout << endl;
709 for (unsigned int i1 = 0; i1 < vars.size(); i1++) {
710 cout << Form("| %10s |", vars[i1].Data());
711 for (unsigned int i2 = 0; i2 < vars.size(); i2++)
712 cout << Form(" %-9.4g +/- %-9.4g |", covMatrix(i1, i2), covMatrixErr(i1, i2));
713 cout << endl;
714 }
715 cout << "--------------";
716 for (unsigned int i = 0; i < vars.size(); i++) cout << "---------------------------";
717 cout << endl;
718 }
719
720 return true;
721}
722
723
724bool Interface::neighbors(const CellInfo& cell, double dRCut, std::vector<unsigned int>& hashes) const
725{
726 for (unsigned int i = 0; i < nChannels(); i++) {
727 const CellInfo* otherCell = cellInfo(i);
728 if (!otherCell) continue;
729 if (cell.position().DeltaR(otherCell->position()) > dRCut) { delete otherCell; continue; }
730 //cout << "Adding hash = " << i << " " << otherCell->location(3) << endl;
731 hashes.push_back(i);
732 delete otherCell;
733 }
734 return true;
735}
736
737
738bool Interface::firstNeighbors(unsigned int hash, std::vector<unsigned int>& hashes, short layer) const
739{
740 const CellInfo* cell = cellInfo(hash);
741 if (!cell) return true;
742 if (!Id::matchCalo(cell->calo(), HEC)) return false; // for now!
743 if (layer < 0) return true;
744 std::vector<unsigned int> allHashes;
745 CacheEntry_t& cache = m_neighborCache[hash];
746 if (!cache.first) {
747 if (!neighbors(*cell, 0.15, cache.second)) return false;
748 cache.first = true;
749 }
750 for (unsigned int h : cache.second) {
751 const CellInfo* info = cellInfo(h);
752 if (!info) continue;
753 if (info->layer() == layer) hashes.push_back(h);
754 delete info;
755 }
756 return true;
757}
758
759
760bool Interface::data(const std::vector<unsigned int>& hashes,const EventData& event, std::vector<const Data*>& data) const
761{
762 if (hashes != m_neighborHistoryPos) {
763 m_neighborHistories.clear();
764 m_neighborHistoryPos.clear();
765 for (std::vector<unsigned int>::const_iterator hash = hashes.begin(); hash != hashes.end(); ++hash) {
766 const History* history = AbsLArCells::newCellHistory(*hash);// bypasses history caching in order not to invalidate cell
767 m_neighborHistories.emplace_back(history);
768 m_neighborHistoryPos.push_back(*hash);
769 }
770 }
771
772 for (const std::unique_ptr<const History>& history : m_neighborHistories) {
773 if (!history) continue;
774 const Data* dataForEvent = history->data_for_event(event);
775 if (dataForEvent) data.push_back(new Data(*dataForEvent));
776 }
777 return true;
778}
779
780
781bool Interface::dumpEventTuple(const TString& variables, const TString& fileName) const
782{
783 std::vector<float> floatVars;
784 std::vector<int> intVars;
785 std::vector<std::vector<float> > floatVects;
786 std::vector<std::vector<int> > intVects;
787 std::map<TString, unsigned int> varIndex;
788
789 std::vector<TString> vars;
790 std::vector<DataFuncSet> funcs;
791 std::vector<DataFuncArgs> args;
792 if (!MonitorBase::parseVariables(variables, vars, funcs, args)) return false;
793
794 cout << "Making trees..." << endl;
795
796 std::unique_ptr<TFile> flatFile (TFile::Open(fileName + "_tmpFlatFile.root", "RECREATE"));
797 TTree flatTree = TTree("flatTree", "Flat tree");
798
799 std::unique_ptr<TFile> eventFile (TFile::Open(fileName, "RECREATE"));
800 TTree eventTree ("eventTree", "Event tree");
801
802 // Ensure that the contents of the vectors won't move.
803 intVars.reserve (vars.size());
804 intVects.reserve (vars.size());
805 floatVars.reserve (vars.size());
806 floatVects.reserve (vars.size());
807
808 for (unsigned int j = 0; j < vars.size(); j++) {
809 unsigned int index = 0;
810 if (funcs[j].isNull()) return false;
811 if (funcs[j].isInt()) {
812 index = intVars.size();
813 intVars.push_back(0);
814 intVects.push_back (std::vector<int>());
815 flatTree.Branch(vars[j], &intVars.back());
816 eventTree.Branch(vars[j], &intVects.back());
817 }
818 else {
819 index = floatVars.size();
820 floatVars.push_back(0);
821 floatVects.push_back(std::vector<float>());
822 flatTree.Branch(vars[j], &floatVars.back());
823 eventTree.Branch(vars[j], &floatVects.back());
824 }
825 varIndex[vars[j]] = index;
826 cout << vars[j] << " -> " << index << endl;
827 }
828 std::map< unsigned int, std::map< unsigned int, std::vector<long long> > > runEventIndices;
829 cout << "Making flat ntuple" << endl;
830 unsigned int count = 0;
831 for (HistoryIterator iter = begin(); iter.isValid(); iter.next()) {
832 const History* hist = iter.history();
833 count++;
834 if (count % 100 == 0) cout << "Processing entry " << count << " (hash = " << iter.pos() << "), size = " << hist->nData() << endl;
835 for (unsigned int k = 0; k < hist->nData(); k++) {
836 for (unsigned int j = 0; j < vars.size(); j++) {
837 if (funcs[j].isInt())
838 intVars[varIndex[vars[j]]] = int(funcs[j].intVal(*hist->data(k), args[j]));
839 else
840 floatVars[varIndex[vars[j]]] = funcs[j].doubleVal(*hist->data(k), args[j]);
841 }
842 runEventIndices[hist->data(k)->run()][hist->data(k)->event()].push_back(flatTree.GetEntries());
843 flatTree.Fill();
844 }
845 }
846
847 cout << "Making event tuple" << endl;
848 unsigned int runCount = 0;
849 for (const auto& run : runEventIndices) {
850 runCount++;
851 cout << "Processing run " << run.first << " (" << runCount << " of " << runEventIndices.size() << ")" << endl;
852 unsigned int eventCount = 0;
853 for (const auto& event : run.second) {
854 eventCount++;
855 if (eventCount % 1000 == 0)
856 cout << " processing event " << event.first << " (" << eventCount << " of " << run.second.size() << "), size = " << event.second.size() << endl;
857 for (std::vector<int>& v : intVects) v.clear();
858 for (std::vector<float>& v : floatVects) v.clear();
859 for (long long index : event.second) {
860 flatTree.GetEntry(index);
861 for (unsigned int j = 0; j < vars.size(); j++) {
862 size_t vi = varIndex[vars[j]];
863 if (funcs[j].isInt())
864 intVects[vi].push_back(intVars[vi]);
865 else
866 floatVects[vi].push_back(floatVars[vi]);
867 }
868 }
869 eventTree.Fill();
870 }
871 }
872
873 cout << "Writing data..." << endl;
874 flatFile->cd();
875 flatTree.Write();
876 eventFile->cd();
877 eventTree.Write();
878
879 cout << "Done!" << endl;
880 return true;
881}
@ Data
Definition BaseObject.h:11
virtual unsigned int nChannels() const
Definition AbsLArCells.h:34
virtual const CellInfo * getCellInfo(unsigned int i) const
const History * pass(unsigned int i, const FilterParams &f) const
virtual const History * cellHistory(unsigned int i) const
virtual const History * getSCHistory(unsigned int i) const =0
virtual const History * newCellHistory(unsigned int i) const
virtual const History * getCellHistory(unsigned int i) const =0
virtual const CellInfo * cellInfo(unsigned int i) const
virtual unsigned int historySize(unsigned int i) const =0
TVector3 position() const
Definition CellInfo.cxx:204
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:108
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:574
bool isValid() const
Definition History.cxx:152
const Data * data(unsigned int i) const
Definition History.cxx:91
void setShapeErrorGetter(const AbsShapeErrorGetter *err) const
Definition History.h:88
unsigned int nData() const
Definition History.h:52
std::unique_ptr< History > filter(const TString &cuts) const
Definition History.cxx:282
void setInterface(const Interface *interface) const
Definition History.h:108
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
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
bool ShowRuns(unsigned int verbosity=1) const
unsigned int nRuns() const
Definition Interface.h:54
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="")
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
bool highEData(double eCut, TArrayI &hashes, TArrayI &indices) const
bool firstNeighbors(unsigned int hash, std::vector< unsigned int > &hashes, short layer=-2) const
const CellInfo * getCellInfo(unsigned int i) 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
bool ShowEvents(const TString &sel="", unsigned int verbosity=1) const
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
bool data(const std::vector< unsigned int > &hashes, const EventData &event, std::vector< const Data * > &data) const
const EventData * eventData(unsigned int i) const
Definition Interface.h:56
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
const History * getCellHistory(unsigned int i) const
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
const History * getSCHistory(unsigned int i) 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
unsigned int nEvents() const
Definition Interface.h:53
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
unsigned int historySize(unsigned int i) const
Definition Interface.h:59
bool dumpEventTuple(const TString &variables, const TString &fileName) const
const History * cellHistory(unsigned int i) const
bool Show(unsigned int hash, unsigned int verbosity=1) const
const RunData * runData(unsigned int i) const
Definition Interface.h:57
static bool parseVariables(TString varStr, std::vector< TString > &vars, std::vector< DataFuncSet > &funcs, std::vector< DataFuncArgs > &args)
std::vector< MultiTreeAccessor * > filterComponents(const FilterList &filterList, const DataTweaker &tweaker) const
static std::unique_ptr< MultiTreeAccessor > open(const std::vector< TString > &files)
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[])