DAS  3.0
Das Analysis System
FriendUtils.cc File Reference
#include <boost/test/included/unit_test.hpp>
#include <filesystem>
#include <TFile.h>
#include "FriendUtils.h"
#include "FileUtils.h"
+ Include dependency graph for FriendUtils.cc:

Macros

#define DOXYGEN_SHOULD_SKIP_THIS
 
#define BOOST_TEST_MODULE   testFriendUtils
 

Functions

void createFile (const char *name, int first, int last)
 
 BOOST_AUTO_TEST_CASE (slice)
 
 BOOST_AUTO_TEST_CASE (pure_root_without_ranges)
 
 BOOST_AUTO_TEST_CASE (slice_element)
 

Macro Definition Documentation

◆ BOOST_TEST_MODULE

#define BOOST_TEST_MODULE   testFriendUtils

◆ DOXYGEN_SHOULD_SKIP_THIS

#define DOXYGEN_SHOULD_SKIP_THIS

SPDX-License-Idendifier: GPL-3.0-or-later SPDX-FileCopyrightText: Louis Moureaux louis.nosp@m..mou.nosp@m.reaux.nosp@m.@cer.nosp@m.n.ch

Function Documentation

◆ BOOST_AUTO_TEST_CASE() [1/3]

BOOST_AUTO_TEST_CASE ( pure_root_without_ranges  )
90  {
91  createFile("friends-hybrid-step0-0.root", 0, 10);
92  createFile("friends-hybrid-step0-1.root", 10, 20);
93 
94  // Create a slice spanning events 5 to 15.
95  auto chain = make_unique<TChain>("tree");
96  chain->Add("friends-hybrid-step0-0.root");
97  chain->Add("friends-hybrid-step0-1.root");
98  chain->GetEntry(0); // necessary to load a(ny) TTree
99 
100  {
101  auto newTree = make_unique<TTree>("tree", "");
102 
103  // Add (empty) entries to the tree.
104  for (int i = 0; i < chain->GetEntries(); ++i) {
105  newTree->Fill();
106  }
107 
108  // Save the tree to a file, otherwise GetEntry() doesn't load branches
109  // from the friend.
110  TFile f("friends-hybrid-step1.root", "RECREATE");
111  f.WriteObject(newTree.get(), "tree");
112  }
113 
114  // try to read with a TChain
115  {
116  auto tIn = new TChain("tree");
117  tIn->Add("friends-hybrid-step1.root");
118  tIn->AddFriend(chain.get(), "step0"); // chain with chain
119  tIn->GetEntry(0);
120 
121  // Now iterate over the tree and check the contents of the branches
122  // inherited from the friends.
123  int index = -1, value = -1;
124  BOOST_REQUIRE( tIn->SetBranchAddress("index", &index) == 0 );
125  BOOST_REQUIRE( tIn->SetBranchAddress("value", &value) == 0 );
126 
127  for (int i = 0; i < tIn->GetEntries(); ++i) {
128  BOOST_TEST( tIn->GetEntry(i) > 0 );
129  BOOST_TEST( index == i );
130  BOOST_TEST( value == 0 );
131  }
132  }
133  }

◆ BOOST_AUTO_TEST_CASE() [2/3]

BOOST_AUTO_TEST_CASE ( slice  )

Tests ChainSlice.

43  {
44  // Create two files with "index" ranging from 0 to 9 then 10 to 19.
45  createFile("friends-ChainSlice-file0.root", 0, 10);
46  createFile("friends-ChainSlice-file1.root", 10, 20);
47 
48  // Create a ChainSlice spanning part of the two input files.
49  auto chain = make_unique<ChainSlice>("tree");
50  chain->Add("friends-ChainSlice-file0.root");
51  chain->Add("friends-ChainSlice-file1.root");
52 
53  // Initially the slice covers everything.
54  BOOST_TEST( chain->GetBegin() == 0 );
55  BOOST_TEST( chain->GetEnd() == -1 );
56  BOOST_TEST( chain->GetEntries() == 20 );
57  BOOST_TEST( chain->GetEntriesFast() == 20 );
58 
59  // Check that we can load value correctly.
60  int index;
61  chain->SetBranchAddress("index", &index);
62 
63  chain->GetEntry(5); // From first file
64  BOOST_TEST( chain->GetReadEntry() == 5 );
65  BOOST_TEST( index == 5 );
66 
67  chain->GetEntry(15); // From second file
68  BOOST_TEST( chain->GetReadEntry() == 15 );
69  BOOST_TEST( index == 15 );
70 
71  // Now we set Begin = 10 => only the second file is available
72  chain->SetBegin(10);
73  BOOST_TEST( chain->GetBegin() == 10 );
74  BOOST_TEST( chain->GetEnd() == -1 );
75  BOOST_TEST( chain->GetEntries() == 10 );
76  BOOST_TEST( chain->GetEntriesFast() == 10 );
77 
78  chain->GetEntry(5); // From second file
79  BOOST_TEST( chain->GetReadEntry() == 5 );
80  BOOST_TEST( index == 15 );
81 
82  // Remove the last 3 elements. What is left is [10,17[.
83  chain->SetEnd(17);
84  BOOST_TEST( chain->GetEntries() == 7 );
85  BOOST_TEST( chain->GetEntriesFast() == 7 );
86  BOOST_TEST( chain->GetReadEntry() == 5 ); // Changing the bounds does not do GetEntry()
87  }

