Loading [MathJax]/extensions/tex2jax.js
ATLAS Offline Software
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
Control/xAODRootAccess/Root/Utils.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 // STL include(s):
6 #include <functional>
7 #include <regex>
8 
9 // ROOT include(s):
10 #include <TError.h>
11 
12 // EDM include(s):
14 
15 // Local include(s):
18 
19 
20 namespace {
21 
30  void removeDefaultTemplateParameters( std::string& name,
31  const std::string& vectorName ) {
32 
33  size_t vecPos = 0;
34  while( ( vecPos = name.find( vectorName + "<", vecPos ) ) !=
35  std::string::npos ) {
36 
37  // Update the position to point at the end of the search string:
38  vecPos += ( vectorName.size() + 1 );
39 
40  // Look for "the comma" that we should remove everything
41  // after:
42  int open = 0;
43  size_t commaPos = std::string::npos;
44  for( size_t pos = vecPos; pos < name.size(); ++pos ) {
45  switch( name[ pos ] ) {
46  case '<':
47  ++open;
48  break;
49  case '>':
50  --open;
51  break;
52  case ',':
53  if( ! open ) {
54  commaPos = pos;
55  }
56  break;
57  default:
58  break;
59  }
60  if( commaPos != std::string::npos ) {
61  break;
62  }
63  }
64 
65  // If we didn't find a comma, then let's give up:
66  if( commaPos == std::string::npos ) {
67  continue;
68  }
69 
70  // If we did, then let's look for the closing '>' of the vector:
71  open = 0;
72  size_t closingPos = std::string::npos;
73  for( size_t pos = commaPos; pos < name.size(); ++pos ) {
74  switch( name[ pos ] ) {
75  case '<':
76  ++open;
77  break;
78  case '>':
79  if( ! open ) {
80  closingPos = pos;
81  break;
82  }
83  --open;
84  break;
85  default:
86  break;
87  }
88  if( closingPos != std::string::npos ) {
89  break;
90  }
91  }
92 
93  // It's bad if we didn't find the closing '>'...
94  if( closingPos == std::string::npos ) {
95  ::Error( "removeDefaultTemplateParameters",
96  XAOD_MESSAGE( "Couldn't find closing '>' in name %s" ),
97  name.c_str() );
98  return;
99  }
100 
101  // Remove the identified part of the string:
102  name.erase( commaPos, closingPos - commaPos );
103  }
104 
105  return;
106  }
107 
108 } // private namespace
109 
110 namespace xAOD {
111 
112  namespace Utils {
113 
125  SG::sgkey_t hash( const std::string& name ) {
126 
127  static constexpr SG::sgkey_t MASK = (~static_cast<SG::sgkey_t>(0)) >> 2;
128 
129  // The helper object:
130  static const std::hash< std::string > helper;
131  // Let the helper do the work:
132  return ( static_cast< SG::sgkey_t >( helper( name ) ) & MASK );
133  }
134 
144  std::string dynBranchPrefix( const std::string& key ) {
145 
146  // Make a copy of the key that we can modify:
147  std::string result = key;
148 
149  // If it ends in a dot, then let's exchange that dot for
150  // "Dyn.":
151  if( result[ result.size() - 1 ] == '.' ) {
152  result.replace( result.size() - 1, 1, "Dyn." );
153  }
154  // If it doesn't end in a dot, then let's just add "Dyn." at
155  // the end, and be done with it:
156  else {
157  result += "Dyn.";
158  }
159 
160  return result;
161  }
162 
170  std::string dynFieldPrefix( const std::string& key ) {
171  // Make a copy of the key that we can modify:
172  std::string result = key;
173 
174  // If it ends in a colon, then let's exchange that colon for
175  // "Dyn:":
176  if( result[ result.size() - 1 ] == ':' ) {
177  result.replace( result.size() - 1, 1, "Dyn:" );
178  }
179  // If it doesn't end in a colon, then let's just add "Dyn:" at
180  // the end:
181  else {
182  result += "Dyn:";
183  }
184 
185  return result;
186  }
187 
194  const std::type_info& getTypeInfo( EDataType type ) {
195 
196  switch( type ) {
197 
198  case kChar_t:
199  case kchar:
200  return typeid( Char_t );
201  case kUChar_t:
202  return typeid( UChar_t );
203  case kShort_t:
204  return typeid( Short_t );
205  case kUShort_t:
206  return typeid( UShort_t );
207  case kInt_t:
208  case kCounter:
209  return typeid( Int_t );
210  case kUInt_t:
211  case kBits:
212  case kDataTypeAliasUnsigned_t:
213  return typeid( UInt_t );
214  case kLong_t:
215  return typeid( Long_t );
216  case kULong_t:
217  return typeid( ULong_t );
218  case kFloat_t:
219  return typeid( Float_t );
220  case kDouble_t:
221  return typeid( Double_t );
222  case kDouble32_t:
223  return typeid( Double32_t );
224  case kBool_t:
225  return typeid( Bool_t );
226  case kLong64_t:
227  return typeid( Long64_t );
228  case kULong64_t:
229  return typeid( ULong64_t );
230  case kFloat16_t:
231  return typeid( Float16_t );
232  case kCharStar:
233  return typeid( char* );
234  case kVoid_t:
235  return typeid( void );
236  default:
237  ::Error( "xAOD::Utils::getTypeInfo",
238  XAOD_MESSAGE( "Unknown data type (%i) received" ),
239  static_cast< int >( type ) );
240  return typeid( void );
241  }
242  }
243 
251  char rootType( char typeidType ) {
252 
253  // Do the hard-coded translation:
254  switch( typeidType ) {
255 
256  case 'c':
257  return 'B';
258  break;
259  case 'h':
260  return 'b';
261  break;
262  case 's':
263  return 'S';
264  break;
265  case 't':
266  return 's';
267  break;
268  case 'i':
269  return 'I';
270  break;
271  case 'j':
272  return 'i';
273  break;
274  case 'f':
275  return 'F';
276  break;
277  case 'd':
278  return 'D';
279  break;
280  case 'x':
281  return 'L';
282  break;
283  case 'y':
284  case 'm': // Not sure how platform-independent this one is...
285  return 'l';
286  break;
287  case 'b':
288  return 'O';
289  break;
290  }
291 
292  // If we didn't find this type:
293  Error( "xAOD::Utils::rootType",
294  XAOD_MESSAGE( "Received an unknown type: %c" ), typeidType );
295  return '\0';
296  }
297 
310  std::string getTypeName( const std::type_info& ti ) {
311 
312  // Get the "raw" type name:
313  std::string result = AthContainers_detail::typeinfoName( ti );
314 
315  // If there are no "tricky" classes in the name, then we're done
316  // already:
317  if( ( result.find( "DataVector<" ) == std::string::npos ) &&
318  ( result.find( "std::vector<" ) == std::string::npos ) &&
319  ( result.find( "std::__1::vector<") == std::string::npos ) ) {
320  return result;
321  }
322 
323  // Deal with the vector types:
324  removeDefaultTemplateParameters( result, "DataVector" );
325  removeDefaultTemplateParameters( result, "std::vector" );
326  removeDefaultTemplateParameters( result, "std::__1::vector" );
327 
328  // Make sure that no ">>" parts are left in the type name:
329  size_t pos = 0;
330  while( ( pos = result.find( ">>" ) ) != std::string::npos ) {
331  result.replace( pos, 2, "> >" );
332  }
333 
334  // Remove all "__1" namespaces from the type name:
335  while( ( pos = result.find( "__1::" ) ) != std::string::npos ) {
336  result.replace( pos, 5, "" );
337  }
338 
339  // Return the massaged name:
340  return result;
341  }
350  std::string getFirstBranchMatch( TTree * tree,
351  const std::string& pre ) {
352  const TObjArray * pBranches = tree->GetListOfBranches();
353  const std::regex pattern( ".*" + pre + ".*" );
354 
355  for( int i = 0, nLast = pBranches->GetLast(); i <= nLast; ++i ) {
356  const std::string name = pBranches->At(i)->GetName();
357 
358  if( std::regex_match( name, pattern ) )
359  return name;
360 
361  }
362 
363  return pre;
364  }
365 
375  std::string getFirstFieldMatch(
376  RNTupleReader& ntupleReader,
377  const std::string& pre ) {
378  const std::regex pattern( ".*" + pre + ".*" );
379  ntupleReader.LoadEntry( 0 );
380  for( const auto& field :
381 #if ROOT_VERSION_CODE >= ROOT_VERSION( 6, 33, 0 )
382  ntupleReader.GetModel().GetConstFieldZero()
383  ) {
384  auto name = field.GetQualifiedFieldName();
385 #else
386  ntupleReader.GetModel().GetFieldZero().GetSubFields()
387  ) {
388  auto name = field->GetQualifiedFieldName();
389 #endif
390  if( std::regex_match( name, pattern ) ) {
391  return name;
392  }
393  }
394  return pre;
395  }
396 
403  ::Bool_t fieldExists(
404  std::string fieldName,
405  RNTupleReader& ntupleReader ) {
406  // If it cannot find a field id it will give the maximum value of
407  // unsigned long
408  if( ntupleReader.GetDescriptor().FindFieldId( fieldName ) ==
410  return kFALSE;
411  }
412  return kTRUE;
413  }
414 
415  } // namespace Utils
416 
417 } // namespace xAOD
mergePhysValFiles.pattern
pattern
Definition: DataQuality/DataQualityUtils/scripts/mergePhysValFiles.py:26
xAOD::name
name
Definition: TriggerMenuJson_v1.cxx:29
get_generator_info.result
result
Definition: get_generator_info.py:21
max
constexpr double max()
Definition: ap_fixedTest.cxx:33
tree
TChain * tree
Definition: tile_monitor.h:30
xAOD::Utils::dynFieldPrefix
std::string dynFieldPrefix(const std::string &key)
This function is used to figure out what to name dynamic auxiliary field coming from a container call...
Definition: Control/xAODRootAccess/Root/Utils.cxx:170
xAOD
ICaloAffectedTool is abstract interface for tools checking if 4 mom is in calo affected region.
Definition: ICaloAffectedTool.h:24
XAOD_MESSAGE
#define XAOD_MESSAGE(MESSAGE)
Simple macro for printing error/verbose messages.
Definition: Control/xAODRootAccess/xAODRootAccess/tools/Message.h:19
ReadOfcFromCool.field
field
Definition: ReadOfcFromCool.py:48
Utils.h
python.CaloAddPedShiftConfig.type
type
Definition: CaloAddPedShiftConfig.py:42
xAOD::Utils::dynBranchPrefix
std::string dynBranchPrefix(const std::string &key)
This function is used to figure out what to name dynamic auxiliary branches coming from a container c...
Definition: Control/xAODRootAccess/Root/Utils.cxx:144
runBeamSpotCalibration.helper
helper
Definition: runBeamSpotCalibration.py:112
PrepareReferenceFile.regex
regex
Definition: PrepareReferenceFile.py:43
xAOD::Utils::getFirstFieldMatch
std::string getFirstFieldMatch(RNTupleReader &ntupleReader, const std::string &pre)
This function is used to search for a field in an RNTupleReader that contains a given substring.
Definition: Control/xAODRootAccess/Root/Utils.cxx:375
Athena::typeinfoName
std::string typeinfoName(const std::type_info &ti)
Convert a type_info to a demangled string.
Definition: AthenaKernel/src/ClassName.cxx:23
xAOD::Utils::getTypeName
std::string getTypeName(const std::type_info &ti)
This function is necessary in order to create type names that ROOT can understand.
Definition: Control/xAODRootAccess/Root/Utils.cxx:310
lumiFormat.i
int i
Definition: lumiFormat.py:85
Message.h
taskman.fieldName
fieldName
Definition: taskman.py:492
error.h
Helper for emitting error messages.
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:228
xAOD::Utils::fieldExists
::Bool_t fieldExists(std::string fieldName, RNTupleReader &ntupleReader)
This function is used to check if a field exists in an RNTupleReader.
Definition: Control/xAODRootAccess/Root/Utils.cxx:403
SG::sgkey_t
uint32_t sgkey_t
Type used for hashed StoreGate key+CLID pairs.
Definition: CxxUtils/CxxUtils/sgkey_t.h:32
python.LumiBlobConversion.pos
pos
Definition: LumiBlobConversion.py:18
xAOD::Utils::getTypeInfo
const std::type_info & getTypeInfo(EDataType type)
This function is used when reading a primitive branch from an input file without the user explicitly ...
Definition: Control/xAODRootAccess/Root/Utils.cxx:194
Trk::open
@ open
Definition: BinningType.h:40
xAOD::Utils::getFirstBranchMatch
std::string getFirstBranchMatch(TTree *tree, const std::string &pre)
This function is used to search for a branch in a TTree that contains a given substring.
Definition: Control/xAODRootAccess/Root/Utils.cxx:350
L1Topo::Error
Error
The different types of error that can be flagged in the L1TopoRDO.
Definition: Error.h:16
Utils
Definition: CaloCellSelectorUtils.cxx:10
xAOD::Utils::hash
SG::sgkey_t hash(const std::string &name)
This function provides a hashed version of the key (branch) names used in the xAOD file,...
Definition: Control/xAODRootAccess/Root/Utils.cxx:125
RoiUtil::MASK
MASK
Definition: RoiSerialise.cxx:35
xAOD::Utils::rootType
char rootType(char typeidType)
This function is used internally in the code when creating primitive dynamic auxiliary branches.
Definition: Control/xAODRootAccess/Root/Utils.cxx:251
xAOD::Utils::RNTupleReader
ROOT::Experimental::RNTupleReader RNTupleReader
Definition: Control/xAODRootAccess/xAODRootAccess/tools/Utils.h:33
mapkey::key
key
Definition: TElectronEfficiencyCorrectionTool.cxx:37