Skip to content

Commit 1bb6d35

Browse files
authored
TPC loopers generator: Fetch models from CCDB or AliEn (#2006)
* Fetch models from CCDB or AliEn
1 parent 4c05962 commit 1bb6d35

File tree

5 files changed

+60
-3
lines changed

5 files changed

+60
-3
lines changed
-632 KB
Binary file not shown.
-638 KB
Binary file not shown.

MC/config/common/external/generator/TPCLoopers.C

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
#include <vector>
44
#include <fstream>
55
#include <rapidjson/document.h>
6+
#include "CCDB/CCDBTimeStampUtils.h"
7+
#include "CCDB/CcdbApi.h"
68

79
// Static Ort::Env instance for multiple onnx model loading
810
static Ort::Env global_env(ORT_LOGGING_LEVEL_WARNING, "GlobalEnv");
@@ -377,6 +379,11 @@ class GenTPCLoopers : public Generator
377379
} // namespace eventgen
378380
} // namespace o2
379381

382+
// ONNX model files can be local, on AliEn or in the ALICE CCDB.
383+
// For local and alien files it is mandatory to provide the filenames, for the CCDB instead the
384+
// path to the object in the CCDB is sufficient. The model files will be downloaded locally.
385+
// Example of CCDB path: "ccdb://Users/n/name/test"
386+
// Example of alien path: "alien:///alice/cern.ch/user/n/name/test/test.onnx"
380387
FairGenerator *
381388
Generator_TPCLoopers(std::string model_pairs = "tpcloopmodel.onnx", std::string model_compton = "tpcloopmodelcompton.onnx",
382389
std::string poisson = "poisson.csv", std::string gauss = "gauss.csv", std::string scaler_pair = "scaler_pair.json",
@@ -390,6 +397,55 @@ FairGenerator *
390397
gauss = gSystem->ExpandPathName(gauss.c_str());
391398
scaler_pair = gSystem->ExpandPathName(scaler_pair.c_str());
392399
scaler_compton = gSystem->ExpandPathName(scaler_compton.c_str());
400+
const std::array<std::string, 2> models = {model_pairs, model_compton};
401+
const std::array<std::string, 2> local_names = {"WGANpair.onnx", "WGANcompton.onnx"};
402+
const std::array<bool, 2> isAlien = {models[0].starts_with("alien://"), models[1].starts_with("alien://")};
403+
const std::array<bool, 2> isCCDB = {models[0].starts_with("ccdb://"), models[1].starts_with("ccdb://")};
404+
if (std::any_of(isAlien.begin(), isAlien.end(), [](bool v) { return v; }))
405+
{
406+
if (!gGrid) {
407+
TGrid::Connect("alien://");
408+
if (!gGrid) {
409+
LOG(fatal) << "AliEn connection failed, check token.";
410+
exit(1);
411+
}
412+
}
413+
for (size_t i = 0; i < models.size(); ++i)
414+
{
415+
if (isAlien[i] && !TFile::Cp(models[i].c_str(), local_names[i].c_str()))
416+
{
417+
LOG(fatal) << "Error: Model file " << models[i] << " does not exist!";
418+
exit(1);
419+
}
420+
}
421+
}
422+
if (std::any_of(isCCDB.begin(), isCCDB.end(), [](bool v) { return v; }))
423+
{
424+
o2::ccdb::CcdbApi ccdb_api;
425+
ccdb_api.init("http://alice-ccdb.cern.ch");
426+
for (size_t i = 0; i < models.size(); ++i)
427+
{
428+
if (isCCDB[i])
429+
{
430+
auto model_path = models[i].substr(7); // Remove "ccdb://"
431+
// Treat filename if provided in the CCDB path
432+
auto extension = model_path.find(".onnx");
433+
if (extension != std::string::npos)
434+
{
435+
auto last_slash = model_path.find_last_of('/');
436+
model_path = model_path.substr(0, last_slash);
437+
}
438+
std::map<std::string, std::string> filter;
439+
if(!ccdb_api.retrieveBlob(model_path, "./" , filter, o2::ccdb::getCurrentTimestamp(), false, local_names[i].c_str()))
440+
{
441+
LOG(fatal) << "Error: issues in retrieving " << model_path << " from CCDB!";
442+
exit(1);
443+
}
444+
}
445+
}
446+
}
447+
model_pairs = isAlien[0] || isCCDB[0] ? local_names[0] : model_pairs;
448+
model_compton = isAlien[1] || isCCDB[1] ? local_names[1] : model_compton;
393449
auto generator = new o2::eventgen::GenTPCLoopers(model_pairs, model_compton, poisson, gauss, scaler_pair, scaler_compton);
394450
generator->SetNLoopers(nloopers_pairs, nloopers_compton);
395451
generator->SetMultiplier(mult);
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
# Example of tpc loopers generator with a poisson distribution of pairs and gaussian distribution of compton electrons
22
[GeneratorExternal]
33
fileName = ${O2DPG_MC_CONFIG_ROOT}/MC/config/common/external/generator/TPCLoopers.C
4-
funcName = Generator_TPCLoopers("${O2DPG_MC_CONFIG_ROOT}/MC/config/common/TPCloopers/generatorWGAN_pair.onnx", "${O2DPG_MC_CONFIG_ROOT}/MC/config/common/TPCloopers/generatorWGAN_compton.onnx", "${O2DPG_MC_CONFIG_ROOT}/MC/config/common/TPCloopers/poisson_params.csv", "${O2DPG_MC_CONFIG_ROOT}/MC/config/common/TPCloopers/gaussian_params.csv", "${O2DPG_MC_CONFIG_ROOT}/MC/config/common/TPCloopers/ScalerPairParams.json", "${O2DPG_MC_CONFIG_ROOT}/MC/config/common/TPCloopers/ScalerComptonParams.json")
4+
funcName = Generator_TPCLoopers("ccdb://Users/m/mgiacalo/WGAN_ExtGenPair", "ccdb://Users/m/mgiacalo/WGAN_ExtGenCompton", "${O2DPG_MC_CONFIG_ROOT}/MC/config/common/TPCloopers/poisson_params.csv", "${O2DPG_MC_CONFIG_ROOT}/MC/config/common/TPCloopers/gaussian_params.csv", "${O2DPG_MC_CONFIG_ROOT}/MC/config/common/TPCloopers/ScalerPairParams.json", "${O2DPG_MC_CONFIG_ROOT}/MC/config/common/TPCloopers/ScalerComptonParams.json")
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
# Example of tpc loopers generator with a fixed number of pairs (10)
1+
# Example of tpc loopers generator with a fixed number of pairs and compton electrons (10)
2+
# Multiplier values are ignored in this case, but kept to 1 for consistency
23
#---> GeneratorTPCloopers
34
[GeneratorExternal]
45
fileName = ${O2DPG_MC_CONFIG_ROOT}/MC/config/common/external/generator/TPCLoopers.C
5-
funcName = Generator_TPCLoopers("${O2DPG_MC_CONFIG_ROOT}/MC/config/common/TPCloopers/generatorWGAN_pair.onnx", "", "${O2DPG_MC_CONFIG_ROOT}/MC/config/common/TPCloopers/ScalerPairParams.json",10)
6+
funcName = Generator_TPCLoopers("ccdb://Users/m/mgiacalo/WGAN_ExtGenPair", "ccdb://Users/m/mgiacalo/WGAN_ExtGenCompton", "", "", "${O2DPG_MC_CONFIG_ROOT}/MC/config/common/TPCloopers/ScalerPairParams.json", "${O2DPG_MC_CONFIG_ROOT}/MC/config/common/TPCloopers/ScalerComptonParams.json",{1.,1.}, 10,10)

0 commit comments

Comments
 (0)