Skip to content

Commit

Permalink
o2-eve, o2-eve-export-workflow, o2-eve-convert: serialisation to root… (
Browse files Browse the repository at this point in the history
AliceO2Group#9497)

* o2-eve, o2-eve-export-workflow, o2-eve-convert: serialisation to root files, conversion tool

* clang

* formatting

* Update VisualisationEventROOTSerializer.cxx

* Please consider the following formatting changes

Co-authored-by: Julian Myrcha <[email protected]>
Co-authored-by: ALICE Action Bot <[email protected]>
  • Loading branch information
3 people authored Jul 27, 2022
1 parent 8bd9492 commit aeb566d
Show file tree
Hide file tree
Showing 27 changed files with 574 additions and 96 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#define O2EVE_DIRECTORYLOADER_H

#include <string>
#include <vector>
#include <deque>

namespace o2
Expand All @@ -27,13 +28,13 @@ namespace event_visualisation
class DirectoryLoader
{
private:
static int getNumberOfFiles(std::string& path, std::string& ext);
static std::string getLatestFile(std::string& path, std::string& ext);
static int getNumberOfFiles(std::string& path, std::vector<std::string>& ext);
static std::string getLatestFile(std::string& path, std::vector<std::string>& ext);

public:
static std::deque<std::string> load(const std::string& path, const std::string& marker, const std::string& ext);
static std::deque<std::string> load(const std::string& path, const std::string& marker, const std::vector<std::string>& ext);
static void reduceNumberOfFiles(const std::string& path, const std::deque<std::string>& files, std::size_t filesInFolder);
static void removeOldestFiles(std::string& path, std::string ext, int remaining);
static void removeOldestFiles(std::string& path, std::vector<std::string>& ext, int remaining);
};

} // namespace event_visualisation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include <string>
#include <deque>
#include <vector>

namespace o2
{
Expand All @@ -33,10 +34,11 @@ class FileWatcher
std::string prevItem(const std::string& item) const;
std::string mDataFolder; ///< folder being observed
std::string mCurrentFile; ///< "current" file name
std::vector<std::string> mExt; ///< extensions of files to be observed
bool currentFileExist();

public:
FileWatcher(const std::string& path);
FileWatcher(const std::string& path, std::vector<std::string> ext);
void changeFolder(const std::string& path); ///< switch to observe other folder
void saveCurrentFileToFolder(const std::string& destinationFolder); ///< copies
int getSize() const; ///< include guards (so >=2 )
Expand Down
2 changes: 1 addition & 1 deletion EventVisualisation/Base/src/DataSourceOnline.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ std::vector<std::pair<VisualisationEvent, EVisualisationGroup>> DataSourceOnline
return res;
}

DataSourceOnline::DataSourceOnline(const std::string path) : mFileWatcher(path)
DataSourceOnline::DataSourceOnline(const std::string path) : mFileWatcher(path, {".json", ".root"})
{
}

Expand Down
14 changes: 7 additions & 7 deletions EventVisualisation/Base/src/DirectoryLoader.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@
using namespace std;
using namespace o2::event_visualisation;

