Skip to content

Commit

Permalink
[treereader] Allow reading Foo<Double32_t> into Foo<double>:
Browse files Browse the repository at this point in the history
As the TClass corresponding to the template parameter of TTreeReaderValue is
found through the typeid => TClass map, make sure that we ignore mismatches
of `Foo<Double32_t>` vs `Foo<double>` - they should not be relevant for reading.

This fixes reading a LorentzVector<Double32_t>.

Fixes root-project#12334
  • Loading branch information
Axel-Naumann committed Mar 20, 2023
1 parent 3fe106b commit 4991532
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 2 deletions.
3 changes: 2 additions & 1 deletion tree/treeplayer/src/TTreeReaderValue.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -553,8 +553,9 @@ void ROOT::Internal::TTreeReaderValueBase::CreateProxy() {
auto dictAsClass = dynamic_cast<TClass*>(fDict);
auto branchActualTypeAsClass = dynamic_cast<TClass*>(branchActualType);
auto inheritance = dictAsClass && branchActualTypeAsClass && branchActualTypeAsClass->InheritsFrom(dictAsClass);
bool typeinfoMatch = dictAsClass && dictAsClass->GetTypeInfo() && dictAsClass->GetTypeInfo() == branchActualTypeAsClass->GetTypeInfo();

if (fDict != branchActualType && !inheritance) {
if (!inheritance && !typeinfoMatch && fDict != branchActualType) {
TDataType *dictdt = dynamic_cast<TDataType*>(fDict);
TDataType *actualdt = dynamic_cast<TDataType*>(branchActualType);
bool complainAboutMismatch = true;
Expand Down
3 changes: 2 additions & 1 deletion tree/treeplayer/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
if(NOT MSVC OR win_broken_tests)
ROOT_ADD_UNITTEST_DIR(TreePlayer)
add_custom_command(TARGET treetreeplayertestUnit POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/data.h data.h)
COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/data.h data.h
COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/branchobject.root branchobject.root)
endif()

if(imt)
Expand Down
35 changes: 35 additions & 0 deletions tree/treeplayer/test/branchobject.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <TTreeReader.h>
#include <TTreeReaderValue.h>
#include <TH1D.h>
#include <TInterpreter.h>

#include "gtest/gtest.h"

Expand Down Expand Up @@ -38,3 +39,37 @@ TEST(TTreeReaderBasic, BranchObject)
EXPECT_STREQ(rv->ClassName(), "TH1D");
EXPECT_DOUBLE_EQ(static_cast<TH1D *>(rv.Get())->GetMean(), 42.);
}

// Issue #12334
// branchobject.root created as
//
// ~~~ {.cpp}
// f = new TFile("branchobject.root", "RECREATE");
// TTree *t = new TTree("branchobject", "test tree for branchobject.cxx");
// ROOT::Math::LorentzVector<ROOT::Math::PtEtaPhiM4D<Double32_t>> lv(1., 2., 3., 4.);
// t->Branch("lv32", &lv);
// t->Fill();
// t->Write();
// ~~~
TEST(TTreeReaderBasic, LorentzVector32)
{
// Ensure that the mismatching `<double>` specialization is available, i.e. will
// be chosen given the typeid of the TTreeReaderValue template argument.
TClass::GetClass("ROOT::Math::LorentzVector<ROOT::Math::PtEtaPhiM4D<double> >");

TFile file("branchobject.root");
TTreeReader reader("branchobject");
std::string code;
{
std::stringstream sstr;
sstr << "TTreeReaderValue<ROOT::Math::PtEtaPhiMVector> lv32(*(TTreeReader*)"
<< &reader << ", \"lv32\");";
code = sstr.str();
}
gInterpreter->Declare(code.c_str());
ASSERT_TRUE(reader.Next());
EXPECT_EQ(gInterpreter->Calc("int(lv32->pt() + 0.5)"), 1);
EXPECT_EQ(gInterpreter->Calc("int(lv32->eta() + 0.5)"), 2);
EXPECT_EQ(gInterpreter->Calc("int(lv32->phi() + 0.5)"), 3);
EXPECT_EQ(gInterpreter->Calc("int(lv32->M() + 0.5)"), 4);
}
Binary file added tree/treeplayer/test/branchobject.root
Binary file not shown.

0 comments on commit 4991532

Please sign in to comment.