Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions ALICE3/Core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ o2physics_add_library(ALICE3Core
SOURCES TOFResoALICE3.cxx
TrackUtilities.cxx
DelphesO2TrackSmearer.cxx
GeometryContainer.cxx
PUBLIC_LINK_LIBRARIES O2::Framework
O2Physics::AnalysisCore)

o2physics_target_root_dictionary(ALICE3Core
HEADERS TOFResoALICE3.h
TrackUtilities.h
DelphesO2TrackSmearer.h
GeometryContainer.h
LINKDEF ALICE3CoreLinkDef.h)

o2physics_add_library(FastTracker
Expand Down
39 changes: 6 additions & 33 deletions ALICE3/Core/DelphesO2TrackSmearer.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@

#include "ALICE3/Core/DelphesO2TrackSmearer.h"

#include "ALICE3/Core/GeometryContainer.h"

#include <CommonConstants/PhysicsConstants.h>
#include <Framework/Logger.h>

Expand All @@ -61,42 +63,13 @@ bool TrackSmearer::loadTable(int pdg, const char* filename, bool forceReload)
LOG(info) << " --- LUT table for PDG " << pdg << " has been already loaded with index " << ipdg << std::endl;
return false;
}
if (strncmp(filename, "ccdb:", 5) == 0) { // Check if filename starts with "ccdb:"
LOG(info) << " --- LUT file source identified as CCDB.";
std::string path = std::string(filename).substr(5); // Remove "ccdb:" prefix
filename = Form("%s/%s/snapshot.root", mOutPath.c_str(), path.c_str());
LOG(info) << " --- Local LUT filename will be: " << filename;
std::ifstream checkFile(filename); // Check if file already exists
if (!checkFile.is_open()) { // File does not exist, retrieve from CCDB
LOG(info) << " --- CCDB source detected for PDG " << pdg << ": " << path;
if (!mCcdbManager) {
LOG(fatal) << " --- CCDB manager not set. Please set it before loading LUT from CCDB.";
}
std::map<std::string, std::string> metadata;
mCcdbManager->getCCDBAccessor().retrieveBlob(path, mOutPath, metadata, 1);
// Add CCDB handling logic here if needed
LOG(info) << " --- Now retrieving LUT file from CCDB to: " << filename;
if (mCleanupDownloadedFile) { // Clean up the downloaded file if needed
bool status = loadTable(pdg, filename, forceReload);
if (std::remove(filename) != 0) {
LOG(warn) << " --- Could not remove temporary LUT file: " << filename;
} else {
LOG(info) << " --- Removed temporary LUT file: " << filename;
}
return status;
}
} else { // File exists, proceed to load
LOG(info) << " --- LUT file already exists: " << filename << ". Skipping download.";
checkFile.close();
}
return loadTable(pdg, filename, forceReload);
}

const std::string localFilename = o2::fastsim::GeometryEntry::accessFile(filename, "./.ALICE3/LUTs/", mCcdbManager, 10);
mLUTHeader[ipdg] = new lutHeader_t;

