20#include "TObjString.h"
23#include "TObjString.h"
39 return std::make_unique<Interface> (std::move(
accessor));
48 return std::make_unique<Interface> (std::move(
accessor));
58 return std::make_unique<Interface> (std::move(
accessor));
68 return std::make_unique<Interface> (std::move(
accessor));
108 for (
unsigned int i = 0; i <
nChannels(); i++)
154 for (
unsigned int i = 0; i <
nChannels(); i++)
163 unsigned int nNull = 0;
165 bool ok = (
size > 0);
168 for (
unsigned int i = 0; i <
nChannels(); i++) {
171 bool ok = (
size > 0);
177 cout << i1 <<
"-" << i2 << endl;
199 std::vector<unsigned int> hashV, indexV;
200 unsigned int nTot = 0;
202 for (
unsigned int i = 0; i <
nChannels(); i++) {
204 if (!history)
continue;
205 for (
unsigned int j = 0; j < history->
nData(); j++) {
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;
218 hashes.Set(hashV.size());
219 indices.Set(indexV.size());
221 for (
unsigned int i = 0; i < hashV.size(); i++) {
222 hashes[i] = hashV[i];
223 indices[i] = indexV[i];
226 cout << hashV.size() <<
"/" << nTot << endl;
233 for (
unsigned int i = 0; i <
nChannels(); i++) {
235 if (!history)
continue;
237 cout <<
"Invalid LArSamplesHistory at hash = " << i << endl;
248 std::vector<const Interface*> interfaces {
this, &other };
249 return merge(interfaces, fileName);
253std::unique_ptr<Interface>
Interface::merge(
const std::vector<const Interface*>& interfaces,
const TString& fileName)
255 std::vector<const Accessor*> accessors;
256 for (
unsigned int i = 0; i < interfaces.size(); i++)
257 accessors.push_back(&interfaces[i]->accessor());
259 return std::make_unique<Interface>(std::move(newAccessor));
264 std::vector<const Interface*> interfaces {
this, &other };
265 return merge(interfaces, fileName, LBFile);
269std::unique_ptr<Interface>
Interface::merge(
const std::vector<const Interface*>& interfaces,
const TString& fileName,
const TString& LBFile)
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));
279std::unique_ptr<Interface>
Interface::merge(
const TString& listFileName,
const TString& fileName)
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);
287std::unique_ptr<Interface>
Interface::merge(
const TString& listFileName,
const TString& fileName,
const TString& LBFile)
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);
300 std::unique_ptr<TObjArray> list (filters.Tokenize(
",;"));
301 if (list->GetEntries() == 0) {
302 cout <<
"No filtering specified, exiting.";
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;
313 TString params = ((TObjString*)(items->At(0)))->String();
314 TString suffix = ((TObjString*)(items->At(1)))->String();
316 if (!f.set(params))
return 0;
317 cout <<
"---" << endl;
323 if (!tweak.
set(tweaks))
return 0;
325 std::unique_ptr<const Interface> multi =
openList(listFileName);
326 if (!multi)
return 0;
331 std::vector<MultiTreeAccessor*> filtered_mts = mt->
filterComponents(filterList, tweak);
332 if (filtered_mts.size() != filterList.
size()){
335 cout <<
"Component filtering done!" << endl;
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++) {
343 cout <<
"Added " <<
files.back() << endl;
345 delete filtered_mts[f];
346 std::unique_ptr<const Interface> filtered_multi =
open(
files);
348 std::vector<const Interface*> justOne { filtered_multi.get() };
349 std::unique_ptr<Interface>
interface =
merge(justOne, filterList.fileName(f));
356std::unique_ptr<Interface>
Interface::filter(
const TString&
sel,
const TString& fileName,
const TString& tweaks)
const
359 if (!f.set(
sel))
return nullptr;
362 if (!tweak.
set(tweaks))
return nullptr;
364 TString thisFN = fileName;
367 if (not pAccess)
return nullptr;
368 TString newFN =
addSuffix(pAccess->fileName(), fileName);
369 if (newFN !=
"") thisFN = std::move(newFN);
371 return filter(f, tweak, thisFN);
377 TString rootName = fileName;
378 if (fileName(fileName.Length() - 5, 5) ==
".root") rootName = fileName(0, fileName.Length() - 5);
380 return rootName +
"_" + suffix +
".root";
387 return std::make_unique<Interface>(std::move(newAcc));
394 return std::make_unique<Interface>(std::move(newAcc));
404 return filter(f, tw, newFileName);
410 for (
unsigned int i = 0; i <
nChannels(); i++) {
414 if (info->layer() != layer)
continue;
415 if (info->iEta() != iEta)
continue;
416 if (info->iPhi() != iPhi)
continue;
417 if (info->region() != region)
continue;
427 for (
unsigned int i = 0; i <
nChannels(); i++) {
431 if (info->feb() != feb)
continue;
432 if (info->channel() != channel)
continue;
442 for (
unsigned int i = 0; i <
nChannels(); i++) {
446 if (ft >= 0 && info->feedThrough() != ft)
continue;
447 if (slot >= 0 && info->slot() != slot)
continue;
448 if (channel >= 0 && info->channel() != channel)
continue;
456TH1D*
Interface::Draw(
const TString& var,
int nBins,
double xMin,
double xMax,
const TString&
sel,
const TString& opt)
const
460 if (!f.set(
sel))
return nullptr;
462 std::vector<TString> vars;
463 std::vector<DataFuncSet> funcs;
464 std::vector<DataFuncArgs> args;
466 cout <<
"Invalid variable specification " << var << endl;
470 TString title = vars[0];
471 if (TString(
sel) !=
"") title = title +
", " +
sel;
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;
482 int nBinsY,
double yMin,
double yMax,
483 const TString&
sel,
const TString& opt)
const
487 if (!f.set(
sel))
return nullptr;
489 std::vector<TString> vars;
490 std::vector<DataFuncSet> funcs;
491 std::vector<DataFuncArgs> args;
493 cout <<
"Invalid variable specification " << varList << endl;
497 TString title = vars[1] +
" vs. " + vars[0];
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;
510 const TString&
sel,
const TString& opt,
515 if (!f.set(
sel))
return nullptr;
517 std::vector<TString> vars;
518 std::vector<DataFuncSet> funcs;
519 std::vector<DataFuncArgs> args;
521 cout <<
"Invalid variable specification " << var << endl;
526 title = title +
", " +
Id::str(partition);
527 if (TString(
sel) !=
"") title = title +
", " +
sel;
529 TH2D*
h = m.partitionMap(funcs[0], args[0], var, partition, title, comb, f);
530 if (!
h)
return nullptr;
537 const TString&
sel,
const TString& opt,
542 if (!f.set(
sel))
return nullptr;
544 std::vector<TString> vars;
545 std::vector<DataFuncSet> funcs;
546 std::vector<DataFuncArgs> args;
548 cout <<
"Invalid variable specification " << var << endl;
552 title += Form(
", %s, layer %d",
Id::str(calo).
Data(), layer);
553 if (TString(
sel) !=
"") title = title +
", " +
sel;
555 TH2D*
h = m.etaPhiMap(funcs[0], args[0], var, calo, layer, title, comb, f);
556 if (!
h)
return nullptr;
566 if (!f.set(
sel))
return 0;
567 return m.dump(vars, f, verbosity);
575 if (!f.set(
sel))
return 0;
576 return m.dump(vars, comb, f, ranges, verbosity);
583 if (!history)
return false;
591 if (!f.set(
sel))
return false;
592 for (
unsigned int i = 0; i <
nChannels(); i++) {
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 <<
"-----------------------------------------------------------------------------"
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;
614 for (
unsigned int i = 0; i <
nChannels(); i++) {
615 if ((i+1) % 50000 == 0) cout <<
"Cell # " << i+1 <<
"/" <<
nChannels() << endl;
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());
625 for (
unsigned int i = 0; i <
nEvents(); i++) {
627 std::pair<unsigned int, unsigned int> id(evtData->
run(), evtData->
event());
628 TString printout = Form(
"%d : ", i) + evtData->
description(verbosity);
630 printout += Form(
" : %6d LAr hits, %7.1f MeV", eventCells[
id], eventEnergy[
id]);
631 cout << printout << endl;
640 std::map<unsigned int, unsigned int> events;
643 for (
unsigned int i = 0; i <
nEvents(); i++)
647 for (
unsigned int i = 0; i <
nRuns(); i++) {
651 printout += Form(
" : %6d events", events[rData->
run()]);
652 cout << printout << endl;
663 if (!f.set(
sel))
return 0;
665 std::vector<TString> vars;
666 std::vector<DataFuncSet> funcs;
667 std::vector<DataFuncArgs> args;
669 cout <<
"Invalid variable specification " << varList << endl;
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;
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;
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 <<
"-------------";
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));
694 cout <<
"--------------";
695 for (
unsigned int i = 0; i < vars.size(); i++) cout <<
"-------------";
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;
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 <<
"---------------------------";
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));
715 cout <<
"--------------";
716 for (
unsigned int i = 0; i < vars.size(); i++) cout <<
"---------------------------";
726 for (
unsigned int i = 0; i <
nChannels(); i++) {
728 if (!otherCell)
continue;
729 if (cell.position().DeltaR(otherCell->
position()) > dRCut) {
delete otherCell;
continue; }
741 if (!cell)
return true;
743 if (layer < 0)
return true;
744 std::vector<unsigned int> allHashes;
747 if (!
neighbors(*cell, 0.15, cache.second))
return false;
750 for (
unsigned int h : cache.second) {
753 if (info->layer() == layer) hashes.push_back(
h);
765 for (std::vector<unsigned int>::const_iterator hash = hashes.begin(); hash != hashes.end(); ++hash) {
773 if (!history)
continue;
774 const Data* dataForEvent = history->data_for_event(event);
775 if (dataForEvent)
data.push_back(
new Data(*dataForEvent));
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;
789 std::vector<TString> vars;
790 std::vector<DataFuncSet> funcs;
791 std::vector<DataFuncArgs> args;
794 cout <<
"Making trees..." << endl;
796 std::unique_ptr<TFile> flatFile (TFile::Open(fileName +
"_tmpFlatFile.root",
"RECREATE"));
797 TTree flatTree = TTree(
"flatTree",
"Flat tree");
799 std::unique_ptr<TFile> eventFile (TFile::Open(fileName,
"RECREATE"));
800 TTree eventTree (
"eventTree",
"Event tree");
803 intVars.reserve (vars.size());
804 intVects.reserve (vars.size());
805 floatVars.reserve (vars.size());
806 floatVects.reserve (vars.size());
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());
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());
825 varIndex[vars[j]] =
index;
826 cout << vars[j] <<
" -> " <<
index << endl;
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;
832 const History* hist = iter.history();
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]));
840 floatVars[varIndex[vars[j]]] = funcs[j].doubleVal(*hist->data(k), args[j]);
842 runEventIndices[hist->data(k)->run()][hist->data(k)->event()].push_back(flatTree.GetEntries());
847 cout <<
"Making event tuple" << endl;
848 unsigned int runCount = 0;
849 for (
const auto&
run : runEventIndices) {
851 cout <<
"Processing run " <<
run.first <<
" (" << runCount <<
" of " << runEventIndices.size() <<
")" << endl;
852 unsigned int eventCount = 0;
853 for (
const auto& event :
run.second) {
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]);
866 floatVects[vi].push_back(floatVars[vi]);
873 cout <<
"Writing data..." << endl;
879 cout <<
"Done!" << endl;
virtual unsigned int nChannels() const
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
void setRefit(bool refit=true)
void setFitParams(Chi2Params params)
bool set(const TString &tweaks)
TString description(unsigned int verbosity) const
void add(const FilterParams ¶ms, const TString &fileName)
unsigned int size() const
storage of the time histories of all the cells
TString description(unsigned int verbosity=1) const
const Data * data(unsigned int i) const
void setShapeErrorGetter(const AbsShapeErrorGetter *err) const
unsigned int nData() const
std::unique_ptr< History > filter(const TString &cuts) const
void setInterface(const Interface *interface) const
static TString str(CaloId id)
static bool matchCalo(CaloId id, CaloId idSpec)
std::pair< bool, std::vector< unsigned int > > CacheEntry_t
Interface(std::unique_ptr< const Accessor > accessor)
Constructor.
const Accessor & accessor() const
const AbsShapeErrorGetter * m_shapeErrorGetter
static std::unique_ptr< Interface > openWild(const TString &wcName)
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
std::vector< unsigned int > m_neighborHistoryPos
static std::unique_ptr< Interface > open(const TString &fileName)
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
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
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
void setShapeError(double k)
std::unique_ptr< Interface > merge(const Interface &other, const TString &fileName) const
std::vector< CacheEntry_t > m_neighborCache
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
std::unique_ptr< const Accessor > m_accessor
static std::unique_ptr< Interface > openList(const TString &fileList)
void setShapeErrorGetter(const AbsShapeErrorGetter *err)
unsigned int historySize(unsigned int i) const
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
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)
TString description(unsigned int verbosity) const
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="")
std::vector< std::string > files
file names and file pointers
int count(std::string s, const std::string ®x)
count how many occurances of a regx are in a string
bool inInterval(const cool::ValidityKey n, const cool::ValidityKey from, const cool::ValidityKey to)
int run(int argc, char *argv[])