Skip to content

Tkuma timeframe splitting #1916

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ flycheck_*.el


*.bck
/.cache/

bin/

Expand Down
2 changes: 1 addition & 1 deletion src/extensions/jana/JOmniFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* which might be changed by user parameters.
*/

#include <JANA/CLI/JVersion.h>
#include <JANA/JVersion.h>
#include <JANA/JMultifactory.h>
#include <JANA/JEvent.h>
#include <spdlog/spdlog.h>
Expand Down
1 change: 1 addition & 0 deletions src/global/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ add_subdirectory(reco)
add_subdirectory(pid)
add_subdirectory(pid_lut)
add_subdirectory(beam)
add_subdirectory(splitting)
42 changes: 42 additions & 0 deletions src/global/splitting/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
cmake_minimum_required(VERSION 3.16)

if(JANA_VERSION VERSION_LESS "2.4.2")

message(STATUS "Skipping `splitting` plugin because JANA_VERSION < 2.4.2")

else()

get_filename_component(PLUGIN_NAME ${CMAKE_CURRENT_LIST_DIR} NAME)

# Function creates ${PLUGIN_NAME}_plugin and ${PLUGIN_NAME}_library targets
# Setting default includes, libraries and installation paths
plugin_add(${PLUGIN_NAME} PLUGIN_USE_CC_ONLY)

# The macro grabs sources as *.cc *.cpp *.c and headers as *.h *.hh *.hpp Then
# correctly sets sources for ${_name}_plugin and ${_name}_library targets Adds
# headers to the correct installation directory
plugin_glob_all(${PLUGIN_NAME})

# Find dependencies plugin_add_dd4hep(${PLUGIN_NAME}#)
# plugin_add_acts(${PLUGIN_NAME}) plugin_add_cern_root(${PLUGIN_NAME})
plugin_add_event_model(${PLUGIN_NAME})

# Add include directories (works same as target_include_directories)
# plugin_include_directories(${PLUGIN_NAME} SYSTEM PUBLIC ... )

# Add libraries (same as target_include_directories but for both plugin and
# library)
plugin_link_libraries(${PLUGIN_NAME} algorithms_digi_library
algorithms_tracking_library)
#
# Add include directories (works same as target_include_directories)
# plugin_include_directories(${PLUGIN_NAME} PUBLIC ${CMAKE_CURRENT_LIST_DIR}
# SYSTEM PUBLIC ${podio_INCLUDE_DIR} ${EDM4HEP_INCLUDE_DIR}
# ${DD4hep_INCLUDE_DIRS} ${ROOT_INCLUDE_DIRS} ${EDM4EIC_INCLUDE_DIRS})
#
# Add libraries (works same as target_include_directories)
# plugin_link_libraries(${PLUGIN_NAME} ${ROOT_LIBRARIES}
# algorithms_tracking_library EDM4HEP::edm4hep EDM4EIC::edm4eic spdlog::spdlog
# Boost::boost ${ROOT_EG_LIBRARY})

endif()
263 changes: 263 additions & 0 deletions src/global/splitting/TimeframeSplitter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,263 @@
// Copyright 2024, Jefferson Science Associates, LLC.
// Subject to the terms in the LICENSE file found in the top-level directory.

#pragma once

#include <edm4hep/EventHeaderCollection.h>
#include <edm4hep/MCParticleCollection.h>
#include <edm4hep/SimCalorimeterHitCollection.h>
#include <edm4hep/SimTrackerHitCollection.h>
#include <edm4hep/CaloHitContributionCollection.h>

#include "services/io/podio/datamodel_glue.h"
#include <JANA/JEventUnfolder.h>

