TLeafB.cxx

Go to the documentation of this file.
00001 // @(#)root/tree:$Id: TLeafB.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 8 bit Integer data type.                              //
00015 //////////////////////////////////////////////////////////////////////////
00016 
00017 #include "TLeafB.h"
00018 #include "TBranch.h"
00019 #include "TClonesArray.h"
00020 #include "Riostream.h"
00021 
00022 ClassImp(TLeafB)
00023 
00024 //______________________________________________________________________________
00025 TLeafB::TLeafB()
00026 : TLeaf()
00027 , fMinimum(0)
00028 , fMaximum(0)
00029 , fValue(0)
00030 , fPointer(0)
00031 {
00032    // -- Default constructor.
00033    fLenType = 1;
00034 }
00035 
00036 //______________________________________________________________________________
00037 TLeafB::TLeafB(TBranch *parent, const char* name, const char* type)
00038    : TLeaf(parent, name, type)
00039    , fMinimum(0)
00040    , fMaximum(0)
00041    , fValue(0)
00042    , fPointer(0)
00043 {
00044    // -- Create a LeafB.
00045    fLenType = 1;
00046 }
00047 
00048 //______________________________________________________________________________
00049 TLeafB::~TLeafB()
00050 {
00051    // -- Destructor.
00052    if (ResetAddress(0, kTRUE)) {
00053       delete[] fValue;
00054       fValue = 0;
00055    }
00056    // Note: We do not own this, the user's object does.
00057    fPointer = 0;
00058 }
00059 
00060 //______________________________________________________________________________
00061 void TLeafB::Export(TClonesArray* list, Int_t n)
00062 {
00063    // -- Export element from local leaf buffer to a ClonesArray.
00064 
00065    for (Int_t i = 0, j = 0; i < n; i++, j += fLen) {
00066       memcpy(((char*) list->UncheckedAt(i)) + fOffset, &fValue[j], fLen);
00067    }
00068 }
00069 
00070 //______________________________________________________________________________
00071 void TLeafB::FillBasket(TBuffer& b)
00072 {
00073    // -- Pack leaf elements into Basket output buffer.
00074 
00075    Int_t len = GetLen();
00076    if (fPointer) {
00077       fValue = *fPointer;
00078    }
00079    if (IsRange()) {
00080       if (fValue[0] > fMaximum) {
00081          fMaximum = fValue[0];
00082       }
00083    }
00084    if (IsUnsigned()) {
00085       for (Int_t i = 0; i < len; i++) {
00086          b << (UChar_t) fValue[i];
00087       }
00088    } else {
00089       b.WriteFastArray(fValue, len);
00090    }
00091 }
00092 
00093 //______________________________________________________________________________
00094 const char *TLeafB::GetTypeName() const
00095 {
00096    // -- Returns name of leaf type.
00097 
00098    if (fIsUnsigned) {
00099       return "UChar_t";
00100    }
00101    return "Char_t";
00102 }
00103 
00104 //______________________________________________________________________________
00105 void TLeafB::Import(TClonesArray *list, Int_t n)
00106 {
00107    // -- Import element from ClonesArray into local leaf buffer.
00108 
00109    for (Int_t i = 0, j = 0; i < n; i++, j+= fLen) {
00110       memcpy(&fValue[j], ((char*) list->UncheckedAt(i)) + fOffset, fLen);
00111    }
00112 }
00113 
00114 //______________________________________________________________________________
00115 void TLeafB::PrintValue(Int_t l) const
00116 {
00117    // -- Prints leaf value.
00118 
00119    if (fIsUnsigned) {
00120       UChar_t *uvalue = (UChar_t*) GetValuePointer();
00121       printf("%u", uvalue[l]);
00122    } else {
00123       Char_t *value = (Char_t*) GetValuePointer();
00124       printf("%d", value[l]);
00125    }
00126 }
00127 
00128 //______________________________________________________________________________
00129 void TLeafB::ReadBasket(TBuffer &b)
00130 {
00131    // -- Read leaf elements from Basket input buffer.
00132 
00133    if (!fLeafCount && (fNdata == 1)) {
00134       b >> fValue[0];
00135    } else {
00136       if (fLeafCount) {
00137          Long64_t entry = fBranch->GetReadEntry();
00138          if (fLeafCount->GetBranch()->GetReadEntry() != entry) {
00139             fLeafCount->GetBranch()->GetEntry(entry);
00140          }
00141          Int_t len = Int_t(fLeafCount->GetValue());
00142          if (len > fLeafCount->GetMaximum()) {
00143             Error("ReadBasket", "leaf: '%s' len: %d max: %d", GetName(), len, fLeafCount->GetMaximum());
00144             len = fLeafCount->GetMaximum();
00145          }
00146          fNdata = len * fLen;
00147          b.ReadFastArray(fValue, len*fLen);
00148       } else {
00149          b.ReadFastArray(fValue, fLen);
00150       }
00151    }
00152 }
00153 
00154 //______________________________________________________________________________
00155 void TLeafB::ReadBasketExport(TBuffer& b, TClonesArray* list, Int_t n)
00156 {
00157    // -- Read leaf elements from Basket input buffer and export buffer to TClonesArray objects.
00158 
00159    b.ReadFastArray(fValue, n*fLen);
00160 
00161    for (Int_t i = 0, j = 0; i < n; i++, j += fLen) {
00162       memcpy(((char*) list->UncheckedAt(i)) + fOffset, &fValue[j], fLen);
00163    }
00164 }
00165 
00166 //______________________________________________________________________________
00167 void TLeafB::ReadValue(istream &s)
00168 {
00169    // -- Read a string from istream s and store it into the branch buffer.
00170    char* value = (char*) GetValuePointer();
00171    s >> value;
00172 }
00173 
00174 //______________________________________________________________________________
00175 void TLeafB::SetAddress(void *addr)
00176 {
00177    // -- Set value buffer address.
00178 
00179    // Check ownership of the value buffer and
00180    // calculate a new size for it.
00181    if (ResetAddress(addr)) {
00182       // -- We owned the old value buffer, delete it.
00183       delete[] fValue;
00184       fValue = 0;
00185    }
00186    if (addr) {
00187       // -- We have been provided a new value buffer.
00188       if (TestBit(kIndirectAddress)) {
00189          // -- The data member is a pointer to an array.
00190          fPointer = (Char_t**) addr;
00191          // Calculate the maximum size we have ever needed
00192          // for the value buffer.
00193          Int_t ncountmax = fLen;
00194          if (fLeafCount) {
00195             ncountmax = (fLeafCount->GetMaximum() + 1) * fLen;
00196          }
00197          // Reallocate the value buffer if needed.
00198          if ((fLeafCount && (Int_t(fLeafCount->GetValue()) < ncountmax)) ||
00199              (fNdata < ncountmax) ||
00200              (*fPointer == 0)) {
00201             // -- Reallocate.
00202             // Note:
00203             //      1) For a varying length array we do this based on
00204             //         an indirect estimator of the size of the value
00205             //         buffer since we have no record of how large it
00206             //         actually is.  If the current length of the
00207             //         varying length array is less than it has been
00208             //         in the past, then reallocate the value buffer
00209             //         to the larger of either the calculated new size
00210             //         or the maximum size it has ever been.
00211             //
00212             //      2) The second condition checks if the new value
00213             //         buffer size calculated by ResetAddress() is
00214             //         smaller than the most we have ever used, and
00215             //         if it is, then we increase the new size and
00216             //         reallocate.
00217             //
00218             //      3) The third condition is checking to see if we
00219             //         have been given a value buffer, if not then
00220             //         we must allocate one.
00221             //
00222             if (fNdata < ncountmax) {
00223                fNdata = ncountmax;
00224             }
00225             delete[] *fPointer;
00226             *fPointer = 0;
00227             *fPointer = new Char_t[fNdata];
00228          }
00229          fValue = *fPointer;
00230       } else {
00231          // -- The data member is fixed size.
00232          // FIXME: What about fPointer???
00233          fValue = (char*) addr;
00234       }
00235    } else {
00236       // -- We must create the value buffer ourselves.
00237       // Note: We are using the size calculated by ResetAddress().
00238       fValue = new char[fNdata];
00239       // FIXME: Why initialize at all?
00240       fValue[0] = 0;
00241    }
00242 }
00243 

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