Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ add_library(triggeralgs SHARED
src/TCMakerBundleNAlgorithm.cpp
src/TAMakerChannelAdjacencyAlgorithm.cpp
src/TCMakerChannelAdjacencyAlgorithm.cpp
src/TAMakerBSMWindowAlgorithm.cpp
src/TCMakerBSMWindowAlgorithm.cpp
src/BSMWindow/DetectorPlaneMap.cpp
src/BSMWindow/PDVDEffectiveChannelMap.cpp
src/BSMWindow/BSMWindow.cpp
src/BSMWindow/treelitemodel.cpp
src/BSMWindow/models/treelitePDHDmodel.cpp
src/BSMWindow/CompiledModelInterface.cpp
src/TAWindow.cpp
src/TPWindow.cpp
src/dbscan/dbscan.cpp
Expand All @@ -66,7 +74,7 @@ target_include_directories(triggeralgs PUBLIC
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/src>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
)
target_link_libraries(triggeralgs PRIVATE nlohmann_json::nlohmann_json detdataformats::detdataformats trgdataformats::trgdataformats cetlib::cetlib detchannelmaps::detchannelmaps)
target_link_libraries(triggeralgs PRIVATE nlohmann_json::nlohmann_json detdataformats::detdataformats trgdataformats::trgdataformats cetlib::cetlib detchannelmaps::detchannelmaps )
install(TARGETS triggeralgs EXPORT triggeralgsTargets)

CONFIGURE_PACKAGE_CONFIG_FILE(cmake/triggeralgsConfig.cmake.in
Expand Down
50 changes: 50 additions & 0 deletions include/triggeralgs/BSMWindow/BSMWindow.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#ifndef TRIGGERALGS_BSMWINDOW_HPP_
#define TRIGGERALGS_BSMWINDOW_HPP_

#include "triggeralgs/TriggerPrimitive.hpp"
#include "triggeralgs/Types.hpp"
#include "triggeralgs/BSMWindow/treelitemodel.hpp"
#include "triggeralgs/BSMWindow/PDVDEffectiveChannelMap.hpp"

#include <ostream>
#include <vector>
#include <numeric>

namespace triggeralgs {

// Class to hold data of TPs within a window in time.
// Functions to move the window along in time by removing
// old TPs and add in newer ones
// Function to bin the TPs in the window as a function of
// channel and time
class BSMWindow {
public:
bool is_empty() const;
void add(TriggerPrimitive const &input_tp);
void clear();
void move(TriggerPrimitive const &input_tp, timestamp_t const &window_length);
void reset(TriggerPrimitive const &input_tp);

// Bins the TPs as a function of channel and time
// If running in PD-VD then effective channel ID is used
void bin_window(std::vector<float> &input, timestamp_t time_bin_width, channel_t chan_bin_width,
int num_time_bins, int num_chan_bins, channel_t first_channel,
std::unique_ptr<PDVDEffectiveChannelMap> const &effective_channel_mapper, bool use_pdvd_map);

void fill_entry_window(std::vector<Entry> &entry_input, std::vector<float> &input);

// Calculate average properties of TPs in a time window
float mean_sadc();
float mean_adc_peak();
float mean_tot();

friend std::ostream& operator<<(std::ostream& os, const BSMWindow& window);

timestamp_t time_start;
uint32_t adc_integral;
uint64_t adc_peak_sum;
uint64_t tot_sum;
std::vector<TriggerPrimitive> tp_list;
};
}
#endif // TRIGGERALGS_BSMWINDOW_HPP_
43 changes: 43 additions & 0 deletions include/triggeralgs/BSMWindow/CompiledModelInterface.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#ifndef TRIGGERALGS_COMPILEDMODELINTERFACE_HPP_
#define TRIGGERALGS_COMPILEDMODELINTERFACE_HPP_

#include "triggeralgs/BSMWindow/treelitemodel.hpp"
#include <string>
#include <fstream>
#include <algorithm>

namespace triggeralgs {

// Interface for deploying XGBoost model using compiled C-code
// XGBoost model converted to treelite and then converted to c-code

// Foward declare base class for the treelite model
class TreeliteModelBase;

class CompiledModelInterface {
public:

CompiledModelInterface(int nbatch);

~CompiledModelInterface();

// Get number of features in model
int GetNumFeatures();

void ModelWarmUp(Entry *input);

// Run prediction with GBDT
void Predict(Entry *input, float *result);

// Is it a neutrino or cosmic according to GBDT?
bool Classify(const float *result, float &bdt_threshold);

protected:

std::unique_ptr<TreeliteModelBase> model_ptr;
int num_batch;

};
} // triggeralgs

#endif // TRIGGERALGS_COMPILEDMODELINTERFACE_HPP_
54 changes: 54 additions & 0 deletions include/triggeralgs/BSMWindow/DetectorPlaneMap.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@

#ifndef TRIGGERALGS_BSMWINDOW_DETECTORPLANEMAP_HPP_
#define TRIGGERALGS_BSMWINDOW_DETECTORPLANEMAP_HPP_

#include <string>
#include <map>
#include <algorithm>

namespace triggeralgs {

struct PlaneInfo {
int min_channel;
int n_channels;
};

// Provides mapping of plane and TPC ID to the first channel
// and number of channels on that plane
struct DetectorPlaneMap {
const std::map<std::pair<int,int>, PlaneInfo> pdhd_plane_map = {
{{0,0}, {400,400}},
{{0,1}, {1200,400}},
{{0,2}, {2080,480}},
{{1,0}, {2560,400}},
{{1,1}, {3360,400}},
{{1,2}, {4160,480}},
{{2,0}, {5520,400}},
{{2,1}, {6320,400}},
{{2,2}, {7200,480}},
{{3,0}, {7680,400}},
{{3,1}, {8480,400}},
{{3,2}, {9280,480}}
};

const std::map<std::pair<int,int>, PlaneInfo> pdvd_plane_map = {
{{2,0}, { 6144, 952}},
{{2,1}, { 7096, 952}},
{{2,2}, { 8048, 1168}},
{{3,0}, { 9216, 952}},
{{3,1}, {10168, 952}},
{{3,2}, {11120, 1168}},
{{4,0}, { 3072, 952}},
{{4,1}, { 4024, 952}},
{{4,2}, { 4976, 1168}},
{{5,0}, { 0, 952}},
{{5,1}, { 952, 952}},
{{5,2}, { 1904, 1168}}
};

// Function to get the plane range map information. The detid controls
// whether to use the PD-HD or PD-VD map
PlaneInfo get_plane_info(std::string channel_map_name, int detelement, int plane);
};
} // triggeralgs
#endif
31 changes: 31 additions & 0 deletions include/triggeralgs/BSMWindow/PDVDEffectiveChannelMap.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

#ifndef TRIGGERALGS_BSMWINDOW_PDVDEFFECTIVECHANNELMAP_HPP_
#define TRIGGERALGS_BSMWINDOW_PDVDEFFECTIVECHANNELMAP_HPP_

namespace triggeralgs {

// Class to provide mapping from true channel to effective channel
// in PD-VD. This is due to the non-intuitive true channel mapping
// of CRP planes that makes image creation challenging. Effective
// channel mapping avoids gaps in the images
class PDVDEffectiveChannelMap {
public:
PDVDEffectiveChannelMap(unsigned int first_channel, unsigned int n_channels);

// Function to do mapping from true -> effective channel
unsigned int remapCollectionPlaneChannel(unsigned int original_channel);

// Number of effective channels on CRP will be 1/2 number of true channels
unsigned int getNEffectiveChannels() { return m_n_effective_channels; }

protected:

unsigned int m_first_channel;
unsigned int m_n_channels;
unsigned int m_n_channels_crp_block;
unsigned int m_n_effective_channels;

};

} // triggeralgs
#endif
91 changes: 91 additions & 0 deletions include/triggeralgs/BSMWindow/TAMakerBSMWindowAlgorithm.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/**
* @file TAMakerBSMWindowAlgorithm.hpp
*
* This is part of the DUNE DAQ Application Framework, copyright 2021.
* Licensing/copyright details are in the COPYING file that you should have
* received with this code.
*/

#ifndef TRIGGERALGS_BSMWINDOW_TRIGGERACTIVITYMAKERBSMWINDOW_HPP_
#define TRIGGERALGS_BSMWINDOW_TRIGGERACTIVITYMAKERBSMWINDOW_HPP_

#include "detchannelmaps/TPCChannelMap.hpp"
#include "triggeralgs/TriggerActivityFactory.hpp"
#include "triggeralgs/Types.hpp"
#include "triggeralgs/BSMWindow/BSMWindow.hpp"
#include "triggeralgs/BSMWindow/CompiledModelInterface.hpp"
#include "triggeralgs/BSMWindow/DetectorPlaneMap.hpp"
#include "triggeralgs/BSMWindow/PDVDEffectiveChannelMap.hpp"

#include <vector>
#include <algorithm>

namespace triggeralgs {
class TAMakerBSMWindowAlgorithm : public TriggerActivityMaker
{

public:
void process(const TriggerPrimitive& input_tp, std::vector<TriggerActivity>& output_ta);
void configure(const nlohmann::json &config);

~TAMakerBSMWindowAlgorithm() override;

private:
// Function to handle XGBoost classification
// Returns true for signal and false for cosmic
bool compute_treelite_classification();

TriggerActivity construct_ta() const;

// The current time window of TPs
BSMWindow m_current_window;

timestamp_t m_last_pred_time;
uint64_t m_primitive_count = 0;

// Possible to do batch predictions with XGBoost
// For now just keep at 1
const int nbatch = 1;
// XGBoost takes a row-major flat array
std::vector<float> flat_batched_inputs;
// row-major input for Entry objects used for compiled model
std::vector<Entry> flat_batched_Entries;

// Configurable parameters.
uint32_t m_adc_threshold = 200000;
float m_ratio_threshold = 0.65;
float m_bdt_threshold = 0.99;
timestamp_t m_window_length = 20000;
std::string m_channel_map_name = "PD2VDTPCChannelMap";
// End of configurable parameters

// Define time binning
timestamp_t m_bin_length = 4000;
int m_num_timebins = 5;
// Define channel binning
channel_t m_chan_bin_length = 100;
int m_num_chanbins = 5;

// Geometry information for binning
std::shared_ptr<dunedaq::detchannelmaps::TPCChannelMap> channelMap;
// First channel in each plane and number of channels
// in a plane not in channel map by default, so add
// a struct to access these values
DetectorPlaneMap m_det_plane_map;
// In PD-VD want to work with effective offline channel
// rather than the true offline channel. Have an object that helps
// to do this. It prevents gaps in channel vs time images
std::unique_ptr<PDVDEffectiveChannelMap> m_pdvd_eff_channel_mapper = nullptr;
// If in NP02 and using a PD-VD channel map, set this to true
bool m_pdvd_map = true;
// first and last channel on the plane
channel_t m_first_channel;
channel_t m_last_channel;

// Compiled treelite model interface
std::unique_ptr<CompiledModelInterface> m_compiled_model_interface;

};
} // namespace triggeralgs

#endif // TRIGGERALGS_BSMWINDOW_TRIGGERACTIVITYMAKERBSMWINDOW_HPP_
37 changes: 37 additions & 0 deletions include/triggeralgs/BSMWindow/TCMakerBSMWindowAlgorithm.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* @file TCMakerBSMWindowAlgorithm.hpp
*
* This is part of the DUNE DAQ Application Framework, copyright 2021.
* Licensing/copyright details are in the COPYING file that you should have
* received with this code.
*/

#ifndef TRIGGERALGS_BSMWINDOW_TRIGGERCANDIDATEMAKERBSMWINDOW_HPP_
#define TRIGGERALGS_BSMWINDOW_TRIGGERCANDIDATEMAKERBSMWINDOW_HPP_

#include "triggeralgs/TriggerCandidateFactory.hpp"

#include <vector>

namespace triggeralgs {
class TCMakerBSMWindowAlgorithm : public TriggerCandidateMaker
{

public:
/// The function that gets call when there is a new activity
//void operator()(const TriggerActivity&, std::vector<TriggerCandidate>&);
void process(const TriggerActivity&, std::vector<TriggerCandidate>&);

void configure(const nlohmann::json &config);

private:

uint64_t m_activity_count = 0; // NOLINT(build/unsigned)

/// @brief Configurable TC type to produce by this TC algorithm
TriggerCandidate::Type m_tc_type = TriggerCandidate::Type::kUnknown;
};

} // namespace triggeralgs

#endif // TRIGGERALGS_BSMWINDOW_TRIGGERCANDIDATEMAKERADCSIMPLEWINDOW_HPP_
Loading