1 #define ROOTHISTCNV_RCWNTUPLECNV_CPP
3 #define ALLOW_ALL_TYPES
6 #include "GaudiKernel/INTupleSvc.h"
7 #include "GaudiKernel/MsgStream.h"
8 #include "GaudiKernel/NTuple.h"
26 #pragma warning(disable:1572)
33 std::string& block_name,
34 std::string& var_name,
44 long item_size =
sizeof(T);
45 long dimension = it->
length();
46 long ndim = it->
ndim()-1;
53 std::string ind_blk, ind_var;
55 if (ind_blk != block_name) {
56 std::cerr <<
"ERROR: Index for CWNT variable " << ind_var
57 <<
" is in a different block: " << ind_blk << std::endl;
61 else if ( it->
dim(ndim) > 1 ) {
65 for (
int i = ndim-1;
i>=0;
i-- ){
73 if (it->
range().lower() != it->
range().min() &&
75 lowerRange = it->
range().lower();
76 upperRange = it->
range().upper();
82 size += item_size * dimension;
92 rtree =
new TTree(desc.c_str(), nt->
title().c_str());
93 log <<
MSG::VERBOSE <<
"created tree id: " << rtree->GetName()
94 <<
" title: "<< nt->
title() <<
" desc: " << desc <<
endmsg;
98 std::string block_name,var_name;
99 long lowerRange, upperRange;
101 long cursize, oldsize = 0;
102 std::vector<std::string> item_fullname;
104 std::vector<long> item_buf_pos, item_buf_len, item_buf_end;
105 std::vector<long> item_range_lower, item_range_upper;
106 std::vector<std::pair<std::string,std::string> > item_name;
108 for (
const auto&
i : nt->
items() ) {
111 switch(
i->type() ) {
115 block_name,var_name,lowerRange,upperRange,size);
120 item, block_name,var_name,lowerRange,upperRange,size);
125 block_name,var_name,lowerRange,upperRange,size);
130 block_name,var_name,lowerRange,upperRange,size);
135 block_name,var_name,lowerRange,upperRange,size);
140 item, block_name,var_name,lowerRange,upperRange,size);
145 item, block_name,var_name,lowerRange,upperRange,size);
150 item, block_name,var_name,lowerRange,upperRange,size);
155 item, block_name,var_name,lowerRange,upperRange,size);
160 item, block_name,var_name,lowerRange,upperRange,size);
165 block_name,var_name,lowerRange,upperRange,size);
170 block_name,var_name,lowerRange,upperRange,size);
175 block_name,var_name,lowerRange,upperRange,size);
181 item_name.emplace_back(block_name,item);
182 cursize = size - oldsize;
184 log <<
MSG::VERBOSE <<
"item: " << item <<
" type " <<
i->type()
185 <<
" blk: " << block_name
186 <<
" var: " << var_name <<
" rng: " << lowerRange <<
" "
187 << upperRange <<
" sz: " << size <<
" " << cursize
188 <<
" buf_pos: " << size-cursize <<
endmsg;
190 item_fullname.push_back(var_name);
191 item_buf_pos.push_back(size-cursize);
192 item_buf_len.push_back(cursize);
193 item_buf_end.push_back(size);
194 item_range_lower.push_back(lowerRange);
195 item_range_upper.push_back(upperRange);
201 char* buff = nt->
setBuffer(
new char[size]);
203 log <<
MSG::VERBOSE <<
"Created buffer size: " << size <<
" at "
204 << (
void*)buff << endmsg;
207 std::fill_n(buff,size,0);
209 char *buf_pos = buff;
211 auto end = item_name.cend();
214 unsigned int i_item = 0;
215 for (
auto itr=item_name.cbegin(); itr!=
end; ++itr, ++i_item) {
217 buf_pos = buff + item_buf_pos[i_item];
226 #if ROOT_VERSION_CODE >= ROOT_VERSION(5,15,0)
227 auto br =
new TBranch(rtree,
229 TBranch *br =
new TBranch(
231 item_fullname[i_item].c_str(),
236 if (itr->first !=
"AUTO_BLK") {
237 std::string tit = itr->first;
238 tit = itr->first +
"::" + br->GetTitle();
239 br->SetTitle(tit.c_str());
242 log <<
MSG::DEBUG <<
"adding TBranch " << br->GetTitle() <<
" at "
243 << (
void*) buf_pos << endmsg;
247 if ( item_range_lower[i_item] < item_range_upper[i_item] ) {
251 TLeafI *index =
nullptr;
252 TObject *tobj = br->GetListOfLeaves()->FindObject( item_fullname[i_item].c_str() );
253 if (tobj->IsA()->InheritsFrom(
"TLeafI")) {
254 index =
dynamic_cast<TLeafI*
>(tobj);
257 index->SetMaximum( item_range_upper[i_item] );
261 log <<
MSG::ERROR <<
"Could dynamic cast to TLeafI: "
262 << item_fullname[i_item] <<
endmsg;
267 rtree->GetListOfBranches()->Add(br);
272 log <<
MSG::INFO <<
"Booked TTree with ID: " << desc
273 <<
" \"" << nt->
title() <<
"\" in directory "
287 char * tar = nt->
buffer();
288 for (
const auto&
i : nt->
items() ) {
289 switch(
i->type() ) {
291 tar += saveItem(tar, (
bool*)
i->buffer(),
i->length());
294 tar += saveItem(tar, (
char*)
i->buffer(),
i->length());
297 tar += saveItem(tar, (
short*)
i->buffer(),
i->length());
300 tar += saveItem(tar, (
int*)
i->buffer(),
i->length());
303 tar += saveItem(tar, (
long*)
i->buffer(),
i->length());
306 tar += saveItem(tar, (
long long*)
i->buffer(),
i->length());
309 tar += saveItem(tar, (
unsigned char*)
i->buffer(),
i->length());
312 tar += saveItem(tar, (
unsigned short*)
i->buffer(),
i->length());
315 tar += saveItem(tar, (
unsigned int*)
i->buffer(),
i->length());
318 tar += saveItem(tar, (
unsigned long*)
i->buffer(),
i->length());
321 tar += saveItem(tar, (
unsigned long long*)
i->buffer(),
i->length());
324 tar += saveItem(tar, (
float*)
i->buffer(),
i->length());
327 tar += saveItem(tar, (
double*)
i->buffer(),
i->length());
348 if (ievt >= rtree->GetEntries()) {
349 log <<
MSG::ERROR <<
"no more entries in tree to read. max: "
350 << rtree->GetEntries() <<
" current: " << ievt
355 rtree->GetEvent(ievt);
361 char *src = ntup->
buffer();
362 for (
auto &
i : ntup->
items() ) {
364 switch(
i->type() ) {
366 src += loadItem(src, (
bool*)
i->buffer(),
i->length());
369 src += loadItem(src, (
char*)
i->buffer(),
i->length());
372 src += loadItem(src, (
short*)
i->buffer(),
i->length());
375 src += loadItem(src, (
int*)
i->buffer(),
i->length());
378 src += loadItem(src, (
long*)
i->buffer(),
i->length());
381 src += loadItem(src, (
long long*)
i->buffer(),
i->length());
384 src += loadItem(src, (
unsigned char*)
i->buffer(),
i->length());
387 src += loadItem(src, (
unsigned short*)
i->buffer(),
i->length());
390 src += loadItem(src, (
unsigned int*)
i->buffer(),
i->length());
393 src += loadItem(src, (
unsigned long*)
i->buffer(),
i->length());
396 src += loadItem(src, (
unsigned long long*)
i->buffer(),
i->length());
399 src += loadItem(src, (
float*)
i->buffer(),
i->length());
402 src += loadItem(src, (
double*)
i->buffer(),
i->length());
424 std::string title = tree->GetTitle();
425 log <<
MSG::VERBOSE <<
"loading CWNT " << title <<
" at: "
428 status = m_ntupleSvc->create(CLID_ColumnWiseTuple, title, pObj);
433 std::string itemName, indexName, item_type, itemTitle, blockName;
435 long size, totsize=0;
436 std::vector< std::pair<TLeaf*,int> > itemList;
442 TObjArray* lbr = tree->GetListOfBranches();
444 while ( TObject *tobjb = bitr() ) {
446 TBranch* br = (TBranch*)tobjb;
447 itemTitle = br->GetTitle();
449 int ipos = itemTitle.find(
"::");
451 blockName = itemTitle.substr(0,ipos);
456 TObjArray* lf = br->GetListOfLeaves();
459 while ( TObject *tobj = litr() ) {
467 TLeaf* tl =
dynamic_cast<TLeaf*
> (tobj);
468 itemName = tl->GetName();
475 if (blockName !=
"") {
476 log <<
MSG::DEBUG <<
"loading NTuple item " << blockName <<
"/"
479 log <<
MSG::DEBUG <<
"loading NTuple item " << itemName;
483 TLeaf* indexLeaf = tl->GetLeafCounter(arraySize);
485 if (arraySize == 0) {
486 log <<
MSG::ERROR <<
"TLeaf counter size = 0. This should not happen!"
493 indexName = indexLeaf->GetName();
495 indexRange = indexLeaf->GetMaximum();
496 itemSize = indexRange * tl->GetLenType() * arraySize;
498 log <<
"[" << indexName;
501 if (arraySize != 1) {
502 log <<
"][" << arraySize;
507 itemSize = tl->GetLenType() * arraySize;
511 if (arraySize == 1) {
515 log <<
"[" << arraySize <<
"]";
527 hasRange = tl->IsRange();
529 itemList.emplace_back(tl,itemSize);
533 if (tobj->IsA()->InheritsFrom(
"TLeafI")) {
535 TLeafI *tli =
dynamic_cast<TLeafI*
>(tobj);
536 if (tli->IsUnsigned()) {
537 unsigned long min=0, max=0;
539 min = tli->GetMinimum();
540 max = tli->GetMaximum();
549 min = tli->GetMinimum();
550 max = tli->GetMaximum();
559 }
else if (tobj->IsA()->InheritsFrom(
"TLeafF")) {
560 float min=0., max=0.;
562 TLeafF *tlf =
dynamic_cast<TLeafF*
>(tobj);
564 min = float(tlf->GetMinimum());
565 max = float(tlf->GetMaximum());
573 }
else if (tobj->IsA()->InheritsFrom(
"TLeafD")) {
574 double min=0., max=0.;
576 TLeafD *tld =
dynamic_cast<TLeafD*
>(tobj);
578 min = tld->GetMinimum();
579 max = tld->GetMaximum();
596 <<
"Unable to create ntuple item \""
597 << itemName <<
"\"" <<
endmsg;
603 log <<
MSG::DEBUG <<
"Total buffer size of NTuple: " << totsize
606 char* buf = ntup->
setBuffer(
new char[totsize] );
610 for (
const auto& iitr : itemList) {
611 TLeaf* leaf = iitr.first;
612 int isize = iitr.second;
614 log <<
MSG::VERBOSE <<
"setting TBranch " << leaf->GetBranch()->GetName()
615 <<
" buffer at " << (
void*) bufpos << endmsg;
617 leaf->GetBranch()->SetAddress((
void*)bufpos);
635 if (totsize != ts ) {
636 log <<
MSG::ERROR <<
"buffer size mismatch: " << ts <<
" " << totsize
std::string getDirectory()
Definition of the MsgStream class used to transmit messages.
string to_string(const T &value)
virtual StatusCode readData(TTree *rtree, INTuple *pObject, long ievt)
Read N tuple data.
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
bool parseName(const std::string &full, std::string &blk, std::string &var)
virtual StatusCode add(INTupleItem *item)=0
Add an item row to the N tuple.
virtual const std::string & title() const =0
Object title.
SmartIF< IMessageSvc > & msgSvc() const
Retrieve pointer to message service.
virtual const ItemRange & range() const =0
Access the range if specified.
#define DECLARE_NAMESPACE_CONVERTER_FACTORY(n, x)
Converter of Column-wise NTuple into ROOT format.
virtual bool hasIndex() const =0
Is the tuple have an index item?
virtual StatusCode book(const std::string &desc, INTuple *pObject, TTree *&tree)
Book the N tuple.
virtual const std::string & name() const =0
Access _Item name.
NTuple interface class definition.
virtual void reset()=0
Reset all entries to their default values.
NTuple interface class definition.
auto end(reverse_wrapper< T > &w)
This class is used for returning status codes from appropriate routines.
virtual long dim(long i) const =0
Access individual dimensions.
Abstract class describing basic data in an Ntuple.
Abstract base class which allows the user to interact with the actual N tuple implementation.
virtual StatusCode writeData(TTree *rtree, INTuple *pObject)
Write N tuple data.
virtual char * setBuffer(char *buff)=0
Attach data buffer.
virtual long ndim() const =0
Dimension.
virtual StatusCode load(TTree *tree, INTuple *&refpObject)
Create the transient representation of an object.
virtual const char * buffer() const =0
Access data buffer (CONST)
virtual ItemContainer & items()=0
Access item container.
void analyzeItem(std::string typ, const NTuple::_Data< T > *it, std::string &desc, std::string &block_name, std::string &var_name, long &lowerRange, long &upperRange, long &size)
virtual const std::string & index() const =0
Access the index _Item.
INTupleItem * createNTupleItem(std::string itemName, std::string blockName, std::string indexName, int indexRange, int arraySize, TYP min, TYP max, INTuple *ntup)
virtual std::string rootVarType(int)
Return ROOT type info:
virtual long length() const =0
Access the buffer length.