Skip to content

Commit e09d587

Browse files
authored
[ALICE3] Add A3 geometry provider task (#14344)
1 parent 543e29b commit e09d587

File tree

6 files changed

+249
-7
lines changed

6 files changed

+249
-7
lines changed

ALICE3/Core/DelphesO2TrackSmearer.cxx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ bool TrackSmearer::loadTable(int pdg, const char* filename, bool forceReload)
6666
std::string path = std::string(filename).substr(5); // Remove "ccdb:" prefix
6767
const std::string outPath = "/tmp/LUTs/";
6868
filename = Form("%s/%s/snapshot.root", outPath.c_str(), path.c_str());
69+
LOG(info) << " --- Local LUT filename will be: " << filename;
6970
std::ifstream checkFile(filename); // Check if file already exists
7071
if (!checkFile.is_open()) { // File does not exist, retrieve from CCDB
7172
LOG(info) << " --- CCDB source detected for PDG " << pdg << ": " << path;

ALICE3/Core/FastTracker.cxx

Lines changed: 83 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,18 @@
1111

1212
#include "FastTracker.h"
1313

14-
#include "ReconstructionDataFormats/TrackParametrization.h"
14+
#include "Common/Core/TableHelper.h"
15+
16+
#include <ReconstructionDataFormats/TrackParametrization.h>
1517

16-
#include "TMath.h"
17-
#include "TMatrixD.h"
18-
#include "TMatrixDSymEigen.h"
19-
#include "TRandom.h"
2018
#include <TEnv.h>
2119
#include <THashList.h>
20+
#include <TMath.h>
21+
#include <TMatrixD.h>
22+
#include <TMatrixDSymEigen.h>
2223
#include <TObject.h>
24+
#include <TRandom.h>
25+
#include <TSystem.h>
2326

2427
#include <fstream>
2528
#include <map>
@@ -31,6 +34,81 @@ namespace o2
3134
namespace fastsim
3235
{
3336

37+
std::map<std::string, std::map<std::string, std::string>> GeometryContainer::parseTEnvConfiguration(std::string filename, std::vector<std::string>& layers)
38+
{
39+
std::map<std::string, std::map<std::string, std::string>> configMap;
40+
filename = gSystem->ExpandPathName(filename.c_str());
41+
TEnv env(filename.c_str());
42+
THashList* table = env.GetTable();
43+
layers.clear();
44+
for (int i = 0; i < table->GetEntries(); ++i) {
45+
const std::string key = table->At(i)->GetName();
46+
// key should contain exactly one dot
47+
if (key.find('.') == std::string::npos || key.find('.') != key.rfind('.')) {
48+
LOG(fatal) << "Key " << key << " does not contain exactly one dot";
49+
continue;
50+
}
51+
const std::string firstPart = key.substr(0, key.find('.'));
52+
if (std::find(layers.begin(), layers.end(), firstPart) == layers.end()) {
53+
layers.push_back(firstPart);
54+
}
55+
}
56+
env.Print();
57+
// Layers
58+
for (const auto& layer : layers) {
59+
LOG(info) << " Reading layer " << layer;
60+
for (int i = 0; i < table->GetEntries(); ++i) {
61+
const std::string key = table->At(i)->GetName();
62+
if (key.find(layer + ".") == 0) {
63+
const std::string paramName = key.substr(key.find('.') + 1);
64+
const std::string value = env.GetValue(key.c_str(), "");
65+
configMap[layer][paramName] = value;
66+
}
67+
}
68+
}
69+
return configMap;
70+
}
71+
72+
void GeometryContainer::init(o2::framework::InitContext& initContext)
73+
{
74+
std::vector<std::string> detectorConfiguration;
75+
const bool found = common::core::getTaskOptionValue(initContext, "on-the-fly-detector-geometry-provider", "detectorConfiguration", detectorConfiguration, false);
76+
if (!found) {
77+
LOG(fatal) << "Could not retrieve detector configuration from OnTheFlyDetectorGeometryProvider task.";
78+
return;
79+
}
80+
LOG(info) << "Size of detector configuration: " << detectorConfiguration.size();
81+
for (const auto& configFile : detectorConfiguration) {
82+
LOG(info) << "Detector geometry configuration file used: " << configFile;
83+
addEntry(configFile);
84+
}
85+
}
86+
87+
std::map<std::string, std::string> GeometryContainer::GeometryEntry::getConfiguration(const std::string& layerName) const
88+
{
89+
auto it = mConfigurations.find(layerName);
90+
if (it != mConfigurations.end()) {
91+
return it->second;
92+
} else {
93+
LOG(fatal) << "Layer " << layerName << " not found in geometry configurations.";
94+
return {};
95+
}
96+
}
97+
98+
std::string GeometryContainer::GeometryEntry::getValue(const std::string& layerName, const std::string& key, bool require) const
99+
{
100+
auto layer = getConfiguration(layerName);
101+
auto entry = layer.find(key);
102+
if (entry != layer.end()) {
103+
return layer.at(key);
104+
} else if (require) {
105+
LOG(fatal) << "Key " << key << " not found in layer " << layerName << " configurations.";
106+
return "";
107+
} else {
108+
return "";
109+
}
110+
}
111+
34112
// +-~-<*>-~-+-~-<*>-~-+-~-<*>-~-+-~-<*>-~-+-~-<*>-~-+-~-<*>-~-+-~-<*>-~-+-~-<*>-~-+
35113

36114
DetLayer* FastTracker::AddLayer(TString name, float r, float z, float x0, float xrho, float resRPhi, float resZ, float eff, int type)

ALICE3/Core/FastTracker.h

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@
1515
#include "DetLayer.h"
1616

1717
#include <CCDB/BasicCCDBManager.h>
18+
#include <Framework/InitContext.h>
19+
#include <Framework/Logger.h>
1820
#include <ReconstructionDataFormats/Track.h>
1921

20-
#include <fairlogger/Logger.h>
21-
2222
#include <map>
2323
#include <string>
2424
#include <vector>
@@ -28,6 +28,63 @@ namespace o2
2828
namespace fastsim
2929
{
3030

31+
class GeometryContainer
32+
{
33+
public:
34+
GeometryContainer() = default;
35+
virtual ~GeometryContainer() = default;
36+
37+
void init(o2::framework::InitContext& initContext);
38+
39+
/**
40+
* @brief Parses a TEnv configuration file and returns the key-value pairs split per entry
41+
* @param filename Path to the TEnv configuration file
42+
* @param layers Vector to store the order of the layers as they appear in the file
43+
* @return A map where each key is a layer name and the value is another map of key-value pairs for that layer
44+
*/
45+
static std::map<std::string, std::map<std::string, std::string>> parseTEnvConfiguration(std::string filename, std::vector<std::string>& layers);
46+
47+
// A container for the geometry info
48+
struct GeometryEntry {
49+
// Default constructor
50+
GeometryEntry() = default;
51+
explicit GeometryEntry(std::string filename) : name(filename)
52+
{
53+
mConfigurations = GeometryContainer::parseTEnvConfiguration(filename, layerNames);
54+
}
55+
std::map<std::string, std::map<std::string, std::string>> getConfigurations() const { return mConfigurations; }
56+
std::map<std::string, std::string> getConfiguration(const std::string& layerName) const;
57+
std::vector<std::string> getLayerNames() const { return layerNames; }
58+
std::string getValue(const std::string& layerName, const std::string& key, bool require = true) const;
59+
float getFloatValue(const std::string& layerName, const std::string& key) const { return std::stof(getValue(layerName, key)); }
60+
int getIntValue(const std::string& layerName, const std::string& key) const { return std::stoi(getValue(layerName, key)); }
61+
62+
private:
63+
std::string name; // Filename of the geometry
64+
std::map<std::string, std::map<std::string, std::string>> mConfigurations;
65+
std::vector<std::string> layerNames; // Ordered names of the layers
66+
};
67+
68+
// Add a geometry entry from a configuration file
69+
void addEntry(const std::string& filename) { entries.emplace_back(filename); }
70+
71+
// Getters
72+
int getNumberOfConfigurations() const { return entries.size(); }
73+
const std::vector<GeometryEntry>& getEntries() const { return entries; }
74+
const GeometryEntry& getEntry(const int id) const { return entries.at(id); }
75+
GeometryEntry getGeometryEntry(const int id) const { return entries.at(id); }
76+
77+
// Get configuration maps
78+
std::map<std::string, std::map<std::string, std::string>> getConfigurations(const int id) const { return entries.at(id).getConfigurations(); }
79+
std::map<std::string, std::string> getConfiguration(const int id, const std::string& layerName) const { return entries.at(id).getConfiguration(layerName); }
80+
81+
// Get specific values
82+
float getFloatValue(const int id, const std::string& layerName, const std::string& key) const { return entries.at(id).getFloatValue(layerName, key); }
83+
84+
private:
85+
std::vector<GeometryEntry> entries;
86+
};
87+
3188
// +-~-<*>-~-+-~-<*>-~-+-~-<*>-~-+-~-<*>-~-+-~-<*>-~-+-~-<*>-~-+-~-<*>-~-+-~-<*>-~-+
3289

3390
// this class implements a synthetic smearer that allows

ALICE3/Core/FastTrackerLinkDef.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#pragma link off all classes;
1717
#pragma link off all functions;
1818

19+
#pragma link C++ class o2::fastsim::GeometryContainer + ;
1920
#pragma link C++ class o2::fastsim::FastTracker + ;
2021
#pragma link C++ class o2::fastsim::DelphesO2LutWriter + ;
2122

ALICE3/TableProducer/OTF/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,8 @@ o2physics_add_dpl_workflow(on-the-fly-tracker-pid
2828
SOURCES onTheFlyTrackerPid.cxx
2929
PUBLIC_LINK_LIBRARIES O2::Framework O2::DetectorsBase O2Physics::AnalysisCore O2::ReconstructionDataFormats O2::DetectorsCommonDataFormats O2Physics::ALICE3Core
3030
COMPONENT_NAME Analysis)
31+
32+
o2physics_add_dpl_workflow(on-the-fly-detector-geometry-provider
33+
SOURCES onTheFlyDetectorGeometryProvider.cxx
34+
PUBLIC_LINK_LIBRARIES O2::Framework O2Physics::AnalysisCore O2Physics::ALICE3Core O2Physics::FastTracker
35+
COMPONENT_NAME Analysis)
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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+
12+
/// \file onTheFlyDetectorGeometryProvider.cxx
13+
///
14+
/// \brief Reader and and provider of the detector geometry for on-the-fly simulation
15+
///
16+
/// \author Nicolò Jacazio <nicolo.jacazio@cern.ch>, Universita del Piemonte Orientale (IT)
17+
///
18+
19+
#include "ALICE3/Core/FastTracker.h"
20+
21+
#include <CCDB/BasicCCDBManager.h>
22+
#include <Framework/AnalysisTask.h>
23+
#include <Framework/runDataProcessing.h>
24+
25+
#include <map>
26+
#include <string>
27+
#include <vector>
28+
29+
struct OnTheFlyDetectorGeometryProvider {
30+
31+
o2::framework::Configurable<std::vector<std::string>> detectorConfiguration{"detectorConfiguration",
32+
std::vector<std::string>{"$O2PHYSICS_ROOT/share/alice3/a3geometry_v3.ini"},
33+
"Paths of the detector geometry configuration files"};
34+
o2::framework::Service<o2::ccdb::BasicCCDBManager> ccdb;
35+
void init(o2::framework::InitContext&)
36+
{
37+
ccdb->setURL("http://alice-ccdb.cern.ch");
38+
ccdb->setTimestamp(-1);
39+
}
40+
void run(o2::framework::ProcessingContext& pc)
41+
{
42+
o2::fastsim::GeometryContainer geometryContainer; // Checking that the geometry files can be accessed and loaded
43+
LOG(info) << "On-the-fly detector geometry provider running.";
44+
if (detectorConfiguration.value.empty()) {
45+
LOG(fatal) << "No detector configuration files provided.";
46+
return;
47+
}
48+
int idx = 0;
49+
for (auto& configFile : detectorConfiguration.value) {
50+
LOG(info) << "Loading detector geometry from configuration file: " << configFile;
51+
// If the filename starts with ccdb: then take the file from the ccdb
52+
if (configFile.rfind("ccdb:", 0) == 0) {
53+
std::string ccdbPath = configFile.substr(5); // remove "ccdb:" prefix
54+
const std::string outPath = "/tmp/DetGeo/";
55+
configFile = Form("%s/%s/snapshot.root", outPath.c_str(), ccdbPath.c_str());
56+
std::ifstream checkFile(configFile); // Check if file already exists
57+
if (!checkFile.is_open()) { // File does not exist, retrieve from CCDB
58+
LOG(info) << " --- CCDB source detected for detector geometry " << configFile;
59+
std::map<std::string, std::string> metadata;
60+
ccdb->getCCDBAccessor().retrieveBlob(ccdbPath, outPath, metadata, 1);
61+
LOG(info) << " --- Now retrieving geometry configuration from CCDB to: " << configFile;
62+
} else { // File exists, proceed to load
63+
LOG(info) << " --- Geometry configuration file already exists: " << configFile << ". Skipping download.";
64+
checkFile.close();
65+
}
66+
detectorConfiguration.value[idx] = configFile; // Update the filename to the local file
67+
}
68+
geometryContainer.addEntry(configFile);
69+
idx++;
70+
}
71+
pc.services().get<o2::framework::ControlService>().endOfStream();
72+
pc.services().get<o2::framework::ControlService>().readyToQuit(o2::framework::QuitRequest::Me);
73+
}
74+
};
75+
76+
// #define VERIFY_ALICE3
77+
#ifdef VERIFY_ALICE3
78+
struct OnTheFlyDetectorGeometryUser {
79+
void init(o2::framework::InitContext& initContext)
80+
{
81+
o2::fastsim::GeometryContainer geometryContainer; // Checking that the configuration can be inherited
82+
geometryContainer.init(initContext);
83+
}
84+
void run(o2::framework::ProcessingContext& pc)
85+
{
86+
pc.services().get<o2::framework::ControlService>().endOfStream();
87+
pc.services().get<o2::framework::ControlService>().readyToQuit(o2::framework::QuitRequest::Me);
88+
}
89+
};
90+
#endif
91+
92+
o2::framework::WorkflowSpec defineDataProcessing(o2::framework::ConfigContext const& cfgc)
93+
{
94+
o2::framework::WorkflowSpec spec;
95+
spec.push_back(adaptAnalysisTask<OnTheFlyDetectorGeometryProvider>(cfgc));
96+
#ifdef VERIFY_ALICE3
97+
spec.push_back(adaptAnalysisTask<OnTheFlyDetectorGeometryUser>(cfgc));
98+
#endif
99+
return spec;
100+
}

0 commit comments

Comments
 (0)