Project
Loading...
Searching...
No Matches
test_GeneratorPythia8Param.cxx
Go to the documentation of this file.
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#define BOOST_TEST_MODULE Test GeneratorPythia8Param class
13#define BOOST_TEST_MAIN
14#define BOOST_TEST_DYN_LINK
15#include <boost/test/unit_test.hpp>
18#include <boost/property_tree/ptree.hpp>
21#include <iostream>
22#include <filesystem>
23#include <unistd.h>
24
25// Tests various aspects of the
26// ConfigurableParamPromoter class, which is used to promote
27// Pythia8GenConfig to a configurable param
28BOOST_AUTO_TEST_CASE(pythia8_Pythia8GenConfig)
29{
31 "GeneratorPythia8.config=Foo;GeneratorPythia8.includePartonEvent=true");
32
34
35 BOOST_CHECK_EQUAL(GeneratorPythia8Param::Instance().config, std::string("Foo"));
36 BOOST_CHECK_EQUAL(GeneratorPythia8Param::Instance().includePartonEvent, true);
37
38 BOOST_CHECK_EQUAL(GeneratorPythia8Param::Instance().includePartonEvent, o2::conf::ConfigurableParam::getValueAs<bool>("GeneratorPythia8.includePartonEvent"));
39 // setValue - getValue
40 o2::conf::ConfigurableParam::setValue("GeneratorPythia8.config", "Baz");
41 BOOST_CHECK_EQUAL(o2::conf::ConfigurableParam::getValueAs<std::string>("GeneratorPythia8.config"), std::string("Baz"));
42 BOOST_CHECK_EQUAL(GeneratorPythia8Param::Instance().config, std::string("Baz"));
43
44 // member provenance
45 BOOST_CHECK_EQUAL(GeneratorPythia8Param::Instance().getMemberProvenance("config"), o2::conf::ConfigurableParam::EParamProvenance::kRT);
46 BOOST_CHECK_EQUAL(GeneratorPythia8Param::Instance().getMemberProvenance("verbose"), o2::conf::ConfigurableParam::EParamProvenance::kCODE);
47
48 // config detach
49 auto config_copy = GeneratorPythia8Param::Instance().detach();
50 BOOST_CHECK_EQUAL(config_copy.config, std::string("Baz"));
51 BOOST_CHECK_EQUAL(config_copy.includePartonEvent, true);
52
53 // file IO
54 TFile tmp_file("GeneratorParamConfig_tmp.root", "RECREATE");
55
56 GeneratorPythia8Param::Instance().serializeTo(&tmp_file);
57 // modify the instance to some intermediate fluent value
58 o2::conf::ConfigurableParam::setValue("GeneratorPythia8.includePartonEvent", "0");
59 BOOST_CHECK_EQUAL(config_copy.includePartonEvent, true);
60 BOOST_CHECK_EQUAL(GeneratorPythia8Param::Instance().includePartonEvent, false);
61 tmp_file.Close();
62
63 // read back
64 TFile tmp_file2("GeneratorParamConfig_tmp.root", "READ");
65 const_cast<GeneratorPythia8Param&>(GeneratorPythia8Param::Instance()).initFrom(&tmp_file2);
66 BOOST_CHECK_EQUAL(GeneratorPythia8Param::Instance().includePartonEvent, true);
67 tmp_file2.Close();
68
69 // CCDB IO
70 std::string ccdbUrl = "http://ccdb-test.cern.ch:8080";
71 bool hostReachable = false;
73 api.init(ccdbUrl);
74 std::string pathA = "/Generators/UnitTest/Pythia8/GeneratorPythia8Param";
75 std::map<std::string, std::string> md;
76 long start = 1000, stop = 2000;
77 api.storeAsTFileAny(&GeneratorPythia8Param::Instance(), pathA, md, start, stop);
78
79 // modify the instance to some intermediate fluent value
80 o2::conf::ConfigurableParam::setValue("GeneratorPythia8.includePartonEvent", "0");
81
82 auto returnedobj = api.retrieveFromTFileAny<o2::eventgen::GeneratorPythia8Param>(pathA, md, (start + stop) / 2);
83 GeneratorPythia8Param::Instance().printKeyValues();
84};
85
86BOOST_AUTO_TEST_CASE(EventPool_Alien_Path)
87{
89 config.eventPoolPath = "alien:///alice/cern.ch/user/s/swenzel/selfjobs/evtpool_pythia8pp_test-20241126-152715";
91 auto files = gen.setupFileUniverse(config.eventPoolPath);
92 BOOST_CHECK(files.size() > 0);
93};
94
95BOOST_AUTO_TEST_CASE(EventPool_Alien_File)
96{
98 config.eventPoolPath = "alien:///alice/cern.ch/user/s/swenzel/selfjobs/evtpool_pythia8pp_test-20241126-152715/001/evtpool.root";
100 auto files = gen.setupFileUniverse(config.eventPoolPath);
101 BOOST_CHECK(files.size() == 1);
102};
103
104BOOST_AUTO_TEST_CASE(EventPool_Alien_WrongFileName)
105{
107 config.eventPoolPath = "alien:///foo_123";
109 auto files = gen.setupFileUniverse(config.eventPoolPath);
110 BOOST_CHECK(files.size() == 0);
111};
112
113BOOST_AUTO_TEST_CASE(EventPool_Local_Path)
114{
115 namespace fs = std::filesystem;
116
117 // we need to create some local tmp files that mimick the event pool
118 // this is a helper to do this
119 auto createPoolFiles = [](const fs::path& tmpDir, int numFiles) {
120 for (int i = 0; i < numFiles; ++i) {
121 // Generate a unique file name
122 fs::path fileDir = tmpDir / std::to_string(i);
124 fs::create_directory(fileDir);
125 // Create and close the file (touch)
126 std::ofstream file(filePath);
127 file.close();
128 }
129 };
130
131 // Seed for randomness
132 std::srand(static_cast<unsigned>(std::time(nullptr)));
133 // process id
134 auto proc = getpid();
135
136 // Create a random directory in the system temp directory
137 fs::path tmpDir = fs::temp_directory_path() / ("eventpool_test_" + std::to_string(proc) + "_" + std::to_string(std::rand()));
138 fs::create_directory(tmpDir);
139 constexpr int numfiles = 11;
140 createPoolFiles(tmpDir, numfiles);
141
143 config.eventPoolPath = tmpDir.string();
145 auto files = gen.setupFileUniverse(config.eventPoolPath);
146 BOOST_CHECK(files.size() == numfiles);
147
148 // remove the files
149 if (fs::exists(tmpDir)) {
150 fs::remove_all(tmpDir); // Remove all files and the directory
151 }
152};
153
154BOOST_AUTO_TEST_CASE(EventPool_Local_RootFile)
155{
156 namespace fs = std::filesystem;
157
158 // we need to create a fake local root file in the right format
159 // Seed for randomness
160 std::srand(static_cast<unsigned>(std::time(nullptr)));
161 // process id
162 auto proc = getpid();
163 // Create a random directory in the system temp directory
164 fs::path tmpDir = fs::temp_directory_path() / ("eventpool_testlocalrootfile_" + std::to_string(proc) + "_" + std::to_string(std::rand()));
165 //
167 fs::create_directory(tmpDir);
168 // Create and close the file (touch); needs to be a ROOT file so using TFile
169 TFile file(filePath.string().c_str(), "CREATE");
170 file.Close();
171
173 config.eventPoolPath = tmpDir.string() + "/evtpool.root";
175 auto files = gen.setupFileUniverse(config.eventPoolPath);
176 BOOST_CHECK(files.size() == 1);
177
178 // remove the files
179 if (fs::exists(tmpDir)) {
180 fs::remove_all(tmpDir); // Remove all files and the directory
181 }
182};
183
184BOOST_AUTO_TEST_CASE(EventPool_Local_ListFile)
185{
186 // test reading list of files from a (txt) file
187 // create this txt file on the fly
188
189 namespace fs = std::filesystem;
190
191 std::srand(static_cast<unsigned>(std::time(nullptr)));
192 // process id
193 auto proc = getpid();
194 // Create a random directory in the system temp directory
195 fs::path tmpDir = fs::temp_directory_path() / ("eventpool_testlocallistfile_" + std::to_string(proc) + "_" + std::to_string(std::rand()));
196 fs::create_directory(tmpDir);
197
198 std::ofstream file(tmpDir / std::string("filelist.txt"));
199
200 constexpr int numfiles = 11;
201 for (int i = 0; i < numfiles; ++i) {
202 // Generate a unique file name
203 fs::path filePath = fs::path(std::string("alien:///foo")) / std::to_string(i) / o2::eventgen::GeneratorFromEventPool::eventpool_filename;
204 file << filePath.string() << "\n";
205 }
206 file.close();
207
209 config.eventPoolPath = tmpDir.string() + std::string("/filelist.txt");
211 auto files = gen.setupFileUniverse(config.eventPoolPath);
212 BOOST_CHECK(files.size() == numfiles);
213
214 // remove the files
215 if (fs::exists(tmpDir)) {
216 fs::remove_all(tmpDir); // Remove all files and the directory
217 }
218};
219
220BOOST_AUTO_TEST_CASE(EventPool_Local_WrongPath)
221{
223 config.eventPoolPath = "/tmp/MyEvtPool/filelist_DOESNOTEXIST.txt";
225 auto files = gen.setupFileUniverse(config.eventPoolPath);
226 BOOST_CHECK(files.size() == 0);
227};
default_random_engine gen(dev())
int32_t i
int storeAsTFileAny(const T *obj, std::string const &path, std::map< std::string, std::string > const &metadata, long startValidityTimestamp=-1, long endValidityTimestamp=-1, std::vector< char >::size_type maxSize=0) const
Definition CcdbApi.h:157
void init(std::string const &hosts)
Definition CcdbApi.cxx:165
std::enable_if<!std::is_base_of< o2::conf::ConfigurableParam, T >::value, T * >::type retrieveFromTFileAny(std::string const &path, std::map< std::string, std::string > const &metadata, long timestamp=-1, std::map< std::string, std::string > *headers=nullptr, std::string const &etag="", const std::string &createdNotAfter="", const std::string &createdNotBefore="") const
Definition CcdbApi.h:660
static void setValue(std::string const &mainkey, std::string const &subkey, T x)
static void updateFromString(std::string const &)
static constexpr std::string_view eventpool_filename
GLuint start
Definition glcorearb.h:469
GLuint pathA
Definition glcorearb.h:5477
std::string to_string(gsl::span< T, Size > span)
Definition common.h:52
std::string ccdbUrl
bool hostReachable
BOOST_AUTO_TEST_CASE(pythia8_Pythia8GenConfig)
BOOST_CHECK(tree)
BOOST_CHECK_EQUAL(triggersD.size(), triggers.size())