18#include <fairlogger/Logger.h>
23#include "rapidjson/document.h"
24#include "rapidjson/prettywriter.h"
25#include "rapidjson/stringbuffer.h"
27using namespace rapidjson;
34 std::string fileName =
location.fileName();
36 std::ofstream out(fileName);
43 LOGF(info,
"VisualisationEventJSONSerializer <- %s", fileName);
44 if (FILE*
file = fopen(fileName.c_str(),
"r")) {
50 inFile.open(fileName);
52 std::stringstream strStream;
53 strStream << inFile.rdbuf();
55 std::string
str = strStream.str();
62 Document
tree(kObjectType);
63 Document::AllocatorType& allocator =
tree.GetAllocator();
66 tree.AddMember(
"runNumber", rapidjson::Value().SetInt(
event.mRunNumber), allocator);
67 tree.AddMember(
"runType", rapidjson::Value().SetInt(
event.mRunType), allocator);
68 tree.AddMember(
"clMask", rapidjson::Value().SetInt(
event.mClMask), allocator);
69 tree.AddMember(
"trkMask", rapidjson::Value().SetInt(
event.mTrkMask), allocator);
70 tree.AddMember(
"tfCounter", rapidjson::Value().SetInt(
event.mTfCounter), allocator);
71 tree.AddMember(
"firstTForbit", rapidjson::Value().SetInt(
event.mFirstTForbit), allocator);
72 tree.AddMember(
"primaryVertex", rapidjson::Value().SetInt(
event.mPrimaryVertex), allocator);
74 tree.AddMember(
"collisionTime", rapidjson::Value().SetString(collisionTime.c_str(), collisionTime.size()), allocator);
75 tree.AddMember(
"creationTime", rapidjson::Value().SetUint64(
event.mCreationTime), allocator);
76 tree.AddMember(
"version", rapidjson::Value().SetUint64(
event.mEveVersion), allocator);
79 tree.AddMember(
"eveVersion", rapidjson::Value().SetString(
version.c_str(),
version.size()), allocator);
80 std::string workflowParameters = collisionTime +
" t:" +
bits(
event.mTrkMask) +
" c:" +
bits(
event.mClMask);
81 tree.AddMember(
"workflowParameters", rapidjson::Value().SetString(workflowParameters.c_str(), workflowParameters.size()), allocator);
83 tree.AddMember(
"trackCount", rapidjson::Value().SetInt(
event.getTrackCount()), allocator);
85 Value jsonTracks(kArrayType);
86 for (
auto track :
event.getTracksSpan()) {
87 jsonTracks.PushBack(jsonTree(track, allocator), allocator);
89 tree.AddMember(
"mTracks", jsonTracks, allocator);
95 Value jsonClusters(kArrayType);
96 for (
auto cluster :
event.getClustersSpan()) {
97 jsonClusters.PushBack(jsonTree(cluster, allocator,
nullptr), allocator);
99 tree.AddMember(
"mClusters", jsonClusters, allocator);
102 rapidjson::Value caloCount(rapidjson::kNumberType);
103 caloCount.SetInt(
event.getCaloCount());
104 tree.AddMember(
"caloCount", caloCount, allocator);
105 Value jsonCalos(kArrayType);
106 for (
auto calo :
event.getCalorimetersSpan()) {
107 jsonCalos.PushBack(jsonTree(calo, allocator), allocator);
109 tree.AddMember(
"mCalo", jsonCalos, allocator);
112 rapidjson::StringBuffer
buffer;
113 rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(
buffer);
115 std::string json_str = std::string(
buffer.GetString(),
buffer.GetSize());
122 rapidjson::Value& jsonValue =
tree[
key];
123 return jsonValue.GetInt();
130 rapidjson::Value& jsonValue =
tree[
key];
131 return jsonValue.GetUint64();
139 rapidjson::Value& jsonValue =
tree[
key];
140 return jsonValue.GetFloat();
148 rapidjson::Value& jsonValue =
tree[
key];
149 return jsonValue.GetString();
156 event.mTracks.clear();
157 event.mClusters.clear();
160 rapidjson::Document
tree;
170 if (
tree.HasMember(
"creationTime")) {
173 if (
event.mCreationTime == 0) {
178 if (
event.mEveVersion == 0) {
180 event.mEveVersion = (
int)(100 * std::stof(
version));
186 rapidjson::Value& jsonTracks =
tree[
"mTracks"];
187 for (
auto&
v : jsonTracks.GetArray()) {
188 event.mTracks.emplace_back(trackFromJSON(
v));
191 if (
tree.HasMember(
"caloCount")) {
192 rapidjson::Value& caloCount =
tree[
"caloCount"];
193 event.mCalo.reserve(caloCount.GetInt());
194 rapidjson::Value& jsonCalo =
tree[
"mCalo"];
195 for (
auto&
v : jsonCalo.GetArray()) {
196 event.mCalo.emplace_back(caloFromJSON(
v));
202 rapidjson::Value& jsonClusters =
tree[
"mClusters"];
203 for (
auto&
v : jsonClusters.GetArray()) {
204 event.mClusters.emplace_back(clusterFromJSON(
v,
nullptr));
206 event.afterLoading();
209VisualisationCluster VisualisationEventJSONSerializer::clusterFromJSON(rapidjson::Value&
tree,
const VisualisationTrack* track)
214 rapidjson::Value& jsonX =
tree[
"X"];
215 rapidjson::Value& jsonY =
tree[
"Y"];
216 rapidjson::Value& jsonZ =
tree[
"Z"];
218 XYZ[0] = jsonX.GetDouble();
219 XYZ[1] = jsonY.GetDouble();
220 XYZ[2] = jsonZ.GetDouble();
226 rapidjson::Value& jsonTime =
tree[
"time"];
227 rapidjson::Value& jsonBGID =
tree[
"bgid"];
228 time = jsonTime.GetDouble();
232 VisualisationCluster cluster(
XYZ,
time, gid);
236rapidjson::Value VisualisationEventJSONSerializer::jsonTree(
const VisualisationCluster& cluster, MemoryPoolAllocator<>& allocator,
const VisualisationTrack* track)
const
238 rapidjson::Value
tree(rapidjson::kObjectType);
239 rapidjson::Value jsonX(rapidjson::kNumberType);
240 rapidjson::Value jsonY(rapidjson::kNumberType);
241 rapidjson::Value jsonZ(rapidjson::kNumberType);
242 jsonX.SetDouble(cluster.mCoordinates[0]);
243 jsonY.SetDouble(cluster.mCoordinates[1]);
244 jsonZ.SetDouble(cluster.mCoordinates[2]);
245 tree.AddMember(
"X", jsonX, allocator);
246 tree.AddMember(
"Y", jsonY, allocator);
247 tree.AddMember(
"Z", jsonZ, allocator);
248 if (track ==
nullptr) {
249 unsigned bgid =
serialize(cluster.mBGID);
250 tree.AddMember(
"bgid", rapidjson::Value().SetUint(bgid), allocator);
251 tree.AddMember(
"time", rapidjson::Value().SetFloat(std::isnan(cluster.mTime) ? 0 : cluster.mTime), allocator);
256VisualisationCalo VisualisationEventJSONSerializer::caloFromJSON(rapidjson::Value&
tree)
258 VisualisationCalo calo;
259 calo.mTime =
tree[
"time"].GetFloat();
260 calo.mEnergy =
tree[
"energy"].GetFloat();
261 calo.mEta =
tree[
"eta"].GetFloat();
262 calo.mPhi =
tree[
"phi"].GetFloat();
263 if (
tree.HasMember(
"bgid")) {
264 unsigned bgid =
tree[
"bgid"].GetUint();
270 calo.mPID =
tree[
"PID"].GetInt();
274rapidjson::Value VisualisationEventJSONSerializer::jsonTree(
const VisualisationCalo& calo, rapidjson::MemoryPoolAllocator<>& allocator)
const
276 rapidjson::Value
tree(rapidjson::kObjectType);
278 tree.AddMember(
"time", rapidjson::Value().SetFloat(std::isnan(calo.mTime) ? 0 : calo.mTime), allocator);
279 tree.AddMember(
"energy", rapidjson::Value().SetFloat(calo.mEnergy), allocator);
280 tree.AddMember(
"eta", rapidjson::Value().SetFloat(std::isnan(calo.mEta) ? 0 : calo.mEta), allocator);
281 tree.AddMember(
"phi", rapidjson::Value().SetFloat(std::isnan(calo.mPhi) ? 0 : calo.mPhi), allocator);
283 tree.AddMember(
"bgid", rapidjson::Value().SetUint(bgid), allocator);
284 tree.AddMember(
"source", rapidjson::Value().SetUint(calo.mBGID.getSource()), allocator);
285 tree.AddMember(
"PID", rapidjson::Value().SetInt(calo.mPID), allocator);
289VisualisationTrack VisualisationEventJSONSerializer::trackFromJSON(rapidjson::Value&
tree)
291 VisualisationTrack track;
292 track.mClusters.clear();
293 rapidjson::Value& jsonStartingXYZ =
tree[
"jsonStartingXYZ"];
294 rapidjson::Value& jsonPolyX =
tree[
"mPolyX"];
295 rapidjson::Value& jsonPolyY =
tree[
"mPolyY"];
296 rapidjson::Value& jsonPolyZ =
tree[
"mPolyZ"];
304 track.mTime =
tree[
"time"].GetFloat();
306 if (
tree.HasMember(
"bgid")) {
310 if (gid !=
"track") {
314 track.mPolyX.reserve(
count.GetInt());
315 track.mPolyY.reserve(
count.GetInt());
316 track.mPolyZ.reserve(
count.GetInt());
317 auto startingXYZ = jsonStartingXYZ.GetArray();
318 track.mStartCoordinates[0] = startingXYZ[0].GetFloat();
319 track.mStartCoordinates[1] = startingXYZ[1].GetFloat();
320 track.mStartCoordinates[2] = startingXYZ[2].GetFloat();
321 for (
auto&
v : jsonPolyX.GetArray()) {
322 track.mPolyX.push_back(
v.GetDouble());
324 for (
auto&
v : jsonPolyY.GetArray()) {
325 track.mPolyY.push_back(
v.GetDouble());
327 for (
auto&
v : jsonPolyZ.GetArray()) {
328 track.mPolyZ.push_back(
v.GetDouble());
330 if (
tree.HasMember(
"mClusters")) {
331 rapidjson::Value& jsonClusters =
tree[
"mClusters"];
332 auto jsonArray = jsonClusters.GetArray();
333 track.mClusters.reserve(jsonArray.Size());
334 for (
auto&
v : jsonClusters.GetArray()) {
335 track.mClusters.emplace_back(clusterFromJSON(
v, &track));
341rapidjson::Value VisualisationEventJSONSerializer::jsonTree(
const VisualisationTrack& track, rapidjson::Document::AllocatorType& allocator)
const
343 rapidjson::Value
tree(rapidjson::kObjectType);
344 rapidjson::Value jsonPolyX(rapidjson::kArrayType);
345 rapidjson::Value jsonPolyY(rapidjson::kArrayType);
346 rapidjson::Value jsonPolyZ(rapidjson::kArrayType);
347 rapidjson::Value jsonStartCoordinates(rapidjson::kArrayType);
349 tree.AddMember(
"count", rapidjson::Value().SetInt(track.getPointCount()), allocator);
351 rapidjson::Value gid;
352 std::string stringGID = track.mBGID.asString();
353 gid.SetString(stringGID.c_str(), stringGID.size(), allocator);
354 tree.AddMember(
"gid", gid, allocator);
356 tree.AddMember(
"bgid", rapidjson::Value().SetUint(bgid), allocator);
358 tree.AddMember(
"time", rapidjson::Value().SetFloat(std::isnan(track.mTime) ? 0 : track.mTime), allocator);
359 tree.AddMember(
"charge", rapidjson::Value().SetInt(track.mCharge), allocator);
360 tree.AddMember(
"theta", rapidjson::Value().SetFloat(std::isnan(track.mTheta) ? 0 : track.mTheta), allocator);
361 tree.AddMember(
"phi", rapidjson::Value().SetFloat(std::isnan(track.mPhi) ? 0 : track.mPhi), allocator);
362 tree.AddMember(
"eta", rapidjson::Value().SetFloat(std::isnan(track.mEta) ? 0 : track.mEta), allocator);
363 tree.AddMember(
"PID", rapidjson::Value().SetInt(track.mPID), allocator);
365 jsonStartCoordinates.PushBack((
float)track.mStartCoordinates[0], allocator);
366 jsonStartCoordinates.PushBack((
float)track.mStartCoordinates[1], allocator);
367 jsonStartCoordinates.PushBack((
float)track.mStartCoordinates[2], allocator);
368 tree.AddMember(
"jsonStartingXYZ", jsonStartCoordinates, allocator);
370 for (
size_t i = 0;
i < track.getPointCount();
i++) {
371 jsonPolyX.PushBack((
float)track.mPolyX[
i], allocator);
372 jsonPolyY.PushBack((
float)track.mPolyY[
i], allocator);
373 jsonPolyZ.PushBack((
float)track.mPolyZ[
i], allocator);
375 tree.AddMember(
"mPolyX", jsonPolyX, allocator);
376 tree.AddMember(
"mPolyY", jsonPolyY, allocator);
377 tree.AddMember(
"mPolyZ", jsonPolyZ, allocator);
379 rapidjson::Value jsonClusters(rapidjson::kArrayType);
381 for (
auto cluster : track.getClustersSpan()) {
382 jsonClusters.PushBack(jsonTree(cluster, allocator, &track), allocator);
384 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, Location &location) 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()))