TLeafI.cxx

Go to the documentation of this file.
00001 // @(#)root/tree:$Id: TLeafI.cxx 36407 2010-10-22 02:04:08Z pcanal $
00002 // Author: Rene Brun   12/01/96
00003 
00004 /*************************************************************************
00005  * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers.               *
00006  * All rights reserved.                                                  *
00007  *                                                                       *
00008  * For the licensing terms see $ROOTSYS/LICENSE.                         *
00009  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
00010  *************************************************************************/
00011 
00012 //////////////////////////////////////////////////////////////////////////
00013 //                                                                      //
00014 // A TLeaf for an Integer data type.                                    //
00015 //////////////////////////////////////////////////////////////////////////
00016 
00017 #include "TLeafI.h"
00018 #include "TBranch.h"
00019 #include "TClonesArray.h"
00020 #include "Riostream.h"
00021 
00022 ClassImp(TLeafI)
00023 
00024 //______________________________________________________________________________
00025 TLeafI::TLeafI(): TLeaf()
00026 {
00027 //*-*-*-*-*-*Default constructor for LeafI*-*-*-*-*-*-*-*-*-*-*-*-*-*
00028 //*-*        ============================
00029 
00030    fLenType = 4;
00031    fMinimum = 0;
00032    fMaximum = 0;
00033    fValue   = 0;
00034    fPointer = 0;
00035 }
00036 
00037 //______________________________________________________________________________
00038 TLeafI::TLeafI(TBranch *parent, const char *name, const char *type)
00039    :TLeaf(parent, name,type)
00040 {
00041 //*-*-*-*-*-*-*-*-*-*-*-*-*Create a LeafI*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
00042 //*-*                      ==============
00043 //*-*
00044 
00045    fLenType = 4;
00046    fMinimum = 0;
00047    fMaximum = 0;
00048    fValue   = 0;
00049    fPointer = 0;
00050 }
00051 
00052 //______________________________________________________________________________
00053 TLeafI::~TLeafI()
00054 {
00055 //*-*-*-*-*-*Default destructor for a LeafI*-*-*-*-*-*-*-*-*-*-*-*
00056 //*-*        ===============================
00057 
00058    if (ResetAddress(0,kTRUE)) delete [] fValue;
00059 }
00060 
00061 
00062 //______________________________________________________________________________
00063 void TLeafI::Export(TClonesArray *list, Int_t n)
00064 {
00065 //*-*-*-*-*-*Export element from local leaf buffer to ClonesArray*-*-*-*-*
00066 //*-*        ======================================================
00067 
00068    Int_t *value = fValue;
00069    for (Int_t i=0;i<n;i++) {
00070       char *first = (char*)list->UncheckedAt(i);
00071       Int_t *ii = (Int_t*)&first[fOffset];
00072       for (Int_t j=0;j<fLen;j++) {
00073          ii[j] = value[j];
00074       }
00075       value += fLen;
00076    }
00077 }
00078 
00079 //______________________________________________________________________________
00080 void TLeafI::FillBasket(TBuffer &b)
00081 {
00082 //*-*-*-*-*-*-*-*-*-*-*Pack leaf elements in Basket output buffer*-*-*-*-*-*-*
00083 //*-*                  =========================================
00084 
00085    Int_t i;
00086    Int_t len = GetLen();
00087    if (fPointer) fValue = *fPointer;
00088    if (IsRange()) {
00089       if (fValue[0] > fMaximum) fMaximum = fValue[0];
00090    }
00091    if (IsUnsigned()) {
00092       for (i=0;i<len;i++) b << (UInt_t)fValue[i];
00093    } else {
00094       b.WriteFastArray(fValue,len);
00095    }
00096 }
00097 
00098 //______________________________________________________________________________
00099 const char *TLeafI::GetTypeName() const
00100 {
00101 //*-*-*-*-*-*-*-*Returns name of leaf type*-*-*-*-*-*-*-*-*-*-*-*
00102 //*-*            =========================
00103 
00104    if (fIsUnsigned) return "UInt_t";
00105    return "Int_t";
00106 }
00107 
00108 
00109 //______________________________________________________________________________
00110 Double_t TLeafI::GetValue(Int_t i) const
00111 {
00112 // Returns current value of leaf
00113 // if leaf is a simple type, i must be set to 0
00114 // if leaf is an array, i is the array element number to be returned
00115 
00116    if (fIsUnsigned) return (UInt_t)fValue[i];
00117    return fValue[i];
00118 }
00119 
00120 
00121 
00122 //______________________________________________________________________________
00123 void TLeafI::Import(TClonesArray *list, Int_t n)
00124 {
00125 //*-*-*-*-*-*Import element from ClonesArray into local leaf buffer*-*-*-*-*
00126 //*-*        ======================================================
00127 
00128    const Int_t kIntUndefined = -9999;
00129    Int_t j = 0;
00130    char *clone;
00131    for (Int_t i=0;i<n;i++) {
00132       clone = (char*)list->UncheckedAt(i);
00133       if (clone) memcpy(&fValue[j],clone + fOffset, 4*fLen);
00134       else       memcpy(&fValue[j],&kIntUndefined,  4*fLen);
00135       j += fLen;
00136    }
00137 }
00138 
00139 //______________________________________________________________________________
00140 void TLeafI::PrintValue(Int_t l) const
00141 {
00142 // Prints leaf value
00143 
00144    if (fIsUnsigned) {
00145       UInt_t *uvalue = (UInt_t*)GetValuePointer();
00146       printf("%u",uvalue[l]);
00147    } else {
00148       Int_t *value = (Int_t*)GetValuePointer();
00149       printf("%d",value[l]);
00150    }
00151 }
00152 
00153 //______________________________________________________________________________
00154 void TLeafI::ReadBasket(TBuffer &b)
00155 {
00156 //*-*-*-*-*-*-*-*-*-*-*Read leaf elements from Basket input buffer*-*-*-*-*-*
00157 //*-*                  ===========================================
00158 
00159    if (!fLeafCount && fNdata == 1) {
00160       b >> fValue[0];
00161    } else {
00162       if (fLeafCount) {
00163          Long64_t entry = fBranch->GetReadEntry();
00164          if (fLeafCount->GetBranch()->GetReadEntry() != entry) {
00165             fLeafCount->GetBranch()->GetEntry(entry);
00166          }
00167          Int_t len = Int_t(fLeafCount->GetValue());
00168          if (len > fLeafCount->GetMaximum()) {
00169             printf("ERROR leaf:%s, len=%d and max=%d\n",GetName(),len,fLeafCount->GetMaximum());
00170             len = fLeafCount->GetMaximum();
00171          }
00172          fNdata = len*fLen;
00173          b.ReadFastArray(fValue,len*fLen);
00174       } else {
00175          b.ReadFastArray(fValue,fLen);
00176       }
00177    }
00178 }
00179 
00180 //______________________________________________________________________________
00181 void TLeafI::ReadBasketExport(TBuffer &b, TClonesArray *list, Int_t n)
00182 {
00183 //*-*-*-*-*-*-*-*-*-*-*Read leaf elements from Basket input buffer*-*-*-*-*-*
00184 //  and export buffer to TClonesArray objects
00185 
00186    if (n*fLen == 1) {
00187       b >> fValue[0];
00188    } else {
00189       b.ReadFastArray(fValue,n*fLen);
00190    }
00191    Int_t *value = fValue;
00192    for (Int_t i=0;i<n;i++) {
00193       char *first = (char*)list->UncheckedAt(i);
00194       Int_t *ii = (Int_t*)&first[fOffset];
00195       for (Int_t j=0;j<fLen;j++) {
00196          ii[j] = value[j];
00197       }
00198       value += fLen;
00199    }
00200 }
00201 
00202 //______________________________________________________________________________
00203 void TLeafI::ReadValue(istream &s)
00204 {
00205 // read an integer from istream s and store it into the branch buffer
00206    if (fIsUnsigned) {
00207       UInt_t *uvalue = (UInt_t*)GetValuePointer();
00208       for (Int_t i=0;i<fLen;i++) s >> uvalue[i];
00209    } else {
00210       Int_t *value = (Int_t*)GetValuePointer();
00211       for (Int_t i=0;i<fLen;i++) s >> value[i];
00212    }
00213 }
00214 
00215 //______________________________________________________________________________
00216 void TLeafI::SetAddress(void *add)
00217 {
00218 //*-*-*-*-*-*-*-*-*-*-*Set leaf buffer data address*-*-*-*-*-*
00219 //*-*                  ============================
00220 
00221    if (ResetAddress(add) && (add!= fValue)) {
00222       delete [] fValue;
00223    }
00224    if (add) {
00225       if (TestBit(kIndirectAddress)) {
00226          fPointer = (Int_t**) add;
00227          Int_t ncountmax = fLen;
00228          if (fLeafCount) ncountmax = fLen*(fLeafCount->GetMaximum() + 1);
00229          if ((fLeafCount && ncountmax > Int_t(fLeafCount->GetValue())) ||
00230              ncountmax > fNdata || *fPointer == 0) {
00231             if (*fPointer) delete [] *fPointer;
00232             if (ncountmax > fNdata) fNdata = ncountmax;
00233             *fPointer = new Int_t[fNdata];
00234          }
00235          fValue = *fPointer;
00236       } else {
00237          fValue = (Int_t*)add;
00238       }
00239    } else {
00240       fValue = new Int_t[fNdata];
00241       fValue[0] = 0;
00242    }
00243 }
00244 

Generated on Tue Jul 5 15:34:03 2011 for ROOT_528-00b_version by  doxygen 1.5.1