Project
Loading...
Searching...
No Matches
TPCFlagsMemberCustomStreamer.cxx
Go to the documentation of this file.
1// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
2// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3// All rights not expressly granted are reserved.
4//
5// This software is distributed under the terms of the GNU General Public
6// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7//
8// In applying this license CERN does not waive the privileges and immunities
9// granted to it by virtue of its status as an Intergovernmental Organization
10// or submit itself to any jurisdiction.
11
12#include "TPCBase/CalArray.h"
13#include <TMemberStreamer.h>
14#include <TBuffer.h>
15#include <TROOT.h>
16#include <DataFormatsTPC/Defs.h>
17#include <iostream>
18
19// to enable assert statements
20#ifdef NDEBUG
21#undef NDEBUG
22#include <cassert>
23#endif
24
25// The following code provides a specific ROOT I/O streaming method
26// for the mData member of CalArray<o2::tpc::PadFlags>
27// This member was written incorrectly to the TFile in ROOT versions < 6.36, causing
28// segfaults when reading on ARM64 (occassionally).
29// 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.
38
39// See also:
40// - https://github.com/root-project/root/pull/17009
41// - https://its.cern.ch/jira/browse/O2-4671
42
43void MemberVectorPadFlagsStreamer(TBuffer& R__b, void* objp, int n)
44{
45 if (n != 1) {
46 std::cerr << "Error in MemberVectorPadFlagsStreamer : Unexpected n " << n << std::endl;
47 return;
48 }
49 std::vector<o2::tpc::PadFlags>* obj = static_cast<std::vector<o2::tpc::PadFlags>*>(objp);
50 if (R__b.IsReading()) {
51 obj->clear();
52 int R__n;
53 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;
73 R__stl.reserve(R__n);
74 for (int R__i = 0; R__i < R__n; R__i++) {
75 Int_t readtemp;
76 R__b >> readtemp;
77 R__stl.push_back(readtemp);
78 }
79 auto data = reinterpret_cast<unsigned short*>(R__stl.data());
80 for (size_t i = 0; i < bloatfactor * R__n; ++i) {
81 obj->push_back(static_cast<o2::tpc::PadFlags>(data[i]));
82 }
83 } else {
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.
86 R__b << (int)obj->size() / 2;
87 for (size_t i = 0; i < obj->size(); i++) {
88 R__b << (short)obj->at(i);
89 }
90 }
91}
92
93// register the streamer via static global initialization (on library load)
94// the streamer is only correct in combination with new ROOT
95#if ROOT_VERSION_CODE >= ROOT_VERSION(6, 33, 00)
96namespace ROOT
97{
98static __attribute__((used)) int _R__dummyStreamer_3 =
99 ([]() {
100 if (!getenv("TPC_PADFLAGS_STREAMER_OFF")) {
101 ROOT::GenerateInitInstance((o2::tpc::CalArray<o2::tpc::PadFlags> *)nullptr)->AdoptMemberStreamer("mData", new TMemberStreamer(MemberVectorPadFlagsStreamer));
102 }
103 return 0;
104 })();
105} // namespace ROOT
106#endif
int32_t i
void MemberVectorPadFlagsStreamer(TBuffer &R__b, void *objp, int n)
GLdouble n
Definition glcorearb.h:1982