1212#include " TPCBase/CalArray.h"
1313#include < TMemberStreamer.h>
1414#include < TBuffer.h>
15+ #include < TROOT.h>
1516#include < DataFormatsTPC/Defs.h>
1617#include < iostream>
1718
2627// This member was written incorrectly to the TFile in ROOT versions < 6.36, causing
2728// segfaults when reading on ARM64 (occassionally).
2829// We continue to write it in the incorrect format and fix the reading back.
30+ //
31+ // Two layouts exist on disk, both with the count N/2 (N = number of flags):
32+ // - written by ROOT < 6.33 (no custom streamer): N/2 ints, each holding a pair of
33+ // flags as they sit in memory;
34+ // - written by this streamer (ROOT >= 6.33): N single shorts.
35+ // Reading the second layout as ints swaps every pair of adjacent flags, so the
36+ // layout is chosen by the ROOT version that wrote the buffer. The class version
37+ // did not change, so this is the only marker.
2938
3039// See also:
3140// - https://github.com/root-project/root/pull/17009
@@ -40,23 +49,40 @@ void MemberVectorPadFlagsStreamer(TBuffer& R__b, void* objp, int n)
4049 std::vector<o2::tpc::PadFlags>* obj = static_cast <std::vector<o2::tpc::PadFlags>*>(objp);
4150 if (R__b.IsReading ()) {
4251 obj->clear ();
43- std::vector<int > R__stl;
44- R__stl.clear ();
4552 int R__n;
4653 R__b >> R__n;
54+ // ROOT version of the file the buffer belongs to (e.g. 63206); 0 without a file (e.g. TMessage):
55+ // then writer and reader run the same ROOT
56+ int writerVersion = R__b.GetVersionOwner ();
57+ if (writerVersion <= 0 ) {
58+ writerVersion = gROOT ->GetVersionInt ();
59+ }
60+ constexpr size_t bloatfactor = sizeof (int ) / sizeof (o2::tpc::PadFlags);
61+ if (writerVersion >= 63300 ) {
62+ // written by this streamer: N single shorts
63+ obj->reserve (bloatfactor * R__n);
64+ for (size_t i = 0 ; i < bloatfactor * R__n; ++i) {
65+ Short_t readtemp;
66+ R__b >> readtemp;
67+ obj->push_back (static_cast <o2::tpc::PadFlags>(static_cast <unsigned short >(readtemp)));
68+ }
69+ return ;
70+ }
71+ // written by ROOT < 6.33: N/2 ints, each a pair of flags in memory order
72+ std::vector<int > R__stl;
4773 R__stl.reserve (R__n);
4874 for (int R__i = 0 ; R__i < R__n; R__i++) {
4975 Int_t readtemp;
5076 R__b >> readtemp;
5177 R__stl.push_back (readtemp);
5278 }
5379 auto data = reinterpret_cast <unsigned short *>(R__stl.data ());
54- constexpr size_t bloatfactor = sizeof (int ) / sizeof (o2::tpc::PadFlags);
55- for (int i = 0 ; i < bloatfactor * R__n; ++i) {
80+ for (size_t i = 0 ; i < bloatfactor * R__n; ++i) {
5681 obj->push_back (static_cast <o2::tpc::PadFlags>(data[i]));
5782 }
5883 } else {
59- // We always save things with the old format.
84+ // count N/2, then N single shorts: NOT the old (ROOT < 6.33) layout of N/2 ints; the reader above
85+ // tells the two apart by the ROOT version. Unchanged, so that existing objects stay readable.
6086 R__b << (int )obj->size () / 2 ;
6187 for (size_t i = 0 ; i < obj->size (); i++) {
6288 R__b << (short )obj->at (i);
0 commit comments