1 #define ROOTHISTCNV_RCWNTUPLECNV_CPP 27 return sizeof( T ) * src.
length();
34 return sizeof( T ) * target.
length();
37 template <
typename POD>
38 decltype(
auto ) downcast_item(
const INTupleItem& i ) {
41 template <
typename POD>
45 template <
typename POD,
typename T>
46 void downcast_item( T&& ) =
delete;
48 template <
typename Item,
typename F>
49 decltype(
auto ) visit( Item& i, F&& f ) {
52 return f( downcast_item<int>( i ) );
54 return f( downcast_item<char>( i ) );
56 return f( downcast_item<short>( i ) );
58 return f( downcast_item<long>( i ) );
60 return f( downcast_item<long long>( i ) );
62 return f( downcast_item<unsigned char>( i ) );
64 return f( downcast_item<unsigned short>( i ) );
66 return f( downcast_item<unsigned int>( i ) );
68 return f( downcast_item<unsigned long>( i ) );
70 return f( downcast_item<unsigned long long>( i ) );
72 return f( downcast_item<double>( i ) );
74 return f( downcast_item<float>( i ) );
76 return f( downcast_item<bool>( i ) );
91 long item_size =
sizeof( T );
92 long dimension = it->
length();
93 long ndim = it->
ndim() - 1;
99 if ( ind_blk != block_name ) {
100 std::cerr <<
"ERROR: Index for CWNT variable " << ind_var <<
" is in a different block: " << ind_blk
104 }
else if ( it->
dim( ndim ) > 1 ) {
108 for (
int i = ndim - 1; i >= 0; i-- ) {
115 lowerRange = it->
range().lower();
116 upperRange = it->
range().upper();
122 size += item_size * dimension;
132 log <<
MSG::VERBOSE <<
"created tree id: " << rtree->GetName() <<
" title: " << nt->
title() <<
" desc: " << desc
138 long lowerRange, upperRange;
140 long cursize, oldsize = 0;
147 for (
const auto& i : nt->
items() ) {
150 visit( *i, [&](
const auto& data ) {
151 analyzeItem( this->
rootVarType( data.type() ), &data, item, block_name, var_name, lowerRange, upperRange, size );
155 cursize = size - oldsize;
157 log <<
MSG::VERBOSE <<
"item: " << item <<
" type " << i->type() <<
" blk: " << block_name <<
" var: " << var_name
158 <<
" rng: " << lowerRange <<
" " << upperRange <<
" sz: " << size <<
" " << cursize
159 <<
" buf_pos: " << size - cursize <<
endmsg;
162 item_buf_pos.
push_back( size - cursize );
165 item_range_lower.
push_back( lowerRange );
166 item_range_upper.
push_back( upperRange );
172 char* buff = nt->
setBuffer(
new char[size] );
174 log <<
MSG::VERBOSE <<
"Created buffer size: " << size <<
" at " << (
void*)buff << endmsg;
179 char* buf_pos = buff;
184 unsigned int i_item = 0;
185 for (
auto itr = item_name.
cbegin(); itr !=
end; ++itr, ++i_item ) {
187 buf_pos = buff + item_buf_pos[i_item];
196 #if ROOT_VERSION_CODE >= ROOT_VERSION( 5, 15, 0 ) 197 auto br =
new TBranch( rtree,
199 TBranch* br =
new TBranch(
201 item_fullname[i_item].c_str(), buf_pos, itr->second.c_str() );
203 if ( itr->first !=
"AUTO_BLK" ) {
205 title = itr->first +
"::" + br->GetTitle();
206 br->SetTitle( title.
c_str() );
209 log <<
MSG::DEBUG <<
"adding TBranch " << br->GetTitle() <<
" at " << (
void*)buf_pos << endmsg;
212 if ( item_range_lower[i_item] < item_range_upper[i_item] ) {
216 TLeafI* index =
nullptr;
217 TObject* tobj = br->GetListOfLeaves()->FindObject( item_fullname[i_item].c_str() );
218 if ( tobj->IsA()->InheritsFrom(
"TLeafI" ) ) {
219 index =
dynamic_cast<TLeafI*
>( tobj );
222 index->SetMaximum( item_range_upper[i_item] );
226 log <<
MSG::ERROR <<
"Could dynamic cast to TLeafI: " << item_fullname[i_item] <<
endmsg;
231 rtree->GetListOfBranches()->Add( br );
247 const auto& items = nt->
items();
249 return dest + visit( *i, [dest](
const auto& item ) {
return saveItem( dest, item ); } );
261 if ( ievt >= rtree->GetEntries() ) {
263 log <<
MSG::ERROR <<
"no more entries in tree to read. max: " << rtree->GetEntries() <<
" current: " << ievt
268 rtree->GetEvent( ievt );
272 auto& items = ntup->
items();
275 return src + visit( *i, [src](
auto& item ) {
return loadItem( src, item ); } );
299 std::string itemName, indexName, item_type, itemTitle, blockName;
301 long size, totsize = 0;
308 TObjArray* lbr = tree->GetListOfBranches();
310 while ( TObject* tobjb = bitr() ) {
312 TBranch* br = (TBranch*)tobjb;
313 itemTitle = br->GetTitle();
315 int ipos = itemTitle.
find(
"::" );
317 blockName = itemTitle.
substr( 0, ipos );
322 TObjArray* lf = br->GetListOfLeaves();
325 while ( TObject* tobj = litr() ) {
327 bool hasRange =
false;
333 TLeaf* tl =
dynamic_cast<TLeaf*
>( tobj );
334 itemName = tl->GetName();
340 if ( blockName !=
"" ) {
341 log <<
MSG::DEBUG <<
"loading NTuple item " << blockName <<
"/" << itemName;
343 log <<
MSG::DEBUG <<
"loading NTuple item " << itemName;
347 TLeaf* indexLeaf = tl->GetLeafCounter( arraySize );
349 if ( arraySize == 0 ) { log <<
MSG::ERROR <<
"TLeaf counter size = 0. This should not happen!" <<
endmsg; }
354 indexName = indexLeaf->GetName();
356 indexRange = indexLeaf->GetMaximum();
357 itemSize = indexRange * tl->GetLenType() * arraySize;
359 log <<
"[" << indexName;
362 if ( arraySize != 1 ) { log <<
"][" << arraySize; }
366 itemSize = tl->GetLenType() * arraySize;
370 if ( arraySize == 1 ) {
374 log <<
"[" << arraySize <<
"]";
386 hasRange = tl->IsRange();
391 if ( tobj->IsA()->InheritsFrom(
"TLeafI" ) ) {
393 TLeafI* tli =
dynamic_cast<TLeafI*
>( tobj );
394 if ( tli->IsUnsigned() ) {
395 unsigned long min = 0,
max = 0;
397 min = tli->GetMinimum();
398 max = tli->GetMaximum();
401 item =
createNTupleItem( itemName, blockName, indexName, indexRange, arraySize, min,
max, ntup );
405 min = tli->GetMinimum();
406 max = tli->GetMaximum();
409 item =
createNTupleItem( itemName, blockName, indexName, indexRange, arraySize, min,
max, ntup );
413 }
else if ( tobj->IsA()->InheritsFrom(
"TLeafF" ) ) {
416 TLeafF* tlf =
dynamic_cast<TLeafF*
>( tobj );
418 min = float( tlf->GetMinimum() );
419 max = float( tlf->GetMaximum() );
422 item =
createNTupleItem( itemName, blockName, indexName, indexRange, arraySize, min,
max, ntup );
425 }
else if ( tobj->IsA()->InheritsFrom(
"TLeafD" ) ) {
426 double min = 0.,
max = 0.;
428 TLeafD* tld =
dynamic_cast<TLeafD*
>( tobj );
430 min = tld->GetMinimum();
431 max = tld->GetMaximum();
434 item =
createNTupleItem( itemName, blockName, indexName, indexRange, arraySize, min,
max, ntup );
443 log <<
MSG::ERROR <<
"Unable to create ntuple item \"" << itemName <<
"\"" <<
endmsg;
449 log <<
MSG::DEBUG <<
"Total buffer size of NTuple: " << totsize <<
" Bytes." <<
endmsg;
451 char* buf = ntup->
setBuffer(
new char[totsize] );
455 for (
const auto& iitr : itemList ) {
456 TLeaf* leaf = iitr.first;
457 int isize = iitr.second;
459 log <<
MSG::VERBOSE <<
"setting TBranch " << leaf->GetBranch()->GetName() <<
" buffer at " << (
void*)bufpos
462 leaf->GetBranch()->SetAddress( (
void*)bufpos );
480 if ( totsize != ts ) { log <<
MSG::ERROR <<
"buffer size mismatch: " << ts <<
" " << totsize <<
endmsg; }
std::string getDirectory()
virtual const std::string & name() const =0
Access _Item name.
Definition of the MsgStream class used to transmit messages.
virtual const ItemRange & range() const =0
Access the range if specified.
bool parseName(const std::string &full, std::string &blk, std::string &var)
EventIDBase min(const EventIDBase &lhs, const EventIDBase &rhs)
virtual StatusCode add(INTupleItem *item)=0
Add an item row to the N tuple.
constexpr static const auto SUCCESS
SmartIF< IMessageSvc > & msgSvc() const
Retrieve pointer to message service.
#define DECLARE_CONVERTER(x)
Converter of Column-wise NTuple into ROOT format.
StatusCode book(const std::string &desc, INTuple *pObject, TTree *&tree) override
Book the N tuple.
EventIDBase max(const EventIDBase &lhs, const EventIDBase &rhs)
constexpr auto size(const C &c) noexcept(noexcept(c.size())) -> decltype(c.size())
NTuple interface class definition.
virtual void reset()=0
Reset all entries to their default values.
virtual long ndim() const =0
Dimension.
NTuple interface class definition.
virtual long length() const =0
Access the buffer length.
virtual const char * buffer() const =0
Access data buffer (CONST)
virtual StatusCode create(const CLID &typ, const std::string &title, NTuple::Tuple *&refpTuple)=0
Create requested N tuple (Hide constructor)
This class is used for returning status codes from appropriate routines.
StatusCode load(TTree *tree, INTuple *&refpObject) override
Create the transient representation of an object.
Abstract class describing basic data in an Ntuple.
Abstract base class which allows the user to interact with the actual N tuple implementation.
StatusCode readData(TTree *rtree, INTuple *pObject, long ievt) override
Read N tuple data.
virtual const void * buffer() const =0
Access data buffer (CONST)
virtual char * setBuffer(char *buff)=0
Attach data buffer.
virtual const std::string & index() const =0
Access the index _Item.
constexpr static const auto FAILURE
virtual long dim(long i) const =0
Access individual dimensions.
INTupleItem * createNTupleItem(std::string itemName, std::string blockName, std::string indexName, int indexRange, int arraySize, TYP min, TYP max, INTuple *ntup)
Add an item of a given type to the N tuple.
virtual ItemContainer & items()=0
Access item container.
AttribStringParser::Iterator begin(const AttribStringParser &parser)
virtual bool hasIndex() const =0
Is the tuple have an index item?
SmartIF< INTupleSvc > m_ntupleSvc
Reference to N tuple service.
virtual const std::string & title() const =0
Object title.
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
virtual std::string rootVarType(int)
Return ROOT type info:
StatusCode writeData(TTree *rtree, INTuple *pObject) override
Write N tuple data.