ATLAS Offline Software
Loading...
Searching...
No Matches
xAOD::Utils Namespace Reference

Functions

SG::sgkey_t hash (const std::string &name)
 This function provides a hashed version of the key (branch) names used in the xAOD file, similar to how Athena saves hashed numbers in persistent ElementLinks.
std::string dynBranchPrefix (const std::string &key)
 This function is used to figure out what to name dynamic auxiliary branches coming from a container called key.
std::string dynFieldPrefix (const std::string &key)
 This function is used to figure out what to name dynamic auxiliary field coming from a container called key.
const std::type_info & getTypeInfo (EDataType type)
 This function is used when reading a primitive branch from an input file without the user explicitly asking for it.
const std::type_info & getTypeInfo (std::string_view typeName)
 Get the type info of a primitive variable, in an RNTuple.
bool isPrimitiveType (std::string_view typeName)
 Check if the type name describes a primitive type.
char rootType (char typeidType)
 This function is used internally in the code when creating primitive dynamic auxiliary branches.
std::string getTypeName (const std::type_info &ti)
 This function is necessary in order to create type names that ROOT can understand.
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.
std::string getFieldName (const ROOT::RNTupleDescriptor &descriptor, const std::string &key)
 This function is used to find the name of the field in an RNTuple that stores the object with a given key.
std::string timeToString (::Double_t secs)
 Function creating a human-readable elapsed time printout.
std::string sizeToString (::Long64_t bytes)
 Function for printing data sizes in human-readable format.
std::string speedToString (::Double_t bytespersec)
 Function for printing data processing speeds in a human-readable format.

Function Documentation

◆ dynBranchPrefix()

std::string xAOD::Utils::dynBranchPrefix ( const std::string & key)

This function is used to figure out what to name dynamic auxiliary branches coming from a container called key.

Get the dynamic auxiliary variable prefix based on a container name (for TTree).

It needs to work in sync with how it is done in the offline code, so will eventually probably end up in another package.

Parameters
keyThe key/branch name of the auxiliary container
Returns
The prefix of the dynamic variables created based on the contents of the object

Definition at line 147 of file Control/xAODRootAccess/Root/Utils.cxx.

147 {
148
149 // Make a copy of the key that we can modify:
150 std::string result = key;
151
152 // If it ends in a dot, then let's exchange that dot for
153 // "Dyn.":
154 if( result[ result.size() - 1 ] == '.' ) {
155 result.replace( result.size() - 1, 1, "Dyn." );
156 }
157 // If it doesn't end in a dot, then let's just add "Dyn." at
158 // the end, and be done with it:
159 else {
160 result += "Dyn.";
161 }
162
163 return result;
164 }

◆ dynFieldPrefix()

std::string xAOD::Utils::dynFieldPrefix ( const std::string & key)

This function is used to figure out what to name dynamic auxiliary field coming from a container called key.

Get the dynamic auxiliary variable prefix based on a container name (for RNTuple).

Parameters
keyThe key/field name of the auxiliary container
Returns
The prefix of the dynamic variables created based on the contents of the object

Definition at line 173 of file Control/xAODRootAccess/Root/Utils.cxx.

173 {
174 // Make a copy of the key that we can modify:
175 std::string result = key;
176
177 // If it ends in a colon, then let's exchange that colon for
178 // "Dyn:":
179 if( result[ result.size() - 1 ] == ':' ) {
180 result.replace( result.size() - 1, 1, "Dyn:" );
181 }
182 // If it doesn't end in a colon, then let's just add "Dyn:" at
183 // the end:
184 else {
185 result += "Dyn:";
186 }
187
188 return result;
189 }

◆ getFieldName()

std::string xAOD::Utils::getFieldName ( const ROOT::RNTupleDescriptor & descriptor,
const std::string & key )

This function is used to find the name of the field in an RNTuple that stores the object with a given key.

Get the name of the RNTuple field storing the object with a given key.

RNTuples store the auxiliary stores with an "Aux:" postfix instead of "Aux.". Athena also replaces all non-alphanumeric characters of the key with underscores when writing the fields (e.g. for trigger chain names containing '-' characters), so that name is used if no field is found for the key itself.

