Skip to content

Commit 107aa72

Browse files
author
Wiktor Pierozak
committed
break down scripts into single-task macros
1 parent 59314b8 commit 107aa72

5 files changed

Lines changed: 231 additions & 40 deletions

File tree

Detectors/FIT/macros/compareLUT.C

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ inline bool operator==(const o2::fit::EntryFEE& lhs, const o2::fit::EntryFEE& rh
5454
return comparer(lhs) == comparer(rhs);
5555
}
5656

57-
void compareLUT(const std::string fileA, const std::string fileB, bool compareEvenForDifferentSize = false, const std::string objectName = "LookupTable")
57+
void compareLUT(const std::string fileA, const std::string fileB, bool compareEvenForDifferentSize = false, const std::string objectName = "ccdb_object")
5858
{
5959
std::vector<o2::fit::EntryFEE> lutA = readLUTFromFile(fileA, objectName);
6060
std::vector<o2::fit::EntryFEE> lutB = readLUTFromFile(fileB, objectName);
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
2+
// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3+
// All rights not expressly granted are reserved.
4+
//
5+
// This software is distributed under the terms of the GNU General Public
6+
// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7+
//
8+
// In applying this license CERN does not waive the privileges and immunities
9+
// granted to it by virtue of its status as an Intergovernmental Organization
10+
// or submit itself to any jurisdiction.
11+
#if !defined(__CLING__) || defined(__ROOTCLING__)
12+
#include <iostream>
13+
#include <array>
14+
#include <ranges>
15+
16+
R__LOAD_LIBRARY(libO2CommonUtils)
17+
R__LOAD_LIBRARY(libO2DataFormatsFIT)
18+
19+
#include "CommonUtils/ConfigurableParamHelper.h"
20+
#include "DataFormatsFIT/LookUpTable.h"
21+
#include "Framework/Logger.h"
22+
#include "CommonConstants/LHCConstants.h"
23+
24+
#endif
25+
26+
void saveToCSV(const std::vector<o2::fit::EntryFEE>& lut, string_view path);
27+
28+
std::vector<o2::fit::EntryFEE> readLUTFromFile(const std::string filePath, const std::string objectName)
29+
{
30+
TFile file(filePath.c_str(), "READ");
31+
if (file.IsOpen() == false) {
32+
LOGP(fatal, "Failed to open {}", filePath);
33+
}
34+
LOGP(info, "Successfully opened {}", filePath);
35+
std::vector<o2::fit::EntryFEE>* lut = nullptr;
36+
file.GetObject<std::vector<o2::fit::EntryFEE>>(objectName.c_str(), lut);
37+
if (lut == nullptr) {
38+
LOGP(fatal, "Failed to read object {}", objectName);
39+
}
40+
LOGP(info, "Successfully get {} object", objectName);
41+
std::vector<o2::fit::EntryFEE> lutCopy = *lut;
42+
file.Close();
43+
return lutCopy;
44+
}
45+
46+
void fetchLUT(const std::string fileName, cosnt std::string objectName, const std::string csvName)
47+
{
48+
if (fileName.empty() || objectName.empty() || csvName.empty()) {
49+
return;
50+
}
51+
std::vector<o2::fit::EntryFEE> lut = readLUTFromFile(fileA, objectName);
52+
saveToCSV(lut, csvName);
53+
}
54+
55+
void saveToCSV(const std::vector<o2::fit::EntryFEE>& lut, string_view path)
56+
{
57+
std::ofstream ofs(path.data());
58+
if (!ofs.is_open()) {
59+
LOGP(error, "Cannot open file for writing: {}", path);
60+
return;
61+
}
62+
ofs << "LinkID,EndPointID,CRUID,FEEID,ModuleType,LocalChannelID,channel #,Module,HV board,HV channel,MCP S/N,HV cable,signal cable\n";
63+
for (const auto& entry : lut) {
64+
ofs << entry.mEntryCRU.mLinkID << ","
65+
<< entry.mEntryCRU.mEndPointID << ","
66+
<< entry.mEntryCRU.mCRUID << ","
67+
<< entry.mEntryCRU.mFEEID << ","
68+
<< entry.mModuleType << ","
69+
<< entry.mLocalChannelID << ","
70+
<< entry.mChannelID << ","
71+
<< entry.mModuleName << ","
72+
<< entry.mBoardHV << ","
73+
<< entry.mChannelHV << ","
74+
<< entry.mSerialNumberMCP << ","
75+
<< entry.mCableHV << ","
76+
<< entry.mCableSignal << "\n";
77+
}
78+
ofs.close();
79+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
2+
// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3+
// All rights not expressly granted are reserved.
4+
//
5+
// This software is distributed under the terms of the GNU General Public
6+
// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7+
//
8+
// In applying this license CERN does not waive the privileges and immunities
9+
// granted to it by virtue of its status as an Intergovernmental Organization
10+
// or submit itself to any jurisdiction.
11+
#if !defined(__CLING__) || defined(__ROOTCLING__)
12+
#include <iostream>
13+
#include <array>
14+
#include <ranges>
15+
16+
R__LOAD_LIBRARY(libO2CommonUtils)
17+
R__LOAD_LIBRARY(libO2CCDB)
18+
R__LOAD_LIBRARY(libO2DataFormatsFIT)
19+
20+
#include "DataFormatsFIT/LookUpTable.h"
21+
#include "Framework/Logger.h"
22+
#include "CommonConstants/LHCConstants.h"
23+
#endif
24+
25+
void saveToRoot(std::vector<o2::fit::EntryFEE>& lut, string_view path);
26+
27+
void convertLUTCSVtoROOT(const std::string csvFilePath, const std::string rootFilePath)
28+
{
29+
std::vector<o2::fit::EntryFEE> lut;
30+
std::ifstream lutCSV(csvFilePath);
31+
if (lutCSV.is_open() == false) {
32+
LOGP(error, "Failed to open {}", csvFilePath);
33+
return;
34+
}
35+
36+
std::string line;
37+
std::getline(lutCSV, line);
38+
std::map<std::string, int> headerMap;
39+
40+
auto headersView = std::string_view(line) | std::views::split(',');
41+
42+
int index = 0;
43+
for (auto&& rng : headersView) {
44+
std::string columnName(rng.begin(), rng.end());
45+
headerMap[columnName] = index++;
46+
}
47+
48+
while (std::getline(lutCSV, line)) {
49+
if (line.size() == 0) {
50+
return;
51+
}
52+
o2::fit::EntryFEE entry;
53+
auto fieldViews = std::string_view(line) | std::views::split(',');
54+
std::vector<std::string> parsedLine;
55+
for (auto&& view : fieldViews) {
56+
parsedLine.emplace_back(view.begin(), view.end());
57+
}
58+
if (parsedLine.size() < headerMap.size()) {
59+
LOGP(error, "Ill-formed line: {}", line);
60+
return;
61+
}
62+
63+
entry.mEntryCRU.mLinkID = std::stoi(parsedLine[headerMap.at("LinkID")]);
64+
entry.mEntryCRU.mEndPointID = std::stoi(parsedLine[headerMap.at("EndPointID")]);
65+
entry.mEntryCRU.mCRUID = std::stoi(parsedLine[headerMap.at("CRUID")]);
66+
entry.mEntryCRU.mFEEID = std::stoi(parsedLine[headerMap.at("FEEID")]);
67+
68+
entry.mModuleType = parsedLine[headerMap.at("ModuleType")];
69+
entry.mLocalChannelID = parsedLine[headerMap.at("LocalChannelID")];
70+
entry.mChannelID = parsedLine[headerMap.at("channel #")];
71+
entry.mModuleName = parsedLine[headerMap.at("Module")];
72+
entry.mBoardHV = parsedLine[headerMap.at("HV board")];
73+
entry.mChannelHV = parsedLine[headerMap.at("HV channel")];
74+
entry.mSerialNumberMCP = parsedLine[headerMap.at("MCP S/N")];
75+
entry.mCableHV = parsedLine[headerMap.at("HV cable")];
76+
entry.mCableSignal = parsedLine[headerMap.at("signal cable")];
77+
lut.emplace_back(entry);
78+
}
79+
saveToRoot(lut, rootFilePath);
80+
}
81+
82+
void saveToRoot(std::vector<o2::fit::EntryFEE>& lut, string_view path)
83+
{
84+
TFile file(path.data(), "RECREATE");
85+
if (file.IsOpen() == false) {
86+
LOGP(fatal, "Failed to open file {}", path.data());
87+
}
88+
89+
file.WriteObject(&lut, "LookupTable");
90+
file.Close();
91+
}

Detectors/FIT/macros/fetchLUT.C

Lines changed: 4 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ R__LOAD_LIBRARY(libO2DataFormatsFIT)
2929
void saveToCSV(const std::vector<o2::fit::EntryFEE>& lut, string_view path);
3030
void saveToRoot(std::shared_ptr<std::vector<o2::fit::EntryFEE>> lut, string_view path);
3131

32-
void fetchLUT(const std::string ccdbUrl, const std::string detector, long timestamp = -1, const std::string fileName = "", bool asCsv = true)
32+
void fetchLUT(const std::string ccdbUrl="alice-ccdb.cern.ch", const std::string detector="FT0", long timestamp = -1, const std::string fileName = "o2_lut.root")
3333
{
3434
o2::ccdb::CcdbApi ccdbApi;
3535
ccdbApi.init(ccdbUrl);
@@ -49,46 +49,11 @@ void fetchLUT(const std::string ccdbUrl, const std::string detector, long timest
4949
LOGP(info, "Successfully fetched LUT for {} from {}", detector, ccdbUrl);
5050
}
5151

52-
std::cout << detector << " lookup table: " << std::endl;
53-
for (const auto& entry : (*lut)) {
54-
std::cout << entry << std::endl;
55-
}
56-
5752
if (fileName.empty()) {
5853
return;
5954
}
6055

61-
if (asCsv) {
62-
saveToCSV(*lut, fileName);
63-
} else {
64-
saveToRoot(lut, fileName);
65-
}
66-
}
67-
68-
void saveToCSV(const std::vector<o2::fit::EntryFEE>& lut, string_view path)
69-
{
70-
std::ofstream ofs(path.data());
71-
if (!ofs.is_open()) {
72-
LOGP(error, "Cannot open file for writing: {}", path);
73-
return;
74-
}
75-
ofs << "LinkID,EndPointID,CRUID,FEEID,ModuleType,LocalChannelID,channel #,Module,HV board,HV channel,MCP S/N,HV cable,signal cable\n";
76-
for (const auto& entry : lut) {
77-
ofs << entry.mEntryCRU.mLinkID << ","
78-
<< entry.mEntryCRU.mEndPointID << ","
79-
<< entry.mEntryCRU.mCRUID << ","
80-
<< entry.mEntryCRU.mFEEID << ","
81-
<< entry.mModuleType << ","
82-
<< entry.mLocalChannelID << ","
83-
<< entry.mChannelID << ","
84-
<< entry.mModuleName << ","
85-
<< entry.mBoardHV << ","
86-
<< entry.mChannelHV << ","
87-
<< entry.mSerialNumberMCP << ","
88-
<< entry.mCableHV << ","
89-
<< entry.mCableSignal << "\n";
90-
}
91-
ofs.close();
56+
saveToRoot(lut, fileName);
9257
}
9358

9459
void saveToRoot(std::shared_ptr<std::vector<o2::fit::EntryFEE>> lut, string_view path)
@@ -98,6 +63,6 @@ void saveToRoot(std::shared_ptr<std::vector<o2::fit::EntryFEE>> lut, string_view
9863
LOGP(fatal, "Failed to open file {}", path.data());
9964
}
10065

101-
file.WriteObject(lut.get(), "LookupTable");
66+
file.WriteObject(lut.get(), "ccdb_object");
10267
file.Close();
103-
}
68+
}

