// @(#)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 16 bit Integer data type.                              //
//////////////////////////////////////////////////////////////////////////

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

ClassImp(TLeafS)

//______________________________________________________________________________
TLeafS::TLeafS(): TLeaf()
{
//*-*-*-*-*-*Default constructor for LeafS*-*-*-*-*-*-*-*-*-*-*-*-*-*
//*-*        ============================

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

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

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

//______________________________________________________________________________
TLeafS::~TLeafS()
{
//*-*-*-*-*-*Default destructor for a LeafS*-*-*-*-*-*-*-*-*-*-*-*
//*-*        ===============================

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


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

   Int_t j = 0;
   for (Int_t i=0;i<n;i++) {
      memcpy((char*)list->UncheckedAt(i) + fOffset,&fValue[j], 2*fLen);
      j += fLen;
   }
}


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

   Int_t i;
   Int_t len = GetLen();
   if (fPointer) fValue = *fPointer;
   if (IsRange()) {
      if (fValue[0] > fMaximum) fMaximum = fValue[0];
   }
   if (IsUnsigned()) {
      for (i=0;i<len;i++) b << (UShort_t)fValue[i];
   } else {
      b.WriteFastArray(fValue,len);
   }
}


//______________________________________________________________________________
const char *TLeafS::GetTypeName() const
{
//*-*-*-*-*-*-*-*Returns name of leaf type*-*-*-*-*-*-*-*-*-*-*-*
//*-*            =========================

   if (fIsUnsigned) return "UShort_t";
   return "Short_t";
}


//______________________________________________________________________________
Double_t TLeafS::GetValue(Int_t i) const
{
// Returns current value of leaf
// if leaf is a simple type, i must be set to 0
// if leaf is an array, i is the array element number to be returned

   if (fIsUnsigned) return (UShort_t)fValue[i];
   return fValue[i];
}

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

   const Short_t kShortUndefined = -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, 2*fLen);
      else       memcpy(&fValue[j],&kShortUndefined,  2*fLen);
      j += fLen;
   }
}

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

   if (fIsUnsigned) {
      UShort_t *uvalue = (UShort_t*)GetValuePointer();
      printf("%u",uvalue[l]);
   } else {
      Short_t *value = (Short_t*)GetValuePointer();
      printf("%d",value[l]);
   }
}

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

   if (!fLeafCount && fNdata == 1) {
      b.ReadShort(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 TLeafS::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);
   }

   Int_t j = 0;
   for (Int_t i=0;i<n;i++) {
      memcpy((char*)list->UncheckedAt(i) + fOffset,&fValue[j], 2*fLen);
      j += fLen;
   }
}

//______________________________________________________________________________
void TLeafS::ReadValue(std::istream &s, Char_t /*delim = ' '*/)
{
// read a integer integer from std::istream s and store it into the branch buffer
   if (fIsUnsigned) {
      UShort_t *uvalue = (UShort_t*)GetValuePointer();
      for (Int_t i=0;i<fLen;i++) s >> uvalue[i];
   } else {
      Short_t *value = (Short_t*)GetValuePointer();
      for (Int_t i=0;i<fLen;i++) s >> value[i];
   }
}

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

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