File indexing completed on 2025-01-30 09:17:15
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015 #include <DD4hep/InstanceCount.h>
0016
0017 #include <DDDigi/DigiData.h>
0018 #include <DDDigi/DigiKernel.h>
0019 #include <DDDigi/DigiContext.h>
0020 #include <DDDigi/DigiStoreDump.h>
0021
0022 using namespace dd4hep::digi;
0023
0024
0025 DigiStoreDump::DigiStoreDump(const DigiKernel& krnl, const std::string& nam)
0026 : DigiEventAction(krnl, nam)
0027 {
0028 declareProperty("dump_history", m_dump_history = false);
0029 declareProperty("segments", m_segments);
0030 m_segments.push_back("input");
0031 m_segments.push_back("deposits");
0032 m_segments.push_back("data");
0033 m_segments.push_back("counts");
0034 m_segments.push_back("output");
0035 declareProperty("containers", m_containers);
0036 declareProperty("masks", m_masks);
0037 m_kernel.register_initialize(std::bind(&DigiStoreDump::initialize,this));
0038 InstanceCount::increment(this);
0039 }
0040
0041
0042 DigiStoreDump::~DigiStoreDump() {
0043 InstanceCount::decrement(this);
0044 }
0045
0046
0047 void DigiStoreDump::initialize() {
0048 m_container_items.clear();
0049 for( const auto& cont : m_containers ) {
0050 Key key(cont, 0x0);
0051 m_container_items.emplace_back(key.item());
0052 }
0053 }
0054
0055 template <typename T> std::string DigiStoreDump::data_header(Key key, const std::string& tag, const T& container) const {
0056 return this->format("%04X %04X %08X %-32s: %6ld %-12s [%s]",
0057 key.segment(), key.mask(), key.item(),
0058 ('"'+Key::key_name(key)+'"').c_str(), container.size(), tag.c_str(),
0059 digiTypeName(typeid(T)).c_str());
0060 }
0061
0062 template <> std::string DigiStoreDump::data_header(Key key, const std::string& tag, const std::type_info& info) const {
0063 std::string typ = digiTypeName(info);
0064 if ( tag.empty() ) {
0065 return this->format("%04X %04X %08X %-32s: %6s %s",
0066 key.segment(), key.mask(), key.item(),
0067 ('"'+Key::key_name(key)+'"').c_str(), "",
0068 typ.c_str());
0069 }
0070 return this->format("%04X %04X %08X %-32s: %-12s %s",
0071 key.segment(), key.mask(), key.item(),
0072 ('"'+Key::key_name(key)+'"').c_str(), tag.c_str(),
0073 typ.c_str());
0074 }
0075
0076 template <> std::string DigiStoreDump::data_header(Key key, const std::string& tag, const std::any& data) const {
0077 return data_header(std::move(key), tag, data.type());
0078 }
0079
0080 template <> std::vector<std::string>
0081 DigiStoreDump::dump_history(DigiContext& context,
0082 Key key,
0083 const std::pair<const ParticleMapping::key_type, Particle>& data,
0084 std::size_t seq_no) const
0085 {
0086 std::stringstream str;
0087 auto& ev = *(context.event);
0088 Key particle_key = data.first;
0089 std::vector<std::string> records;
0090 History::hist_entry_t hist { particle_key, 1.0 };
0091 const Particle& par = hist.get_particle(ev);
0092 const Direction& mom = par.momentum;
0093 const Position& vtx = par.start_position;
0094
0095 str << Key::key_name(key) << "[" << seq_no << "]:";
0096 std::string line =
0097 format("|---- %-18s Segment:%04X Mask:%04X Item:%04X %016lX",
0098 str.str().c_str(), particle_key.segment(), particle_key.mask(), particle_key.item(),
0099 long(&data.second));
0100 records.emplace_back(line);
0101 line = format("| PDG:%6d Charge:%-2d Mass:%7.2f v:%7.5g %7.5g %7.5g p:%12g %12g %12g %016lX",
0102 par.pdgID, int(par.charge), par.mass, vtx.X(), vtx.Y(), vtx.Z(), mom.X(), mom.Y(), mom.Z(), long(&par));
0103 records.emplace_back(line);
0104 return records;
0105 }
0106
0107 template <> std::vector<std::string>
0108 DigiStoreDump::dump_history(DigiContext& context,
0109 Key container_key,
0110 const std::pair<const DepositMapping::key_type, EnergyDeposit>& data,
0111 std::size_t seq_no) const
0112 {
0113 std::string line;
0114 std::stringstream str;
0115 auto& ev = *(context.event);
0116 const auto& item = data.second;
0117 const CellID cell = data.first;
0118 std::vector<std::string> records;
0119
0120 str << Key::key_name(container_key) << "[" << seq_no << "]:";
0121 line = format("+----- %-30s Container: Segment:%04X Mask:%04X Item:%08X Cell:%016lX History: Hits:%ld Parts:%ld",
0122 str.str().c_str(), int(container_key.segment()), int(container_key.mask()), int(container_key.item()),
0123 cell, item.history.hits.size(), item.history.particles.size());
0124 records.emplace_back(line);
0125 for( std::size_t i=0; i<item.history.hits.size(); ++i ) {
0126 const auto& entry = item.history.hits[i];
0127 const EnergyDeposit& dep = entry.get_deposit(ev, container_key.item());
0128 const Position& pos = dep.position;
0129 const Position& mom = dep.momentum;
0130 Key k = entry.source;
0131 str.str("");
0132 str << "| Hit-history[" << i << "]:";
0133 line = format("%-30s Segment:%04X Mask:%04X Cell:%08X Weight:%.8g",
0134 str.str().c_str(), k.segment(), k.mask(), k.item(), entry.weight);
0135 records.emplace_back(line);
0136 line = format("| pos: %7.3f %7.3f %7.3f p: %7.3f %7.3f %7.3f deposit: %7.3f",
0137 pos.X(), pos.Y(), pos.Z(), mom.X(), mom.Y(), mom.Z(), dep.deposit);
0138 records.emplace_back(line);
0139 }
0140 for( std::size_t i=0; i<item.history.particles.size(); ++i ) {
0141 const auto& ent = item.history.particles[i];
0142 const Particle& par = ent.get_particle(ev);
0143 const Direction& mom = par.momentum;
0144 const Position& vtx = par.start_position;
0145 Key key = ent.source;
0146 str.str("");
0147 str << "| Part-history[" << i << "]:";
0148 line = format("%-30s Segment:%04X Mask:%04X Key: %08X %.8g",
0149 str.str().c_str(), key.segment(), key.mask(), key.item(), ent.weight);
0150 records.emplace_back(line);
0151 line = format("| PDG:%6d Charge:%-2d Mass:%7.3f v:%7.3f %7.3f %7.3f p:%7.3f %7.3f %7.3f",
0152 par.pdgID, int(par.charge), par.mass, vtx.X(), vtx.Y(), vtx.Z(), mom.X(), mom.Y(), mom.Z());
0153 records.emplace_back(line);
0154 }
0155 return records;
0156 }
0157
0158 template <> std::vector<std::string>
0159 DigiStoreDump::dump_history(DigiContext& context,
0160 Key container_key,
0161 const std::pair<const CellID, History>& data,
0162 std::size_t seq_no) const
0163 {
0164 std::string line;
0165 std::stringstream str;
0166 auto& ev = *(context.event);
0167 const auto& history = data.second;
0168 const CellID cell = data.first;
0169 std::vector<std::string> records;
0170
0171 str << Key::key_name(container_key) << "[" << seq_no << "]:";
0172 line = format("+----- %-30s Container: Segment:%04X Mask:%04X Item:%08X Cell:%016lX Hist: Hits:%ld Parts:%ld",
0173 str.str().c_str(), container_key.segment(), container_key.mask(), container_key.item(),
0174 cell, history.hits.size(), history.particles.size());
0175 records.emplace_back(line);
0176 for( std::size_t i=0; i<history.hits.size(); ++i ) {
0177 const auto& entry = history.hits[i];
0178 const EnergyDeposit& dep = entry.get_deposit(ev, container_key.item());
0179 const Position& pos = dep.position;
0180 const Position& mom = dep.momentum;
0181 Key k = entry.source;
0182 str.str("");
0183 str << "| Hit-history[" << i << "]:";
0184 line = format("%-30s Segment:%04X Mask:%04X Cell:%08X Weight:%.8g",
0185 str.str().c_str(), k.segment(), k.mask(), k.item(), entry.weight);
0186 records.emplace_back(line);
0187 line = format("| pos: %7.3f %7.3f %7.3f p: %7.3f %7.3f %7.3f deposit: %7.3f",
0188 pos.X(), pos.Y(), pos.Z(), mom.X(), mom.Y(), mom.Z(), dep.deposit);
0189 records.emplace_back(line);
0190 }
0191 for( std::size_t i=0; i<history.particles.size(); ++i ) {
0192 const auto& ent = history.particles[i];
0193 const Particle& par = ent.get_particle(ev);
0194 const Direction& mom = par.momentum;
0195 const Position& vtx = par.start_position;
0196 Key key = ent.source;
0197 str.str("");
0198 str << "| Part-history[" << i << "]:";
0199 line = format("%-30s Segment:%04X Mask:%04X Key: %08X %.8g",
0200 str.str().c_str(), key.segment(), key.mask(), key.item(), ent.weight);
0201 records.emplace_back(line);
0202 line = format("| PDG:%6d Charge:%-2d Mass:%7.3f v:%7.3f %7.3f %7.3f p:%7.3f %7.3f %7.3f",
0203 par.pdgID, int(par.charge), par.mass, vtx.X(), vtx.Y(), vtx.Z(), mom.X(), mom.Y(), mom.Z());
0204 records.emplace_back(line);
0205 }
0206 return records;
0207 }
0208
0209 template <typename T> std::vector<std::string>
0210 DigiStoreDump::dump_history(DigiContext& context, Key container_key, const T& container) const {
0211 std::size_t count = 0;
0212 std::vector<std::string> records;
0213 for( const auto& item : container ) {
0214 auto rec = dump_history(context, container_key, item, count++);
0215 records.insert(records.end(), rec.begin(), rec.end());
0216 }
0217 return records;
0218 }
0219
0220 template <typename T> std::vector<std::string>
0221 DigiStoreDump::dump_deposit_history(DigiContext& context, Key container_key, const T& container) const {
0222 std::vector<std::string> records;
0223 auto line = format("|---- %s", data_header(container_key, "deposits", container).c_str());
0224 records.emplace_back(line);
0225 std::size_t count = 0;
0226 for( const auto& item : container ) {
0227 auto rec = dump_history(context, container_key, item, count++);
0228 records.insert(records.end(), rec.begin(), rec.end());
0229 }
0230 return records;
0231 }
0232 template std::vector<std::string>
0233 DigiStoreDump::dump_deposit_history(DigiContext& context, Key container_key, const DepositMapping& container) const;
0234
0235 template std::vector<std::string>
0236 DigiStoreDump::dump_deposit_history(DigiContext& context, Key container_key, const DepositVector& container) const;
0237
0238 std::vector<std::string>
0239 DigiStoreDump::dump_particle_history(DigiContext& context, Key container_key, const ParticleMapping& container) const {
0240 std::size_t count = 0;
0241 std::vector<std::string> records;
0242 auto line = format("|---- %s", data_header(container_key, "particles", container).c_str());
0243 records.emplace_back(line);
0244 for( const auto& item : container ) {
0245 auto rec = dump_history(context, container_key, item, count++);
0246 records.insert(records.end(), rec.begin(), rec.end());
0247 }
0248 return records;
0249 }
0250
0251
0252 void DigiStoreDump::dump_history(DigiContext& context,
0253 const std::string& tag,
0254 const DigiEvent& event,
0255 const DataSegment& segment) const
0256 {
0257 std::vector<std::string> records;
0258 std::lock_guard<std::mutex> lock(segment.lock);
0259
0260 records.emplace_back(format("+--- %-12s segment: %ld entries", tag.c_str(), segment.size()));
0261 for ( const auto& entry : segment ) {
0262 Key key {entry.first};
0263 const std::any& data = entry.second;
0264 bool use = m_containers.empty() ||
0265 std::find(m_container_items.begin(), m_container_items.end(), key.item()) != m_container_items.end();
0266 use &= m_masks.empty() ||
0267 std::find(m_masks.begin(), m_masks.end(), key.mask()) != m_masks.end();
0268
0269 if ( use ) {
0270 std::vector<std::string> rec;
0271 if ( const auto* mapping = std::any_cast<DepositMapping>(&data) ) {
0272 rec = dump_deposit_history(context, std::move(key), *mapping);
0273 }
0274 else if ( const auto* vector = std::any_cast<DepositVector>(&data) ) {
0275 rec = dump_deposit_history(context, std::move(key), *vector);
0276 }
0277 else if ( const auto* parts = std::any_cast<ParticleMapping>(&data) ) {
0278 rec = dump_particle_history(context, std::move(key), *parts);
0279 }
0280 else if ( const auto* adcs = std::any_cast<DetectorResponse>(&data) ) {
0281 rec = { format("|---- %s", data_header(std::move(key), "ADC values", *adcs).c_str()) };
0282 }
0283 else if ( const auto* hist = std::any_cast<DetectorHistory>(&data) ) {
0284 rec = { format("|---- %s", data_header(std::move(key), "histories", *hist).c_str()) };
0285 }
0286 else {
0287 rec = { format("|---- %s", data_header(std::move(key), "", data).c_str()) };
0288 }
0289 records.insert(records.end(), rec.begin(), rec.end());
0290 }
0291 }
0292 std::lock_guard<std::mutex> record_lock(m_kernel.global_output_lock());
0293 for(const auto& s : records)
0294 info("%s%s", event.id(), s.c_str());
0295 }
0296
0297
0298 void DigiStoreDump::dump_headers(const std::string& tag,
0299 const DigiEvent& event,
0300 const DataSegment& segment) const
0301 {
0302 int first = 1;
0303 std::string str;
0304 std::vector<std::string> records;
0305 std::lock_guard<std::mutex> lock(segment.lock);
0306 records.push_back(format("+--- %-12s segment: %ld entries", tag.c_str(), segment.size()));
0307 records.push_back(format("| Segt Mask Item-id Item-name"));
0308 for ( const auto& entry : segment ) {
0309 Key key {entry.first};
0310 const std::any& data = entry.second;
0311 if ( const auto* mapping = std::any_cast<DepositMapping>(&data) )
0312 str = "| " + data_header(std::move(key), "deposits", *mapping);
0313 else if ( const auto* vector = std::any_cast<DepositVector>(&data) )
0314 str = "| " + data_header(std::move(key), "deposits", *vector);
0315 else if ( const auto* parts = std::any_cast<ParticleMapping>(&data) )
0316 str = "| " + data_header(std::move(key), "particles", *parts);
0317 else if ( const auto* adcs = std::any_cast<DetectorResponse>(&data) )
0318 str = "| " + data_header(std::move(key), "ADC values", *adcs);
0319 else if ( const auto* hist = std::any_cast<DetectorHistory>(&data) )
0320 str = "| " + data_header(std::move(key), "histories", *hist);
0321 else if ( data.type() == typeid(void) )
0322 str = "| " + data_header(std::move(key), "void data", data);
0323 else
0324 str = "| " + data_header(std::move(key), "", data);
0325 if ( first ) {
0326 first = false;
0327 }
0328 records.push_back(str);
0329 }
0330 if ( records.size() == 2 ) records.pop_back();
0331 std::lock_guard<std::mutex> record_lock(m_kernel.global_output_lock());
0332 for(const auto& s : records)
0333 info("%s|---- %s", event.id(), s.c_str());
0334 }
0335
0336
0337 void DigiStoreDump::execute(DigiContext& context) const {
0338 const auto& event = context.event;
0339 for( const auto& segment : this->m_segments ) {
0340 std::string seg = detail::str_upper(segment);
0341 if ( m_dump_history ) {
0342 dump_history(context, seg, *event, event->get_segment(segment));
0343 continue;
0344 }
0345 dump_headers(seg, *event, event->get_segment(segment));
0346 }
0347 }
0348