18#include <fairlogger/Logger.h>
23#include "rapidjson/document.h"
24#include "rapidjson/prettywriter.h"
25#include "rapidjson/stringbuffer.h"
27using namespace rapidjson;
35 std::ofstream out(fileName);
42 LOGF(info,
"VisualisationEventJSONSerializer <- %s", fileName);
43 if (FILE*
file = fopen(fileName.c_str(),
"r")) {
49 inFile.open(fileName);
51 std::stringstream strStream;
52 strStream << inFile.rdbuf();
54 std::string
str = strStream.str();
61 Document
tree(kObjectType);
62 Document::AllocatorType& allocator =
tree.GetAllocator();
65 tree.AddMember(
"runNumber", rapidjson::Value().SetInt(
event.mRunNumber), allocator);
66 tree.AddMember(
"runType", rapidjson::Value().SetInt(
event.mRunType), allocator);
67 tree.AddMember(
"clMask", rapidjson::Value().SetInt(
event.mClMask), allocator);
68 tree.AddMember(
"trkMask", rapidjson::Value().SetInt(
event.mTrkMask), allocator);
69 tree.AddMember(
"tfCounter", rapidjson::Value().SetInt(
event.mTfCounter), allocator);
70 tree.AddMember(
"firstTForbit", rapidjson::Value().SetInt(
event.mFirstTForbit), allocator);
71 tree.AddMember(
"primaryVertex", rapidjson::Value().SetInt(
event.mPrimaryVertex), allocator);
73 tree.AddMember(
"collisionTime", rapidjson::Value().SetString(collisionTime.c_str(), collisionTime.size()), allocator);
74 tree.AddMember(
"creationTime", rapidjson::Value().SetUint64(
event.mCreationTime), allocator);
75 tree.AddMember(
"version", rapidjson::Value().SetUint64(
event.mEveVersion), allocator);
78 tree.AddMember(
"eveVersion", rapidjson::Value().SetString(
version.c_str(),
version.size()), allocator);
79 std::string workflowParameters = collisionTime +
" t:" +
bits(
event.mTrkMask) +
" c:" +
bits(
event.mClMask);
80 tree.AddMember(
"workflowParameters", rapidjson::Value().SetString(workflowParameters.c_str(), workflowParameters.size()), allocator);
82 tree.AddMember(
"trackCount", rapidjson::Value().SetInt(
event.getTrackCount()), allocator);
84 Value jsonTracks(kArrayType);
85 for (
auto track :
event.getTracksSpan()) {
86 jsonTracks.PushBack(jsonTree(track, allocator), allocator);
88 tree.AddMember(
"mTracks", jsonTracks, allocator);
94 Value jsonClusters(kArrayType);
95 for (
auto cluster :
event.getClustersSpan()) {
96 jsonClusters.PushBack(jsonTree(cluster, allocator,
nullptr), allocator);
98 tree.AddMember(
"mClusters", jsonClusters, allocator);
101 rapidjson::Value caloCount(rapidjson::kNumberType);
102 caloCount.SetInt(
event.getCaloCount());
103 tree.AddMember(
"caloCount", caloCount, allocator);
104 Value jsonCalos(kArrayType);
105 for (
auto calo :
event.getCalorimetersSpan()) {
106 jsonCalos.PushBack(jsonTree(calo, allocator), allocator);
108 tree.AddMember(
"mCalo", jsonCalos, allocator);
111 rapidjson::StringBuffer
buffer;
112 rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(
buffer);
114 std::string json_str = std::string(
buffer.GetString(),
buffer.GetSize());
121 rapidjson::Value& jsonValue =
tree[
key];
122 return jsonValue.GetInt();
129 rapidjson::Value& jsonValue =
tree[
key];
130 return jsonValue.GetUint64();
138 rapidjson::Value& jsonValue =
tree[
key];
139 return jsonValue.GetFloat();
147 rapidjson::Value& jsonValue =
tree[
key];
148 return jsonValue.GetString();
155 event.mTracks.clear();
156 event.mClusters.clear();
159 rapidjson::Document
tree;
169 if (
tree.HasMember(
"creationTime")) {
172 if (
event.mCreationTime == 0) {
177 if (
event.mEveVersion == 0) {
179 event.mEveVersion = (
int)(100 * std::stof(
version));
185 rapidjson::Value& jsonTracks =
tree[
"mTracks"];
186 for (
auto&
v : jsonTracks.GetArray()) {
187 event.mTracks.emplace_back(trackFromJSON(
v));
190 if (
tree.HasMember(
"caloCount")) {
191 rapidjson::Value& caloCount =
tree[
"caloCount"];
192 event.mCalo.reserve(caloCount.GetInt());
193 rapidjson::Value& jsonCalo =
tree[
"mCalo"];
194 for (
auto&
v : jsonCalo.GetArray()) {
195 event.mCalo.emplace_back(caloFromJSON(
v));
201 rapidjson::Value& jsonClusters =
tree[
"mClusters"];
202 for (
auto&
v : jsonClusters.GetArray()) {
203 event.mClusters.emplace_back(clusterFromJSON(
v,
nullptr));
205 event.afterLoading();
208VisualisationCluster VisualisationEventJSONSerializer::clusterFromJSON(rapidjson::Value&
tree,
const VisualisationTrack* track)
213 rapidjson::Value& jsonX =
tree[
"X"];
214 rapidjson::Value& jsonY =
tree[
"Y"];
215 rapidjson::Value& jsonZ =
tree[
"Z"];
217 XYZ[0] = jsonX.GetDouble();
218 XYZ[1] = jsonY.GetDouble();
219 XYZ[2] = jsonZ.GetDouble();
225 rapidjson::Value& jsonTime =
tree[
"time"];
226 rapidjson::Value& jsonBGID =
tree[
"bgid"];
227 time = jsonTime.GetDouble();
231 VisualisationCluster cluster(
XYZ,
time, gid);
235rapidjson::Value VisualisationEventJSONSerializer::jsonTree(
const VisualisationCluster& cluster, MemoryPoolAllocator<>& allocator,
const VisualisationTrack* track)
const
237 rapidjson::Value
tree(rapidjson::kObjectType);
238 rapidjson::Value jsonX(rapidjson::kNumberType);
239 rapidjson::Value jsonY(rapidjson::kNumberType);
240 rapidjson::Value jsonZ(rapidjson::kNumberType);
241 jsonX.SetDouble(cluster.mCoordinates[0]);
242 jsonY.SetDouble(cluster.mCoordinates[1]);
243 jsonZ.SetDouble(cluster.mCoordinates[2]);
244 tree.AddMember(
"X", jsonX, allocator);
245 tree.AddMember(
"Y", jsonY, allocator);
246 tree.AddMember(
"Z", jsonZ, allocator);
247 if (track ==
nullptr) {
248 unsigned bgid =
serialize(cluster.mBGID);
249 tree.AddMember(
"bgid", rapidjson::Value().SetUint(bgid), allocator);
250 tree.AddMember(
"time", rapidjson::Value().SetFloat(std::isnan(cluster.mTime) ? 0 : cluster.mTime), allocator);
255VisualisationCalo VisualisationEventJSONSerializer::caloFromJSON(rapidjson::Value&
tree)
257 VisualisationCalo calo;
258 calo.mTime =
tree[
"time"].GetFloat();
259 calo.mEnergy =
tree[
"energy"].GetFloat();
260 calo.mEta =
tree[
"eta"].GetFloat();
261 calo.mPhi =
tree[
"phi"].GetFloat();
262 if (
tree.HasMember(
"bgid")) {
263 unsigned bgid =
tree[
"bgid"].GetUint();
269 calo.mPID =
tree[
"PID"].GetInt();
273rapidjson::Value VisualisationEventJSONSerializer::jsonTree(
const VisualisationCalo& calo, rapidjson::MemoryPoolAllocator<>& allocator)
const
275 rapidjson::Value
tree(rapidjson::kObjectType);
277 tree.AddMember(
"time", rapidjson::Value().SetFloat(std::isnan(calo.mTime) ? 0 : calo.mTime), allocator);
278 tree.AddMember(
"energy", rapidjson::Value().SetFloat(calo.mEnergy), allocator);
279 tree.AddMember(
"eta", rapidjson::Value().SetFloat(std::isnan(calo.mEta) ? 0 : calo.mEta), allocator);
280 tree.AddMember(
"phi", rapidjson::Value().SetFloat(std::isnan(calo.mPhi) ? 0 : calo.mPhi), allocator);
282 tree.AddMember(
"bgid", rapidjson::Value().SetUint(bgid), allocator);
283 tree.AddMember(
"source", rapidjson::Value().SetUint(calo.mBGID.getSource()), allocator);
284 tree.AddMember(
"PID", rapidjson::Value().SetInt(calo.mPID), allocator);
288VisualisationTrack VisualisationEventJSONSerializer::trackFromJSON(rapidjson::Value&
tree)
290 VisualisationTrack track;
291 track.mClusters.clear();
292 rapidjson::Value& jsonStartingXYZ =
tree[
"jsonStartingXYZ"];
293 rapidjson::Value& jsonPolyX =
tree[
"mPolyX"];
294 rapidjson::Value& jsonPolyY =
tree[
"mPolyY"];
295 rapidjson::Value& jsonPolyZ =
tree[
"mPolyZ"];
303 track.mTime =
tree[
"time"].GetFloat();
305 if (
tree.HasMember(
"bgid")) {
309 if (gid !=
"track") {
313 track.mPolyX.reserve(
count.GetInt());
314 track.mPolyY.reserve(
count.GetInt());
315 track.mPolyZ.reserve(
count.GetInt());
316 auto startingXYZ = jsonStartingXYZ.GetArray();
317 track.mStartCoordinates[0] = startingXYZ[0].GetFloat();
318 track.mStartCoordinates[1] = startingXYZ[1].GetFloat();
319 track.mStartCoordinates[2] = startingXYZ[2].GetFloat();
320 for (
auto&
v : jsonPolyX.GetArray()) {
321 track.mPolyX.push_back(
v.GetDouble());
323 for (
auto&
v : jsonPolyY.GetArray()) {
324 track.mPolyY.push_back(
v.GetDouble());
326 for (
auto&
v : jsonPolyZ.GetArray()) {
327 track.mPolyZ.push_back(
v.GetDouble());
329 if (
tree.HasMember(
"mClusters")) {
330 rapidjson::Value& jsonClusters =
tree[
"mClusters"];
331 auto jsonArray = jsonClusters.GetArray();
332 track.mClusters.reserve(jsonArray.Size());
333 for (
auto&
v : jsonClusters.GetArray()) {
334 track.mClusters.emplace_back(clusterFromJSON(
v, &track));
340rapidjson::Value VisualisationEventJSONSerializer::jsonTree(
const VisualisationTrack& track, rapidjson::Document::AllocatorType& allocator)
const
342 rapidjson::Value
tree(rapidjson::kObjectType);
343 rapidjson::Value jsonPolyX(rapidjson::kArrayType);
344 rapidjson::Value jsonPolyY(rapidjson::kArrayType);
345 rapidjson::Value jsonPolyZ(rapidjson::kArrayType);
346 rapidjson::Value jsonStartCoordinates(rapidjson::kArrayType);
348 tree.AddMember(
"count", rapidjson::Value().SetInt(track.getPointCount()), allocator);
350 rapidjson::Value gid;
351 std::string stringGID = track.mBGID.asString();
352 gid.SetString(stringGID.c_str(), stringGID.size(), allocator);
353 tree.AddMember(
"gid", gid, allocator);
355 tree.AddMember(
"bgid", rapidjson::Value().SetUint(bgid), allocator);
357 tree.AddMember(
"time", rapidjson::Value().SetFloat(std::isnan(track.mTime) ? 0 : track.mTime), allocator);
358 tree.AddMember(
"charge", rapidjson::Value().SetInt(track.mCharge), allocator);
359 tree.AddMember(
"theta", rapidjson::Value().SetFloat(std::isnan(track.mTheta) ? 0 : track.mTheta), allocator);
360 tree.AddMember(
"phi", rapidjson::Value().SetFloat(std::isnan(track.mPhi) ? 0 : track.mPhi), allocator);
361 tree.AddMember(
"eta", rapidjson::Value().SetFloat(std::isnan(track.mEta) ? 0 : track.mEta), allocator);
362 tree.AddMember(
"PID", rapidjson::Value().SetInt(track.mPID), allocator);
364 jsonStartCoordinates.PushBack((
float)track.mStartCoordinates[0], allocator);
365 jsonStartCoordinates.PushBack((
float)track.mStartCoordinates[1], allocator);
366 jsonStartCoordinates.PushBack((
float)track.mStartCoordinates[2], allocator);
367 tree.AddMember(
"jsonStartingXYZ", jsonStartCoordinates, allocator);
369 for (
size_t i = 0;
i < track.getPointCount();
i++) {
370 jsonPolyX.PushBack((
float)track.mPolyX[
i], allocator);
371 jsonPolyY.PushBack((
float)track.mPolyY[
i], allocator);
372 jsonPolyZ.PushBack((
float)track.mPolyZ[
i], allocator);
374 tree.AddMember(
"mPolyX", jsonPolyX, allocator);
375 tree.AddMember(
"mPolyY", jsonPolyY, allocator);
376 tree.AddMember(
"mPolyZ", jsonPolyZ, allocator);
378 rapidjson::Value jsonClusters(rapidjson::kArrayType);
380 for (
auto cluster : track.getClustersSpan()) {
381 jsonClusters.PushBack(jsonTree(cluster, allocator, &track), allocator);
383 tree.AddMember(
"mClusters", jsonClusters, allocator);
o2::monitoring::tags::Value Value
Global index for barrel track: provides provenance (detectors combination), index in respective array...
void toFile(const VisualisationEvent &event, std::string fileName) override
static int getIntOrDefault(rapidjson::Value &tree, const char *key, int defaultValue=0)
static std::string getStringOrDefault(rapidjson::Value &tree, const char *key, const char *defaultValue="")
static uint64_t getUIntOrDefault(rapidjson::Value &tree, const char *key, uint64_t defaultValue=0)
static float getFloatOrDefault(rapidjson::Value &tree, const char *key, float defaultValue=0.0f)
bool fromFile(VisualisationEvent &event, std::string fileName) override
static unsigned serialize(o2::dataformats::GlobalTrackID gidValue)
static std::string DateTime(time_t time)
static o2::dataformats::GlobalTrackID deserialize(unsigned seralizedValue)
static o2::dataformats::GlobalTrackID gidFromString(const std::string &gid)
static time_t parseDateTime(const char *datetimeString)
GLsizei GLsizei GLchar * source
GLenum GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const void * bits
std::string to_string(gsl::span< T, Size > span)
std::unique_ptr< TTree > tree((TTree *) flIn.Get(std::string(o2::base::NameConf::CTFTREENAME).c_str()))