deque<string> DirectoryLoader::load(const std::string& path, const std::string& marker, const std::string& ext)
deque<string> DirectoryLoader::load(const std::string& path, const std::string& marker, const std::vector<std::string>& ext)
{
deque<string> result;
for (const auto& entry : std::filesystem::directory_iterator(path)) {
if (entry.path().extension() == ext) {
if (std::find(ext.begin(), ext.end(), entry.path().extension()) != ext.end()) {
result.push_back(entry.path().filename());
}
}
Expand Down Expand Up @@ -58,22 +58,22 @@ std::time_t to_time_t(TP tp)
return system_clock::to_time_t(sctp);
}

int DirectoryLoader::getNumberOfFiles(std::string& path, std::string& ext)
int DirectoryLoader::getNumberOfFiles(std::string& path, std::vector<std::string>& ext)
{
int res = 0;
for (const auto& entry : std::filesystem::directory_iterator(path)) {
if (entry.path().extension() == ext) {
if (std::find(ext.begin(), ext.end(), entry.path().extension()) != ext.end()) {
res++;
}
}
return res;
}
std::string DirectoryLoader::getLatestFile(std::string& path, std::string& ext)
std::string DirectoryLoader::getLatestFile(std::string& path, std::vector<std::string>& ext)
{
std::string oldest_file_name = "";
std::time_t oldest_file_time = LONG_MAX;
for (const auto& entry : std::filesystem::directory_iterator(path)) {
if (entry.path().extension() == ext) {
if (std::find(ext.begin(), ext.end(), entry.path().extension()) != ext.end()) {
auto file_time = entry.last_write_time();
std::time_t tt = to_time_t(file_time);
if (tt < oldest_file_time) {
Expand All @@ -85,7 +85,7 @@ std::string DirectoryLoader::getLatestFile(std::string& path, std::string& ext)
return oldest_file_name;
}

void DirectoryLoader::removeOldestFiles(std::string& path, std::string ext, int remaining)
void DirectoryLoader::removeOldestFiles(std::string& path, std::vector<std::string>& ext, int remaining)
{
while (getNumberOfFiles(path, ext) > remaining) {
LOG(info) << "removing oldest file in folder: " << path << " : " << getLatestFile(path, ext);
Expand Down
5 changes: 3 additions & 2 deletions EventVisualisation/Base/src/FileWatcher.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,15 @@ using namespace o2::event_visualisation;
const char* FileWatcher::mLowGuard = " 0"; /// start guard
const char* FileWatcher::mEndGuard = "~0"; /// stop guard

FileWatcher::FileWatcher(const string& path)
FileWatcher::FileWatcher(const string& path, std::vector<std::string> ext)
{
//LOG(info) << "FileWatcher::FileWatcher(" << path << ")";
this->mDataFolder = path;
this->mCurrentFile = mEndGuard;
this->mFiles.clear();
this->mFiles.push_front(mLowGuard);
this->mFiles.push_back(mEndGuard);
this->mExt = ext;
//LOG(info) << "FileWatcher" << this->getSize();
}

Expand Down Expand Up @@ -131,7 +132,7 @@ bool FileWatcher::refresh()
LOG(info) << "previous:" << previous;
LOG(info) << "currentFile:" << this->mCurrentFile;

this->mFiles = DirectoryLoader::load(this->mDataFolder, "_", ".json"); // already sorted according part staring with marker
this->mFiles = DirectoryLoader::load(this->mDataFolder, "_", this->mExt); // already sorted according part staring with marker
if (this->mCurrentFile != mEndGuard) {
if (this->mFiles.empty()) {
this->mCurrentFile = mEndGuard; // list empty - stick to last element
Expand Down
18 changes: 17 additions & 1 deletion EventVisualisation/DataConverter/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,28 @@
# or submit itself to any jurisdiction.

o2_add_library(EventVisualisationDataConverter
SOURCES src/VisualisationEvent.cxx
SOURCES src/VisualisationEvent.cxx
src/VisualisationTrack.cxx
src/VisualisationCluster.cxx
src/VisualisationCalo.cxx
src/VisualisationEventSerializer.cxx
src/VisualisationEventJSONSerializer.cxx
src/VisualisationEventROOTSerializer.cxx
PUBLIC_LINK_LIBRARIES RapidJSON::RapidJSON
O2::ReconstructionDataFormats
)

o2_add_executable(eve-convert
SOURCES src/converter.cxx
src/VisualisationEvent.cxx
src/VisualisationEventSerializer.cxx
src/VisualisationEventJSONSerializer.cxx
src/VisualisationEventROOTSerializer.cxx
src/VisualisationTrack.cxx
src/VisualisationCluster.cxx
src/VisualisationCalo.cxx
PUBLIC_LINK_LIBRARIES
O2::EventVisualisationView
RapidJSON::RapidJSON
O2::ReconstructionDataFormats
)
Original file line number Diff line number Diff line change
Expand Up @@ -162,14 +162,17 @@ class VisualisationEvent
mCalo.clear();
}

void afterLoading(); // compute internal fields which are not persisted

const VisualisationCluster& getCluster(int i) const { return mClusters[i]; };
size_t getClusterCount() const { return mClusters.size(); } // Returns number of clusters
void setWorkflowVersion(const std::string& workflowVersion) { this->mWorkflowVersion = workflowVersion; }
void setWorkflowParameters(const std::string& workflowParameters) { this->mWorkflowParameters = workflowParameters; }

std::string getCollisionTime() const { return this->mCollisionTime; }
void setCollisionTime(std::string collisionTime) { this->mCollisionTime = collisionTime; }

void setEveVersion(std::string eveVersion) { this->mEveVersion = eveVersion; }

float getMinTimeOfTracks() const { return this->mMinTimeOfTracks; }
float getMaxTimeOfTracks() const { return this->mMaxTimeOfTracks; } /// maximum time of tracks in the event

Expand Down Expand Up @@ -202,7 +205,7 @@ class VisualisationEvent

float mMinTimeOfTracks; /// minimum time of tracks in the event
float mMaxTimeOfTracks; /// maximum time of tracks in the event
std::string mWorkflowVersion; /// workflow version used to generate this Event
std::string mEveVersion; /// workflow version used to generate this Event
std::string mWorkflowParameters; /// workflow parameters used to generate this Event
int mEventNumber; /// event number in file
double mEnergy; /// energy of the collision
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ namespace event_visualisation
class VisualisationEventJSONSerializer : public VisualisationEventSerializer
{
static int getIntOrDefault(rapidjson::Value& tree, const char* key, int defaultValue = 0);
float getFloatOrDefault(rapidjson::Value& tree, const char* key, float defaultValue = 0.0f);
std::string getStringOrDefault(rapidjson::Value& tree, const char* key, const char* defaultValue = "");

std::string toJson(const VisualisationEvent& event) const;
void fromJson(VisualisationEvent& event, std::string json);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// 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.

///
/// \file VisualisationEventSerializer.h
/// \author Julian Myrcha
///

#ifndef O2EVE_VISUALISATIONEVENTROOTSERIALIZER_H
#define O2EVE_VISUALISATIONEVENTROOTSERIALIZER_H

#include "EventVisualisationDataConverter/VisualisationEventSerializer.h"
#include "EventVisualisationDataConverter/VisualisationTrack.h"
#include <string>
#include <TFile.h>

namespace o2
{
namespace event_visualisation
{

class VisualisationEventROOTSerializer : public VisualisationEventSerializer
{
static void save(const char* name, int value);
static int readInt(TFile& f, const char* name);
static void save(const char* name, const std::string& value);
static std::string readString(TFile& f, const char* name);

public:
bool fromFile(VisualisationEvent& event, std::string fileName) override;
void toFile(const VisualisationEvent& event, std::string fileName) override;
~VisualisationEventROOTSerializer() override = default;
};

} // namespace event_visualisation
} // namespace o2

#endif // O2EVE_VISUALISATIONEVENTROOTSERIALIZER_H
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include "EventVisualisationDataConverter/VisualisationEvent.h"
#include <string>
#include <map>

namespace o2
{
Expand All @@ -26,19 +27,14 @@ namespace event_visualisation

class VisualisationEventSerializer
{
static VisualisationEventSerializer* instance;
static std::map<std::string, VisualisationEventSerializer*> instances;

protected:
VisualisationEventSerializer() = default;
static std::string fileNameIndexed(const std::string fileName, const int index);

public:
static VisualisationEventSerializer* getInstance() { return instance; }
static void setInstance(VisualisationEventSerializer* newInstance)
{ // take ownership
delete instance;
instance = newInstance;
}
static VisualisationEventSerializer* getInstance(std::string ext) { return instances[ext]; }
virtual bool fromFile(VisualisationEvent& event, std::string fileName) = 0;
virtual void toFile(const VisualisationEvent& event, std::string fileName) = 0;
virtual ~VisualisationEventSerializer() = default;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ class VisualisationTrack
void addChild(int childID);
// Add xyz coordinates of the point along the track
void addPolyPoint(float x, float y, float z);
void addPolyPoint(const float p[]);
// Time getter
float getTime() const { return mTime; }
// Charge getter
Expand Down
10 changes: 10 additions & 0 deletions EventVisualisation/DataConverter/src/VisualisationEvent.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -143,5 +143,15 @@ VisualisationEvent::VisualisationEvent()
this->mCollisionTime = ""; // collision time not set
}

void VisualisationEvent::afterLoading()
{
this->mMinTimeOfTracks = std::numeric_limits<float>::max();
this->mMaxTimeOfTracks = std::numeric_limits<float>::min();
for (auto& v : this->mTracks) {
this->mMinTimeOfTracks = std::min(this->mMinTimeOfTracks, v.getTime());
this->mMaxTimeOfTracks = std::max(this->mMaxTimeOfTracks, v.getTime());
}
}

} // namespace event_visualisation
} // namespace o2
Loading

0 comments on commit aeb566d

Please sign in to comment.