Commit 3ded1bf6 authored by Simon Spannagel's avatar Simon Spannagel
Browse files

Clipboard: use templating instead of string comparisons for types. Move...

Clipboard: use templating instead of string comparisons for types. Move reinterpret_cast into Clipboard
parent 6ed6d9dc
......@@ -4,26 +4,11 @@
using namespace corryvreckan;
void Clipboard::put(std::string name, Objects* objects) {
m_data.insert(ClipboardData::value_type(name, objects));
}
void Clipboard::put(std::string name, std::string type, Objects* objects) {
m_data.insert(ClipboardData::value_type(name + type, objects));
}
void Clipboard::put_persistent(std::string name, double value) {
m_persistent_data[name] = value;
}
Objects* Clipboard::get(std::string name, std::string type) {
if(m_data.count(name + type) == 0) {
return nullptr;
}
return m_data[name + type];
}
double Clipboard::get_persistent(std::string name) {
double Clipboard::get_persistent(std::string name) const {
try {
return m_persistent_data.at(name);
} catch(std::out_of_range&) {
......@@ -31,25 +16,40 @@ double Clipboard::get_persistent(std::string name) {
}
}
bool Clipboard::has_persistent(std::string name) {
bool Clipboard::has_persistent(std::string name) const {
return m_persistent_data.find(name) != m_persistent_data.end();
}
void Clipboard::clear() {
for(auto set = m_data.cbegin(); set != m_data.cend();) {
Objects* collection = set->second;
for(Objects::iterator it = collection->begin(); it != collection->end(); ++it) {
delete(*it);
// Loop over all data types
for(auto block = m_data.cbegin(); block != m_data.cend();) {
auto collections = block->second;
// Loop over all stored collections of this type
for(auto set = collections.cbegin(); set != collections.cend();) {
Objects* collection = set->second;
// Loop over all objects and delete them
for(Objects::iterator it = collection->begin(); it != collection->end(); ++it) {
delete(*it);
}
delete collection;
set = collections.erase(set);
}
delete collection;
set = m_data.erase(set);
block = m_data.erase(block);
}
}
std::vector<std::string> Clipboard::listCollections() {
std::vector<std::string> collections;
for(auto& dataset : m_data) {
collections.push_back(dataset.first);
for(const auto& block : m_data) {
std::string line(corryvreckan::demangle(block.first.name()));
line += ": ";
for(const auto& set : block.second) {
line += set.first + " (" + set.second->size() + ") ";
}
line += "\n";
collections.push_back(line);
}
return collections;
}
......@@ -12,6 +12,7 @@
#include <iostream>
#include <string>
#include <typeindex>
#include <unordered_map>
#include "core/utils/log.h"
......@@ -41,20 +42,13 @@ namespace corryvreckan {
*/
virtual ~Clipboard() {}
/**
* @brief Add object to the clipboard
* @param name Name of the collection to be stored
* @param objects vector of Objects to store
*/
void put(std::string name, Objects* objects);
/**
* @brief Add object to the clipboard
* @param name Name of the collection to be stored
* @param type Type of the object collection to be stored
* @param Objects vector of Objects to store
*/
void put(std::string name, std::string type, Objects* objects);
template <typename T> void put(T* objects, const std::string& key = "");
/**
* @brief Retrieve objects from the clipboard
......@@ -62,7 +56,7 @@ namespace corryvreckan {
* @param type Type of objects to be retrieved
* @return Vector of Object pointers
*/
Objects* get(std::string name, std::string type = "");
template <typename T> T* get(const std::string& key = "") const;
/**
* @brief Store or update variable on the persistent clipboard storage
......@@ -77,14 +71,14 @@ namespace corryvreckan {
* @return Stored value from the persistent clipboard storage
* @throws MissingKeyError in case the key is not found.
*/
double get_persistent(std::string name);
double get_persistent(std::string name) const;
/**
* @brief Check if variable exists on the persistent clipboard storage
* @param name Name of the variable
* @return True if value exists, false if it does not exist.
*/
bool has_persistent(std::string name);
bool has_persistent(std::string name) const;
/**
* @brief Clear the event storage of the clipboard
......@@ -98,7 +92,7 @@ namespace corryvreckan {
std::vector<std::string> listCollections();
private:
typedef std::map<std::string, Objects*> ClipboardData;
typedef std::map<std::type_index, std::map<std::string, Objects*>> ClipboardData;
// Container for data, list of all data held
ClipboardData m_data;
......@@ -108,4 +102,7 @@ namespace corryvreckan {
};
} // namespace corryvreckan
// Include template members
#include "Clipboard.tpp"
#endif // CORRYVRECKAN_CLIPBOARD_H
namespace corryvreckan {
template <typename T> void Clipboard::put(T* objects, const std::string& key) {
// Do not insert empty sets:
if(objects->empty()) {
return;
}
// Iterator for data type:
ClipboardData::iterator type = m_data.begin();
/* If data type exists, returns iterator to offending key, if data type does not exist yet, creates new entry and
* returns iterator to the newly created element
*/
type = m_data.insert(type, ClipboardData::value_type(typeid(T), std::map<std::string, Objects*>()));
// Insert data into data type element, silently fail if it exists already
auto test = type->second.insert(std::make_pair(key, reinterpret_cast<Objects*>(objects)));
if(!test.second) {
LOG(WARNING) << "Not inserted for " << key;
}
}
template <typename T> T* Clipboard::get(const std::string& key) const {
if(m_data.count(typeid(T)) == 0 || m_data.at(typeid(T)).count(key) == 0) {
return nullptr;
}
return reinterpret_cast<T*>(m_data.at(typeid(T)).at(key));
}
} // namespace corryvreckan
......@@ -607,7 +607,7 @@ void ModuleManager::run() {
}
// Print statistics:
Tracks* tracks = reinterpret_cast<Tracks*>(m_clipboard->get("tracks"));
auto tracks = m_clipboard->get<Tracks>();
m_tracks += (tracks == nullptr ? 0 : static_cast<int>(tracks->size()));
if(m_events % 100 == 0) {
......
......@@ -59,7 +59,7 @@ void AlignmentDUTResidual::initialise() {
StatusCode AlignmentDUTResidual::run(std::shared_ptr<Clipboard> clipboard) {
// Get the tracks
Tracks* tracks = reinterpret_cast<Tracks*>(clipboard->get("tracks"));
auto tracks = clipboard->get<Tracks>();
if(tracks == nullptr) {
return StatusCode::Success;
}
......
......@@ -69,7 +69,7 @@ void AlignmentMillepede::initialise() {
StatusCode AlignmentMillepede::run(std::shared_ptr<Clipboard> clipboard) {
// Get the tracks
Tracks* tracks = reinterpret_cast<Tracks*>(clipboard->get("tracks"));
auto tracks = clipboard->get<Tracks>();
if(tracks == nullptr) {
return StatusCode::Success;
}
......
......@@ -34,7 +34,7 @@ AlignmentTrackChi2::AlignmentTrackChi2(Configuration config, std::vector<std::sh
StatusCode AlignmentTrackChi2::run(std::shared_ptr<Clipboard> clipboard) {
// Get the tracks
Tracks* tracks = reinterpret_cast<Tracks*>(clipboard->get("tracks"));
auto tracks = clipboard->get<Tracks>();
if(tracks == nullptr) {
return StatusCode::Success;
}
......
......@@ -219,7 +219,7 @@ void AnalysisCLICpix::initialise() {
StatusCode AnalysisCLICpix::run(std::shared_ptr<Clipboard> clipboard) {
// Get the clicpix clusters in this event
Clusters* clusters = reinterpret_cast<Clusters*>(clipboard->get(m_detector->name(), "clusters"));
auto clusters = clipboard->get<Clusters>(m_detector->name());
if(clusters == nullptr) {
LOG(DEBUG) << "No clusters for " << m_detector->name() << " on the clipboard";
return StatusCode::Success;
......@@ -236,7 +236,7 @@ StatusCode AnalysisCLICpix::run(std::shared_ptr<Clipboard> clipboard) {
fillClusterHistos(clusters);
// Get the tracks in this event
Tracks* tracks = reinterpret_cast<Tracks*>(clipboard->get("tracks"));
auto tracks = clipboard->get<Tracks>();
if(tracks == nullptr) {
LOG(DEBUG) << "No tracks on the clipboard";
return StatusCode::Success;
......
......@@ -187,7 +187,7 @@ void AnalysisDUT::initialise() {
StatusCode AnalysisDUT::run(std::shared_ptr<Clipboard> clipboard) {
// Get the telescope tracks from the clipboard
Tracks* tracks = reinterpret_cast<Tracks*>(clipboard->get("tracks"));
auto tracks = clipboard->get<Tracks>();
if(tracks == nullptr) {
LOG(DEBUG) << "No tracks on the clipboard";
return StatusCode::Success;
......@@ -247,7 +247,7 @@ StatusCode AnalysisDUT::run(std::shared_ptr<Clipboard> clipboard) {
auto ymod = static_cast<double>(Units::convert(m_detector->inPixelY(localIntercept), "um"));
// Get the DUT clusters from the clipboard
Clusters* clusters = reinterpret_cast<Clusters*>(clipboard->get(m_detector->name(), "clusters"));
auto clusters = clipboard->get<Clusters>(m_detector->name());
if(clusters == nullptr) {
LOG(DEBUG) << " - no DUT clusters";
} else {
......
......@@ -67,7 +67,7 @@ void AnalysisEfficiency::initialise() {
StatusCode AnalysisEfficiency::run(std::shared_ptr<Clipboard> clipboard) {
// Get the telescope tracks from the clipboard
Tracks* tracks = reinterpret_cast<Tracks*>(clipboard->get("tracks"));
auto tracks = clipboard->get<Tracks>();
if(tracks == nullptr) {
LOG(DEBUG) << "No tracks on the clipboard";
return StatusCode::Success;
......@@ -129,7 +129,7 @@ StatusCode AnalysisEfficiency::run(std::shared_ptr<Clipboard> clipboard) {
auto ymod = static_cast<double>(Units::convert(m_detector->inPixelY(localIntercept), "um"));
// Get the DUT clusters from the clipboard
Clusters* clusters = reinterpret_cast<Clusters*>(clipboard->get(m_detector->name(), "clusters"));
auto clusters = clipboard->get<Clusters>(m_detector->name());
if(clusters == nullptr) {
LOG(DEBUG) << " - no DUT clusters";
} else {
......
......@@ -91,7 +91,7 @@ StatusCode AnalysisPowerPulsing::run(std::shared_ptr<Clipboard> clipboard) {
*/
// Now update the power pulsing with any new signals
auto spidrData = reinterpret_cast<SpidrSignals*>(clipboard->get(m_detector->name(), "SpidrSignals"));
auto spidrData = clipboard->get<SpidrSignals>(m_detector->name());
// If there are new signals
if(spidrData != nullptr) {
......@@ -135,7 +135,7 @@ StatusCode AnalysisPowerPulsing::run(std::shared_ptr<Clipboard> clipboard) {
}
// Get the DUT clusters from the clipboard
auto clusters = reinterpret_cast<Clusters*>(clipboard->get(m_detector->name(), "clusters"));
auto clusters = clipboard->get<Clusters>(m_detector->name());
if(clusters == nullptr) {
LOG(DEBUG) << "No DUT clusters on the clipboard!";
return StatusCode::Success;
......
......@@ -74,7 +74,7 @@ ROOT::Math::XYZPoint AnalysisTelescope::closestApproach(ROOT::Math::XYZPoint pos
StatusCode AnalysisTelescope::run(std::shared_ptr<Clipboard> clipboard) {
// Get the tracks from the clipboard
Tracks* tracks = reinterpret_cast<Tracks*>(clipboard->get("tracks"));
auto tracks = clipboard->get<Tracks>();
if(tracks == nullptr) {
LOG(DEBUG) << "No tracks on the clipboard";
return StatusCode::Success;
......@@ -103,7 +103,7 @@ StatusCode AnalysisTelescope::run(std::shared_ptr<Clipboard> clipboard) {
telescopeResidualsY[name]->Fill(cluster->global().y() - intercept.Y());
// Get the MC particles from the clipboard
MCParticles* mcParticles = reinterpret_cast<MCParticles*>(clipboard->get(name, "mcparticles"));
auto mcParticles = clipboard->get<MCParticles>(name);
if(mcParticles == nullptr) {
continue;
}
......@@ -122,7 +122,7 @@ StatusCode AnalysisTelescope::run(std::shared_ptr<Clipboard> clipboard) {
}
// Get the MC particles from the clipboard
MCParticles* mcParticles = reinterpret_cast<MCParticles*>(clipboard->get(detector->name(), "mcparticles"));
auto mcParticles = clipboard->get<MCParticles>(detector->name());
if(mcParticles == nullptr) {
continue;
}
......
......@@ -39,7 +39,7 @@ StatusCode Clustering4D::run(std::shared_ptr<Clipboard> clipboard) {
}
// Get the pixels
Pixels* pixels = reinterpret_cast<Pixels*>(clipboard->get(m_detector->name(), "pixels"));
auto pixels = clipboard->get<Pixels>(m_detector->name());
if(pixels == nullptr) {
LOG(DEBUG) << "Detector " << m_detector->name() << " does not have any pixels on the clipboard";
return StatusCode::Success;
......@@ -124,7 +124,7 @@ StatusCode Clustering4D::run(std::shared_ptr<Clipboard> clipboard) {
// Put the clusters on the clipboard
if(deviceClusters->size() > 0) {
clipboard->put(m_detector->name(), "clusters", reinterpret_cast<Objects*>(deviceClusters));
clipboard->put(deviceClusters, m_detector->name());
}
LOG(DEBUG) << "Made " << deviceClusters->size() << " clusters for device " << m_detector->name();
......
......@@ -42,14 +42,14 @@ void ClusteringSpatial::initialise() {
StatusCode ClusteringSpatial::run(std::shared_ptr<Clipboard> clipboard) {
// Get the pixels
Pixels* pixels = reinterpret_cast<Pixels*>(clipboard->get(m_detector->name(), "pixels"));
auto pixels = clipboard->get<Pixels>(m_detector->name());
if(pixels == nullptr) {
LOG(DEBUG) << "Detector " << m_detector->name() << " does not have any pixels on the clipboard";
return StatusCode::Success;
}
// Make the cluster container and the maps for clustering
Objects* deviceClusters = new Objects();
Clusters* deviceClusters = new Clusters();
map<Pixel*, bool> used;
map<int, map<int, Pixel*>> hitmap;
bool addedPixel;
......@@ -131,7 +131,7 @@ StatusCode ClusteringSpatial::run(std::shared_ptr<Clipboard> clipboard) {
deviceClusters->push_back(cluster);
}
clipboard->put(m_detector->name(), "clusters", deviceClusters);
clipboard->put(deviceClusters, m_detector->name());
LOG(DEBUG) << "Put " << deviceClusters->size() << " clusters on the clipboard for detector " << m_detector->name()
<< ". From " << pixels->size() << " pixels";
......
......@@ -13,14 +13,14 @@ DUTAssociation::DUTAssociation(Configuration config, std::shared_ptr<Detector> d
StatusCode DUTAssociation::run(std::shared_ptr<Clipboard> clipboard) {
// Get the tracks from the clipboard
Tracks* tracks = reinterpret_cast<Tracks*>(clipboard->get("tracks"));
auto tracks = clipboard->get<Tracks>();
if(tracks == nullptr) {
LOG(DEBUG) << "No tracks on the clipboard";
return StatusCode::Success;
}
// Get the DUT clusters from the clipboard
Clusters* clusters = reinterpret_cast<Clusters*>(clipboard->get(m_detector->name(), "clusters"));
auto clusters = clipboard->get<Clusters>(m_detector->name());
if(clusters == nullptr) {
LOG(DEBUG) << "No DUT clusters on the clipboard";
return StatusCode::Success;
......
......@@ -69,7 +69,7 @@ void EtaCalculation::calculateEta(Track* track, Cluster* cluster) {
StatusCode EtaCalculation::run(std::shared_ptr<Clipboard> clipboard) {
// Get the tracks from the clipboard
Tracks* tracks = reinterpret_cast<Tracks*>(clipboard->get("tracks"));
auto tracks = clipboard->get<Tracks>();
if(tracks == nullptr) {
LOG(DEBUG) << "No tracks on the clipboard";
return StatusCode::Success;
......
......@@ -81,7 +81,7 @@ void EtaCorrection::applyEta(Cluster* cluster) {
StatusCode EtaCorrection::run(std::shared_ptr<Clipboard> clipboard) {
// Get the clusters
Clusters* clusters = reinterpret_cast<Clusters*>(clipboard->get(m_detector->name(), "clusters"));
auto clusters = clipboard->get<Clusters>(m_detector->name());
if(clusters == nullptr) {
LOG(DEBUG) << "Detector " << m_detector->name() << " does not have any clusters on the clipboard";
return StatusCode::Success;
......
......@@ -201,7 +201,7 @@ StatusCode EventLoaderATLASpix::run(std::shared_ptr<Clipboard> clipboard) {
// Put the data on the clipboard
if(!pixels->empty()) {
clipboard->put(m_detector->name(), "pixels", reinterpret_cast<Objects*>(pixels));
clipboard->put(pixels, m_detector->name());
} else {
LOG(DEBUG) << "Returning <NoData> status, no hits found.";
return StatusCode::NoData;
......
......@@ -122,7 +122,7 @@ StatusCode EventLoaderCLICpix::run(std::shared_ptr<Clipboard> clipboard) {
LOG(TRACE) << "Loaded " << npixels << " pixels";
// Put the data on the clipboard
if(pixels->size() > 0)
clipboard->put(m_detector->name(), "pixels", reinterpret_cast<Objects*>(pixels));
clipboard->put(pixels, m_detector->name());
// Fill histograms
hPixelsPerFrame->Fill(npixels);
......
......@@ -299,7 +299,7 @@ StatusCode EventLoaderCLICpix2::run(std::shared_ptr<Clipboard> clipboard) {
// Put the data on the clipboard
if(!pixels->empty()) {
clipboard->put(m_detector->name(), "pixels", reinterpret_cast<Objects*>(pixels));
clipboard->put(pixels, m_detector->name());
} else {
delete pixels;
return StatusCode::NoData;
......
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