Project
Loading...
Searching...
No Matches
ConfigurationMacroHelper.h
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
13
14#ifndef ALICEO2_CONF_CONFIGURATIONMACRO_H_
15#define ALICEO2_CONF_CONFIGURATIONMACRO_H_
16
17#include "TROOT.h"
18#include "TSystem.h"
19#include "TGlobal.h"
20#include "TFunction.h"
21#include <fairlogger/Logger.h>
22#include <string>
23
24namespace o2
25{
26namespace conf
27{
28
29template <typename T>
30T GetFromMacro(const std::string& file, const std::string& funcname, const std::string& type, const std::string& unique)
31{
32
34 auto func = funcname;
35 if (func.empty()) {
36 auto size = file.size();
37 auto firstindex = file.find_last_of("/") + 1;
38 auto lastindex = file.find_last_of(".");
39 func = file.substr(firstindex < size ? firstindex : 0,
40 lastindex < size ? lastindex - firstindex : size - firstindex) +
41 "()";
42 }
43 auto gfunc = func.substr(0, func.find_first_of('('));
44
46 if (!gROOT->GetGlobalFunction(gfunc.c_str())) {
47 if (gROOT->LoadMacro(file.c_str()) != 0) {
48 LOG(fatal) << "Cannot find " << file;
49 return nullptr;
50 }
51 if (!gROOT->GetGlobalFunction(gfunc.c_str())) {
52 LOG(fatal) << "Global function '" << gfunc << "' not defined";
53 return nullptr;
54 }
55 }
56
58 auto returnedtype = gROOT->GetGlobalFunction(gfunc.c_str())->GetReturnTypeName();
59 if (strcmp(returnedtype, type.c_str())) {
60 LOG(info) << "Global function '" << gfunc << "' does not return a '" << type << "' type ( but " << returnedtype << " )";
61 return nullptr;
62 }
63
65 gROOT->ProcessLine(Form("%s __%s__ = %s;", type.c_str(), unique.c_str(), func.c_str()));
66 auto ptr = (T*)gROOT->GetGlobal(Form("__%s__", unique.c_str()))->GetAddress();
67
69 return *ptr;
70}
71
72// just-in-time interpret some C++ function using ROOT and make result available to runtime
73// functiondecl: A string coding the function to call (example "bool foo(){ return true; }")
74// funcname: The name of the function to call (example "foo()")
75// type: Return type of function (example "bool")
76// unique: Some unique string identifier under which the result will be stored in gROOT global variable space
77template <typename T>
78T JITAndEvalFunction(const std::string& functiondecl, const std::string& funcname, const std::string& type, const std::string& unique)
79{
81 auto line = Form("%s; %s __%s__ = %s;", functiondecl.c_str(), type.c_str(), unique.c_str(), funcname.c_str());
82 gROOT->ProcessLine(line);
83 auto ptr = (T*)gROOT->GetGlobal(Form("__%s__", unique.c_str()))->GetAddress();
84 return *ptr;
85}
86
87} // namespace conf
88} // namespace o2
89
90#endif /* ALICEO2_CONF_CONFIGURATIONMACRO_H_ */
TBranch * ptr
GLenum func
Definition glcorearb.h:778
GLsizeiptr size
Definition glcorearb.h:659
GLint GLint GLsizei GLint GLenum GLenum type
Definition glcorearb.h:275
T JITAndEvalFunction(const std::string &functiondecl, const std::string &funcname, const std::string &type, const std::string &unique)
T GetFromMacro(const std::string &file, const std::string &funcname, const std::string &type, const std::string &unique)
a couple of static helper functions to create timestamp values for CCDB queries or override obsolete ...
LOG(info)<< "Compressed in "<< sw.CpuTime()<< " s"