// @(#)root/tree:$Id$
// Author: Rene Brun   12/01/96

/*************************************************************************
 * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers.               *
 * All rights reserved.                                                  *
 *                                                                       *
 * For the licensing terms see $ROOTSYS/LICENSE.                         *
 * For the list of contributors see $ROOTSYS/README/CREDITS.             *
 *************************************************************************/

//////////////////////////////////////////////////////////////////////////
//                                                                      //
// A TLeaf for a 32 bit floating point data type.                       //
//////////////////////////////////////////////////////////////////////////

#include "TLeafF.h"
#include "TBranch.h"
#include "TClonesArray.h"
#include "Riostream.h"

ClassImp(TLeafF)

//______________________________________________________________________________
TLeafF::TLeafF(): TLeaf()
{
//*-*-*-*-*-*Default constructor for LeafF*-*-*-*-*-*-*-*-*-*-*-*-*-*
//*-*        ============================

   fLenType = 4;
   fMinimum = 0;
   fMaximum = 0;
   fValue   = 0;
   fPointer = 0;
}

//______________________________________________________________________________
TLeafF::TLeafF(TBranch *parent, const char *name, const char *type)
   :TLeaf(parent, name,type)
{
//*-*-*-*-*-*-*-*-*-*-*-*-*Create a LeafF*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
//*-*                      ==============
//*-*

   fLenType = 4;
   fMinimum = 0;
   fMaximum = 0;
   fValue   = 0;
   fPointer = 0;
}

//______________________________________________________________________________
TLeafF::~TLeafF()
{
//*-*-*-*-*-*Default destructor for a LeafF*-*-*-*-*-*-*-*-*-*-*-*
//*-*        ===============================

   if (ResetAddress(0,kTRUE)) delete [] fValue;
}



//______________________________________________________________________________
void TLeafF::Export(TClonesArray *list, Int_t n)
{
//*-*-*-*-*-*Export element from local leaf buffer to ClonesArray*-*-*-*-*
//*-*        ====================================================

   Float_t *value = fValue;
   for (Int_t i=0;i<n;i++) {
      char *first = (char*)list->UncheckedAt(i);
      Float_t *ff = (Float_t*)&first[fOffset];
      for (Int_t j=0;j<fLen;j++) {
         ff[j] = value[j];
      }
      value += fLen;
   }
}

//______________________________________________________________________________
void TLeafF::FillBasket(TBuffer &b)
{
//*-*-*-*-*-*-*-*-*-*-*Pack leaf elements in Basket output buffer*-*-*-*-*-*-*
//*-*                  ==========================================

   Int_t len = GetLen();
   if (fPointer) fValue = *fPointer;
   b.WriteFastArray(fValue,len);
}


//______________________________________________________________________________
void TLeafF::Import(TClonesArray *list, Int_t n)
{
//*-*-*-*-*-*Import element from ClonesArray into local leaf buffer*-*-*-*-*
//*-*        ======================================================

   const Float_t kFloatUndefined = -9999.;
   Int_t j = 0;
   char *clone;
   for (Int_t i=0;i<n;i++) {
      clone = (char*)list->UncheckedAt(i);
      if (clone) memcpy(&fValue[j],clone + fOffset,  4*fLen);
      else       memcpy(&fValue[j],&kFloatUndefined, 4*fLen);
      j += fLen;
   }
}

//______________________________________________________________________________
void TLeafF::PrintValue(Int_t l) const
{
// Prints leaf value

   Float_t *value = (Float_t *)GetValuePointer();
   printf("%g",value[l]);
}

//______________________________________________________________________________
void TLeafF::ReadBasket(TBuffer &b)
{
//*-*-*-*-*-*-*-*-*-*-*Read leaf elements from Basket input buffer*-*-*-*-*-*
//*-*                  ===========================================

   if (!fLeafCount && fNdata == 1) {
      b.ReadFloat(fValue[0]);
   }else {
      if (fLeafCount) {
         Long64_t entry = fBranch->GetReadEntry();
         if (fLeafCount->GetBranch()->GetReadEntry() != entry) {
            fLeafCount->GetBranch()->GetEntry(entry);
         }
         Int_t len = Int_t(fLeafCount->GetValue());
         if (len > fLeafCount->GetMaximum()) {
            printf("ERROR leaf:%s, len=%d and max=%d\n",GetName(),len,fLeafCount->GetMaximum());
            len = fLeafCount->GetMaximum();
         }
         fNdata = len*fLen;
         b.ReadFastArray(fValue,len*fLen);
      } else {
         b.ReadFastArray(fValue,fLen);
      }
   }
}