std::ifstream lutFile(filename, std::ifstream::binary);
std::ifstream lutFile(localFilename, std::ifstream::binary);
if (!lutFile.is_open()) {
LOG(info) << " --- cannot open covariance matrix file for PDG " << pdg << ": " << filename << std::endl;
LOG(info) << " --- cannot open covariance matrix file for PDG " << pdg << ": " << localFilename << std::endl;
delete mLUTHeader[ipdg];
mLUTHeader[ipdg] = nullptr;
return false;
Expand Down Expand Up @@ -147,7 +120,7 @@ bool TrackSmearer::loadTable(int pdg, const char* filename, bool forceReload)
mLUTEntry[ipdg][inch][irad][ieta][ipt] = new lutEntry_t;
lutFile.read(reinterpret_cast<char*>(mLUTEntry[ipdg][inch][irad][ieta][ipt]), sizeof(lutEntry_t));
if (lutFile.gcount() != sizeof(lutEntry_t)) {
LOG(info) << " --- troubles reading covariance matrix entry for PDG " << pdg << ": " << filename << std::endl;
LOG(info) << " --- troubles reading covariance matrix entry for PDG " << pdg << ": " << localFilename << std::endl;
LOG(info) << " --- expected/detected " << sizeof(lutHeader_t) << "/" << lutFile.gcount() << std::endl;
return false;
}
Expand Down
4 changes: 0 additions & 4 deletions ALICE3/Core/DelphesO2TrackSmearer.h
Original file line number Diff line number Diff line change
Expand Up @@ -251,8 +251,6 @@ class TrackSmearer
}
void setdNdEta(float val) { mdNdEta = val; } //;
void setCcdbManager(o2::ccdb::BasicCCDBManager* mgr) { mCcdbManager = mgr; } //;
void setCleanupDownloadedFile(bool val) { mCleanupDownloadedFile = val; } //;
void setDownloadPath(const std::string& path) { mOutPath = path; } //;

protected:
static constexpr unsigned int nLUTs = 9; // Number of LUT available
Expand All @@ -266,8 +264,6 @@ class TrackSmearer

