ARTEMIS-CRIB
 
Loading...
Searching...
No Matches
TProcessorUtil.h
Go to the documentation of this file.
1/**
2 * @file TProcessorUtil.h
3 * @brief Utility functions for handling input and parameter objects in TEventCollection.
4 * @author Kodai Okawa<okawa@cns.s.u-tokyo.ac.jp>
5 * @date 2025-01-02 14:48:14
6 * @note last modified: 2025-03-05 18:34:27
7 * @details
8 */
9
10#ifndef CRIB_TPROCESSORUTIL_H_
11#define CRIB_TPROCESSORUTIL_H_
12
13#include <type_traits>
14#include <variant>
15
16#include <TClonesArray.h>
17#include <TEventCollection.h>
18#include <TString.h>
19
20namespace art::crib::util {
21
22/**
23 * @brief Retrieve an object from TEventCollection with type validation.
24 *
25 * @details
26 * This function checks if the specified object exists in the TEventCollection and verifies that its type matches the expected ROOT class type.
27 * If the object is of type TClonesArray, it further checks that the elements of the array match the specified element type.
28 *
29 * - `col->GetObjectRef(name)` is used to retrieve the object reference.
30 * - If the object type does not match `expectedTypeName`, an error message is returned.
31 * - For `TClonesArray`, the element type is verified using `GetClass`.
32 *
33 * @tparam T Expected type of the object, must derive from TObject.
34 * @param col Pointer to the TEventCollection.
35 * @param name Name of the object in the collection.
36 * @param expectedTypeName Expected ROOT class name of the object.
37 * @param elementTypeName (Optional) Expected type of elements if T is TClonesArray.
38 * @return std::variant containing a pointer to the object if successful, or a TString with an error message.
39 */
40template <typename T>
41std::enable_if_t<
42 std::is_base_of_v<TObject, T>,
43 std::variant<T **, TString>>
44GetInputObject(TEventCollection *col,
45 const TString &name,
46 const TString &expectedTypeName,
47 const TString &elementTypeName = "TObject") {
48 void **objRef = col->GetObjectRef(name);
49 if (!objRef) {
50 return TString::Format("No input collection '%s'", name.Data());
51 }
52
53 T **cast_obj = reinterpret_cast<T **>(objRef);
54
55 if constexpr (std::is_same_v<T, TClonesArray>) {
56 if (!cast_obj) {
57 return TString::Format("Input collection '%s' is null", name.Data());
58 }
59 const auto *cl = (*cast_obj)->GetClass();
60 if (!cl || !cl->InheritsFrom(elementTypeName)) {
61 return TString::Format("Invalid input collection '%s': not %s elements",
62 name.Data(), elementTypeName.Data());
63 }
64 } else {
65 TObject *obj = static_cast<TObject *>(*cast_obj);
66 if (!obj->InheritsFrom(expectedTypeName)) {
67 return TString::Format("Invalid input collection '%s': not %s",
68 name.Data(), expectedTypeName.Data());
69 }
70 }
71 return cast_obj;
72}
73
74/**
75 * @brief Retrieves a parameter object from a TEventCollection.
76 *
77 * @tparam T The type of the parameter object to retrieve. Must be derived from TObject.
78 * When T is TClonesArray, the element type is additionally validated.
79 * @param col Pointer to the TEventCollection from which the parameter object is obtained.
80 * @param name Name of the parameter object.
81 * @param expectedTypeName The expected type name for the parameter object.
82 * @param elementTypeName (Optional) For TClonesArray types, the expected type name for its elements
83 * (default is "art::TParameterObject").
84 * @return A std::variant containing either:
85 * - A pointer to the parameter object of type T if successful, or
86 * - A TString error message indicating the failure reason.
87 *
88 * @details
89 * The function retrieves the parameter object via col->GetInfo(name) and performs several checks:
90 * - If no object is found, an error message is returned.
91 * - The object is verified to inherit from the expected type.
92 * - If T is TClonesArray, the object's element type is also checked using its GetClass() method.
93 */
94template <typename T>
95std::enable_if_t<
96 std::is_base_of_v<TObject, T>,
97 std::variant<T *, TString>>
98GetParameterObject(TEventCollection *col,
99 const TString &name,
100 const TString &expectedTypeName,
101 const TString &elementTypeName = "art::TParameterObject") {
102 // Parameter objects can be get by `GetInfo`
103 auto *objRef = col->GetInfo(name);
104 if (!objRef) {
105 return TString::Format("No parameter object '%s'", name.Data());
106 }
107
108 auto *obj = static_cast<TObject *>(objRef);
109 if (!obj->InheritsFrom(expectedTypeName)) {
110 return TString::Format("Invalid input parameter '%s': not '%s'",
111 name.Data(), expectedTypeName.Data());
112 }
113
114 auto *prm_obj = static_cast<T *>(obj);
115 if constexpr (std::is_same_v<T, TClonesArray>) {
116 const auto *cl = prm_obj->GetClass();
117 if (!cl || !cl->InheritsFrom(elementTypeName)) {
118 return TString::Format("Invalid parameter element '%s': not %s",
119 name.Data(), elementTypeName.Data());
120 }
121 }
122 return prm_obj;
123}
124
125} // namespace art::crib::util
126
127#endif // CRIB_TPROCESSORUTIL_H
std::enable_if_t< std::is_base_of_v< TObject, T >, std::variant< T **, TString > > GetInputObject(TEventCollection *col, const TString &name, const TString &expectedTypeName, const TString &elementTypeName="TObject")
Retrieve an object from TEventCollection with type validation.
std::enable_if_t< std::is_base_of_v< TObject, T >, std::variant< T *, TString > > GetParameterObject(TEventCollection *col, const TString &name, const TString &expectedTypeName, const TString &elementTypeName="art::TParameterObject")
Retrieves a parameter object from a TEventCollection.
return to the guide