struct TimeframeSplitter : public JEventUnfolder {

float m_timeframe_width = 2000.0; // ns
float m_timesplit_width = 20.0; // ns
bool m_use_timeframe = false; // Use timeframes to split events, or use timeslices

// std::vector<std::string> m_simtrackerhit_collection_names = {
// "B0TrackerHits_aligned", "BackwardMPGDEndcapHits_aligned", "DIRCBarHits_aligned",
// "DRICHHits_aligned", "ForwardMPGDEndcapHits_aligned", "ForwardOffMTrackerHits_aligned",
// "ForwardRomanPotHits_aligned", "LumiSpecTrackerHits_aligned", "MPGDBarrelHits_aligned",
// "OuterMPGDBarrelHits_aligned", "RICHEndcapNHits_aligned", "SiBarrelHits_aligned",
// "TOFBarrelHits_aligned", "TOFEndcapHits_aligned", "TaggerTrackerHits_aligned",
// "TrackerEndcapHits_aligned", "VertexBarrelHits_aligned"};

// std::vector<std::string> m_simtrackerhit_collection_names_out = {
// "B0TrackerHits", "BackwardMPGDEndcapHits", "DIRCBarHits",
// "DRICHHits", "ForwardMPGDEndcapHits", "ForwardOffMTrackerHits",
// "ForwardRomanPotHits", "LumiSpecTrackerHits", "MPGDBarrelHits",
// "OuterMPGDBarrelHits", "RICHEndcapNHits", "SiBarrelHits",
// "TOFBarrelHits", "TOFEndcapHits", "TaggerTrackerHits",
// "TrackerEndcapHits", "VertexBarrelHits"};

std::vector<std::string> m_simtrackerhit_collection_names = {"SiBarrelHits_aligned"};
std::vector<std::string> m_simtrackerhit_collection_names_out = {"SiBarrelHits"};

// std::vector<std::string> m_simtrackerhit_collection_names = {"SiBarrelHits",
// "VertexBarrelHits"};
// std::vector<std::string> m_simtrackerhit_collection_names_out = {"SiBarrelHits",
// "VertexBarrelHits"};

std::vector<std::string> m_simcalorimeterhit_collection_names = {
"B0ECalHits", "EcalBarrelImagingHits", "EcalBarrelScFiHits", "EcalEndcapNHits",
"EcalEndcapPHits", "EcalEndcapPInsertHits", "EcalFarForwardZDCHits", "EcalLumiSpecHits",
"HcalBarrelHits", "HcalEndcapNHits", "HcalEndcapPInsertHits", "HcalFarForwardZDCHits",
"LFHCALHits", "LumiDirectPCALHits"};

std::vector<std::string> m_calohitcontribution_collection_names = {
"B0ECalHitsContributions",
"EcalBarrelImagingHitsContributions",
"EcalBarrelScFiHitsContributions",
"EcalEndcapNHitsContributions",
"EcalEndcapPHitsContributions",
"EcalEndcapPInsertHitsContributions",
"EcalLumiSpecHitsContributions",
"EcalFarForwardZDCHitsContributions",
"HcalBarrelHitsContributions",
"HcalEndcapNHitsContributions",
"HcalEndcapPInsertHitsContributions",
"HcalFarForwardZDCHitsContributions",
"LFHCALHitsContributions",
"LumiDirectPCALHitsContributions"};

PodioInput<edm4hep::EventHeader> m_event_header_in{this,
{.name = "EventHeader", .is_optional = true}};
PodioOutput<edm4hep::EventHeader> m_event_header_out{this, "EventHeader"};

PodioInput<edm4hep::MCParticle> m_mcparticles_in{this, {.name = "MCParticles"}};
PodioOutput<edm4hep::MCParticle> m_mcparticles_out{this, "MCParticles"};

VariadicPodioInput<edm4hep::SimTrackerHit> m_simtrackerhits_in{
this, {.names = m_simtrackerhit_collection_names, .is_optional = true}};
VariadicPodioOutput<edm4hep::SimTrackerHit> m_simtrackerhits_out{
this, m_simtrackerhit_collection_names_out};

VariadicPodioInput<edm4hep::SimCalorimeterHit> m_simcalorimeterhits_in{
this, {.names = m_simcalorimeterhit_collection_names, .is_optional = true}};
VariadicPodioOutput<edm4hep::SimCalorimeterHit> m_simcalorimeterhits_out{
this, m_simcalorimeterhit_collection_names};

VariadicPodioInput<edm4hep::CaloHitContribution> m_calohitcontributions_in{
this, {.names = m_calohitcontribution_collection_names, .is_optional = true}};
VariadicPodioOutput<edm4hep::CaloHitContribution> m_calohitcontributions_out{
this, m_calohitcontribution_collection_names};

TimeframeSplitter() {
SetTypeName(NAME_OF_THIS);
SetParentLevel(JEventLevel::Timeslice);
SetChildLevel(JEventLevel::PhysicsEvent);
}

std::vector<std::tuple<size_t, const edm4hep::SimTrackerHitCollection*, size_t>>
m_hitStartIndices_simTracker;
std::vector<std::tuple<size_t, const edm4hep::SimCalorimeterHitCollection*, size_t>>
m_hitStartIndices_simCalorimeter;

Result Unfold(const JEvent& parent, JEvent& child, int child_idx) override {
if (child_idx == 0) {

m_hitStartIndices_simTracker.clear();
m_hitStartIndices_simTracker.resize(m_simtrackerhits_in().size(),
std::make_tuple(0, nullptr, 0));
for (size_t detID = 0; detID < m_simtrackerhits_in().size(); ++detID) {
const auto* detHitPtr = m_simtrackerhits_in().at(detID);
if (detHitPtr == nullptr)
continue;
m_hitStartIndices_simTracker.emplace_back(detID, detHitPtr, 0);
}

m_hitStartIndices_simCalorimeter.clear();
m_hitStartIndices_simCalorimeter.resize(m_simcalorimeterhits_in().size(),
std::make_tuple(0, nullptr, 0));
for (size_t detID = 0; detID < m_simcalorimeterhits_in().size(); ++detID) {
const auto* detHitPtr = m_simcalorimeterhits_in().at(detID);
if (detHitPtr == nullptr)
continue;
m_hitStartIndices_simCalorimeter.emplace_back(detID, detHitPtr, 0);
}
}

LOG_INFO(GetLogger()) << "Running TimeframeSplitter::Unfold() on timeslice #"
<< parent.GetEventNumber() << LOG_END;

// == s == Internal Interval loop ========================================================-
bool bAdmitedInterval = false;
float iTimeSlice = m_timesplit_width * child_idx;
float eTimeSlice = m_timesplit_width * (child_idx + 1.0);
while (eTimeSlice <= m_timeframe_width) {
// LOG_INFO(GetLogger()) << "TimeframeSplitter: timeslice " << parent.GetEventNumber() << " timeStamp " << timeStamp << LOG_END;
iTimeSlice = m_timesplit_width * child_idx;
eTimeSlice = m_timesplit_width * (child_idx + 1.0);
// std::cout << "child_idx = " << child_idx << ":: TimeframeSplitter: timeslice "
// << parent.GetEventNumber() << " iTimeSlice " << iTimeSlice << " eTimeSlice "
// << eTimeSlice << std::endl;

std::vector<std::unique_ptr<edm4hep::SimTrackerHitCollection>> tempAllDetectorSimTrackerHits;
// Loop through SimTrackerHit collections and split them into time slice
for (auto& [detID, detHitPtr, start_index] : m_hitStartIndices_simTracker) {
if (detHitPtr == nullptr)
continue;

bool bAllScan =
true; // Scan all hits in the collection until we find a hit that is later than the end of the time slice
auto tempSimTrackerHits =
std::make_unique<edm4hep::SimTrackerHitCollection>(); // ??? Kuma test
tempSimTrackerHits->setSubsetCollection(true);

for (size_t hitID = start_index; hitID < detHitPtr->size(); ++hitID) {
const auto& hit = detHitPtr->at(hitID);
auto hitTime = hit.getTime();

if (hitTime >= eTimeSlice) {
start_index = hitID;
bAllScan = false;
// std::cout << "ChecKuma BreaKumaaaaaa!! : " << hitTime << " hitId: " << hitID << ", start_index: " << start_index << std::endl;
break;
}
tempSimTrackerHits->push_back(hit);
}
tempAllDetectorSimTrackerHits.push_back(std::move(tempSimTrackerHits));
if (bAllScan)
start_index = detHitPtr->size();
}
// Loop through SimTrackerHit collections and split them into time slice
for (auto& [detID, detHitPtr, start_index] : m_hitStartIndices_simCalorimeter) {
auto& coll_out = m_simcalorimeterhits_out().at(detID);
coll_out->setSubsetCollection(true);
if (detHitPtr == nullptr)
continue;
bool bAllScan = true;
for (size_t hitID = start_index; hitID < detHitPtr->size(); ++hitID) {
const auto& hit = detHitPtr->at(hitID);

auto hitContributions = hit.getContributions();
bool contributeFlag = false;
for (const auto& contribution : hitContributions) {
auto hitTime = contribution.getTime();
if (hitTime >= eTimeSlice) {
start_index = hitID;
bAllScan = false;
contributeFlag = true;
// std::cout << "ChecKuma BreaKumaaaaaa!! : " << hitTime << " hitId: " << hitID << ", start_index: " << start_index << std::endl;
break;
}

auto& coll_out_contribution = m_calohitcontributions_out().at(detID);
coll_out_contribution->setSubsetCollection(true);

coll_out_contribution->push_back(contribution);
}
if (contributeFlag)
break;

coll_out->push_back(hit);
}
if (bAllScan)
start_index = detHitPtr->size();
}

for (const auto& tempSimTrackerHits : tempAllDetectorSimTrackerHits) {
if (!tempSimTrackerHits->empty()) {
bAdmitedInterval = true;
break;
}
}

if (bAdmitedInterval) {
Int_t detID = 0;
for (auto& tempSimTrackerHits : tempAllDetectorSimTrackerHits) {

if (!tempSimTrackerHits->empty()) {
// std::cout << "ChecKuma: TimeframeSplitter: timeslice "
// << parent.GetEventNumber() << " child_idx " << child_idx
// << " detID " << detID << " has hits in time slice: "
// << tempSimTrackerHits->size() << std::endl;
auto& coll_out = m_simtrackerhits_out().at(detID);
coll_out->setSubsetCollection(true);

coll_out = std::move(tempSimTrackerHits);
detID++;
}
}

// For now, a one-to-one relationship between timeslices and events
child.SetEventNumber(parent.GetEventNumber());
child.SetRunNumber(parent.GetRunNumber());

// Insert an EventHeader object into the physics event
// For now this is just a ref to the timeslice header
m_event_header_out()->setSubsetCollection(true);
m_event_header_out()->push_back(m_event_header_in()->at(0));

// == s == Basic container for simulatin data (but not related to data) =========
// Insert MCParticles into the physics event
m_mcparticles_out()->setSubsetCollection(true);
for (const auto& mcparticle : *m_mcparticles_in) {
// TODO: Decide which of these belong to this physics event
m_mcparticles_out()->push_back(mcparticle);
}
// == e == Basic container for simulatin data (but not related to data) =========

break; // Break out of the interval loop if we have already admitted this interval
}

child_idx++;
}
// == s == Internal Interval loop ========================================================-

// Produce exactly one physics event per timeframe for now
// return JEventUnfolder::Result::NextChildNextParent;
if (eTimeSlice + 1 > m_timeframe_width && !bAdmitedInterval) {
// std::cout << "CheeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeecKuma child_idx: " << child_idx
// << ", eTimeSlice: " << eTimeSlice << ", m_timeframe_width: " << m_timeframe_width
// << std::endl;
return Result::KeepChildNextParent;
}
return (eTimeSlice + 1 > m_timeframe_width) ? Result::NextChildNextParent
: Result::NextChildKeepParent;
}
};
68 changes: 68 additions & 0 deletions src/global/splitting/splitting.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Copyright 2024, Jefferson Science Associates, LLC.
// Subject to the terms in the LICENSE file found in the top-level directory.
// kuma edit