private:
o2::ccdb::BasicCCDBManager* mCcdbManager = nullptr;
bool mCleanupDownloadedFile = true; // Flag to cleanup the LUT after it's used
std::string mOutPath = "./.ALICE3/LUTs/"; // Path where to download LUTs from CCDB
};

} // namespace delphes
Expand Down
128 changes: 1 addition & 127 deletions ALICE3/Core/FastTracker.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -36,132 +36,6 @@ namespace o2
namespace fastsim
{

std::map<std::string, std::map<std::string, std::string>> GeometryContainer::parseTEnvConfiguration(std::string& filename, std::vector<std::string>& layers)
{
std::map<std::string, std::map<std::string, std::string>> configMap;
filename = gSystem->ExpandPathName(filename.c_str());
LOG(info) << "Parsing TEnv configuration file: " << filename;
TEnv env(filename.c_str());
THashList* table = env.GetTable();
layers.clear();
for (int i = 0; i < table->GetEntries(); ++i) {
const std::string key = table->At(i)->GetName();
// key should contain exactly one dot
if (key.find('.') == std::string::npos || key.find('.') != key.rfind('.')) {
LOG(fatal) << "Key " << key << " does not contain exactly one dot";
continue;
}
const std::string firstPart = key.substr(0, key.find('.'));
if (std::find(layers.begin(), layers.end(), firstPart) == layers.end()) {
layers.push_back(firstPart);
}
}
env.Print();
// Layers
for (const auto& layer : layers) {
LOG(info) << " Reading layer " << layer;
for (int i = 0; i < table->GetEntries(); ++i) {
const std::string key = table->At(i)->GetName();
if (key.find(layer + ".") == 0) {
const std::string paramName = key.substr(key.find('.') + 1);
const std::string value = env.GetValue(key.c_str(), "");
configMap[layer][paramName] = value;
}
}
}
return configMap;
}

void GeometryContainer::init(o2::framework::InitContext& initContext)
{
std::vector<std::string> detectorConfiguration;
const bool foundDetectorConfiguration = common::core::getTaskOptionValue(initContext, "on-the-fly-detector-geometry-provider", "detectorConfiguration", detectorConfiguration, false);
if (!foundDetectorConfiguration) {
LOG(fatal) << "Could not retrieve detector configuration from OnTheFlyDetectorGeometryProvider task.";
return;
}
LOG(info) << "Size of detector configuration: " << detectorConfiguration.size();

bool cleanLutWhenLoaded;
const bool foundCleanLutWhenLoaded = common::core::getTaskOptionValue(initContext, "on-the-fly-detector-geometry-provider", "cleanLutWhenLoaded", cleanLutWhenLoaded, false);
if (!foundCleanLutWhenLoaded) {
LOG(fatal) << "Could not retrieve foundCleanLutWhenLoaded option from OnTheFlyDetectorGeometryProvider task.";
return;
}

for (std::string& configFile : detectorConfiguration) {
if (configFile.rfind("ccdb:", 0) == 0) {
LOG(info) << "ccdb source detected from on-the-fly-detector-geometry-provider";
const std::string ccdbPath = configFile.substr(5); // remove "ccdb:" prefix
const std::string outPath = "./.ALICE3/Configuration/";
configFile = Form("%s/%s/snapshot.root", outPath.c_str(), ccdbPath.c_str());

int timeout = 600; // Wait max 10 minutes
while (--timeout > 0) {
std::ifstream file(configFile);
if (file.good()) {
break;
}

std::this_thread::sleep_for(std::chrono::seconds(1));
}

std::ifstream checkFile(configFile);
if (!checkFile.good()) {
LOG(fatal) << "Timed out waiting for geometry snapshot: " << configFile;
return;
}
}

LOG(info) << "Detector geometry configuration file used: " << configFile;
addEntry(configFile);
setLutCleanupSetting(cleanLutWhenLoaded);
}
}

std::map<std::string, std::string> GeometryContainer::GeometryEntry::getConfiguration(const std::string& layerName) const
{
auto it = mConfigurations.find(layerName);
if (it != mConfigurations.end()) {
return it->second;
} else {
LOG(fatal) << "Layer " << layerName << " not found in geometry configurations.";
return {};
}
}

bool GeometryContainer::GeometryEntry::hasValue(const std::string& layerName, const std::string& key) const
{
auto layerIt = mConfigurations.find(layerName);
if (layerIt != mConfigurations.end()) {
auto keyIt = layerIt->second.find(key);
return keyIt != layerIt->second.end();
}
return false;
}

std::string GeometryContainer::GeometryEntry::getValue(const std::string& layerName, const std::string& key, bool require) const
{
auto layer = getConfiguration(layerName);
auto entry = layer.find(key);
if (entry != layer.end()) {
return layer.at(key);
} else if (require) {
LOG(fatal) << "Key " << key << " not found in layer " << layerName << " configurations.";
return "";
} else {
return "";
}
}

void GeometryContainer::GeometryEntry::replaceValue(const std::string& layerName, const std::string& key, const std::string& value)
{
if (!hasValue(layerName, key)) { // check that the key exists
LOG(fatal) << "Key " << key << " does not exist in layer " << layerName << ". Cannot replace value.";
}
setValue(layerName, key, value);
}

// +-~-<*>-~-+-~-<*>-~-+-~-<*>-~-+-~-<*>-~-+-~-<*>-~-+-~-<*>-~-+-~-<*>-~-+-~-<*>-~-+

DetLayer* FastTracker::AddLayer(TString name, float r, float z, float x0, float xrho, float resRPhi, float resZ, float eff, int type)
Expand Down Expand Up @@ -267,7 +141,7 @@ void FastTracker::AddTPC(float phiResMean, float zResMean)
}
}

