40constexpr const char *kFileName =
"ntpl019_attributes.root";
41constexpr const char *kNTupleName =
"ntpl";
57 auto file = std::unique_ptr<TFile>(
TFile::Open(kFileName,
"RECREATE"));
62 attrModel->SetDescription(
"Metadata containing the events' provenance");
63 auto pRunNumber = attrModel->MakeField<std::int32_t>(
"runNumber");
66 auto attrSet =
writer->CreateAttributeSet(std::move(attrModel),
"Provenance");
78 auto attrRange = attrSet->BeginRange();
88 for (
int i = 0; i < 100; ++i) {
89 *pEvent = {rng.
Rndm()};
93 if (i % 10 == 0 && i > 0) {
96 attrSet->CommitRange(std::move(attrRange));
98 attrRange = attrSet->BeginRange();
109 attrSet->CommitRange(std::move(attrRange));
113static bool IsGoodRunNumber(std::int32_t runNo)
125 std::cout <<
"Here are the attribute sets linked to the RNTuple '" << kNTupleName <<
"':\n";
126 for (
const auto &attrSetDesc : reader->GetDescriptor().GetAttrSetIterable()) {
127 std::cout <<
" " << attrSetDesc.GetName() <<
"\n";
131 auto attrSet = reader->OpenAttributeSet(
"Provenance");
133 auto pRunNumber = attrSet->GetModel().GetDefaultEntry().GetPtr<std::int32_t>(
"runNumber");
135 std::cout <<
"\nOpened attribute set '" << attrSet->GetDescriptor().GetName() <<
"' with description: \""
136 << attrSet->GetDescriptor().GetDescription() <<
"\"\n";
138 for (
auto mainIdx : reader->GetEntryRange()) {
141 std::cout <<
"Entry " << mainIdx <<
" has the following attributes associated to it:\n";
142 for (
auto attrIdx : attrSet->GetAttributes(mainIdx)) {
143 auto range = attrSet->LoadEntry(attrIdx);
144 std::cout <<
" runNumber = " << *pRunNumber <<
" (valid for range [" << *range.GetFirst() <<
", "
145 << *range.GetLast() <<
"])\n";
150 auto pEvent = reader->GetModel().GetDefaultEntry().GetPtr<
Event>(
"event");
151 for (
auto attrIdx : attrSet->GetAttributes()) {
152 auto range = attrSet->LoadEntry(attrIdx);
154 if (IsGoodRunNumber(*pRunNumber)) {
155 std::cout <<
"\nRun " << *pRunNumber <<
" is good. Events:\n";
156 for (
auto mainIdx = range.GetStart(); mainIdx < range.GetEnd(); ++mainIdx) {
157 reader->LoadEntry(mainIdx);
158 std::cout <<
" Event " << mainIdx <<
" with pt = " << pEvent->pt <<
"\n";
164void ntpl019_attributes()
static std::unique_ptr< RNTupleModel > Create()
static std::unique_ptr< RNTupleReader > Open(std::string_view ntupleName, std::string_view storage, const ROOT::RNTupleReadOptions &options=ROOT::RNTupleReadOptions())
Open an RNTuple for reading.
static std::unique_ptr< RNTupleWriter > Append(std::unique_ptr< ROOT::RNTupleModel > model, std::string_view ntupleName, TDirectory &fileOrDirectory, const ROOT::RNTupleWriteOptions &options=ROOT::RNTupleWriteOptions())
Creates an RNTupleWriter that writes into an existing TFile or TDirectory, without overwriting its co...
static TFile * Open(const char *name, Option_t *option="", const char *ftitle="", Int_t compress=ROOT::RCompressionSetting::EDefaults::kUseCompiledDefault, Int_t netopt=0)
Create / open a file.
Random number generator class based on M.
Double_t Rndm() override
Machine independent random number generator.