forked from Kavaldrin/AliceO2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfigurationMacroHelper.h
More file actions
73 lines (61 loc) · 2.35 KB
/
ConfigurationMacroHelper.h
File metadata and controls
73 lines (61 loc) · 2.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Copyright CERN and copyright holders of ALICE O2. This software is
// distributed under the terms of the GNU General Public License v3 (GPL
// Version 3), copied verbatim in the file "COPYING".
//
// See http://alice-o2.web.cern.ch/license for full licensing information.
//
// In applying this license CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.
/// \author R+Preghenella - February 2020
#ifndef ALICEO2_EVENTGEN_CONFIGURATIONMACRO_H_
#define ALICEO2_EVENTGEN_CONFIGURATIONMACRO_H_
#include "FairLogger.h"
#include "TROOT.h"
#include "TSystem.h"
#include "TGlobal.h"
#include "TFunction.h"
#include <string>
namespace o2
{
namespace eventgen
{
template <typename T>
T GetFromMacro(const std::string file, const std::string funcname, const std::string type, const std::string unique)
{
/** tweak the string to get the required global function **/
auto func = funcname;
if (func.empty()) {
auto size = file.size();
auto firstindex = file.find_last_of("/") + 1;
auto lastindex = file.find_last_of(".");
func = file.substr(firstindex < size ? firstindex : 0,
lastindex < size ? lastindex - firstindex : size - firstindex) +
"()";
}
auto gfunc = func.substr(0, func.find_first_of('('));
/** load macro is global function is not already defined **/
if (!gROOT->GetGlobalFunction(gfunc.c_str())) {
if (gROOT->LoadMacro(file.c_str()) != 0) {
LOG(FATAL) << "Cannot find " << file;
return nullptr;
}
if (!gROOT->GetGlobalFunction(gfunc.c_str())) {
LOG(FATAL) << "Global function '" << gfunc << "' not defined";
return nullptr;
}
}
/** check the return type matches the required one **/
if (strcmp(gROOT->GetGlobalFunction(gfunc.c_str())->GetReturnTypeName(), type.c_str())) {
LOG(INFO) << "Global function '" << gfunc << "' does not return a '" << type << "' type";
return nullptr;
}
/** process function and retrieve pointer to the returned type **/
gROOT->ProcessLine(Form("%s __%s__ = %s;", type.c_str(), unique.c_str(), func.c_str()));
auto ptr = (T*)gROOT->GetGlobal(Form("__%s__", unique.c_str()))->GetAddress();
/** success **/
return *ptr;
}
} // namespace eventgen
} // namespace o2
#endif /* ALICEO2_EVENTGEN_CONFIGURATIONMACRO_H_ */