Detectors/FIT/macros/printLUT.C

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
2+
// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3+
// All rights not expressly granted are reserved.
4+
//
5+
// This software is distributed under the terms of the GNU General Public
6+
// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7+
//
8+
// In applying this license CERN does not waive the privileges and immunities
9+
// granted to it by virtue of its status as an Intergovernmental Organization
10+
// or submit itself to any jurisdiction.
11+
#if !defined(__CLING__) || defined(__ROOTCLING__)
12+
#include <iostream>
13+
#include <array>
14+
15+
R__LOAD_LIBRARY(libO2CommonUtils)
16+
R__LOAD_LIBRARY(libO2DataFormatsFIT)
17+
18+
#include "CommonUtils/ConfigurableParamHelper.h"
19+
#include "DataFormatsFIT/LookUpTable.h"
20+
#include "Framework/Logger.h"
21+
#include "CommonConstants/LHCConstants.h"
22+
#endif
23+
24+
std::vector<o2::fit::EntryFEE> readLUTFromFile(const std::string filePath, const std::string objectName)
25+
{
26+
TFile file(filePath.c_str(), "READ");
27+
if (file.IsOpen() == false) {
28+
LOGP(fatal, "Failed to open {}", filePath);
29+
}
30+
LOGP(info, "Successfully opened {}", filePath);
31+
32+
std::vector<o2::fit::EntryFEE>* lut = nullptr;
33+
file.GetObject<std::vector<o2::fit::EntryFEE>>(objectName.c_str(), lut);
34+
35+
if (lut == nullptr) {
36+
LOGP(fatal, "Failed to read object {}", objectName);
37+
}
38+
LOGP(info, "Successfully get {} object", objectName);
39+
40+
std::vector<o2::fit::EntryFEE> lutCopy = *lut;
41+
file.Close();
42+
43+
return lutCopy;
44+
}
45+
46+
void printLUT(const std::string fileA, const std::string objectName = "ccdb_object")
47+
{
48+
std::vector<o2::fit::EntryFEE> lut = readLUTFromFile(fileA, objectName);
49+
const size_t size = lut.size();
50+
51+
std::cout << "--- Lookup table ---" << std::endl;
52+
53+
for (size_t idx = 0; idx < size; idx++) {
54+
std::cout << lut[idx] << std::endl;
55+
}
56+
}

0 commit comments

Comments
 (0)