Parameters
descriptorThe descriptor of the RNTuple to search in
keyThe key (branch name) of the object
Returns
The name of the field to read the object from

Definition at line 426 of file Control/xAODRootAccess/Root/Utils.cxx.

427 {
428
429 std::string fieldName = key;
430 if( fieldName.ends_with( "Aux." ) ) {
431 fieldName.back() = ':';
432 }
433 if( descriptor.FindFieldId( fieldName ) !=
434 ROOT::kInvalidDescriptorId ) {
435 return fieldName;
436 }
437
438 // Replace the characters the same way as Athena does when writing.
439 fieldName = key;
440 std::replace_if( fieldName.begin(), fieldName.end(),
441 []( unsigned char c ) { return !std::isalnum( c ); },
442 '_' );
443 if( fieldName.ends_with( "Aux_" ) ) {
444 fieldName.back() = ':';
445 }
446 return fieldName;
447 }

◆ getFirstBranchMatch()

std::string xAOD::Utils::getFirstBranchMatch ( TTree * tree,
const std::string & pre )

This function is used to search for a branch in a TTree that contains a given substring.

Search for branches, returns search term on no result.

It returns the name of the first branch that contains the search term. If no branch is found, the function returns the search term itself.

Parameters
treeThe TTree to search in
preThe search term
Returns
The name of the first branch that contains the search term

Definition at line 397 of file Control/xAODRootAccess/Root/Utils.cxx.

398 {
399 const TObjArray * pBranches = tree->GetListOfBranches();
400 const std::regex pattern( ".*" + pre + ".*" );
401
402 for( int i = 0, nLast = pBranches->GetLast(); i <= nLast; ++i ) {
403 const std::string name = pBranches->At(i)->GetName();
404
405 if( std::regex_match( name, pattern ) )
406 return name;
407
408 }
409
410 return pre;
411 }
TChain * tree

◆ getTypeInfo() [1/2]

const std::type_info & xAOD::Utils::getTypeInfo ( EDataType type)

This function is used when reading a primitive branch from an input file without the user explicitly asking for it.

Get the type info of a primitive variable, as declared by ROOT.

Parameters
typeThe type taken from ROOT
Returns
The typeid of the primitive type in question

Definition at line 197 of file Control/xAODRootAccess/Root/Utils.cxx.

197 {
198
199 switch( type ) {
200
201 case kChar_t:
202 case kchar:
203 return typeid( Char_t );
204 case kUChar_t:
205 return typeid( UChar_t );
206 case kShort_t:
207 return typeid( Short_t );
208 case kUShort_t:
209 return typeid( UShort_t );
210 case kInt_t:
211 case kCounter:
212 return typeid( Int_t );
213 case kUInt_t:
214 case kBits:
215 case kDataTypeAliasUnsigned_t:
216 return typeid( UInt_t );
217 case kLong_t:
218 return typeid( Long_t );
219 case kULong_t:
220 return typeid( ULong_t );
221 case kFloat_t:
222 return typeid( Float_t );
223 case kDouble_t:
224 return typeid( Double_t );
225 case kDouble32_t:
226 return typeid( Double32_t );
227 case kBool_t:
228 return typeid( Bool_t );
229 case kLong64_t:
230 return typeid( Long64_t );
231 case kULong64_t:
232 return typeid( ULong64_t );
233 case kFloat16_t:
234 return typeid( Float16_t );
235 case kCharStar:
236 return typeid( char* );
237 case kVoid_t:
238 return typeid( void );
239 default:
240 ::Error( "xAOD::Utils::getTypeInfo",
241 XAOD_MESSAGE( "Unknown data type (%i) received" ),
242 static_cast< int >( type ) );
243 return typeid( void );
244 }
245 }
#define XAOD_MESSAGE(MESSAGE)
Simple macro for printing error/verbose messages.

◆ getTypeInfo() [2/2]

const std::type_info & xAOD::Utils::getTypeInfo ( std::string_view typeName)

Get the type info of a primitive variable, in an RNTuple.

Definition at line 268 of file Control/xAODRootAccess/Root/Utils.cxx.

