50TH1* utils::getBinInfoXY(
int& binx,
int& biny,
float& bincx,
float& bincy)
52 TObject* select = gPad->GetSelected();
56 if (!select->InheritsFrom(
"TH2")) {
61 TH1*
h = (TH1*)select;
62 gPad->GetCanvas()->FeedbackMode(kTRUE);
64 const int px = gPad->GetEventX();
65 const int py = gPad->GetEventY();
66 const float xx = gPad->AbsPixeltoX(px);
67 const float x = gPad->PadtoX(xx);
68 const float yy = gPad->AbsPixeltoY(py);
69 const float y = gPad->PadtoX(yy);
70 binx =
h->GetXaxis()->FindBin(
x);
71 biny =
h->GetYaxis()->FindBin(
y);
72 bincx =
h->GetXaxis()->GetBinCenter(binx);
73 bincy =
h->GetYaxis()->GetBinCenter(biny);
78void utils::addFECInfo()
81 const int event = gPad->GetEvent();
88 TH1*
h = utils::getBinInfoXY(binx, biny, bincx, bincy);
93 const float binValue =
h->GetBinContent(binx, biny);
94 const int row =
int(std::floor(bincx));
95 const int cpad =
int(std::floor(bincy));
99 const int roc =
h->GetUniqueID();
103 if (row < 0 || row >= (
int)mapper.getNumberOfRowsROC(
roc)) {
106 const int nPads = mapper.getNumberOfPadsInRowROC(
roc,
row);
107 const int pad = cpad + nPads / 2;
108 if (pad < 0 || pad >= (
int)nPads) {
111 const int channel = mapper.getPadNumberInROC(
PadROCPos(
roc,
row, pad));
115 const int cruNumber = mapper.getCRU(
ROC(
roc).
getSector(), channel + padOffset);
116 const CRU cru(cruNumber);
119 const int fecOffset = (nFECs + 1) / 2;
121 const int dataWrapperID = fecInPartition >= fecOffset;
122 const int globalLinkID = (fecInPartition % fecOffset) + dataWrapperID * 12;
124 const std::string title = fmt::format(
125 "#splitline{{#lower[.1]{{#scale[.5]{{"
126 "{}{:02d} ({:02d}) row: {:02d}, pad: {:03d}, globalpad: {:05d} (in roc)"
127 "}}}}}}{{#scale[.5]{{FEC: "
128 "{:02d}, Chip: {:02d}, Chn: {:02d}, CRU: {:d}, Link: {:02d} ({}{:02d}), Value: {:.3f}"
130 (
roc / 18 % 2 == 0) ?
"A" :
"C",
roc % 18,
roc,
row, pad, channel, fecInfo.getIndex(), fecInfo.getSampaChip(),
131 fecInfo.getSampaChannel(), cruNumber %
CRU::CRUperSector, globalLinkID, dataWrapperID ?
"B" :
"A", globalLinkID % 12, binValue);
133 h->SetTitle(title.data());
136void utils::saveCanvases(TObjArray& arr, std::string_view outDir, std::string_view
types, std::string_view singleOutFileName, std::string nameAdd)
140 utils::saveCanvas(*
static_cast<TCanvas*
>(
c), outDir,
types, nameAdd);
144 if (singleOutFileName.size()) {
146 for (
const auto& outFileName : outFileNames) {
147 auto fileName = fmt::format(
"{}/{}", outDir, outFileName);
149 std::unique_ptr<TFile> outFile(TFile::Open(fileName.data(),
"recreate"));
150 arr.Write(arr.GetName(), TObject::kSingleKey);
153 const auto nCanv = arr.GetEntries();
154 for (
int i = 0;
i < nCanv; ++
i) {
155 auto fileName2 = fileName;
158 }
else if (
i == nCanv - 1) {
161 auto c =
static_cast<TCanvas*
>(arr.UncheckedAt(
i));
162 c->Print(fileName2.data(), fmt::format(
"Title:{}",
c->GetTitle()).data());
169void utils::saveCanvases(std::vector<TCanvas*>& canvases, std::string_view outDir, std::string_view
types, std::string_view singleOutFileName, std::string nameAdd)
172 for (
auto c : canvases) {
190std::vector<CalPad*> utils::readCalPads(
const std::string_view fileName,
const std::vector<std::string>& calPadNames)
192 using CalPadMapType = std::unordered_map<std::string, CalPad>;
194 std::vector<CalPad*> calPads(calPadNames.size());
196 std::unique_ptr<TFile>
file(TFile::Open(fileName.data()));
202 auto firstKey = (TKey*)
file->GetListOfKeys()->At(0);
203 const auto clMap = TClass::GetClass(
typeid(CalPadMapType));
204 if (std::string_view(firstKey->GetClassName()) == std::string_view(clMap->GetName())) {
205 auto calPadMap = firstKey->ReadObject<CalPadMapType>();
206 for (
size_t iCalPad = 0; iCalPad < calPadNames.size(); ++iCalPad) {
207 calPads[iCalPad] =
new CalPad(calPadMap->at(calPadNames[iCalPad]));
211 for (
size_t iCalPad = 0; iCalPad < calPadNames.size(); ++iCalPad) {
212 file->GetObject(calPadNames[iCalPad].
data(), calPads[iCalPad]);
226void utils::mergeCalPads(std::string_view outputFileName, std::string_view inputFileNames, std::string_view calPadNames,
bool average)
230 const auto calPadNamesVec = utils::tokenize(calPadNames,
",");
232 std::string_view cmd =
"ls";
233 if (inputFileNames.rfind(
".root") == std::string_view::npos) {
236 auto files = gSystem->GetFromPipe(TString::Format(
"%s %s", cmd.data(), inputFileNames.data()));
237 std::unique_ptr<TObjArray> arrFiles(files.Tokenize(
"\n"));
239 std::vector<CalPad*> mergedCalPads;
241 for (
auto ofile : *arrFiles) {
242 auto calPads = utils::readCalPads(ofile->GetName(), calPadNamesVec);
243 if (!calPads.size()) {
246 if (!mergedCalPads.size()) {
247 mergedCalPads = calPads;
249 for (
size_t iCalPad = 0; iCalPad < calPads.size(); ++iCalPad) {
250 auto calPadName = calPadNamesVec[iCalPad];
251 auto calPadMerged = mergedCalPads[iCalPad];
252 calPadMerged->setName(calPadName);
253 auto calPadToMerge = calPads[iCalPad];
255 *calPadMerged += *calPadToMerge;
257 delete calPadToMerge;
263 const auto n = arrFiles->GetEntriesFast();
265 for (
auto calPad : mergedCalPads) {
271 std::unique_ptr<TFile> outFile(TFile::Open(outputFileName.data(),
"recreate"));
272 for (
auto calPad : mergedCalPads) {
273 outFile->WriteObject(calPad, calPad->getName().data());
278TChain* utils::buildChain(std::string_view command, std::string_view treeName, std::string_view treeTitle,
bool checkSubDir)
280 const TString files = gSystem->GetFromPipe(command.data());
281 std::unique_ptr<TObjArray> arrFiles(files.Tokenize(
"\n"));
282 if (!arrFiles->GetEntriesFast()) {
283 LOGP(error,
"command '{}' did not return results", command);
287 auto c =
new TChain(treeName.data(), treeTitle.data());
288 for (
const auto o : *arrFiles) {
290 LOGP(fatal,
"could not open alien connection to read {}",
o->GetName());
294 std::unique_ptr<TFile>
f(TFile::Open(
o->GetName()));
295 if (!
f->IsOpen() ||
f->IsZombie()) {
298 for (
auto ok : *
f->GetListOfKeys()) {
299 auto k =
static_cast<TKey*
>(ok);
300 if (std::string_view(k->GetClassName()) !=
"TDirectoryFile") {
303 auto df =
f->Get<TDirectoryFile>(k->GetName());
304 if (df->GetListOfKeys() && df->GetListOfKeys()->FindObject(treeName.data())) {
305 const auto fullTreePath = fmt::format(
"{}/{}", df->GetName(), treeName);
306 c->AddFile(
o->GetName(), TTree::kMaxEntries, fullTreePath.data());
307 LOGP(info,
"Adding file '{}', with tree {}",
o->GetName(), fullTreePath);
311 LOGP(info,
"Adding file '{}'",
o->GetName());
312 c->AddFile(
o->GetName());
void saveCanvases(TObjArray &arr, std::string_view outDir, std::string_view types="png,pdf", std::string_view rootFileName="", std::string nameAdd="")