File indexing completed on 2025-01-18 10:10:45
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016 #ifndef ROOT7_RNTuplerImporter
0017 #define ROOT7_RNTuplerImporter
0018
0019 #include <ROOT/REntry.hxx>
0020 #include <ROOT/RError.hxx>
0021 #include <ROOT/RField.hxx>
0022 #include <ROOT/RNTupleCollectionWriter.hxx>
0023 #include <ROOT/RNTupleModel.hxx>
0024 #include <ROOT/RNTupleWriteOptions.hxx>
0025 #include <ROOT/RNTupleWriter.hxx>
0026 #include <string_view>
0027
0028 #include <TFile.h>
0029 #include <TTree.h>
0030
0031 #include <cstdlib>
0032 #include <map>
0033 #include <memory>
0034 #include <vector>
0035
0036 class TLeaf;
0037
0038 namespace ROOT {
0039 namespace Experimental {
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070
0071
0072
0073
0074
0075
0076
0077
0078
0079
0080
0081
0082
0083
0084
0085
0086
0087
0088
0089
0090
0091
0092
0093
0094
0095
0096
0097
0098
0099
0100
0101
0102
0103 class RNTupleImporter {
0104 public:
0105
0106 class RProgressCallback {
0107 public:
0108 virtual ~RProgressCallback() = default;
0109 void operator()(std::uint64_t nbytesWritten, std::uint64_t neventsWritten)
0110 {
0111 Call(nbytesWritten, neventsWritten);
0112 }
0113 virtual void Call(std::uint64_t nbytesWritten, std::uint64_t neventsWritten) = 0;
0114 virtual void Finish(std::uint64_t nbytesWritten, std::uint64_t neventsWritten) = 0;
0115 };
0116
0117 private:
0118 struct RImportBranch {
0119 RImportBranch() = default;
0120 RImportBranch(const RImportBranch &other) = delete;
0121 RImportBranch(RImportBranch &&other) = default;
0122 RImportBranch &operator=(const RImportBranch &other) = delete;
0123 RImportBranch &operator=(RImportBranch &&other) = default;
0124 std::string fBranchName;
0125 std::unique_ptr<unsigned char[]> fBranchBuffer;
0126 };
0127
0128 struct RImportField {
0129 RImportField() = default;
0130 ~RImportField() = default;
0131 RImportField(const RImportField &other) = delete;
0132 RImportField(RImportField &&other) = default;
0133 RImportField &operator=(const RImportField &other) = delete;
0134 RImportField &operator=(RImportField &&other) = default;
0135
0136
0137 RFieldBase *fField = nullptr;
0138 std::unique_ptr<RFieldBase::RValue> fValue;
0139 void *fFieldBuffer = nullptr;
0140 bool fIsInUntypedCollection = false;
0141 bool fIsClass = false;
0142 };
0143
0144
0145 struct RImportTransformation {
0146 std::size_t fImportBranchIdx = 0;
0147 std::size_t fImportFieldIdx = 0;
0148
0149 RImportTransformation(std::size_t branchIdx, std::size_t fieldIdx)
0150 : fImportBranchIdx(branchIdx), fImportFieldIdx(fieldIdx)
0151 {
0152 }
0153 virtual ~RImportTransformation() = default;
0154 virtual RResult<void> Transform(const RImportBranch &branch, RImportField &field) = 0;
0155 virtual void ResetEntry() = 0;
0156 };
0157
0158
0159
0160 struct RImportGuard {
0161 RNTupleImporter &fImporter;
0162
0163 explicit RImportGuard(RNTupleImporter &importer) : fImporter(importer) {}
0164 RImportGuard(const RImportGuard &) = delete;
0165 RImportGuard &operator=(const RImportGuard &) = delete;
0166 RImportGuard(RImportGuard &&) = delete;
0167 RImportGuard &operator=(RImportGuard &&) = delete;
0168 ~RImportGuard() { fImporter.ResetSchema(); }
0169 };
0170
0171
0172
0173 struct RImportLeafCountCollection {
0174 RImportLeafCountCollection() = default;
0175 RImportLeafCountCollection(const RImportLeafCountCollection &other) = delete;
0176 RImportLeafCountCollection(RImportLeafCountCollection &&other) = default;
0177 RImportLeafCountCollection &operator=(const RImportLeafCountCollection &other) = delete;
0178 RImportLeafCountCollection &operator=(RImportLeafCountCollection &&other) = default;
0179 std::unique_ptr<RNTupleModel> fCollectionModel;
0180 std::shared_ptr<RNTupleCollectionWriter> fCollectionWriter;
0181 std::unique_ptr<REntry> fCollectionEntry;
0182
0183
0184 std::unique_ptr<Int_t> fCountVal;
0185 std::vector<size_t> fImportFieldIndexes;
0186
0187 std::vector<std::unique_ptr<RImportTransformation>> fTransformations;
0188 Int_t fMaxLength = 0;
0189 std::string fFieldName;
0190 };
0191
0192
0193 struct RCStringTransformation : public RImportTransformation {
0194 RCStringTransformation(std::size_t b, std::size_t f) : RImportTransformation(b, f) {}
0195 ~RCStringTransformation() override = default;
0196 RResult<void> Transform(const RImportBranch &branch, RImportField &field) final;
0197 void ResetEntry() final {}
0198 };
0199
0200
0201
0202
0203 struct RLeafArrayTransformation : public RImportTransformation {
0204 std::int64_t fNum = 0;
0205 RLeafArrayTransformation(std::size_t b, std::size_t f) : RImportTransformation(b, f) {}
0206 ~RLeafArrayTransformation() override = default;
0207 RResult<void> Transform(const RImportBranch &branch, RImportField &field) final;
0208 void ResetEntry() final { fNum = 0; }
0209 };
0210
0211 RNTupleImporter() = default;
0212
0213 std::unique_ptr<TFile> fSourceFile;
0214 TTree *fSourceTree;
0215
0216 std::string fDestFileName;
0217 std::string fNTupleName;
0218 std::unique_ptr<TFile> fDestFile;
0219 RNTupleWriteOptions fWriteOptions;
0220
0221
0222
0223 bool fConvertDotsInBranchNames = false;
0224
0225
0226 std::int64_t fMaxEntries = -1;
0227
0228
0229 bool fIsQuiet = false;
0230 std::unique_ptr<RProgressCallback> fProgressCallback;
0231
0232 std::unique_ptr<RNTupleModel> fModel;
0233 std::unique_ptr<REntry> fEntry;
0234 std::vector<RImportBranch> fImportBranches;
0235 std::vector<RImportField> fImportFields;
0236
0237 std::map<std::string, RImportLeafCountCollection> fLeafCountCollections;
0238
0239 std::vector<std::unique_ptr<RImportTransformation>> fImportTransformations;
0240
0241 ROOT::Experimental::RResult<void> InitDestination(std::string_view destFileName);
0242
0243 void ResetSchema();
0244
0245
0246 RResult<void> PrepareSchema();
0247 void ReportSchema();
0248
0249 public:
0250 RNTupleImporter(const RNTupleImporter &other) = delete;
0251 RNTupleImporter &operator=(const RNTupleImporter &other) = delete;
0252 RNTupleImporter(RNTupleImporter &&other) = delete;
0253 RNTupleImporter &operator=(RNTupleImporter &&other) = delete;
0254 ~RNTupleImporter() = default;
0255
0256
0257 static std::unique_ptr<RNTupleImporter>
0258 Create(std::string_view sourceFileName, std::string_view treeName, std::string_view destFileName);
0259
0260
0261 static std::unique_ptr<RNTupleImporter> Create(TTree *sourceTree, std::string_view destFileName);
0262
0263 RNTupleWriteOptions GetWriteOptions() const { return fWriteOptions; }
0264 void SetWriteOptions(RNTupleWriteOptions options) { fWriteOptions = options; }
0265 void SetNTupleName(const std::string &name) { fNTupleName = name; }
0266 void SetMaxEntries(std::uint64_t maxEntries) { fMaxEntries = maxEntries; };
0267
0268
0269
0270 void SetConvertDotsInBranchNames(bool value) { fConvertDotsInBranchNames = value; }
0271
0272
0273 void SetIsQuiet(bool value) { fIsQuiet = value; }
0274
0275
0276
0277
0278
0279
0280 void Import();
0281 };
0282
0283 }
0284 }
0285
0286 #endif