Logo ROOT   6.10/09
Reference Guide
TDFUtils.cxx
Go to the documentation of this file.
1 // Author: Enrico Guiraud, Danilo Piparo CERN 03/2017
2 
3 /*************************************************************************
4  * Copyright (C) 1995-2016, Rene Brun and Fons Rademakers. *
5  * All rights reserved. *
6  * *
7  * For the licensing terms see $ROOTSYS/LICENSE. *
8  * For the list of contributors see $ROOTSYS/README/CREDITS. *
9  *************************************************************************/
10 
11 #include "RConfigure.h" // R__USE_IMT
12 #include "ROOT/TDFNodes.hxx" // ColumnName2ColumnTypeName requires TCustomColumnBase
13 #include "ROOT/TDFUtils.hxx"
14 #include "TBranch.h"
15 #include "TBranchElement.h"
16 #include "TClassRef.h"
17 #include "TROOT.h" // IsImplicitMTEnabled, GetImplicitMTPoolSize
18 
19 #include <stdexcept>
20 #include <string>
21 class TTree;
22 using namespace ROOT::Detail::TDF;
23 
24 namespace ROOT {
25 namespace Internal {
26 namespace TDF {
27 
28 /// Return a string containing the type of the given branch. Works both with real TTree branches and with temporary
29 /// column created by Define.
30 std::string ColumnName2ColumnTypeName(const std::string &colName, TTree *tree, TCustomColumnBase *tmpBranch)
31 {
32  TBranch* branch = nullptr;
33  if (tree) branch = tree->GetBranch(colName.c_str());
34  if (!branch and !tmpBranch) {
35  throw std::runtime_error("Column \"" + colName + "\" is not in a file and has not been defined.");
36  }
37  if (branch) {
38  // this must be a real TTree branch
39  static const TClassRef tbranchelRef("TBranchElement");
40  if (branch->InheritsFrom(tbranchelRef)) {
41  return static_cast<TBranchElement *>(branch)->GetClassName();
42  } else { // Try the fundamental type
43  auto title = branch->GetTitle();
44  auto typeCode = title[strlen(title) - 1];
45  if (typeCode == 'B')
46  return "char";
47  else if (typeCode == 'b')
48  return "unsigned char";
49  else if (typeCode == 'I')
50  return "int";
51  else if (typeCode == 'i')
52  return "unsigned int";
53  else if (typeCode == 'S')
54  return "short";
55  else if (typeCode == 's')
56  return "unsigned short";
57  else if (typeCode == 'D')
58  return "double";
59  else if (typeCode == 'F')
60  return "float";
61  else if (typeCode == 'L')
62  return "Long64_t";
63  else if (typeCode == 'l')
64  return "ULong64_t";
65  else if (typeCode == 'O')
66  return "bool";
67  }
68  } else {
69  // this must be a temporary branch
70  const auto &type_id = tmpBranch->GetTypeId();
71  if (auto c = TClass::GetClass(type_id)) {
72  return c->GetName();
73  } else if (type_id == typeid(char))
74  return "char";
75  else if (type_id == typeid(unsigned char))
76  return "unsigned char";
77  else if (type_id == typeid(int))
78  return "int";
79  else if (type_id == typeid(unsigned int))
80  return "unsigned int";
81  else if (type_id == typeid(short))
82  return "short";
83  else if (type_id == typeid(unsigned short))
84  return "unsigned short";
85  else if (type_id == typeid(long))
86  return "long";
87  else if (type_id == typeid(unsigned long))
88  return "unsigned long";
89  else if (type_id == typeid(double))
90  return "double";
91  else if (type_id == typeid(float))
92  return "float";
93  else if (type_id == typeid(Long64_t))
94  return "Long64_t";
95  else if (type_id == typeid(ULong64_t))
96  return "ULong64_t";
97  else if (type_id == typeid(bool))
98  return "bool";
99  else {
100  std::string msg("Cannot deduce type of temporary column ");
101  msg += colName.c_str();
102  msg += ". The typename is ";
103  msg += tmpBranch->GetTypeId().name();
104  msg += ".";
105  throw std::runtime_error(msg);
106  }
107  }
108 
109  std::string msg("Cannot deduce type of column ");
110  msg += colName.c_str();
111  msg += ".";
112  throw std::runtime_error(msg);
113 }
114 
115 const char *ToConstCharPtr(const char *s)
116 {
117  return s;
118 }
119 
120 const char *ToConstCharPtr(const std::string s)
121 {
122  return s.c_str();
123 }
124 
125 unsigned int GetNSlots()
126 {
127  unsigned int nSlots = 1;
128 #ifdef R__USE_IMT
130 #endif // R__USE_IMT
131  return nSlots;
132 }
133 
134 void CheckTmpBranch(std::string_view branchName, TTree *treePtr)
135 {
136  if (treePtr != nullptr) {
137  std::string branchNameInt(branchName);
138  auto branch = treePtr->GetBranch(branchNameInt.c_str());
139  if (branch != nullptr) {
140  auto msg = "branch \"" + branchNameInt + "\" already present in TTree";
141  throw std::runtime_error(msg);
142  }
143  }
144 }
145 
146 /// Returns local BranchNames or default BranchNames according to which one should be used
147 const ColumnNames_t &PickBranchNames(unsigned int nArgs, const ColumnNames_t &bl, const ColumnNames_t &defBl)
148 {
149  bool useDefBl = false;
150  if (nArgs != bl.size()) {
151  if (bl.size() == 0 && nArgs == defBl.size()) {
152  useDefBl = true;
153  } else {
154  auto msg = "mismatch between number of filter/define arguments (" + std::to_string(nArgs) +
155  ") and number of columns specified (" + std::to_string(bl.size() ? bl.size() : defBl.size()) +
156  "). Please check the number of arguments of the function/lambda/functor and the number of branches "
157  "specified.";
158  throw std::runtime_error(msg);
159  }
160  }
161 
162  return useDefBl ? defBl : bl;
163 }
164 
165 } // end NS TDF
166 } // end NS Internal
167 } // end NS ROOT
UInt_t GetImplicitMTPoolSize()
Returns the size of the pool used for implicit multi-threading.
Definition: TROOT.cxx:567
long long Long64_t
Definition: RtypesCore.h:69
Namespace for new ROOT classes and functions.
Definition: StringConv.hxx:21
TTree()
Default constructor and I/O constructor.
Definition: TTree.cxx:652
const ColumnNames_t & PickBranchNames(unsigned int nArgs, const ColumnNames_t &bl, const ColumnNames_t &defBl)
Returns local BranchNames or default BranchNames according to which one should be used...
Definition: TDFUtils.cxx:147
virtual Bool_t InheritsFrom(const char *classname) const
Returns kTRUE if object inherits from class "classname".
Definition: TObject.cxx:436
std::string ColumnName2ColumnTypeName(const std::string &colName, TTree *tree, TCustomColumnBase *tmpBranch)
Return a string containing the type of the given branch.
Definition: TDFUtils.cxx:30
A Branch for the case of an object.
virtual const std::type_info & GetTypeId() const =0
unsigned long long ULong64_t
Definition: RtypesCore.h:70
static TClass * GetClass(const char *name, Bool_t load=kTRUE, Bool_t silent=kFALSE)
Static method returning pointer to TClass of the specified class name.
Definition: TClass.cxx:2885
TClassRef is used to implement a permanent reference to a TClass object.
Definition: TClassRef.h:29
Bool_t IsImplicitMTEnabled()
Returns true if the implicit multi-threading in ROOT is enabled.
Definition: TROOT.cxx:552
const char * ToConstCharPtr(const std::string s)
Definition: TDFUtils.cxx:120
unsigned int GetNSlots()
Definition: TDFUtils.cxx:125
void CheckTmpBranch(std::string_view branchName, TTree *treePtr)
Definition: TDFUtils.cxx:134
Definition: tree.py:1
A TTree is a list of TBranches.
Definition: TBranch.h:57
virtual const char * GetTitle() const
Returns title of object.
Definition: TNamed.h:48