-
Notifications
You must be signed in to change notification settings - Fork 496
Expand file tree
/
Copy pathDataRefUtils.cxx
More file actions
151 lines (141 loc) · 5.76 KB
/
DataRefUtils.cxx
File metadata and controls
151 lines (141 loc) · 5.76 KB
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
// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
// All rights not expressly granted are reserved.
//
// This software is distributed under the terms of the GNU General Public
// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
//
// In applying this license CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.
#include <typeinfo>
#include <cstring>
#include "Framework/DataRefUtils.h"
#include "Framework/RuntimeError.h"
#include "Framework/Logger.h"
#include <TMemFile.h>
#include <TTree.h>
#include <TH1.h>
#include <TError.h>
#include <algorithm>
namespace o2::framework
{
namespace
{
void* extractFromTFile(TFile& file, TClass const* cl, const char* what)
{
if (!cl) {
return nullptr;
}
auto object = file.GetObjectChecked(what, cl);
if (!object) {
// it could be that object was stored with previous convention
// where the classname was taken as key
std::string objectName(cl->GetName());
objectName.erase(std::find_if(objectName.rbegin(), objectName.rend(), [](unsigned char ch) {
return !std::isspace(ch);
}).base(),
objectName.end());
objectName.erase(objectName.begin(), std::find_if(objectName.begin(), objectName.end(), [](unsigned char ch) {
return !std::isspace(ch);
}));
object = file.GetObjectChecked(objectName.c_str(), cl);
LOG(warn) << "Did not find object under expected name " << what;
if (!object) {
return nullptr;
}
LOG(warn) << "Found object under deprecated name " << cl->GetName();
}
auto result = object;
// We need to handle some specific cases as ROOT ties them deeply
// to the file they are contained in
if (cl->InheritsFrom("TObject")) {
// make a clone
// detach from the file
auto tree = dynamic_cast<TTree*>((TObject*)object);
if (tree) {
tree->LoadBaskets(0x1L << 32); // make tree memory based
tree->SetDirectory(nullptr);
result = tree;
} else {
auto h = dynamic_cast<TH1*>((TObject*)object);
if (h) {
h->SetDirectory(nullptr);
result = h;
}
}
}
return result;
}
} // namespace
// Adapted from CcdbApi private method interpretAsTMemFileAndExtract
// If the former is moved to public, throws on error and could be changed to
// not require a mutex we could use it.
void* DataRefUtils::decodeCCDB(DataRef const& ref, std::type_info const& tinfo)
{
void* result = nullptr;
Int_t previousErrorLevel = gErrorIgnoreLevel;
gErrorIgnoreLevel = kFatal;
auto* dh = o2::header::get<o2::header::DataHeader*>(ref.header);
const char* buff = const_cast<char*>(ref.payload);
size_t flSize = dh->payloadSize;
// does it have a flattened headers map attached in the end?
constexpr char FlatHeaderAnnot[] = "$HEADER$";
constexpr int Offset = sizeof(int) + sizeof(FlatHeaderAnnot);
int headerSize = 0;
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});
if (dh->payloadSize >= Offset &&
!std::strncmp(buff + dh->payloadSize - sizeof(FlatHeaderAnnot), FlatHeaderAnnot, sizeof(FlatHeaderAnnot))) {
headerSize = *reinterpret_cast<const int*>(buff + dh->payloadSize - Offset);
}
if (headerSize < 0) {
LOGP(fatal, "Anomalous flattened header size {} extracted for CCDB object {}/{}", headerSize, dh->dataOrigin.as<std::string>(), dh->dataDescription.as<std::string>());
}
TMemFile memFile("name", const_cast<char*>(ref.payload), dh->payloadSize - headerSize, "READ");
gErrorIgnoreLevel = previousErrorLevel;
if (memFile.IsZombie()) {
return nullptr;
}
TClass* tcl = TClass::GetClass(tinfo);
result = extractFromTFile(memFile, tcl, "ccdb_object");
if (!result) {
throw runtime_error_f("Couldn't retrieve object corresponding to %s from TFile", tcl->GetName());
}
memFile.Close();
return result;
}
std::map<std::string, std::string> DataRefUtils::extractCCDBHeaders(DataRef const& ref)
{
auto* dh = o2::header::get<o2::header::DataHeader*>(ref.header);
const char* buff = const_cast<char*>(ref.payload);
// does it have a flattened headers map attached in the end?
constexpr char FlatHeaderAnnot[] = "$HEADER$";
constexpr int Offset = sizeof(int) + sizeof(FlatHeaderAnnot);
int headerSize = 0, ss0 = 0;
std::map<std::string, std::string> res;
if (dh->payloadSize >= Offset && !std::strncmp(buff + dh->payloadSize - sizeof(FlatHeaderAnnot), FlatHeaderAnnot, sizeof(FlatHeaderAnnot))) {
headerSize = *reinterpret_cast<const int*>(buff + dh->payloadSize - Offset);
} else { // header was not added
LOGP(warn, "CCDB headers were not added to condition object blob, returning dummy header map");
return res;
}
if (headerSize < 0) {
LOGP(fatal, "Anomalous flattened header size {} extracted for CCDB object {}/{}", headerSize, dh->dataOrigin.as<std::string>(), dh->dataDescription.as<std::string>());
}
buff += dh->payloadSize - headerSize; // jump to the start of flattened header
headerSize -= Offset;
const char* str0 = &buff[ss0++];
while (ss0 < headerSize) {
if (buff[ss0++] == 0) {
if (!str0) {
str0 = &buff[ss0]; // new key string is found
} else {
res.emplace(std::string(str0), std::string(&buff[ss0])); // new value string found, add key value to the map
LOGP(debug, "Header{} {}:{}", res.size(), std::string(str0), std::string(&buff[ss0]));
str0 = nullptr;
}
}
}
return res;
}
} // namespace o2::framework