//______________________________________________________________________________
void TLeafF::ReadBasketExport(TBuffer &b, TClonesArray *list, Int_t n)
{
//*-*-*-*-*-*-*-*-*-*-*Read leaf elements from Basket input buffer*-*-*-*-*-*
//  and export buffer to TClonesArray objects

   if (n*fLen == 1) {
      b >> fValue[0];
   } else {
      b.ReadFastArray(fValue,n*fLen);
   }

   Float_t *value = fValue;
   for (Int_t i=0;i<n;i++) {
      char *first = (char*)list->UncheckedAt(i);
      Float_t *ff = (Float_t*)&first[fOffset];
      for (Int_t j=0;j<fLen;j++) {
         ff[j] = value[j];
      }
      value += fLen;
   }
}

//______________________________________________________________________________
void TLeafF::ReadValue(std::istream &s, Char_t /*delim = ' '*/)
{
// read a float from std::istream s and store it into the branch buffer
   Float_t *value = (Float_t*)GetValuePointer();
   for (Int_t i=0;i<fLen;i++) s >> value[i];
}

//______________________________________________________________________________
void TLeafF::SetAddress(void *add)
{
//*-*-*-*-*-*-*-*-*-*-*Set leaf buffer data address*-*-*-*-*-*
//*-*                  ============================

   if (ResetAddress(add) && (add!= fValue)) {
      delete [] fValue;
   }

   if (add) {
      if (TestBit(kIndirectAddress)) {
         fPointer = (Float_t**) add;
         Int_t ncountmax = fLen;
         if (fLeafCount) ncountmax = fLen*(fLeafCount->GetMaximum() + 1);
         if ((fLeafCount && ncountmax > Int_t(fLeafCount->GetValue())) ||
             ncountmax > fNdata || *fPointer == 0) {
            if (*fPointer) delete [] *fPointer;
            if (ncountmax > fNdata) fNdata = ncountmax;
            *fPointer = new Float_t[fNdata];
         }
         fValue = *fPointer;
      } else {
         fValue = (Float_t*)add;
      }
   } else {
      fValue = new Float_t[fNdata];
      fValue[0] = 0;
   }
}
 TLeafF.cxx:1
 TLeafF.cxx:2
 TLeafF.cxx:3
 TLeafF.cxx:4
 TLeafF.cxx:5
 TLeafF.cxx:6
 TLeafF.cxx:7
 TLeafF.cxx:8
 TLeafF.cxx:9
 TLeafF.cxx:10
 TLeafF.cxx:11
 TLeafF.cxx:12
 TLeafF.cxx:13
 TLeafF.cxx:14
 TLeafF.cxx:15
 TLeafF.cxx:16
 TLeafF.cxx:17
 TLeafF.cxx:18
 TLeafF.cxx:19
 TLeafF.cxx:20
 TLeafF.cxx:21
 TLeafF.cxx:22
 TLeafF.cxx:23
 TLeafF.cxx:24
 TLeafF.cxx:25
 TLeafF.cxx:26
 TLeafF.cxx:27
 TLeafF.cxx:28
 TLeafF.cxx:29
 TLeafF.cxx:30
 TLeafF.cxx:31
 TLeafF.cxx:32
 TLeafF.cxx:33
 TLeafF.cxx:34
 TLeafF.cxx:35
 TLeafF.cxx:36
 TLeafF.cxx:37
 TLeafF.cxx:38
 TLeafF.cxx:39
 TLeafF.cxx:40
 TLeafF.cxx:41
 TLeafF.cxx:42
 TLeafF.cxx:43
 TLeafF.cxx:44
 TLeafF.cxx:45
 TLeafF.cxx:46
 TLeafF.cxx:47
 TLeafF.cxx:48
 TLeafF.cxx:49
 TLeafF.cxx:50
 TLeafF.cxx:51
 TLeafF.cxx:52
 TLeafF.cxx:53
 TLeafF.cxx:54
 TLeafF.cxx:55
 TLeafF.cxx:56
 TLeafF.cxx:57
 TLeafF.cxx:58
 TLeafF.cxx:59
 TLeafF.cxx:60
 TLeafF.cxx:61
 TLeafF.cxx:62
 TLeafF.cxx:63
 TLeafF.cxx:64
 TLeafF.cxx:65
 TLeafF.cxx:66
 TLeafF.cxx:67
 TLeafF.cxx:68
 TLeafF.cxx:69
 TLeafF.cxx:70
 TLeafF.cxx:71
 TLeafF.cxx:72
 TLeafF.cxx:73
 TLeafF.cxx:74
 TLeafF.cxx:75
 TLeafF.cxx:76
 TLeafF.cxx:77
 TLeafF.cxx:78
 TLeafF.cxx:79
 TLeafF.cxx:80
 TLeafF.cxx:81
 TLeafF.cxx:82
 TLeafF.cxx:83
 TLeafF.cxx:84
 TLeafF.cxx:85
 TLeafF.cxx:86
 TLeafF.cxx:87
 TLeafF.cxx:88
 TLeafF.cxx:89
 TLeafF.cxx:90
 TLeafF.cxx:91
 TLeafF.cxx:92
 TLeafF.cxx:93
 TLeafF.cxx:94
 TLeafF.cxx:95
 TLeafF.cxx:96
 TLeafF.cxx:97
 TLeafF.cxx:98
 TLeafF.cxx:99
 TLeafF.cxx:100
 TLeafF.cxx:101
 TLeafF.cxx:102
 TLeafF.cxx:103
 TLeafF.cxx:104
 TLeafF.cxx:105
 TLeafF.cxx:106
 TLeafF.cxx:107
 TLeafF.cxx:108
 TLeafF.cxx:109
 TLeafF.cxx:110
 TLeafF.cxx:111
 TLeafF.cxx:112
 TLeafF.cxx:113
 TLeafF.cxx:114
 TLeafF.cxx:115
 TLeafF.cxx:116
 TLeafF.cxx:117
 TLeafF.cxx:118
 TLeafF.cxx:119
 TLeafF.cxx:120
 TLeafF.cxx:121
 TLeafF.cxx:122
 TLeafF.cxx:123
 TLeafF.cxx:124
 TLeafF.cxx:125
 TLeafF.cxx:126
 TLeafF.cxx:127
 TLeafF.cxx:128
 TLeafF.cxx:129
 TLeafF.cxx:130
 TLeafF.cxx:131
 TLeafF.cxx:132
 TLeafF.cxx:133
 TLeafF.cxx:134
 TLeafF.cxx:135
 TLeafF.cxx:136
 TLeafF.cxx:137
 TLeafF.cxx:138
 TLeafF.cxx:139
 TLeafF.cxx:140
 TLeafF.cxx:141
 TLeafF.cxx:142
 TLeafF.cxx:143
 TLeafF.cxx:144
 TLeafF.cxx:145
 TLeafF.cxx:146
 TLeafF.cxx:147
 TLeafF.cxx:148
 TLeafF.cxx:149
 TLeafF.cxx:150
 TLeafF.cxx:151
 TLeafF.cxx:152
 TLeafF.cxx:153
 TLeafF.cxx:154
 TLeafF.cxx:155
 TLeafF.cxx:156
 TLeafF.cxx:157
 TLeafF.cxx:158
 TLeafF.cxx:159
 TLeafF.cxx:160
 TLeafF.cxx:161
 TLeafF.cxx:162
 TLeafF.cxx:163
 TLeafF.cxx:164
 TLeafF.cxx:165
 TLeafF.cxx:166
 TLeafF.cxx:167
 TLeafF.cxx:168
 TLeafF.cxx:169
 TLeafF.cxx:170
 TLeafF.cxx:171
 TLeafF.cxx:172
 TLeafF.cxx:173
 TLeafF.cxx:174
 TLeafF.cxx:175
 TLeafF.cxx:176
 TLeafF.cxx:177
 TLeafF.cxx:178
 TLeafF.cxx:179
 TLeafF.cxx:180
 TLeafF.cxx:181
 TLeafF.cxx:182
 TLeafF.cxx:183
 TLeafF.cxx:184
 TLeafF.cxx:185
 TLeafF.cxx:186
 TLeafF.cxx:187
 TLeafF.cxx:188
 TLeafF.cxx:189
 TLeafF.cxx:190
 TLeafF.cxx:191
 TLeafF.cxx:192
 TLeafF.cxx:193
 TLeafF.cxx:194
 TLeafF.cxx:195
 TLeafF.cxx:196
 TLeafF.cxx:197
 TLeafF.cxx:198
 TLeafF.cxx:199
 TLeafF.cxx:200
 TLeafF.cxx:201
 TLeafF.cxx:202
 TLeafF.cxx:203
 TLeafF.cxx:204
 TLeafF.cxx:205