1 #define ROOTHISTCNV_RCWNTUPLECNV_CPP
3 #define ALLOW_ALL_TYPES
27 #pragma warning(disable:1572)
34 std::string& block_name,
35 std::string& var_name,
42 std::string full_name = it->
name();
46 long item_size =
sizeof(T);
47 long dimension = it->
length();
48 long ndim = it->
ndim()-1;
49 std::ostringstream text;
56 std::string ind_blk, ind_var;
57 std::string ind = it->
index();
59 if (ind_blk != block_name) {
60 std::cerr <<
"ERROR: Index for CWNT variable " << ind_var
61 <<
" is in a different block: " << ind_blk << std::endl;
65 else if ( it->
dim(ndim) > 1 ) {
66 text << it->
dim(ndim);
69 for (
int i = ndim-1;
i>=0;
i-- ){
70 text <<
"][" << it->
dim(
i);
76 if (it->
range().lower() != it->
range().min() &&
78 lowerRange = (long) it->
range().lower();
79 upperRange = (long) it->
range().upper();
87 size += item_size * dimension;
97 rtree =
new TTree(desc.c_str(), nt->
title().c_str());
98 log <<
MSG::VERBOSE <<
"created tree id: " << rtree->GetName()
99 <<
" title: "<< nt->
title() <<
" desc: " << desc <<
endmsg;
103 std::string block_name,var_name;
104 long lowerRange, upperRange;
106 long cursize, oldsize = 0;
107 std::vector<std::string> item_fullname;
109 std::vector<long> item_buf_pos, item_buf_len, item_buf_end;
110 std::vector<long> item_range_lower, item_range_upper;
111 std::vector<std::pair<std::string,std::string> > item_name;
114 for (INTuple::ItemContainer::const_iterator
i = cols.begin();
115 i != cols.end(); ++
i ) {
116 std::string
item =
"";
118 switch( (*i)->type() ) {
122 block_name,var_name,lowerRange,upperRange,size);
127 item, block_name,var_name,lowerRange,upperRange,size);
132 block_name,var_name,lowerRange,upperRange,size);
137 block_name,var_name,lowerRange,upperRange,size);
142 block_name,var_name,lowerRange,upperRange,size);
147 item, block_name,var_name,lowerRange,upperRange,size);
152 item, block_name,var_name,lowerRange,upperRange,size);
157 item, block_name,var_name,lowerRange,upperRange,size);
162 item, block_name,var_name,lowerRange,upperRange,size);
167 item, block_name,var_name,lowerRange,upperRange,size);
172 block_name,var_name,lowerRange,upperRange,size);
177 block_name,var_name,lowerRange,upperRange,size);
182 block_name,var_name,lowerRange,upperRange,size);
188 item_name.push_back(std::pair<std::string,std::string>(block_name,item));
189 cursize = size - oldsize;
191 log <<
MSG::VERBOSE <<
"item: " << item <<
" type " << (*i)->type()
192 <<
" blk: " << block_name
193 <<
" var: " << var_name <<
" rng: " << lowerRange <<
" "
194 << upperRange <<
" sz: " << size <<
" " << cursize
195 <<
" buf_pos: " << size-cursize <<
endmsg;
197 item_fullname.push_back(var_name);
198 item_buf_pos.push_back(size-cursize);
199 item_buf_len.push_back(cursize);
200 item_buf_end.push_back(size);
201 item_range_lower.push_back(lowerRange);
202 item_range_upper.push_back(upperRange);
207 char* buff =
new char[size];
208 log <<
MSG::VERBOSE <<
"Created buffer size: " << size <<
" at "
209 << (
void*)buff << endmsg;
212 memset (buff,0,size);
217 char *buf_pos = buff;
219 std::vector<std::pair<std::string,std::string> >::const_iterator itr,
end;
220 end = item_name.end();
223 unsigned int i_item = 0;
224 for (itr=item_name.begin(); itr!=
end; ++itr, ++i_item) {
226 buf_pos = buff + item_buf_pos[i_item];
235 #if ROOT_VERSION_CODE >= ROOT_VERSION(5,15,0)
236 TBranch *br =
new TBranch(rtree,
238 TBranch *br =
new TBranch(
240 item_fullname[i_item].c_str(),
245 if (itr->first !=
"AUTO_BLK") {
246 std::string tit = itr->first;
247 tit = itr->first +
"::" + br->GetTitle();
248 br->SetTitle(tit.c_str());
251 log <<
MSG::DEBUG <<
"adding TBranch " << br->GetTitle() <<
" at "
252 << (
void*) buf_pos << endmsg;
256 if ( item_range_lower[i_item] < item_range_upper[i_item] ) {
261 TObject *tobj = br->GetListOfLeaves()->FindObject( item_fullname[i_item].c_str() );
262 if (tobj->IsA()->InheritsFrom(
"TLeafI")) {
263 index =
dynamic_cast<TLeafI*
>(tobj);
266 index->SetMaximum( item_range_upper[i_item] );
270 log <<
MSG::ERROR <<
"Could dynamic cast to TLeafI: "
271 << item_fullname[i_item] <<
endmsg;
276 rtree->GetListOfBranches()->Add(br);
281 log <<
MSG::INFO <<
"Booked TTree with ID: " << desc
282 <<
" \"" << nt->
title() <<
"\" in directory "
297 char * tar = nt->
buffer();
298 for (INTuple::ItemContainer::const_iterator
i = cols.begin();
i != cols.end();
i++ ) {
299 switch( (*i)->type() ) {
301 tar += saveItem(tar, (
bool*)(*i)->buffer(), (*i)->length());
304 tar += saveItem(tar, (
char*)(*i)->buffer(), (*i)->length());
307 tar += saveItem(tar, (
short*)(*i)->buffer(), (*i)->length());
310 tar += saveItem(tar, (
int*)(*i)->buffer(), (*i)->length());
313 tar += saveItem(tar, (
long*)(*i)->buffer(), (*i)->length());
316 tar += saveItem(tar, (
long long*)(*i)->buffer(), (*i)->length());
319 tar += saveItem(tar, (
unsigned char*)(*i)->buffer(), (*i)->length());
322 tar += saveItem(tar, (
unsigned short*)(*i)->buffer(), (*i)->length());
325 tar += saveItem(tar, (
unsigned int*)(*i)->buffer(), (*i)->length());
328 tar += saveItem(tar, (
unsigned long*)(*i)->buffer(), (*i)->length());
331 tar += saveItem(tar, (
unsigned long long*)(*i)->buffer(), (*i)->length());
334 tar += saveItem(tar, (
float*)(*i)->buffer(), (*i)->length());
337 tar += saveItem(tar, (
double*)(*i)->buffer(), (*i)->length());
358 if (ievt >= rtree->GetEntries()) {
359 log <<
MSG::ERROR <<
"no more entries in tree to read. max: "
360 << rtree->GetEntries() <<
" current: " << ievt
365 rtree->GetEvent(ievt);
370 char * src = ntup->
buffer();
374 for (INTuple::ItemContainer::iterator
i = cols.begin();
i != cols.end();
i++ ) {
377 switch( (*i)->type() ) {
379 src += loadItem(src, (
bool*)(*i)->buffer(), (*i)->length());
382 src += loadItem(src, (
char*)(*i)->buffer(), (*i)->length());
385 src += loadItem(src, (
short*)(*i)->buffer(), (*i)->length());
388 src += loadItem(src, (
int*)(*i)->buffer(), (*i)->length());
391 src += loadItem(src, (
long*)(*i)->buffer(), (*i)->length());
394 src += loadItem(src, (
long long*)(*i)->buffer(), (*i)->length());
397 src += loadItem(src, (
unsigned char*)(*i)->buffer(), (*i)->length());
400 src += loadItem(src, (
unsigned short*)(*i)->buffer(), (*i)->length());
403 src += loadItem(src, (
unsigned int*)(*i)->buffer(), (*i)->length());
406 src += loadItem(src, (
unsigned long*)(*i)->buffer(), (*i)->length());
409 src += loadItem(src, (
unsigned long long*)(*i)->buffer(), (*i)->length());
412 src += loadItem(src, (
float*)(*i)->buffer(), (*i)->length());
415 src += loadItem(src, (
double*)(*i)->buffer(), (*i)->length());
437 std::string title = tree->GetTitle();
438 log <<
MSG::VERBOSE <<
"loading CWNT " << title <<
" at: "
441 status = m_ntupleSvc->create(CLID_ColumnWiseTuple, title, pObj);
446 std::string itemName, indexName, item_type, itemTitle, blockName;
448 long size, totsize=0;
449 std::list< std::pair<TLeaf*,int> > itemList;
455 TObjArray* lbr = tree->GetListOfBranches();
457 while ( TObject *tobjb = bitr() ) {
459 TBranch* br = (TBranch*)tobjb;
460 itemTitle = br->GetTitle();
462 int ipos = itemTitle.find(
"::");
464 blockName = itemTitle.substr(0,ipos);
469 TObjArray* lf = br->GetListOfLeaves();
472 while ( TObject *tobj = litr() ) {
480 TLeaf* tl =
dynamic_cast<TLeaf*
> (tobj);
481 itemName = tl->GetName();
488 if (blockName !=
"") {
489 log <<
MSG::DEBUG <<
"loading NTuple item " << blockName <<
"/"
492 log <<
MSG::DEBUG <<
"loading NTuple item " << itemName;
496 TLeaf* indexLeaf = tl->GetLeafCounter(arraySize);
498 if (arraySize == 0) {
499 log <<
MSG::ERROR <<
"TLeaf counter size = 0. This should not happen!"
503 if (indexLeaf != 0) {
506 indexName = indexLeaf->GetName();
508 indexRange = indexLeaf->GetMaximum();
509 itemSize = indexRange * tl->GetLenType() * arraySize;
511 log <<
"[" << indexName;
514 if (arraySize != 1) {
515 log <<
"][" << arraySize;
520 itemSize = tl->GetLenType() * arraySize;
524 if (arraySize == 1) {
528 log <<
"[" << arraySize <<
"]";
540 hasRange = tl->IsRange();
542 itemList.push_back(std::pair<TLeaf*,int>(tl,itemSize));
546 if (tobj->IsA()->InheritsFrom(
"TLeafI")) {
548 TLeafI *tli =
dynamic_cast<TLeafI*
>(tobj);
549 if (tli->IsUnsigned()) {
550 unsigned long min=0, max=0;
552 min = tli->GetMinimum();
553 max = tli->GetMaximum();
562 min = tli->GetMinimum();
563 max = tli->GetMaximum();
572 }
else if (tobj->IsA()->InheritsFrom(
"TLeafF")) {
573 float min=0., max=0.;
575 TLeafF *tlf =
dynamic_cast<TLeafF*
>(tobj);
577 min = float(tlf->GetMinimum());
578 max = float(tlf->GetMaximum());
586 }
else if (tobj->IsA()->InheritsFrom(
"TLeafD")) {
587 double min=0., max=0.;
589 TLeafD *tld =
dynamic_cast<TLeafD*
>(tobj);
591 min = tld->GetMinimum();
592 max = tld->GetMaximum();
609 <<
"Unable to create ntuple item \""
610 << itemName <<
"\"" <<
endmsg;
616 log <<
MSG::DEBUG <<
"Total buffer size of NTuple: " << totsize
619 char* buf =
new char[totsize];
624 std::list< std::pair<TLeaf*,int> >::const_iterator iitr = itemList.begin();
626 for (; iitr!= itemList.end(); ++iitr) {
627 TLeaf* leaf = (*iitr).first;
628 int isize = (*iitr).second;
630 log <<
MSG::VERBOSE <<
"setting TBranch " << leaf->GetBranch()->GetName()
631 <<
" buffer at " << (
void*) bufpos << endmsg;
633 leaf->GetBranch()->SetAddress((
void*)bufpos);
651 if (totsize != ts ) {
652 log <<
MSG::ERROR <<
"buffer size mismatch: " << ts <<
" " << totsize
std::string getDirectory()
Definition of the MsgStream class used to transmit messages.
virtual StatusCode readData(TTree *rtree, INTuple *pObject, long ievt)
Read N tuple data.
virtual StatusCode add(INTupleItem *item)=0
Add an item row to the N tuple.
std::vector< INTupleItem * > ItemContainer
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.
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.
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 void setBuffer(char *buff)=0
Attach data buffer.
virtual StatusCode writeData(TTree *rtree, INTuple *pObject)
Write N tuple data.
#define DECLARE_NAMESPACE_CONVERTER_FACTORY(n, x)
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.
bool parseName(std::string full, std::string &blk, std::string &var)
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)
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
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.