268 {
270 ::Error("xAOD::Utils::getTypeInfo",
271 XAOD_MESSAGE("Unknown data type (%s) received"),
272 typeName.data());
273 return typeid(void);
274 }
#define TYPE_CHECK_INSTANTIATE(MACRO)
Macro instantiating another macro for all usual "primitive" types.
#define GETTYPEINFO_SPECIALIZE(TYPE)
Macro returning the type info for a given type name.

◆ getTypeName()

std::string xAOD::Utils::getTypeName ( const std::type_info & ti)

This function is necessary in order to create type names that ROOT can understand.

Get the type name as it is known to ROOT, based on std::type_info.

For instance, a type name like std::vector<ElementLink<DataVector<xAOD::IParticle, DataModel_detail::NoBase> >, std::allocator<ElementLink<DataVector<xAOD::IParticle, DataModel_detail::NoBase> > > > as seen by the compiler, needs to become std::vector<ElementLink<DataVector<xAOD::IParticle> > > for ROOT to find a dictionary for it.

This function executes the string manipulation necessary to do this.

Parameters
tiThe type for which we want to find a string type name
Returns
The type name in a format that ROOT understands

Definition at line 357 of file Control/xAODRootAccess/Root/Utils.cxx.

357 {
358
359 // Get the "raw" type name:
360 std::string result = AthContainers_detail::typeinfoName( ti );
361
362 // If there are no "tricky" classes in the name, then we're done
363 // already:
364 if( ( result.find( "DataVector<" ) == std::string::npos ) &&
365 ( result.find( "std::vector<" ) == std::string::npos ) &&
366 ( result.find( "std::__1::vector<") == std::string::npos ) ) {
367 return result;
368 }
369
370 // Deal with the vector types:
371 removeDefaultTemplateParameters( result, "DataVector" );
372 removeDefaultTemplateParameters( result, "std::vector" );
373 removeDefaultTemplateParameters( result, "std::__1::vector" );
374
375 // Make sure that no ">>" parts are left in the type name:
376 size_t pos = 0;
377 while( ( pos = result.find( ">>" ) ) != std::string::npos ) {
378 result.replace( pos, 2, "> >" );
379 }
380
381 // Remove all "__1" namespaces from the type name:
382 while( ( pos = result.find( "__1::" ) ) != std::string::npos ) {
383 result.replace( pos, 5, "" );
384 }
385
386 // Return the massaged name:
387 return result;
388 }

◆ hash()

SG::sgkey_t xAOD::Utils::hash ( const std::string & name)

This function provides a hashed version of the key (branch) names used in the xAOD file, similar to how Athena saves hashed numbers in persistent ElementLinks.

Function creating a hash out of a "key name".

Since Athena uses the top 2 bits in a hashed key to save additional state information about the link, the top 2 bits of the hash are always set to 0.

Parameters
nameThe name that we want to create a unique hash for
Returns
A more or less unique hash for the string

Definition at line 128 of file Control/xAODRootAccess/Root/Utils.cxx.

128 {
129
130 static constexpr SG::sgkey_t MASK = (~static_cast<SG::sgkey_t>(0)) >> 2;
131
132 // The helper object:
133 static const std::hash< std::string > helper;
134 // Let the helper do the work:
135 return ( static_cast< SG::sgkey_t >( helper( name ) ) & MASK );
136 }
uint32_t sgkey_t
Type used for hashed StoreGate key+CLID pairs.
Definition sgkey_t.h:32

◆ isPrimitiveType()

bool xAOD::Utils::isPrimitiveType ( std::string_view typeName)

Check if the type name describes a primitive type.

Definition at line 283 of file Control/xAODRootAccess/Root/Utils.cxx.

283 {
285 return false;
286 }
#define ISPRIMITIVETYPE_SPECIALIZE(TYPE)

◆ rootType()

char xAOD::Utils::rootType ( char typeidType)

This function is used internally in the code when creating primitive dynamic auxiliary branches.

Get the character describing a given primitive type for ROOT.

I just took the code from SFrame to be honest...

Parameters
typeidTypeThe type name coming from typeid(...).name()
Returns
The character describing this type for TTree::Branch(...)

Definition at line 298 of file Control/xAODRootAccess/Root/Utils.cxx.