void FastTracker::AddGenericDetector(GeometryContainer::GeometryEntry configMap, o2::ccdb::BasicCCDBManager* ccdbManager)
void FastTracker::AddGenericDetector(o2::fastsim::GeometryEntry configMap, o2::ccdb::BasicCCDBManager* ccdbManager)
{
// Layers
for (const auto& layer : configMap.getLayerNames()) {
Expand Down
72 changes: 2 additions & 70 deletions ALICE3/Core/FastTracker.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#define ALICE3_CORE_FASTTRACKER_H_

#include "DetLayer.h"
#include "GeometryContainer.h"

#include <CCDB/BasicCCDBManager.h>
#include <Framework/InitContext.h>
Expand All @@ -28,75 +29,6 @@ namespace o2
namespace fastsim
{

class GeometryContainer
{
public:
GeometryContainer() = default;
virtual ~GeometryContainer() = default;

void init(o2::framework::InitContext& initContext);

/**
* @brief Parses a TEnv configuration file and returns the key-value pairs split per entry
* @param filename Path to the TEnv configuration file
* @param layers Vector to store the order of the layers as they appear in the file
* @return A map where each key is a layer name and the value is another map of key-value pairs for that layer
*/
static std::map<std::string, std::map<std::string, std::string>> parseTEnvConfiguration(std::string& filename, std::vector<std::string>& layers);

// A container for the geometry info
struct GeometryEntry {
// Default constructor
GeometryEntry() = default;
explicit GeometryEntry(std::string filename)
{
mFileName = filename;
mConfigurations = GeometryContainer::parseTEnvConfiguration(mFileName, mLayerNames);
LOG(info) << "Loaded geometry configuration from file: " << filename << " with " << mLayerNames.size() << " layers.";
if (mLayerNames.empty()) {
LOG(warning) << "No layers found in geometry configuration file: " << filename;
}
}
std::map<std::string, std::map<std::string, std::string>> getConfigurations() const { return mConfigurations; }
std::map<std::string, std::string> getConfiguration(const std::string& layerName) const;
std::vector<std::string> getLayerNames() const { return mLayerNames; }
bool hasValue(const std::string& layerName, const std::string& key) const;
std::string getValue(const std::string& layerName, const std::string& key, bool require = true) const;
void setValue(const std::string& layerName, const std::string& key, const std::string& value) { mConfigurations[layerName][key] = value; }
void replaceValue(const std::string& layerName, const std::string& key, const std::string& value);
float getFloatValue(const std::string& layerName, const std::string& key) const { return std::stof(getValue(layerName, key)); }
int getIntValue(const std::string& layerName, const std::string& key) const { return std::stoi(getValue(layerName, key)); }

private:
std::string mFileName; // Filename of the geometry
std::map<std::string, std::map<std::string, std::string>> mConfigurations; // Layer configurations
std::vector<std::string> mLayerNames; // Ordered names of the layers
};

// Add a geometry entry from a configuration file
void addEntry(const std::string& filename) { entries.emplace_back(filename); }
void setLutCleanupSetting(const bool cleanLutWhenLoaded) { mCleanLutWhenLoaded = cleanLutWhenLoaded; }

// Getters
int getNumberOfConfigurations() const { return entries.size(); }
const std::vector<GeometryEntry>& getEntries() const { return entries; }
const GeometryEntry& getEntry(const int id) const { return entries.at(id); }
GeometryEntry getGeometryEntry(const int id) const { return entries.at(id); }
bool cleanLutWhenLoaded() const { return mCleanLutWhenLoaded; }

// Get configuration maps
std::map<std::string, std::map<std::string, std::string>> getConfigurations(const int id) const { return entries.at(id).getConfigurations(); }
std::map<std::string, std::string> getConfiguration(const int id, const std::string& layerName) const { return entries.at(id).getConfiguration(layerName); }

// Get specific values
std::string getValue(const int id, const std::string& layerName, const std::string& key, bool require = true) const { return entries.at(id).getValue(layerName, key, require); }
float getFloatValue(const int id, const std::string& layerName, const std::string& key) const { return entries.at(id).getFloatValue(layerName, key); }

private:
std::vector<GeometryEntry> entries;
bool mCleanLutWhenLoaded = true;
};

// +-~-<*>-~-+-~-<*>-~-+-~-<*>-~-+-~-<*>-~-+-~-<*>-~-+-~-<*>-~-+-~-<*>-~-+-~-<*>-~-+

// this class implements a synthetic smearer that allows
Expand Down Expand Up @@ -145,7 +77,7 @@ class FastTracker
*
* @param configMap Configuration map describing the detector.
*/
void AddGenericDetector(GeometryContainer::GeometryEntry configMap, o2::ccdb::BasicCCDBManager* ccdbManager = nullptr);
void AddGenericDetector(o2::fastsim::GeometryEntry configMap, o2::ccdb::BasicCCDBManager* ccdbManager = nullptr);

void Print();

Expand Down
Loading
Loading