◆ BOOST_AUTO_TEST_CASE() [3/3]

BOOST_AUTO_TEST_CASE ( slice_element  )

Tests SlicedFriendElement.

This test uses two input files and checks that we can load a subset of them through another tree to which a SlicedFriendElement has been added.

143  {
144  createFile("friends-SlicedFriendElement-step0-0.root", 0, 10);
145  createFile("friends-SlicedFriendElement-step0-1.root", 10, 20);
146 
147  const int begin = 5, end = 15;
148  {
149  // Create a slice spanning events 5 to 15.
150  auto inputSlice = make_unique<ChainSlice>("tree");
151  inputSlice->Add("friends-SlicedFriendElement-step0-0.root");
152  inputSlice->Add("friends-SlicedFriendElement-step0-1.root");
153  inputSlice->SetBegin(begin);
154  inputSlice->SetEnd(end);
155 
156  // Set up a new tree and give it the other two as friends.
157  auto newTree = make_unique<TTree>("tree", "step1");
158  SlicedFriendElement::AddTo(newTree.get(), inputSlice.get(), "step0");
159 
160  // Add (empty) entries to the tree.
161  for (int i = 0; i < inputSlice->GetEntries(); ++i) {
162  newTree->Fill();
163  }
164 
165  // Save the tree to a file, otherwise GetEntry() doesn't load branches
166  // from the friend.
167  TFile f("friends-SlicedFriendElement-step1.root", "RECREATE");
168  f.WriteObject(newTree.get(), "tree");
169  }
170 
171  // try to read with a standard TChain
172  {
173  auto chainIn = make_unique<TChain>("tree");
174  chainIn->Add("friends-SlicedFriendElement-step1.root");
175  chainIn->GetEntry(0); // necessary for `TChain::GetTree()`
176  auto tIn = chainIn->GetTree();
177  auto friends = tIn->GetListOfFriends();
178  auto fe = dynamic_cast<TFriendElement *>(friends->At(0));
179  friends->Clear("nodelete");
180  auto chainslice = fe->GetTree();
181  chainIn->AddFriend(chainslice);
182  chainIn->GetEntry(0); // necessary for `TChain::SetBranchAddress()`
183 
184  // Now iterate over the tree and check the contents of the branches
185  // inherited from the friends.
186  int index = -1, value = -1;
187  BOOST_REQUIRE( chainIn->SetBranchAddress("index", &index) == 0 );
188  BOOST_REQUIRE( chainIn->SetBranchAddress("value", &value) == 0 );
189 
190  // entry 0 in chainIn should be entry 5 in the first tree
191  for (int i = 0; i < chainIn->GetEntries(); ++i) {
192  BOOST_TEST( chainIn->GetEntry(i) > 0 );
193  BOOST_TEST( index == i + begin );
194  BOOST_TEST( value == 0 );
195  }
196  }
197 
198  // try to read with a custom ChainSlice
199  {
200  auto chainIn = make_unique<ChainSlice>("tree");
201  chainIn->Add("friends-SlicedFriendElement-step1.root");
202  chainIn->GetEntry(0); // all the gymnastic of the previous block is done inside the override of `GetEntry()`
203 
204  // Now iterate over the tree and check the contents of the branches
205  // inherited from the friends.
206  int index = -1, value = -1;
207  BOOST_REQUIRE( chainIn->SetBranchAddress("index", &index) == 0 );
208  BOOST_REQUIRE( chainIn->SetBranchAddress("value", &value) == 0 );
209 
210  for (int i = 0; i < chainIn->GetEntries(); ++i) {
211  BOOST_TEST( chainIn->GetEntry(i) > 0 );
212  BOOST_TEST( index == i + begin );
213  BOOST_TEST( value == 0 );
214  }
215  }
216  }

◆ createFile()

void createFile ( const char *  name,
int  first,
int  last 
)

Creates a file with a tree, and fills the index branch in the tree with numbers in [first, last).

23 {
24  TFile file(name, "RECREATE");
25  TTree tree("", "");
26 
27  int index, value = 0;
28  tree.Branch("index", &index);
29  tree.Branch("value", &value); // This will be a constant 0
30 
31  for (index = first; index < last; ++index) tree.Fill();
32 
33  file.WriteObject(&tree, "tree");
34  file.Close();
35 }
DYToLL_M-50_13TeV_pythia8_cff_GEN_SIM_RECOBEFMIX_DIGI_L1_DIGI2RAW_L1Reco_RECO.name
name
Definition: DYToLL_M-50_13TeV_pythia8_cff_GEN_SIM_RECOBEFMIX_DIGI_L1_DIGI2RAW_L1Reco_RECO.py:48
BOOST_TEST
BOOST_TEST(gendijet.Rapidity()==1.3573785791881385)
Ntupliser_cfg.f
f
Definition: Ntupliser_cfg.py:252
createFile
void createFile(const char *name, int first, int last)
Definition: FriendUtils.cc:22