298 {
299
300 // Do the hard-coded translation:
301 switch( typeidType ) {
302
303 case 'c':
304 return 'B';
305 break;
306 case 'h':
307 return 'b';
308 break;
309 case 's':
310 return 'S';
311 break;
312 case 't':
313 return 's';
314 break;
315 case 'i':
316 return 'I';
317 break;
318 case 'j':
319 return 'i';
320 break;
321 case 'f':
322 return 'F';
323 break;
324 case 'd':
325 return 'D';
326 break;
327 case 'x':
328 return 'L';
329 break;
330 case 'y':
331 case 'm': // Not sure how platform-independent this one is...
332 return 'l';
333 break;
334 case 'b':
335 return 'O';
336 break;
337 }
338
339 // If we didn't find this type:
340 Error( "xAOD::Utils::rootType",
341 XAOD_MESSAGE( "Received an unknown type: %c" ), typeidType );
342 return '\0';
343 }

◆ sizeToString()

std::string xAOD::Utils::sizeToString ( ::Long64_t bytes)

Function for printing data sizes in human-readable format.

This function is used to produce nicely readable printouts for amounts of data.

Parameters
bytesThe amount of data expressed in bytes
Returns
A human-readable printout of the data size

Definition at line 131 of file Event/xAOD/xAODCore/Root/Utils.cxx.

131 {
132
133 if( std::abs( bytes ) > 1e12 ) {
134 return std::format( "{} TB", bytes * 1e-12 );
135 } else if( std::abs( bytes ) > 1e9 ) {
136 return std::format( "{} GB", bytes * 1e-9 );
137 } else if( std::abs( bytes ) > 1e6 ) {
138 return std::format( "{} MB", bytes * 1e-6 );
139 } else if( std::abs( bytes ) > 1e3 ) {
140 return std::format( "{} kB", bytes * 1e-3 );
141 } else {
142 return std::format( "{} bytes", bytes );
143 }
144 }

◆ speedToString()

std::string xAOD::Utils::speedToString ( ::Double_t bytespersec)

Function for printing data processing speeds in a human-readable format.

Parameters
bytespersecThe speed expressed in bytes / seconds
Returns
A human-readable printout of the data processing speed

Definition at line 149 of file Event/xAOD/xAODCore/Root/Utils.cxx.

149 {
150
151 if( ::fabs( bytespersec ) > 1e12 ) {
152 return std::format( "{} TB/s", bytespersec * 1e-12 );
153 } else if( ::fabs( bytespersec ) > 1e9 ) {
154 return std::format( "{} GB/s", bytespersec * 1e-9 );
155 } else if( ::fabs( bytespersec ) > 1e6 ) {
156 return std::format( "{} MB/s", bytespersec * 1e-6 );
157 } else if( ::fabs( bytespersec ) > 1e3 ) {
158 return std::format( "{} kB/s", bytespersec * 1e-3 );
159 } else {
160 return std::format( "{} B/s", bytespersec );
161 }
162 }

◆ timeToString()

std::string xAOD::Utils::timeToString ( ::Double_t secs)

Function creating a human-readable elapsed time printout.

Since I wasn't able to find a nice function printing elapsed times in a human-readable format, I ended up writing one.

This function is used in printing the statistics about an analysis.

Parameters
secsAn amount of time passed, expressed in seconds
Returns
A formatted, human-readable version of the amount of time passed

Definition at line 101 of file Event/xAOD/xAODCore/Root/Utils.cxx.

101 {
102
103 // Do meat of the calculation:
104 const TimeStruct ts = timeToStruct( secs );
105
106 // Construct the string:
107 std::ostringstream result;
108 if( ts.days ) {
109 std::print (result, "{}d ", ts.days);
110 }
111 if( ts.hours || ts.days ) {
112 std::print (result, "{}h ", ts.hours);
113 }
114 if( ts.minutes || ts.hours || ts.days ) {
115 std::print (result, "{}m ", ts.minutes);
116 }
117 if( ts.seconds || ts.minutes || ts.hours || ts.days ) {
118 std::print (result, "{}s ", ts.seconds);
119 }
120 std::print (result, "{}ms", ts.milliseconds);
121
122 return result.str();
123 }
int ts
Definition globals.cxx:24