#include "HitChecker.h"

#include "timeAlignmentFactory.h"
#include "TimeframeSplitter.h"

#include <JANA/Components/JOmniFactoryGeneratorT.h>
#include <JANA/JApplication.h>

extern "C" {
void InitPlugin(JApplication* app) {

// This is the plugin initialization function that JANA will call.
std::vector<std::string> m_simtrackerhit_collection_names_aligned = {
"B0TrackerHits_aligned", "BackwardMPGDEndcapHits_aligned",
"DIRCBarHits_aligned", "DRICHHits_aligned",
"ForwardMPGDEndcapHits_aligned", "ForwardOffMTrackerHits_aligned",
"ForwardRomanPotHits_aligned", "LumiSpecTrackerHits_aligned",
"MPGDBarrelHits_aligned", "OuterMPGDBarrelHits_aligned",
"RICHEndcapNHits_aligned", "SiBarrelHits_aligned",
"TOFBarrelHits_aligned", "TOFEndcapHits_aligned",
"TaggerTrackerHits_aligned", "TrackerEndcapHits_aligned",
"VertexBarrelHits_aligned"};

std::vector<std::vector<std::string>> m_simtrackerhit_collection_names = {
{"B0TrackerHits", "BackwardMPGDEndcapHits", "DIRCBarHits", "DRICHHits",
"ForwardMPGDEndcapHits", "ForwardOffMTrackerHits", "ForwardRomanPotHits",
"LumiSpecTrackerHits", "MPGDBarrelHits", "OuterMPGDBarrelHits", "RICHEndcapNHits",
"SiBarrelHits", "TOFBarrelHits", "TOFEndcapHits", "TaggerTrackerHits", "TrackerEndcapHits",
"VertexBarrelHits"}};

InitJANAPlugin(app);

// app->Add(new JOmniFactoryGeneratorT<timeAlignmentFactory>(
// { .tag = "timeAlignment",
// .level = JEventLevel::Timeslice,
// .input_names = {"VertexBarrelHits"},
// .output_names = {"VertexBarrelHits_aligned"}
// }));

app->Add(new JOmniFactoryGeneratorT<timeAlignmentFactory>(
jana::components::JOmniFactoryGeneratorT<timeAlignmentFactory>::TypedWiring{
.tag = "timeAlignment",
.level = JEventLevel::Timeslice,
.variadic_input_names = m_simtrackerhit_collection_names,
.output_names = m_simtrackerhit_collection_names_aligned}));

// Unfolder that takes timeframes and splits them into physics events.
app->Add(new TimeframeSplitter());

// app->Add(new JOmniFactoryGeneratorT<HitChecker>(jana::components::JOmniFactoryGeneratorT<HitChecker>::TypedWiring
// {.tag = "timeslice_hit_checker",
// .level = JEventLevel::Timeslice,
// .input_names = {"SiBarrelHits_aligned"},
// .output_names = {"ts_checked_hits"}
// }));

// app->Add(new JOmniFactoryGeneratorT<HitChecker>(jana::components::JOmniFactoryGeneratorT<HitChecker>::TypedWiring
// {.tag = "physics_hit_checker",
// .level = JEventLevel::PhysicsEvent,
// .input_names = {"SiBarrelHits"},
// .output_names = {"phys_checked_hits"}
// }));
}
} // "C"
Loading
Loading