Project
Loading...
Searching...
No Matches
DataRefUtils.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 <span>
13#include <typeinfo>
14#include <cstring>
17#include "Framework/Logger.h"
18#include <TMemFile.h>
19#include <TTree.h>
20#include <TH1.h>
21#include <TError.h>
22#include <algorithm>
23#if defined(_LIBCPP_VERSION)
24#include <TList.h>
25#include <TStreamerElement.h>
26#include <TStreamerInfo.h>
27#endif
28
29namespace o2::framework
30{
31
32namespace
33{
34void* extractFromTFile(TFile& file, TClass const* cl, const char* what)
35{
36 if (!cl) {
37 return nullptr;
38 }
39#if defined(_LIBCPP_VERSION)
40 DataRefUtils::registerLibstdcxxStdArray(file, cl);
41#endif
42 auto object = file.GetObjectChecked(what, cl);
43 if (!object) {
44 // it could be that object was stored with previous convention
45 // where the classname was taken as key
46 std::string objectName(cl->GetName());
47 objectName.erase(std::find_if(objectName.rbegin(), objectName.rend(), [](unsigned char ch) {
48 return !std::isspace(ch);
49 }).base(),
50 objectName.end());
51 objectName.erase(objectName.begin(), std::find_if(objectName.begin(), objectName.end(), [](unsigned char ch) {
52 return !std::isspace(ch);
53 }));
54
55 object = file.GetObjectChecked(objectName.c_str(), cl);
56 LOG(warn) << "Did not find object under expected name " << what;
57 if (!object) {
58 return nullptr;
59 }
60 LOG(warn) << "Found object under deprecated name " << cl->GetName();
61 }
62 auto result = object;
63 // We need to handle some specific cases as ROOT ties them deeply
64 // to the file they are contained in
65 if (cl->IsTObject()) {
66 // make a clone
67 // detach from the file
68 auto tree = dynamic_cast<TTree*>((TObject*)object);
69 if (tree) {
70 tree->LoadBaskets(0x1L << 32); // make tree memory based
71 tree->SetDirectory(nullptr);
72 result = tree;
73 } else {
74 auto h = dynamic_cast<TH1*>((TObject*)object);
75 if (h) {
76 h->SetDirectory(nullptr);
77 result = h;
78 }
79 }
80 }
81 return result;
82}
83} // namespace
84
85#if defined(_LIBCPP_VERSION)
86// The only member of std::array<T,N> is T _M_elems[N] in libstdc++, T __elems_[N] in libc++, so
87// objects written on Linux refer to a StreamerInfo checksum the libc++ class does not have, and
88// the read fails with "Could not find the StreamerInfo with a checksum", leaving the array
89// zero-filled (or -1 where the constructor put that). This happens for an std::array stored as
90// the object itself (TRD's chamber status) and as a member (ITS GeometryTGeo::mLayerToWrapper).
91// For each std::array the file describes, register its libstdc++ layout as a version of its own,
92// under the in-memory member name, so that the payload, T[N] for both, is read as usual.
93namespace
94{
95// TStreamerInfo::GetCheckSum for a class whose only member is "name" of type T[N]
96UInt_t stdArrayCheckSum(TClass const* cl, TStreamerElement const* el, char const* name)
97{
98 UInt_t id = 0;
99 auto add = [&id](char const* s) { for (; *s; ++s) { id = id * 3 + *s; } };
100 add(cl->GetName());
101 add(name);
102 add(el->GetTypeName());
103 for (int i = 0; i < el->GetArrayDim(); ++i) {
104 id = id * 3 + el->GetMaxIndex(i);
105 }
106 return id;
107}
108} // namespace
109
110void DataRefUtils::registerLibstdcxxStdArray(TFile& file, TClass const* /*cl*/)
111{
112 auto* infos = file.GetStreamerInfoCache();
113 if (!infos) {
114 return;
115 }
116 for (auto* obj : *infos) {
117 auto* onfile = dynamic_cast<TStreamerInfo*>(obj);
118 if (!onfile || std::strncmp(onfile->GetName(), "array<", 6) != 0 || onfile->GetNelement() != 1) {
119 continue;
120 }
121 auto* cl = TClass::GetClass(onfile->GetName());
122 if (!cl) {
123 continue;
124 }
125 // Exists before BuildCheck, so that the libstdc++ layout gets a version of its own
126 auto* current = static_cast<TStreamerInfo*>(cl->GetStreamerInfo());
127 if (!current || current->GetNelement() != 1) {
128 continue;
129 }
130 // ROOT may already have replaced the file's layout by the in-memory one (members of other
131 // classes): then rebuild the libstdc++ one, which differs only by the member name.
132 auto checksum = std::strcmp(onfile->GetElement(0)->GetName(), "_M_elems") == 0 ? onfile->GetCheckSum() : stdArrayCheckSum(cl, current->GetElement(0), "_M_elems");
133 auto* info = static_cast<TStreamerInfo*>(cl->FindStreamerInfo(checksum));
134 if (!info) {
135 info = static_cast<TStreamerInfo*>((std::strcmp(onfile->GetElement(0)->GetName(), "_M_elems") == 0 ? onfile : current)->Clone());
136 info->GetElement(0)->SetName("_M_elems");
137 info->SetCheckSum(checksum);
138 info->BuildCheck(&file);
139 info = static_cast<TStreamerInfo*>(cl->FindStreamerInfo(checksum));
140 if (!info) {
141 continue;
142 }
143 }
144 // Also when ROOT registered it itself: it then reads nothing, without any error
145 if (std::strcmp(info->GetElement(0)->GetName(), current->GetElement(0)->GetName()) != 0) {
146 info->GetElement(0)->SetName(current->GetElement(0)->GetName());
147 info->Clear("build");
148 info->BuildOld();
149 }
150 }
151}
152#endif
153
154// Adapted from CcdbApi private method interpretAsTMemFileAndExtract
155// If the former is moved to public, throws on error and could be changed to
156// not require a mutex we could use it.
157void* DataRefUtils::decodeCCDB(DataRef const& ref, std::type_info const& tinfo)
158{
159 void* result = nullptr;
160 Int_t previousErrorLevel = gErrorIgnoreLevel;
161 gErrorIgnoreLevel = kFatal;
162 auto* dh = o2::header::get<o2::header::DataHeader*>(ref.header);
163 const char* buff = const_cast<char*>(ref.payload);
164 size_t flSize = dh->payloadSize;
165 // does it have a flattened headers map attached in the end?
166 constexpr char FlatHeaderAnnot[] = "$HEADER$";
167 constexpr int Offset = sizeof(int) + sizeof(FlatHeaderAnnot);
168 int headerSize = 0;
169 LOGP(debug, "DHPayloadSize={}>{} Ref:{}/{} Cmp {}:{}", dh->payloadSize, Offset, dh->dataOrigin.as<std::string>(), dh->dataDescription.as<std::string>(), std::string{buff + dh->payloadSize - sizeof(FlatHeaderAnnot)}, std::string{FlatHeaderAnnot});
170
171 if (dh->payloadSize >= Offset &&
172 !std::strncmp(buff + dh->payloadSize - sizeof(FlatHeaderAnnot), FlatHeaderAnnot, sizeof(FlatHeaderAnnot))) {
173 headerSize = *reinterpret_cast<const int*>(buff + dh->payloadSize - Offset);
174 }
175 if (headerSize < 0) {
176 LOGP(fatal, "Anomalous flattened header size {} extracted for CCDB object {}/{}", headerSize, dh->dataOrigin.as<std::string>(), dh->dataDescription.as<std::string>());
177 }
178 TMemFile memFile("name", const_cast<char*>(ref.payload), dh->payloadSize - headerSize, "READ");
179 gErrorIgnoreLevel = previousErrorLevel;
180 if (memFile.IsZombie()) {
181 return nullptr;
182 }
183
184 TClass* tcl = TClass::GetClass(tinfo);
185 result = extractFromTFile(memFile, tcl, "ccdb_object");
186 if (!result) {
187 throw runtime_error_f("Couldn't retrieve object corresponding to %s from TFile", tcl->GetName());
188 }
189 memFile.Close();
190 return result;
191}
192
193std::map<std::string, std::string> DataRefUtils::extractCCDBHeaders(DataRef const& ref)
194{
195 auto* dh = o2::header::get<o2::header::DataHeader*>(ref.header);
196 const char* buff = const_cast<char*>(ref.payload);
197 // does it have a flattened headers map attached in the end?
198 constexpr char FlatHeaderAnnot[] = "$HEADER$";
199 constexpr int Offset = sizeof(int) + sizeof(FlatHeaderAnnot);
200 int headerSize = 0, ss0 = 0;
201 std::map<std::string, std::string> res;
202 if (dh->payloadSize >= Offset && !std::strncmp(buff + dh->payloadSize - sizeof(FlatHeaderAnnot), FlatHeaderAnnot, sizeof(FlatHeaderAnnot))) {
203 headerSize = *reinterpret_cast<const int*>(buff + dh->payloadSize - Offset);
204 } else { // header was not added
205 LOGP(warn, "CCDB headers were not added to condition object blob, returning dummy header map");
206 return res;
207 }
208
209 if (headerSize < 0) {
210 LOGP(fatal, "Anomalous flattened header size {} extracted for CCDB object {}/{}", headerSize, dh->dataOrigin.as<std::string>(), dh->dataDescription.as<std::string>());
211 }
212
213 buff += dh->payloadSize - headerSize; // jump to the start of flattened header
214 headerSize -= Offset;
215 const char* str0 = &buff[ss0++];
216 while (ss0 < headerSize) {
217 if (buff[ss0++] == 0) {
218 if (!str0) {
219 str0 = &buff[ss0]; // new key string is found
220 } else {
221 res.emplace(std::string(str0), std::string(&buff[ss0])); // new value string found, add key value to the map
222 LOGP(debug, "Header{} {}:{}", res.size(), std::string(str0), std::string(&buff[ss0]));
223 str0 = nullptr;
224 }
225 }
226 }
227 return res;
228}
229
230std::span<const char> DataRefUtils::getCCDBPayloadBlob(DataRef const& ref)
231{
232 auto* dh = o2::header::get<o2::header::DataHeader*>(ref.header);
233 const char* buff = ref.payload;
234 size_t payloadSize = dh->payloadSize;
235 constexpr char FlatHeaderAnnot[] = "$HEADER$";
236 constexpr size_t Offset = sizeof(int) + sizeof(FlatHeaderAnnot);
237 int headerSize = 0;
238 if (payloadSize >= Offset &&
239 !std::strncmp(buff + payloadSize - sizeof(FlatHeaderAnnot), FlatHeaderAnnot, sizeof(FlatHeaderAnnot))) {
240 headerSize = *reinterpret_cast<const int*>(buff + payloadSize - Offset);
241 }
242 return {buff, payloadSize - headerSize};
243}
244
245} // namespace o2::framework
std::ostringstream debug
int32_t i
configStandalone Light OpenGL display display_light Apply smoothing to points Enable Z buffer Highlight links Highlight seeds Highlight sector tracks Highlight final tracks Show individual Show related Highlight grid Show only individual collision Color clusters belonging to track objects Set line Enable drawing TPC data Enable drawing TOF data Enable drawing magnetic field Set field line step count Invert colors configStandalone Heavy OpenGL display display_heavy Show tracks with ITS contribution Show tracks with TOF contribution Enable propagation of loopers Show clusters at nominal x of pad row for early transformed data Split CE tracks when they cross the central Mark clusters Mark adjacent clusters Hide rejected tracks Apply filter on tracks to be displayed Separate increase X h configStandalone Camera window renderer OpenGL display display_camera Orient the camera such that the y axis is always upwards Full Screen MultiSample Anti Downsampling Anti Enable Vertical Sync Target frame or if the backend cannot limit to the refresh Use OpenGL indirect draws to reduce number of draw calls Display h configStandalone Vulkan display display_vulkan Have uniform buffers in host accessible device memory h configStandalone Internal display display_internal Number of updates the Qt GUI may send ahead of the last one the display has before it waits for the display Opacity of the console background Number of console lines the history is shortened to this when it reaches twice the size Time in seconds an info message fades out after setInfoTime h OpenGL display display Font size Do not use Freetype for font Renderer for event Frontend to use for event ROOT macros used as track filter Console script file
Timeframe settings Average distance in cm of events merged into Randomize distance around average distance of merged events Simulate correct bunch interactions instead of placing only the average number of events A value[n] sets TFlen for[n] collisions in Number of bunches per trainsort Number of bunch trains Instantaneous interaction rate Do not simulate border Total number of collisions to be placed in the interior of all time Do not select random but walk over array of events in stride steps h Event generator settings h configStandalone Use CPU for GPU Force usage of the specified GPU device no CPU fallback Continue processing after an error s
uint32_t res
Definition RawData.h:0
Class for time synchronization of RawReader instances.
GLuint64EXT * result
Definition glcorearb.h:5662
GLuint const GLchar * name
Definition glcorearb.h:781
GLuint object
Definition glcorearb.h:4041
GLint ref
Definition glcorearb.h:291
GLuint id
Definition glcorearb.h:650
Defining ITS Vertex explicitly as messageable.
Definition Cartesian.h:288
RuntimeErrorRef runtime_error_f(const char *,...)
static std::map< std::string, std::string > extractCCDBHeaders(DataRef const &ref)
static void * decodeCCDB(DataRef const &ref, std::type_info const &info)
static std::span< const char > getCCDBPayloadBlob(DataRef const &ref)
LOG(info)<< "Compressed in "<< sw.CpuTime()<< " s"
std::unique_ptr< TTree > tree((TTree *) flIn.Get(std::string(o2::base::NameConf::CTFTREENAME).c_str()))