11 #include "RConfigure.h"
30 using namespace ROOT::Detail::RDF;
31 using namespace ROOT::RDF;
41 const std::type_info &TypeName2TypeID(
const std::string &name)
43 if (
auto c = TClass::GetClass(name.c_str())) {
44 return *c->GetTypeInfo();
45 }
else if (name ==
"char" || name ==
"Char_t")
47 else if (name ==
"unsigned char" || name ==
"UChar_t")
48 return typeid(
unsigned char);
49 else if (name ==
"int" || name ==
"Int_t")
51 else if (name ==
"unsigned int" || name ==
"UInt_t")
52 return typeid(
unsigned int);
53 else if (name ==
"short" || name ==
"Short_t")
55 else if (name ==
"unsigned short" || name ==
"UShort_t")
56 return typeid(
unsigned short);
57 else if (name ==
"long" || name ==
"Long_t")
59 else if (name ==
"unsigned long" || name ==
"ULong_t")
60 return typeid(
unsigned long);
61 else if (name ==
"double" || name ==
"Double_t")
62 return typeid(double);
63 else if (name ==
"float" || name ==
"Float_t")
65 else if (name ==
"long long" || name ==
"long long int" || name ==
"Long64_t")
66 return typeid(Long64_t);
67 else if (name ==
"unsigned long long" || name ==
"unsigned long long int" || name ==
"ULong64_t")
68 return typeid(ULong64_t);
69 else if (name ==
"bool" || name ==
"Bool_t")
72 std::string msg(
"Cannot extract type_info of type ");
75 throw std::runtime_error(msg);
83 std::string TypeID2TypeName(
const std::type_info &
id)
85 if (
auto c = TClass::GetClass(
id)) {
87 }
else if (
id ==
typeid(
char))
89 else if (
id ==
typeid(
unsigned char))
90 return "unsigned char";
91 else if (
id ==
typeid(
int))
93 else if (
id ==
typeid(
unsigned int))
94 return "unsigned int";
95 else if (
id ==
typeid(
short))
97 else if (
id ==
typeid(
unsigned short))
98 return "unsigned short";
99 else if (
id ==
typeid(
long))
101 else if (
id ==
typeid(
unsigned long))
102 return "unsigned long";
103 else if (
id ==
typeid(
double))
105 else if (
id ==
typeid(
float))
107 else if (
id ==
typeid(Long64_t))
109 else if (
id ==
typeid(ULong64_t))
111 else if (
id ==
typeid(
bool))
117 std::string ComposeRVecTypeName(
const std::string &valueType)
119 return "ROOT::VecOps::RVec<" + valueType +
">";
122 std::string GetLeafTypeName(TLeaf *leaf,
const std::string &colName)
124 std::string colType = leaf->GetTypeName();
126 throw std::runtime_error(
"Could not deduce type of leaf " + colName);
127 if (leaf->GetLeafCount() !=
nullptr && leaf->GetLenStatic() == 1) {
129 colType = ComposeRVecTypeName(colType);
130 }
else if (leaf->GetLeafCount() ==
nullptr && leaf->GetLenStatic() > 1) {
132 colType = ComposeRVecTypeName(colType);
133 }
else if (leaf->GetLeafCount() !=
nullptr && leaf->GetLenStatic() > 1) {
135 throw std::runtime_error(
"TTree leaf " + colName +
136 " has both a leaf count and a static length. This is not supported.");
148 std::string GetBranchOrLeafTypeName(TTree &t,
const std::string &colName)
151 auto leaf = t.GetLeaf(colName.c_str());
153 const auto dotPos = colName.find_last_of(
'.');
154 const auto hasDot = dotPos != std::string::npos;
156 const auto branchName = colName.substr(0, dotPos);
157 const auto leafName = colName.substr(dotPos + 1);
158 leaf = t.GetLeaf(branchName.c_str(), leafName.c_str());
162 return GetLeafTypeName(leaf, colName);
165 auto branch = t.GetBranch(colName.c_str());
167 static const TClassRef tbranchelement(
"TBranchElement");
168 if (branch->InheritsFrom(tbranchelement)) {
169 auto be =
static_cast<TBranchElement *
>(branch);
170 if (
auto currentClass = be->GetCurrentClass())
171 return currentClass->GetName();
175 auto mother = be->GetMother();
176 if (mother && mother->InheritsFrom(tbranchelement)) {
177 auto beMom =
static_cast<TBranchElement *
>(mother);
178 auto beMomClass = beMom->GetClass();
179 if (beMomClass && 0 == strcmp(
"TClonesArray", beMomClass->GetName()))
180 return be->GetTypeName();
182 return be->GetClassName();
188 return std::string();
197 std::string ColumnName2ColumnTypeName(
const std::string &colName,
unsigned int namespaceID, TTree *tree,
198 RDataSource *ds,
bool isCustomColumn,
bool vector2rvec,
unsigned int customColID)
202 if (ds && ds->HasColumn(colName))
203 colType = ds->GetTypeName(colName);
205 if (colType.empty() && tree) {
206 colType = GetBranchOrLeafTypeName(*tree, colName);
207 if (vector2rvec && TClassEdit::IsSTLCont(colType) == ROOT::ESTLType::kSTLvector) {
208 std::vector<std::string> split;
210 TClassEdit::GetSplit(colType.c_str(), split, dummy);
211 auto &valueType = split[1];
212 colType = ComposeRVecTypeName(valueType);
216 if (colType.empty() && isCustomColumn) {
218 colType =
"__rdf" + std::to_string(namespaceID) +
"::" + colName + std::to_string(customColID) +
"_type";
222 throw std::runtime_error(
"Column \"" + colName +
223 "\" is not in a dataset and is not a custom column been defined.");
230 char TypeName2ROOTTypeName(
const std::string &b)
232 if (b ==
"Char_t" || b ==
"char")
234 if (b ==
"UChar_t" || b ==
"unsigned char")
236 if (b ==
"Short_t" || b ==
"short" || b ==
"short int")
238 if (b ==
"UShort_t" || b ==
"unsigned short" || b ==
"unsigned short int")
240 if (b ==
"Int_t" || b ==
"int")
242 if (b ==
"UInt_t" || b ==
"unsigned" || b ==
"unsigned int")
244 if (b ==
"Float_t" || b ==
"float")
246 if (b ==
"Double_t" || b ==
"double")
248 if (b ==
"Long64_t" || b ==
"long" || b ==
"long int")
250 if (b ==
"ULong64_t" || b ==
"unsigned long" || b ==
"unsigned long int")
252 if (b ==
"Bool_t" || b ==
"bool")
257 unsigned int GetNSlots()
259 unsigned int nSlots = 1;
261 if (ROOT::IsImplicitMTEnabled())
262 nSlots = ROOT::GetImplicitMTPoolSize();
270 std::vector<std::string> ReplaceDotWithUnderscore(
const std::vector<std::string> &columnNames)
272 auto newColNames = columnNames;
273 for (
auto &col : newColNames) {
274 const auto dotPos = col.find(
'.');
275 if (dotPos != std::string::npos && dotPos != col.size() - 1 && dotPos != 0u) {
277 std::replace(col.begin(), col.end(),
'.',
'_');
278 if (std::find(columnNames.begin(), columnNames.end(), col) != columnNames.end())
279 throw std::runtime_error(
"Column " + oldName +
" would be written as " + col +
280 " but this column already exists. Please use Alias to select a new name for " +
282 Info(
"Snapshot",
"Column %s will be saved as %s", oldName.c_str(), col.c_str());
289 void InterpreterDeclare(
const std::string &code)
291 if (!gInterpreter->Declare(code.c_str())) {
292 const auto msg =
"\nAn error occurred while jitting. The lines above might indicate the cause of the crash\n";
293 throw std::runtime_error(msg);
297 Long64_t InterpreterCalc(
const std::string &code,
const std::string &context)
299 TInterpreter::EErrorCode errorCode(TInterpreter::kNoError);
300 auto res = gInterpreter->Calc(code.c_str(), &errorCode);
301 if (errorCode != TInterpreter::EErrorCode::kNoError) {
302 std::string msg =
"\nAn error occurred while jitting";
303 if (!context.empty())
304 msg +=
" in " + context;
305 msg +=
". The lines above might indicate the cause of the crash\n";
306 throw std::runtime_error(msg);