#include "TLeafS.h"
#include "TBranch.h"
#include "TClonesArray.h"
#include "Riostream.h"
ClassImp(TLeafS)
TLeafS::TLeafS(): TLeaf()
{
fValue = 0;
fPointer = 0;
fMinimum = 0;
fMaximum = 0;
fLenType = 2;
}
TLeafS::TLeafS(TBranch *parent, const char *name, const char *type)
:TLeaf(parent,name,type)
{
fLenType = 2;
fMinimum = 0;
fMaximum = 0;
fValue = 0;
fPointer = 0;
}
TLeafS::~TLeafS()
{
if (ResetAddress(0,kTRUE)) delete [] fValue;
}
void TLeafS::Export(TClonesArray *list, Int_t n)
{
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)
{
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
{
if (fIsUnsigned) return "UShort_t";
return "Short_t";
}
Double_t TLeafS::GetValue(Int_t i) const
{
if (fIsUnsigned) return (UShort_t)fValue[i];
return fValue[i];
}
void TLeafS::Import(TClonesArray *list, Int_t n)
{
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
{
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)
{
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)
{
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 )
{
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)
{
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;
}
}