Commit ec762f70 authored by Simon Spannagel's avatar Simon Spannagel
Browse files

Clipboard: start moving to std::shared_ptr<Objects> instead of raw pointers

parent 3717307c
......@@ -27,12 +27,11 @@ void Clipboard::clear() {
// Loop over all stored collections of this type
for(auto set = collections.cbegin(); set != collections.cend();) {
Objects* collection = set->second;
std::shared_ptr<Objects> collection = std::static_pointer_cast<Objects>(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);
}
block = m_data.erase(block);
......@@ -46,7 +45,8 @@ std::vector<std::string> Clipboard::listCollections() {
std::string line(corryvreckan::demangle(block.first.name()));
line += ": ";
for(const auto& set : block.second) {
line += set.first + " (" + set.second->size() + ") ";
std::shared_ptr<Objects> collection = std::static_pointer_cast<Objects>(set.second);
line += set.first + " (" + collection->size() + ") ";
}
line += "\n";
collections.push_back(line);
......
......@@ -48,7 +48,7 @@ namespace corryvreckan {
* @param type Type of the object collection to be stored
* @param Objects vector of Objects to store
*/
template <typename T> void put(T* objects, const std::string& key = "");
template <typename T> void put(std::shared_ptr<T> objects, const std::string& key = "");
/**
* @brief Retrieve objects from the clipboard
......@@ -56,7 +56,7 @@ namespace corryvreckan {
* @param type Type of objects to be retrieved
* @return Vector of Object pointers
*/
template <typename T> T* get(const std::string& key = "") const;
template <typename T> std::shared_ptr<T> get(const std::string& key = "") const;
/**
* @brief Store or update variable on the persistent clipboard storage
......@@ -92,7 +92,7 @@ namespace corryvreckan {
std::vector<std::string> listCollections();
private:
typedef std::map<std::type_index, std::map<std::string, Objects*>> ClipboardData;
typedef std::map<std::type_index, std::map<std::string, std::shared_ptr<void>>> ClipboardData;
// Container for data, list of all data held
ClipboardData m_data;
......
namespace corryvreckan {
template <typename T> void Clipboard::put(T* objects, const std::string& key) {
template <typename T> void Clipboard::put(std::shared_ptr<T> objects, const std::string& key) {
// Do not insert empty sets:
if(objects->empty()) {
return;
......@@ -13,20 +13,20 @@ namespace corryvreckan {
/* 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*>()));
type = m_data.insert(type, ClipboardData::value_type(typeid(T), std::map<std::string, std::shared_ptr<void>>()));
// 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)));
auto test = type->second.insert(std::make_pair(key, std::static_pointer_cast<void>(objects)));
if(!test.second) {
LOG(WARNING) << "Not inserted for " << key;
}
}
template <typename T> T* Clipboard::get(const std::string& key) const {
template <typename T> std::shared_ptr<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));
return std::static_pointer_cast<T>(m_data.at(typeid(T)).at(key));
}
} // namespace corryvreckan
......@@ -502,7 +502,7 @@ bool AnalysisCLICpix::checkMasked(double chipInterceptRow, double chipInterceptC
// Check if there is another track close to the selected track.
// "Close" is defined as the intercept at the clicpix
bool AnalysisCLICpix::checkProximity(Track* track, Tracks* tracks) {
bool AnalysisCLICpix::checkProximity(Track* track, std::shared_ptr<Tracks> tracks) {
// Get the intercept of the interested track at the dut
bool close = false;
......@@ -528,7 +528,7 @@ bool AnalysisCLICpix::checkProximity(Track* track, Tracks* tracks) {
}
// Small sub-routine to fill histograms that only need clusters
void AnalysisCLICpix::fillClusterHistos(Clusters* clusters) {
void AnalysisCLICpix::fillClusterHistos(std::shared_ptr<Clusters> clusters) {
// Pick up column to generate unique pixel id
int nCols = m_detector->nPixels().X();
......
......@@ -26,8 +26,8 @@ namespace corryvreckan {
private:
std::shared_ptr<Detector> m_detector;
bool checkMasked(double, double);
void fillClusterHistos(Clusters*);
bool checkProximity(Track*, Tracks*);
void fillClusterHistos(std::shared_ptr<Clusters>);
bool checkProximity(Track*, std::shared_ptr<Tracks>);
void fillResponseHistos(double, double, Cluster*);
// Cluster/pixel histograms
......
......@@ -54,7 +54,8 @@ void AnalysisTelescope::initialise() {
}
}
ROOT::Math::XYZPoint AnalysisTelescope::closestApproach(ROOT::Math::XYZPoint position, MCParticles* particles) {
ROOT::Math::XYZPoint AnalysisTelescope::closestApproach(ROOT::Math::XYZPoint position,
std::shared_ptr<MCParticles> particles) {
// Find the closest MC particle
double smallestDistance(DBL_MAX);
ROOT::Math::XYZPoint particlePosition;
......
......@@ -21,7 +21,7 @@ namespace corryvreckan {
StatusCode run(std::shared_ptr<Clipboard> clipboard);
private:
ROOT::Math::XYZPoint closestApproach(ROOT::Math::XYZPoint position, MCParticles* particles);
ROOT::Math::XYZPoint closestApproach(ROOT::Math::XYZPoint position, std::shared_ptr<MCParticles> particles);
// Histograms for each of the devices
std::map<std::string, TH1F*> telescopeMCresidualsLocalX;
......
......@@ -46,7 +46,7 @@ StatusCode Clustering4D::run(std::shared_ptr<Clipboard> clipboard) {
size_t totalPixels = pixels->size();
// Make the cluster storage
Clusters* deviceClusters = new Clusters();
std::shared_ptr<Clusters> deviceClusters;
// Keep track of which pixels are used
map<Pixel*, bool> used;
......
......@@ -49,7 +49,7 @@ StatusCode ClusteringSpatial::run(std::shared_ptr<Clipboard> clipboard) {
}
// Make the cluster container and the maps for clustering
Clusters* deviceClusters = new Clusters();
std::shared_ptr<Clusters> deviceClusters;
map<Pixel*, bool> used;
map<int, map<int, Pixel*>> hitmap;
bool addedPixel;
......
......@@ -178,7 +178,8 @@ StatusCode EventLoaderATLASpix::run(std::shared_ptr<Clipboard> clipboard) {
bool busy_at_start = m_detectorBusy;
// Read pixel data
Pixels* pixels = (m_legacyFormat ? read_legacy_data(start_time, end_time) : read_caribou_data(start_time, end_time));
std::shared_ptr<Pixels> pixels =
(m_legacyFormat ? read_legacy_data(start_time, end_time) : read_caribou_data(start_time, end_time));
if(busy_at_start || m_detectorBusy) {
LOG(DEBUG) << "Returning <DeadTime> status, ATLASPix is BUSY.";
......@@ -212,13 +213,13 @@ StatusCode EventLoaderATLASpix::run(std::shared_ptr<Clipboard> clipboard) {
return StatusCode::Success;
}
Pixels* EventLoaderATLASpix::read_caribou_data(double start_time, double end_time) {
std::shared_ptr<Pixels> EventLoaderATLASpix::read_caribou_data(double start_time, double end_time) {
LOG(DEBUG) << "Searching for events in interval from " << Units::display(start_time, {"s", "us", "ns"}) << " to "
<< Units::display(end_time, {"s", "us", "ns"}) << ", file read position " << m_file.tellg()
<< ", old_fpga_ts = " << old_fpga_ts << ".";
// Pixel container
Pixels* pixels = new Pixels();
std::shared_ptr<Pixels> pixels;
// Read file and load data
uint32_t datain;
......@@ -487,10 +488,10 @@ Pixels* EventLoaderATLASpix::read_caribou_data(double start_time, double end_tim
return pixels;
}
Pixels* EventLoaderATLASpix::read_legacy_data(double, double) {
std::shared_ptr<Pixels> EventLoaderATLASpix::read_legacy_data(double, double) {
// Pixel container
Pixels* pixels = new Pixels();
std::shared_ptr<Pixels> pixels;
// Read file and load data
while(!m_file.eof()) {
......
......@@ -39,12 +39,12 @@ namespace corryvreckan {
/*
* @brief Read data in the format written by the Karlsruhe readout system
*/
Pixels* read_legacy_data(double start_time, double end_time);
std::shared_ptr<Pixels> read_legacy_data(double start_time, double end_time);
/*
* @brief Read data in the format written by the Caribou readout system
*/
Pixels* read_caribou_data(double start_time, double end_time);
std::shared_ptr<Pixels> read_caribou_data(double start_time, double end_time);
std::shared_ptr<Detector> m_detector;
unsigned long long int m_oldtoa;
......
......@@ -57,7 +57,7 @@ StatusCode EventLoaderCLICpix::run(std::shared_ptr<Clipboard> clipboard) {
// Otherwise load a new frame
// Pixel container, shutter information
Pixels* pixels = new Pixels();
std::shared_ptr<Pixels> pixels;
double shutterStartTime = 0, shutterStopTime = 0;
string data;
......
......@@ -178,7 +178,7 @@ StatusCode EventLoaderCLICpix2::run(std::shared_ptr<Clipboard> clipboard) {
}
// Pixel container, shutter information
Pixels* pixels = new Pixels();
std::shared_ptr<Pixels> pixels;
long long int shutterStartTimeInt = 0, shutterStopTimeInt = 0;
double shutterStartTime, shutterStopTime;
string datastring;
......@@ -301,7 +301,6 @@ StatusCode EventLoaderCLICpix2::run(std::shared_ptr<Clipboard> clipboard) {
if(!pixels->empty()) {
clipboard->put(pixels, m_detector->name());
} else {
delete pixels;
return StatusCode::NoData;
}
......
......@@ -82,7 +82,7 @@ StatusCode EventLoaderTimepix1::run(std::shared_ptr<Clipboard> clipboard) {
// Make the container object for pixels. Some devices may not have
// any hits, so each event we have to check which detectors are present
// and add their hits
map<string, Pixels*> dataContainers;
map<string, std::shared_ptr<Pixels>> dataContainers;
vector<string> detectors;
// If there are no files open and there are more files to look at, open a new
......@@ -117,7 +117,7 @@ StatusCode EventLoaderTimepix1::run(std::shared_ptr<Clipboard> clipboard) {
m_prevHeader = "";
detectors.push_back(device);
m_currentDevice = device;
dataContainers[device] = new Pixels();
dataContainers[device] = std::make_shared<Pixels>();
LOG(DEBUG) << "Detector: " << device << ", time: " << time;
}
......@@ -149,7 +149,7 @@ StatusCode EventLoaderTimepix1::run(std::shared_ptr<Clipboard> clipboard) {
// Record that this device has been made
detectors.push_back(device);
m_currentDevice = device;
dataContainers[device] = new Pixels();
dataContainers[device] = std::make_shared<Pixels>();
LOG(DEBUG) << "Detector: " << device << ", time: " << time;
} else {
......
......@@ -231,8 +231,8 @@ StatusCode EventLoaderTimepix3::run(std::shared_ptr<Clipboard> clipboard) {
}
// Make a new container for the data
Pixels* deviceData = new Pixels();
SpidrSignals* spidrData = new SpidrSignals();
std::shared_ptr<Pixels> deviceData;
std::shared_ptr<SpidrSignals> spidrData;
// Load the next chunk of data
bool data = loadData(clipboard, deviceData, spidrData);
......@@ -241,14 +241,10 @@ StatusCode EventLoaderTimepix3::run(std::shared_ptr<Clipboard> clipboard) {
if(data) {
LOG(DEBUG) << "Loaded " << deviceData->size() << " pixels for device " << m_detector->name();
clipboard->put(deviceData, m_detector->name());
} else {
delete deviceData;
}
if(!spidrData->empty()) {
clipboard->put(spidrData, m_detector->name());
} else {
delete spidrData;
}
// Otherwise tell event loop to keep running
......@@ -331,7 +327,9 @@ void EventLoaderTimepix3::loadCalibration(std::string path, char delim, std::vec
}
// Function to load data for a given device, into the relevant container
bool EventLoaderTimepix3::loadData(std::shared_ptr<Clipboard> clipboard, Pixels* devicedata, SpidrSignals* spidrData) {
bool EventLoaderTimepix3::loadData(std::shared_ptr<Clipboard> clipboard,
std::shared_ptr<Pixels>& devicedata,
std::shared_ptr<SpidrSignals>& spidrData) {
std::string detectorID = m_detector->name();
......
......@@ -38,7 +38,7 @@ namespace corryvreckan {
TH2F* pixelTOAParameterT;
TH1F* timeshiftPlot;
bool loadData(std::shared_ptr<Clipboard> clipboard, Pixels*, SpidrSignals*);
bool loadData(std::shared_ptr<Clipboard> clipboard, std::shared_ptr<Pixels>&, std::shared_ptr<SpidrSignals>&);
void loadCalibration(std::string path, char delim, std::vector<std::vector<float>>& dat);
void maskPixels(std::string);
......
......@@ -70,7 +70,7 @@ StatusCode Tracking4D::run(std::shared_ptr<Clipboard> clipboard) {
// Container for all clusters, and detectors in tracking
map<string, KDTree*> trees;
vector<string> detectors;
Clusters* referenceClusters = nullptr;
std::shared_ptr<Clusters> referenceClusters = nullptr;
// Loop over all planes and get clusters
bool firstDetector = true;
......@@ -111,7 +111,7 @@ StatusCode Tracking4D::run(std::shared_ptr<Clipboard> clipboard) {
}
// Output track container
Tracks* tracks = new Tracks();
std::shared_ptr<Tracks> tracks;
// Loop over all clusters
map<Cluster*, bool> used;
......
......@@ -67,7 +67,7 @@ StatusCode TrackingSpatial::run(std::shared_ptr<Clipboard> clipboard) {
// Container for all clusters, and detectors in tracking
map<string, KDTree*> trees;
vector<std::shared_ptr<Detector>> detectors;
Clusters* referenceClusters = nullptr;
std::shared_ptr<Clusters> referenceClusters = nullptr;
Clusters dutClusters;
// Loop over all detectors and get clusters
......@@ -103,7 +103,7 @@ StatusCode TrackingSpatial::run(std::shared_ptr<Clipboard> clipboard) {
}
// Output track container
Tracks* tracks = new Tracks();
std::shared_ptr<Tracks> tracks;
// Keep a note of which clusters have been used
map<Cluster*, bool> used;
......
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