Skip to content

Commit af12c27

Browse files
committed
Introduce helper function to load MClabels from a TTree/TFile
Function returns a constant MC label container. The persistent format can be either a MCTruthContainer or IOMCTruthContainer with special split format.
1 parent 20f07e6 commit af12c27

4 files changed

Lines changed: 86 additions & 0 deletions

File tree

DataFormats/simulation/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ o2_add_library(SimulationDataFormat
2020
src/MCUtils.cxx
2121
src/O2DatabasePDG.cxx
2222
src/InteractionSampler.cxx
23+
src/ConstMCTruthContainer.cxx
2324
PUBLIC_LINK_LIBRARIES Microsoft.GSL::GSL
2425
FairRoot::Base
2526
O2::DetectorsCommonDataFormats

DataFormats/simulation/include/SimulationDataFormat/ConstMCTruthContainer.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
#include <Framework/Traits.h>
2222
#endif
2323

24+
class TTree;
25+
2426
namespace o2
2527
{
2628
namespace dataformats
@@ -211,6 +213,14 @@ class ConstMCTruthContainerView
211213
using ConstMCLabelContainer = o2::dataformats::ConstMCTruthContainer<o2::MCCompLabel>;
212214
using ConstMCLabelContainerView = o2::dataformats::ConstMCTruthContainerView<o2::MCCompLabel>;
213215

216+
class MCLabelIOHelper
217+
{
218+
public:
219+
/// Convenience function to loads MC labels for some entry from a TTree and TBranch.
220+
/// Labels can be stored as either MCTruthContainer or IOMCTruthContainer. The caller takes ownership of the returned pointer.
221+
static ConstMCTruthContainer<o2::MCCompLabel>* loadFromTTree(TTree* tree, std::string const& brname, int entry);
222+
};
223+
214224
} // namespace dataformats
215225
} // namespace o2
216226

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
2+
// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3+
// All rights not expressly granted are reserved.
4+
//
5+
// This software is distributed under the terms of the GNU General Public
6+
// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7+
//
8+
// In applying this license CERN does not waive the privileges and immunities
9+
// granted to it by virtue of its status as an Intergovernmental Organization
10+
// or submit itself to any jurisdiction.
11+
12+
#include <SimulationDataFormat/ConstMCTruthContainer.h>
13+
#include <SimulationDataFormat/MCTruthContainer.h>
14+
#include <SimulationDataFormat/IOMCTruthContainerView.h>
15+
#include <SimulationDataFormat/MCCompLabel.h>
16+
#include <TFile.h>
17+
#include <TTree.h>
18+
#include <iostream>
19+
20+
using namespace o2::dataformats;
21+
22+
ConstMCTruthContainer<o2::MCCompLabel>* MCLabelIOHelper::loadFromTTree(TTree* tree, std::string const& brname, int entry)
23+
{
24+
o2::dataformats::MCTruthContainer<o2::MCCompLabel>* labels = nullptr;
25+
o2::dataformats::IOMCTruthContainerView* labelROOTbuffer = nullptr;
26+
o2::dataformats::ConstMCTruthContainer<o2::MCCompLabel>* constlabels = new ConstMCTruthContainer<o2::MCCompLabel>();
27+
28+
auto branch = tree->GetBranch(brname.c_str());
29+
if (!branch) {
30+
return nullptr;
31+
}
32+
auto labelClass = branch->GetClassName();
33+
bool oldlabelformat = false;
34+
35+
// we check how the labels are persisted and react accordingly
36+
if (TString(labelClass).Contains("IOMCTruthContainer")) {
37+
branch->SetAddress(&labelROOTbuffer);
38+
branch->GetEntry(entry);
39+
if (labelROOTbuffer) {
40+
labelROOTbuffer->copyandflatten(*constlabels);
41+
delete labelROOTbuffer;
42+
}
43+
} else {
44+
// case when we directly streamed MCTruthContainer
45+
// TODO: support more cases such as ConstMCTruthContainer etc
46+
std::string serializedtype("o2::dataformats::MCTruthContainer<o2::MCCompLabel>");
47+
if (!TString(labelClass).EqualTo(serializedtype.c_str())) {
48+
std::cerr << "Error: expected serialized type " << serializedtype << " but found " << labelClass;
49+
return nullptr;
50+
}
51+
branch->SetAddress(&labels);
52+
branch->GetEntry(entry);
53+
if (labels) {
54+
labels->flatten_to(*constlabels);
55+
delete labels;
56+
}
57+
}
58+
return constlabels;
59+
}

DataFormats/simulation/test/testMCTruthContainer.cxx

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,7 @@ BOOST_AUTO_TEST_CASE(MCTruthContainer_ROOTIO)
355355
TFile f("tmp2.root", "RECREATE");
356356
TTree tree("o2sim", "o2sim");
357357
auto br = tree.Branch("Labels", &io, 32000, 2);
358+
tree.Branch("LabelsOriginal", &container, 32000, 2);
358359
tree.Fill();
359360
tree.Write();
360361
f.Close();
@@ -383,6 +384,21 @@ BOOST_AUTO_TEST_CASE(MCTruthContainer_ROOTIO)
383384
BOOST_CHECK(cc.getLabels(BIGSIZE - 1).size() == 2);
384385
BOOST_CHECK(cc.getLabels(BIGSIZE - 1)[0] == TruthElement(BIGSIZE - 1, BIGSIZE - 1, BIGSIZE - 1));
385386
BOOST_CHECK(cc.getLabels(BIGSIZE - 1)[1] == TruthElement(BIGSIZE, BIGSIZE - 1, BIGSIZE - 1));
387+
388+
// testing convenience API to retrieve a constant label container from a ROOT file, entry 0
389+
auto cont = o2::dataformats::MCLabelIOHelper::loadFromTTree(tree2, "Labels", 0);
390+
auto cont2 = o2::dataformats::MCLabelIOHelper::loadFromTTree(tree2, "LabelsOriginal", 0);
391+
392+
BOOST_CHECK(cont);
393+
BOOST_CHECK(cont2);
394+
BOOST_CHECK(cont->getNElements() == (BIGSIZE - 1) * 2);
395+
BOOST_CHECK(cont2->getNElements() == (BIGSIZE - 1) * 2);
396+
BOOST_CHECK(cont->getLabels(0).size() == 0);
397+
BOOST_CHECK(cont2->getLabels(0).size() == 0);
398+
BOOST_CHECK(cont->getLabels(BIGSIZE - 1)[0] == TruthElement(BIGSIZE - 1, BIGSIZE - 1, BIGSIZE - 1));
399+
BOOST_CHECK(cont->getLabels(BIGSIZE - 1)[1] == TruthElement(BIGSIZE, BIGSIZE - 1, BIGSIZE - 1));
400+
BOOST_CHECK(cont2->getLabels(BIGSIZE - 1)[0] == TruthElement(BIGSIZE - 1, BIGSIZE - 1, BIGSIZE - 1));
401+
BOOST_CHECK(cont2->getLabels(BIGSIZE - 1)[1] == TruthElement(BIGSIZE, BIGSIZE - 1, BIGSIZE - 1));
386402
}
387403

388404
} // namespace o2

0 commit comments

Comments
 (0)