Commit 5a7be004 authored by Alexander Ferk's avatar Alexander Ferk
Browse files

added JSONWriter module for saving clipboard data as json array

parent d9f74619
# Define module and return the generated name as MODULE_NAME
CORRYVRECKAN_GLOBAL_MODULE(MODULE_NAME)
# Add source files to library
CORRYVRECKAN_MODULE_SOURCES(${MODULE_NAME}
JSONWriter.cpp
# ADD SOURCE FILES HERE...
)
TARGET_LINK_LIBRARIES(${MODULE_NAME} ROOT::Tree ROOT::RIO)
# Provide standard install target
CORRYVRECKAN_MODULE_INSTALL(${MODULE_NAME})
/**
* @file
* @brief Implementation of module TextWriter
*
* @copyright Copyright (c) 2019-2020 CERN and the Corryvreckan authors.
* This software is distributed under the terms of the MIT License, copied verbatim in the file "LICENSE.md".
* 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 "JSONWriter.h"
#include "core/utils/file.h"
using namespace corryvreckan;
JSONWriter::JSONWriter(Configuration config, std::vector<std::shared_ptr<Detector>> detectors)
: Module(std::move(config), std::move(detectors)) {}
void JSONWriter::initialise() {
for(auto& detector : get_detectors()) {
LOG(DEBUG) << "Initialise for detector " + detector->getName();
}
// Create output file
output_file_name_ =
createOutputFile(corryvreckan::add_file_extension(m_config.get<std::string>("file_name", "data"), "json"), true);
output_file_ = std::make_unique<std::ofstream>(output_file_name_);
*output_file_ << "[" << std::endl;
// Read include and exclude list
if(m_config.has("include") && m_config.has("exclude")) {
throw InvalidValueError(m_config, "exclude", "include and exclude parameter are mutually exclusive");
} else if(m_config.has("include")) {
auto inc_arr = m_config.getArray<std::string>("include");
include_.insert(inc_arr.begin(), inc_arr.end());
} else if(m_config.has("exclude")) {
auto exc_arr = m_config.getArray<std::string>("exclude");
exclude_.insert(exc_arr.begin(), exc_arr.end());
}
// Initialise member variables
m_eventNumber = 0;
}
StatusCode JSONWriter::run(std::shared_ptr<Clipboard> clipboard) {
if(!clipboard->isEventDefined()) {
ModuleError("No Clipboard event defined, cannot continue");
}
auto data = clipboard->getAll();
LOG(DEBUG) << "Clipboard has " << data.size() << " different object types.";
for(auto& block : data) {
try {
auto type_idx = block.first;
auto class_name = corryvreckan::demangle(type_idx.name());
auto class_name_full = corryvreckan::demangle(type_idx.name(), true);
LOG(TRACE) << "Received objects of type \"" << class_name << "\"";
// Check if these objects hsould be read
if((!include_.empty() && include_.find(class_name) == include_.end()) ||
(!exclude_.empty() && exclude_.find(class_name) != exclude_.end())) {
LOG(TRACE) << "Ignoring object " << corryvreckan::demangle(type_idx.name())
<< " because it has been excluded or not explicitly included";
continue;
}
for(auto& detector_block : block.second) {
// Get the detector name
std::string detector_name;
if(!detector_block.first.empty()) {
detector_name = detector_block.first;
}
auto objects = std::static_pointer_cast<ObjectVector>(detector_block.second);
for(auto& object : *objects) {
*output_file_ << TBufferJSON::ToJSON(object) << "," << std::endl;
}
}
} catch(...) {
LOG(WARNING) << "Cannot process object of type" << corryvreckan::demangle(block.first.name());
return StatusCode::NoData;
}
}
// Increment event counter
m_eventNumber++;
// Return value telling analysis to keep running
return StatusCode::Success;
}
void JSONWriter::finalise() {
// finalize the JSON Array, add one empty Object to satisfy JSON Rules
*output_file_ << "{" << std::endl << "}" << std::endl << "]";
}
/**
* @file
* @brief Definition of module TextWriter
*
* @copyright Copyright (c) 2019-2020 CERN and the Corryvreckan authors.
* This software is distributed under the terms of the MIT License, copied verbatim in the file "LICENSE.md".
* 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.
*
* This module writes specific objects into a user specified text file
*
* Refer to the User's Manual for more details.
*/
#include <TBufferJSON.h>
#include <TCanvas.h>
#include <TH1F.h>
#include <TH2F.h>
#include <iostream>
#include "core/module/Module.hpp"
#include "objects/Cluster.hpp"
#include "objects/Pixel.hpp"
#include "objects/Track.hpp"
namespace corryvreckan {
/** @ingroup Modules
* @brief Module to write objects into a JSON file
*
* Loops over the selected objects and writes them into a text file in as elements of a JSON Array.
*/
class JSONWriter : public Module {
public:
/**
* @brief Constructor for this unique module
* @param config Configuration object for this module as retrieved from the steering file
* @param detectors Vector of pointers to the detectors
*/
JSONWriter(Configuration config, std::vector<std::shared_ptr<Detector>> detectors);
/**
* @brief Reads the configuration and opens the file to write to
*/
void initialise();
/**
* @brief Loops over the selected objects on the clipboard and writes them to file
* @param clipboard Pointer to the clipboard
*/
StatusCode run(std::shared_ptr<Clipboard> clipboard);
/**
* @brief No specific actions implemented
*/
void finalise();
private:
int m_eventNumber;
// Object names to include or exclude from writing
std::set<std::string> include_;
std::set<std::string> exclude_;
// Output data file to write
std::string output_file_name_{};
std::unique_ptr<std::ofstream> output_file_;
};
} // namespace corryvreckan
# JSONWriter
**Maintainer**: Paul Schuetze (<paul.schuetze@desy.de>)
**Module Type**: *GLOBAL*
**Status**: Functional
### Description
This module writes objects to a file as JSON array using `TBufferJSON::ToJASON(object)`. For this, the data content of the selected objects available on the clipboard are printed to the specified file. Beware that this results in a flat structure unlike the root file.
With `include` and `exclude` certain object types can be selected to be printed.
### Parameters
* `file_name` : Name of the data file to create, relative to the output directory of the framework. The file extension `.txt` will be appended if not present.
* `include` : Array of object names to write to the ASCII text file, all other object names are ignored (cannot be used together simultaneously with the *exclude* parameter).
* `exclude`: Array of object names that are not written to the ASCII text file (cannot be used together simultaneously with the *include* parameter).
### Usage
```toml
[JSONWriter]
file_name = "exampleFileName"
include = "Cluster","'"Track"
```
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment