-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathTBufferFile.cxx
3750 lines (3140 loc) · 122 KB
/
TBufferFile.cxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// @(#)root/io:$Id$
// Author: Rene Brun 17/01/2007
/*************************************************************************
* Copyright (C) 1995-2007, Rene Brun and Fons Rademakers. *
* All rights reserved. *
* *
* For the licensing terms see $ROOTSYS/LICENSE. *
* For the list of contributors see $ROOTSYS/README/CREDITS. *
*************************************************************************/
/**
\file TBufferFile.cxx
\class TBufferFile
\ingroup IO
The concrete implementation of TBuffer for writing/reading to/from a ROOT file or socket.
*/
#include <string.h>
#include <typeinfo>
#include <string>
#include <limits>
#include <cassert>
#include "TFile.h"
#include "TBufferFile.h"
#include "TExMap.h"
#include "TClass.h"
#include "TStorage.h"
#include "TError.h"
#include "TStreamer.h"
#include "TStreamerInfo.h"
#include "TStreamerElement.h"
#include "TSchemaRuleSet.h"
#include "TStreamerInfoActions.h"
#include "TInterpreter.h"
#include "TVirtualMutex.h"
#if (defined(__linux) || defined(__APPLE__)) && defined(__i386__) && \
defined(__GNUC__)
#define USE_BSWAPCPY
#endif
#ifdef USE_BSWAPCPY
#include "Bswapcpy.h"
#endif
const UInt_t kNewClassTag = 0xFFFFFFFF;
const UInt_t kClassMask = 0x80000000; // OR the class index with this
const UInt_t kByteCountMask = 0x40000000; // OR the byte count with this
const UInt_t kMaxMapCount = 0x3FFFFFFE; // last valid fMapCount and byte count
const Version_t kByteCountVMask = 0x4000; // OR the version byte count with this
const Version_t kMaxVersion = 0x3FFF; // highest possible version number
const Int_t kMapOffset = 2; // first 2 map entries are taken by null obj and self obj
ClassImp(TBufferFile);
////////////////////////////////////////////////////////////////////////////////
/// Thread-safe check on StreamerInfos of a TClass
static inline bool Class_Has_StreamerInfo(const TClass* cl)
{
// NOTE: we do not need a R__LOCKGUARD2 since we know the
// mutex is available since the TClass constructor will make
// it
R__LOCKGUARD(gInterpreterMutex);
return cl->GetStreamerInfos()->GetLast()>1;
}
////////////////////////////////////////////////////////////////////////////////
/// Create an I/O buffer object. Mode should be either TBuffer::kRead or
/// TBuffer::kWrite. By default the I/O buffer has a size of
/// TBuffer::kInitialSize (1024) bytes.
TBufferFile::TBufferFile(TBuffer::EMode mode)
:TBufferIO(mode),
fInfo(nullptr), fInfoStack()
{
}
////////////////////////////////////////////////////////////////////////////////
/// Create an I/O buffer object. Mode should be either TBuffer::kRead or
/// TBuffer::kWrite.
TBufferFile::TBufferFile(TBuffer::EMode mode, Int_t bufsiz)
:TBufferIO(mode,bufsiz),
fInfo(nullptr), fInfoStack()
{
}
////////////////////////////////////////////////////////////////////////////////
/// Create an I/O buffer object.
/// Mode should be either TBuffer::kRead or
/// TBuffer::kWrite. By default the I/O buffer has a size of
/// TBuffer::kInitialSize (1024) bytes. An external buffer can be passed
/// to TBuffer via the buf argument. By default this buffer will be adopted
/// unless adopt is false.
/// If the new buffer is <b>not</b> adopted and no memory allocation routine
/// is provided, a Fatal error will be issued if the Buffer attempts to
/// expand.
TBufferFile::TBufferFile(TBuffer::EMode mode, Int_t bufsiz, void *buf, Bool_t adopt, ReAllocCharFun_t reallocfunc) :
TBufferIO(mode,bufsiz,buf,adopt,reallocfunc),
fInfo(nullptr), fInfoStack()
{
}
////////////////////////////////////////////////////////////////////////////////
/// Delete an I/O buffer object.
TBufferFile::~TBufferFile()
{
}
////////////////////////////////////////////////////////////////////////////////
/// Increment level.
void TBufferFile::IncrementLevel(TVirtualStreamerInfo* info)
{
fInfoStack.push_back(fInfo);
fInfo = (TStreamerInfo*)info;
}
////////////////////////////////////////////////////////////////////////////////
/// Decrement level.
void TBufferFile::DecrementLevel(TVirtualStreamerInfo* /*info*/)
{
fInfo = fInfoStack.back();
fInfoStack.pop_back();
}
////////////////////////////////////////////////////////////////////////////////
/// Handle old file formats.
///
/// Files written with versions older than 3.00/06 had a non-portable
/// implementation of Long_t/ULong_t. These types should not have been
/// used at all. However, because some users had already written many
/// files with these types we provide this dirty patch for "backward
/// compatibility"
static void frombufOld(char *&buf, Long_t *x)
{
#ifdef R__BYTESWAP
#ifdef R__B64
char *sw = (char *)x;
sw[0] = buf[7];
sw[1] = buf[6];
sw[2] = buf[5];
sw[3] = buf[4];
sw[4] = buf[3];
sw[5] = buf[2];
sw[6] = buf[1];
sw[7] = buf[0];
#else
char *sw = (char *)x;
sw[0] = buf[3];
sw[1] = buf[2];
sw[2] = buf[1];
sw[3] = buf[0];
#endif
#else
memcpy(x, buf, sizeof(Long_t));
#endif
buf += sizeof(Long_t);
}
////////////////////////////////////////////////////////////////////////////////
/// Read Long from TBuffer.
void TBufferFile::ReadLong(Long_t &l)
{
TFile *file = (TFile*)fParent;
if (file && file->GetVersion() < 30006) {
frombufOld(fBufCur, &l);
} else {
frombuf(fBufCur, &l);
}
}
////////////////////////////////////////////////////////////////////////////////
/// Read TString from TBuffer.
void TBufferFile::ReadTString(TString &s)
{
Int_t nbig;
UChar_t nwh;
*this >> nwh;
if (nwh == 0) {
s.UnLink();
s.Zero();
} else {
if (nwh == 255)
*this >> nbig;
else
nbig = nwh;
nbig = s.Clobber(nbig); // update length since Clobber clamps to MaxSize (if Fatal does not abort)
char *data = s.GetPointer();
data[nbig] = 0;
s.SetSize(nbig);
ReadFastArray(data, nbig);
}
}
////////////////////////////////////////////////////////////////////////////////
/// Write TString to TBuffer.
void TBufferFile::WriteTString(const TString &s)
{
Int_t nbig = s.Length();
UChar_t nwh;
if (nbig > 254) {
nwh = 255;
*this << nwh;
*this << nbig;
} else {
nwh = UChar_t(nbig);
*this << nwh;
}
const char *data = s.GetPointer();
WriteFastArray(data, nbig);
}
////////////////////////////////////////////////////////////////////////////////
/// Read std::string from TBuffer.
void TBufferFile::ReadStdString(std::string *obj)
{
if (obj == nullptr) {
Error("TBufferFile::ReadStdString","The std::string address is nullptr but should not");
return;
}
Int_t nbig;
UChar_t nwh;
*this >> nwh;
if (nwh == 0) {
obj->clear();
} else {
if( obj->size() ) {
// Insure that the underlying data storage is not shared
(*obj)[0] = '\0';
}
if (nwh == 255) {
*this >> nbig;
obj->resize(nbig,'\0');
ReadFastArray((char*)obj->data(),nbig);
}
else {
obj->resize(nwh,'\0');
ReadFastArray((char*)obj->data(),nwh);
}
}
}
////////////////////////////////////////////////////////////////////////////////
/// Write std::string to TBuffer.
void TBufferFile::WriteStdString(const std::string *obj)
{
if (obj==0) {
*this << (UChar_t)0;
WriteFastArray("",0);
return;
}
UChar_t nwh;
Int_t nbig = obj->length();
if (nbig > 254) {
nwh = 255;
*this << nwh;
*this << nbig;
} else {
nwh = UChar_t(nbig);
*this << nwh;
}
WriteFastArray(obj->data(),nbig);
}
////////////////////////////////////////////////////////////////////////////////
/// Read char* from TBuffer.
void TBufferFile::ReadCharStar(char* &s)
{
delete [] s;
s = nullptr;
Int_t nch;
*this >> nch;
if (nch > 0) {
s = new char[nch+1];
ReadFastArray(s, nch);
s[nch] = 0;
}
}
////////////////////////////////////////////////////////////////////////////////
/// Write char* into TBuffer.
void TBufferFile::WriteCharStar(char *s)
{
Int_t nch = 0;
if (s) {
nch = strlen(s);
*this << nch;
WriteFastArray(s,nch);
} else {
*this << nch;
}
}
////////////////////////////////////////////////////////////////////////////////
/// Set byte count at position cntpos in the buffer. Generate warning if
/// count larger than kMaxMapCount. The count is excluded its own size.
/// \note If underflow or overflow, an Error ir raised (stricter checks in Debug mode)
void TBufferFile::SetByteCount(UInt_t cntpos, Bool_t packInVersion)
{
assert( (sizeof(UInt_t) + cntpos) < static_cast<UInt_t>(fBufCur - fBuffer)
&& (fBufCur >= fBuffer)
&& static_cast<ULong64_t>(fBufCur - fBuffer) <= std::numeric_limits<UInt_t>::max()
&& "Byte count position is after the end of the buffer");
const UInt_t cnt = UInt_t(fBufCur - fBuffer) - cntpos - sizeof(UInt_t);
char *buf = (char *)(fBuffer + cntpos);
// if true, pack byte count in two consecutive shorts, so it can
// be read by ReadVersion()
if (packInVersion) {
union {
UInt_t cnt;
Version_t vers[2];
} v;
v.cnt = cnt;
#ifdef R__BYTESWAP
tobuf(buf, Version_t(v.vers[1] | kByteCountVMask));
tobuf(buf, v.vers[0]);
#else
tobuf(buf, Version_t(v.vers[0] | kByteCountVMask));
tobuf(buf, v.vers[1]);
#endif
} else
tobuf(buf, cnt | kByteCountMask);
if (cnt >= kMaxMapCount) {
Error("WriteByteCount", "bytecount too large (more than %d)", kMaxMapCount);
// exception
}
}
////////////////////////////////////////////////////////////////////////////////
/// Check byte count with current buffer position. They should
/// match. If not print warning and position buffer in correct
/// place determined by the byte count. Startpos is position of
/// first byte where the byte count is written in buffer.
/// Returns 0 if everything is ok, otherwise the bytecount offset
/// (< 0 when read too little, >0 when read too much).
Int_t TBufferFile::CheckByteCount(UInt_t startpos, UInt_t bcnt, const TClass *clss, const char *classname)
{
if (!bcnt) return 0;
Int_t offset = 0;
Longptr_t endpos = Longptr_t(fBuffer) + startpos + bcnt + sizeof(UInt_t);
if (Longptr_t(fBufCur) != endpos) {
offset = Int_t(Longptr_t(fBufCur) - endpos);
const char *name = clss ? clss->GetName() : classname ? classname : 0;
if (name) {
if (offset < 0) {
Error("CheckByteCount", "object of class %s read too few bytes: %d instead of %d",
name,bcnt+offset,bcnt);
}
if (offset > 0) {
Error("CheckByteCount", "object of class %s read too many bytes: %d instead of %d",
name,bcnt+offset,bcnt);
if (fParent)
Warning("CheckByteCount","%s::Streamer() not in sync with data on file %s, fix Streamer()",
name, fParent->GetName());
else
Warning("CheckByteCount","%s::Streamer() not in sync with data, fix Streamer()",
name);
}
}
if ( ((char *)endpos) > fBufMax ) {
offset = fBufMax-fBufCur;
Error("CheckByteCount",
"Byte count probably corrupted around buffer position %d:\n\t%d for a possible maximum of %d",
startpos, bcnt, offset);
fBufCur = fBufMax;
} else {
fBufCur = (char *) endpos;
}
}
return offset;
}
////////////////////////////////////////////////////////////////////////////////
/// Check byte count with current buffer position. They should
/// match. If not print warning and position buffer in correct
/// place determined by the byte count. Startpos is position of
/// first byte where the byte count is written in buffer.
/// Returns 0 if everything is ok, otherwise the bytecount offset
/// (< 0 when read too little, >0 when read too much).
Int_t TBufferFile::CheckByteCount(UInt_t startpos, UInt_t bcnt, const TClass *clss)
{
if (!bcnt) return 0;
return CheckByteCount( startpos, bcnt, clss, nullptr);
}
////////////////////////////////////////////////////////////////////////////////
/// Check byte count with current buffer position. They should
/// match. If not print warning and position buffer in correct
/// place determined by the byte count. Startpos is position of
/// first byte where the byte count is written in buffer.
/// Returns 0 if everything is ok, otherwise the bytecount offset
/// (< 0 when read too little, >0 when read too much).
Int_t TBufferFile::CheckByteCount(UInt_t startpos, UInt_t bcnt, const char *classname)
{
if (!bcnt) return 0;
return CheckByteCount( startpos, bcnt, nullptr, classname);
}
////////////////////////////////////////////////////////////////////////////////
/// Read a Float16_t from the buffer,
/// see comments about Float16_t encoding at TBufferFile::WriteFloat16().
void TBufferFile::ReadFloat16(Float_t *f, TStreamerElement *ele)
{
if (ele && ele->GetFactor() != 0) {
ReadWithFactor(f, ele->GetFactor(), ele->GetXmin());
} else {
Int_t nbits = 0;
if (ele) nbits = (Int_t)ele->GetXmin();
if (!nbits) nbits = 12;
ReadWithNbits(f, nbits);
}
}
////////////////////////////////////////////////////////////////////////////////
/// Read a Double32_t from the buffer,
/// see comments about Double32_t encoding at TBufferFile::WriteDouble32().
void TBufferFile::ReadDouble32(Double_t *d, TStreamerElement *ele)
{
if (ele && ele->GetFactor() != 0) {
ReadWithFactor(d, ele->GetFactor(), ele->GetXmin());
} else {
Int_t nbits = 0;
if (ele) nbits = (Int_t)ele->GetXmin();
if (!nbits) {
//we read a float and convert it to double
Float_t afloat;
*this >> afloat;
d[0] = (Double_t)afloat;
} else {
ReadWithNbits(d, nbits);
}
}
}
////////////////////////////////////////////////////////////////////////////////
/// Read a Float16_t from the buffer when the factor and minimum value have been specified
/// see comments about Double32_t encoding at TBufferFile::WriteDouble32().
void TBufferFile::ReadWithFactor(Float_t *ptr, Double_t factor, Double_t minvalue)
{
//a range was specified. We read an integer and convert it back to a double.
UInt_t aint;
frombuf(this->fBufCur,&aint);
ptr[0] = (Float_t)(aint/factor + minvalue);
}
////////////////////////////////////////////////////////////////////////////////
/// Read a Float16_t from the buffer when the number of bits is specified (explicitly or not)
/// see comments about Float16_t encoding at TBufferFile::WriteFloat16().
void TBufferFile::ReadWithNbits(Float_t *ptr, Int_t nbits)
{
//we read the exponent and the truncated mantissa of the float
//and rebuild the float.
union {
Float_t fFloatValue;
Int_t fIntValue;
} temp;
UChar_t theExp;
UShort_t theMan;
frombuf(this->fBufCur,&theExp);
frombuf(this->fBufCur,&theMan);
temp.fIntValue = theExp;
temp.fIntValue <<= 23;
temp.fIntValue |= (theMan & ((1<<(nbits+1))-1)) <<(23-nbits);
if(1<<(nbits+1) & theMan) temp.fFloatValue = -temp.fFloatValue;
ptr[0] = temp.fFloatValue;
}
////////////////////////////////////////////////////////////////////////////////
/// Read a Double32_t from the buffer when the factor and minimum value have been specified
/// see comments about Double32_t encoding at TBufferFile::WriteDouble32().
void TBufferFile::ReadWithFactor(Double_t *ptr, Double_t factor, Double_t minvalue)
{
//a range was specified. We read an integer and convert it back to a double.
UInt_t aint;
frombuf(this->fBufCur,&aint);
ptr[0] = (Double_t)(aint/factor + minvalue);
}
////////////////////////////////////////////////////////////////////////////////
/// Read a Double32_t from the buffer when the number of bits is specified (explicitly or not)
/// see comments about Double32_t encoding at TBufferFile::WriteDouble32().
void TBufferFile::ReadWithNbits(Double_t *ptr, Int_t nbits)
{
//we read the exponent and the truncated mantissa of the float
//and rebuild the float.
union {
Float_t fFloatValue;
Int_t fIntValue;
} temp;
UChar_t theExp;
UShort_t theMan;
frombuf(this->fBufCur,&theExp);
frombuf(this->fBufCur,&theMan);
temp.fIntValue = theExp;
temp.fIntValue <<= 23;
temp.fIntValue |= (theMan & ((1<<(nbits+1))-1)) <<(23-nbits);
if(1<<(nbits+1) & theMan) temp.fFloatValue = -temp.fFloatValue;
ptr[0] = (Double_t)temp.fFloatValue;
}
////////////////////////////////////////////////////////////////////////////////
/// Write a Float16_t to the buffer.
///
/// The following cases are supported for streaming a Float16_t type
/// depending on the range declaration in the comment field of the data member:
/// Case | Example |
/// -----|---------|
/// A | Float16_t fNormal; |
/// B | Float16_t fTemperature; //[0,100]|
/// C | Float16_t fCharge; //[-1,1,2]|
/// D | Float16_t fVertex[3]; //[-30,30,10]|
/// E | Float16_t fChi2; //[0,0,6]|
/// F | Int_t fNsp;<br>Float16_t* fPointValue; //[fNsp][0,3]|
///
/// - In case A fNormal is converted from a Float_t to a Float_t with mantissa truncated to 12 bits
/// - In case B fTemperature is converted to a 32 bit unsigned integer
/// - In case C fCharge is converted to a 2 bits unsigned integer
/// - In case D the array elements of fVertex are converted to an unsigned 10 bits integer
/// - In case E fChi2 is converted to a Float_t with truncated precision at 6 bits
/// - In case F the fNsp elements of array fPointvalue are converted to an unsigned 32 bit integer
/// Note that the range specifier must follow the dimension specifier.
/// Case B has more precision (9 to 10 significative digits than case A (6 to 7 digits).
/// In Case A and E, the exponent is stored as is (8 bits), for a total of 21 bits (including 1 bit for sign)
///
/// The range specifier has the general format: [xmin,xmax] or [xmin,xmax,nbits]
/// - [0,1];
/// - [-10,100];
/// - [-pi,pi], [-pi/2,pi/4],[-2pi,2*pi]
/// - [-10,100,16]
/// - [0,0,8]
/// if nbits is not specified, or nbits <2 or nbits>16 it is set to 16. If
/// (xmin==0 and xmax==0 and nbits <=14) the float word will have
/// its mantissa truncated to nbits significative bits.
///
/// ## IMPORTANT NOTE
/// ### NOTE 1
/// Lets assume an original variable float x:
/// When using the format [0,0,8] (ie range not specified) you get the best
/// relative precision when storing and reading back the truncated x, say xt.
/// The variance of (x-xt)/x will be better than when specifying a range
/// for the same number of bits. However the precision relative to the
/// range (x-xt)/(xmax-xmin) will be worst, and vice-versa.
/// The format [0,0,8] is also interesting when the range of x is infinite
/// or unknown.
///
/// ### NOTE 2
/// It is important to understand the difference with the meaning of nbits
/// - in case of [-1,1,nbits], nbits is the total number of bits used to make
/// the conversion from a float to an integer
/// - in case of [0,0,nbits], nbits is the number of bits used for the mantissa, to which is added 8 bits for the exponent.
///
/// See example of use of the Float16_t data type in tutorial float16.C
/// \image html tbufferfile_double32.gif
void TBufferFile::WriteFloat16(Float_t *f, TStreamerElement *ele)
{
if (ele && ele->GetFactor() != 0) {
//A range is specified. We normalize the double to the range and
//convert it to an integer using a scaling factor that is a function of nbits.
//see TStreamerElement::GetRange.
Double_t x = f[0];
Double_t xmin = ele->GetXmin();
Double_t xmax = ele->GetXmax();
if (x < xmin) x = xmin;
if (x > xmax) x = xmax;
UInt_t aint = UInt_t(0.5+ele->GetFactor()*(x-xmin)); *this << aint;
} else {
Int_t nbits = 0;
//number of bits stored in fXmin (see TStreamerElement::GetRange)
if (ele) nbits = (Int_t)ele->GetXmin();
if (!nbits) nbits = 12;
//a range is not specified, but nbits is.
//In this case we truncate the mantissa to nbits and we stream
//the exponent as a UChar_t and the mantissa as a UShort_t.
union {
Float_t fFloatValue;
Int_t fIntValue;
};
fFloatValue = f[0];
UChar_t theExp = (UChar_t)(0x000000ff & ((fIntValue<<1)>>24));
UShort_t theMan = ((1<<(nbits+1))-1) & (fIntValue>>(23-nbits-1));
theMan++;
theMan = theMan>>1;
if (theMan&1<<nbits) theMan = (1<<nbits) - 1;
if (fFloatValue < 0) theMan |= 1<<(nbits+1);
*this << theExp;
*this << theMan;
}
}
////////////////////////////////////////////////////////////////////////////////
/// Write a Double32_t to the buffer.
///
/// The following cases are supported for streaming a Double32_t type
/// depending on the range declaration in the comment field of the data member:
/// Case | Example |
/// -----|---------|
/// A | Double32_t fNormal; |
/// B | Double32_t fTemperature; //[0,100]|
/// C | Double32_t fCharge; //[-1,1,2]|
/// D | Double32_t fVertex[3]; //[-30,30,10]|
/// E | Double32_t fChi2; //[0,0,6]|
/// F | Int_t fNsp;<br>Double32_t* fPointValue; //[fNsp][0,3]|
///
/// In case A fNormal is converted from a Double_t to a Float_t
/// In case B fTemperature is converted to a 32 bit unsigned integer
/// In case C fCharge is converted to a 2 bits unsigned integer
/// In case D the array elements of fVertex are converted to an unsigned 10 bits integer
/// In case E fChi2 is converted to a Float_t with mantissa truncated precision at 6 bits
/// In case F the fNsp elements of array fPointvalue are converted to an unsigned 32 bit integer
/// Note that the range specifier must follow the dimension specifier.
/// Case B has more precision (9 to 10 significative digits than case A (6 to 7 digits).
/// See TBufferFile::WriteFloat16 for more information.
///
/// see example of use of the Double32_t data type in tutorial double32.C
/// \image html tbufferfile_double32.gif
void TBufferFile::WriteDouble32(Double_t *d, TStreamerElement *ele)
{
if (ele && ele->GetFactor() != 0) {
//A range is specified. We normalize the double to the range and
//convert it to an integer using a scaling factor that is a function of nbits.
//see TStreamerElement::GetRange.
Double_t x = d[0];
Double_t xmin = ele->GetXmin();
Double_t xmax = ele->GetXmax();
if (x < xmin) x = xmin;
if (x > xmax) x = xmax;
UInt_t aint = UInt_t(0.5+ele->GetFactor()*(x-xmin)); *this << aint;
} else {
Int_t nbits = 0;
//number of bits stored in fXmin (see TStreamerElement::GetRange)
if (ele) nbits = (Int_t)ele->GetXmin();
if (!nbits) {
//if no range and no bits specified, we convert from double to float
Float_t afloat = (Float_t)d[0];
*this << afloat;
} else {
//a range is not specified, but nbits is.
//In this case we truncate the mantissa to nbits and we stream
//the exponent as a UChar_t and the mantissa as a UShort_t.
union {
Float_t fFloatValue;
Int_t fIntValue;
};
fFloatValue = (Float_t)d[0];
UChar_t theExp = (UChar_t)(0x000000ff & ((fIntValue<<1)>>24));
UShort_t theMan = ((1<<(nbits+1))-1) & (fIntValue>>(23-nbits-1)) ;
theMan++;
theMan = theMan>>1;
if (theMan&1<<nbits) theMan = (1<<nbits)-1 ;
if (fFloatValue < 0) theMan |= 1<<(nbits+1);
*this << theExp;
*this << theMan;
}
}
}
////////////////////////////////////////////////////////////////////////////////
/// Read array of bools from the I/O buffer. Returns the number of
/// bools read. If argument is a 0 pointer then space will be
/// allocated for the array.
Int_t TBufferFile::ReadArray(Bool_t *&b)
{
R__ASSERT(IsReading());
Int_t n;
*this >> n;
if (n <= 0 || n > fBufSize) return 0;
if (!b) b = new Bool_t[n];
if (sizeof(Bool_t) > 1) {
for (int i = 0; i < n; i++)
frombuf(fBufCur, &b[i]);
} else {
Int_t l = sizeof(Bool_t)*n;
memcpy(b, fBufCur, l);
fBufCur += l;
}
return n;
}
////////////////////////////////////////////////////////////////////////////////
/// Read array of characters from the I/O buffer. Returns the number of
/// characters read. If argument is a 0 pointer then space will be
/// allocated for the array.
Int_t TBufferFile::ReadArray(Char_t *&c)
{
R__ASSERT(IsReading());
Int_t n;
*this >> n;
Int_t l = sizeof(Char_t)*n;
if (l <= 0 || l > fBufSize) return 0;
if (!c) c = new Char_t[n];
memcpy(c, fBufCur, l);
fBufCur += l;
return n;
}
////////////////////////////////////////////////////////////////////////////////
/// Read array of shorts from the I/O buffer. Returns the number of shorts
/// read. If argument is a 0 pointer then space will be allocated for the
/// array.
Int_t TBufferFile::ReadArray(Short_t *&h)
{
R__ASSERT(IsReading());
Int_t n;
*this >> n;
Int_t l = sizeof(Short_t)*n;
if (l <= 0 || l > fBufSize) return 0;
if (!h) h = new Short_t[n];
#ifdef R__BYTESWAP
# ifdef USE_BSWAPCPY
bswapcpy16(h, fBufCur, n);
fBufCur += l;
# else
for (int i = 0; i < n; i++)
frombuf(fBufCur, &h[i]);
# endif
#else
memcpy(h, fBufCur, l);
fBufCur += l;
#endif
return n;
}
////////////////////////////////////////////////////////////////////////////////
/// Read array of ints from the I/O buffer. Returns the number of ints
/// read. If argument is a 0 pointer then space will be allocated for the
/// array.
Int_t TBufferFile::ReadArray(Int_t *&ii)
{
R__ASSERT(IsReading());
Int_t n;
*this >> n;
Int_t l = sizeof(Int_t)*n;
if (l <= 0 || l > fBufSize) return 0;
if (!ii) ii = new Int_t[n];
#ifdef R__BYTESWAP
# ifdef USE_BSWAPCPY
bswapcpy32(ii, fBufCur, n);
fBufCur += l;
# else
for (int i = 0; i < n; i++)
frombuf(fBufCur, &ii[i]);
# endif
#else
memcpy(ii, fBufCur, l);
fBufCur += l;
#endif
return n;
}
////////////////////////////////////////////////////////////////////////////////
/// Read array of longs from the I/O buffer. Returns the number of longs
/// read. If argument is a 0 pointer then space will be allocated for the
/// array.
Int_t TBufferFile::ReadArray(Long_t *&ll)
{
R__ASSERT(IsReading());
Int_t n;
*this >> n;
Int_t l = sizeof(Long_t)*n;
if (l <= 0 || l > fBufSize) return 0;
if (!ll) ll = new Long_t[n];
TFile *file = (TFile*)fParent;
if (file && file->GetVersion() < 30006) {
for (int i = 0; i < n; i++) frombufOld(fBufCur, &ll[i]);
} else {
for (int i = 0; i < n; i++) frombuf(fBufCur, &ll[i]);
}
return n;
}
////////////////////////////////////////////////////////////////////////////////
/// Read array of long longs from the I/O buffer. Returns the number of
/// long longs read. If argument is a 0 pointer then space will be
/// allocated for the array.
Int_t TBufferFile::ReadArray(Long64_t *&ll)
{
R__ASSERT(IsReading());
Int_t n;
*this >> n;
Int_t l = sizeof(Long64_t)*n;
if (l <= 0 || l > fBufSize) return 0;
if (!ll) ll = new Long64_t[n];
#ifdef R__BYTESWAP
for (int i = 0; i < n; i++)
frombuf(fBufCur, &ll[i]);
#else
memcpy(ll, fBufCur, l);
fBufCur += l;
#endif
return n;
}
////////////////////////////////////////////////////////////////////////////////
/// Read array of floats from the I/O buffer. Returns the number of floats
/// read. If argument is a 0 pointer then space will be allocated for the
/// array.
Int_t TBufferFile::ReadArray(Float_t *&f)
{
R__ASSERT(IsReading());
Int_t n;
*this >> n;
Int_t l = sizeof(Float_t)*n;
if (l <= 0 || l > fBufSize) return 0;
if (!f) f = new Float_t[n];
#ifdef R__BYTESWAP
# ifdef USE_BSWAPCPY
bswapcpy32(f, fBufCur, n);
fBufCur += l;
# else
for (int i = 0; i < n; i++)
frombuf(fBufCur, &f[i]);
# endif
#else
memcpy(f, fBufCur, l);
fBufCur += l;
#endif
return n;
}
////////////////////////////////////////////////////////////////////////////////
/// Read array of doubles from the I/O buffer. Returns the number of doubles
/// read. If argument is a 0 pointer then space will be allocated for the
/// array.
Int_t TBufferFile::ReadArray(Double_t *&d)
{
R__ASSERT(IsReading());
Int_t n;
*this >> n;
Int_t l = sizeof(Double_t)*n;
if (l <= 0 || l > fBufSize) return 0;
if (!d) d = new Double_t[n];
#ifdef R__BYTESWAP
for (int i = 0; i < n; i++)
frombuf(fBufCur, &d[i]);
#else
memcpy(d, fBufCur, l);
fBufCur += l;
#endif
return n;
}
////////////////////////////////////////////////////////////////////////////////
/// Read array of floats (written as truncated float) from the I/O buffer.
/// Returns the number of floats read.
/// If argument is a 0 pointer then space will be allocated for the array.
/// see comments about Float16_t encoding at TBufferFile::WriteFloat16
Int_t TBufferFile::ReadArrayFloat16(Float_t *&f, TStreamerElement *ele)
{
R__ASSERT(IsReading());
Int_t n;
*this >> n;
if (n <= 0 || 3*n > fBufSize) return 0;
if (!f) f = new Float_t[n];
ReadFastArrayFloat16(f,n,ele);
return n;
}
////////////////////////////////////////////////////////////////////////////////
/// Read array of doubles (written as float) from the I/O buffer.
/// Returns the number of doubles read.
/// If argument is a 0 pointer then space will be allocated for the array.
/// see comments about Double32_t encoding at TBufferFile::WriteDouble32
Int_t TBufferFile::ReadArrayDouble32(Double_t *&d, TStreamerElement *ele)
{
R__ASSERT(IsReading());
Int_t n;
*this >> n;
if (n <= 0 || 3*n > fBufSize) return 0;
if (!d) d = new Double_t[n];
ReadFastArrayDouble32(d,n,ele);
return n;
}
////////////////////////////////////////////////////////////////////////////////
/// Read array of bools from the I/O buffer. Returns the number of bools
/// read.
Int_t TBufferFile::ReadStaticArray(Bool_t *b)
{
R__ASSERT(IsReading());
Int_t n;
*this >> n;
if (n <= 0 || n > fBufSize) return 0;
if (!b) return 0;
if (sizeof(Bool_t) > 1) {
for (int i = 0; i < n; i++)
frombuf(fBufCur, &b[i]);
} else {
Int_t l = sizeof(Bool_t)*n;
memcpy(b, fBufCur, l);
fBufCur += l;