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