Belle II Software development
AsicBackgroundDetector.cc
1/**************************************************************************
2 * basf2 (Belle II Analysis Software Framework) *
3 * Author: The Belle II Collaboration *
4 * *
5 * See git log for contributors and copyright holders. *
6 * This file is licensed under LGPL-3.0, see LICENSE.md. *
7 **************************************************************************/
8#include <tracking/trackFindingCDC/findlets/minimal/AsicBackgroundDetector.h>
9#include <tracking/trackingUtilities/eventdata/hits/CDCWireHit.h>
10#include <tracking/trackingUtilities/utilities/StringManipulation.h>
11#include <framework/core/ModuleParamList.templateDetails.h>
12#include <cdc/dataobjects/CDCHit.h>
13#include <framework/logging/Logger.h>
14#include <iostream>
15
16using namespace Belle2;
17using namespace TrackFindingCDC;
18using namespace TrackingUtilities;
19
21{
23 // database:
24 m_channelMapFromDB = std::make_unique<DBArray<CDCChannelMap>> ();
25
26 if ((*m_channelMapFromDB).isValid()) {
27 B2DEBUG(25, "CDC Channel map is valid");
28 } else {
29 B2FATAL("CDC Channel map is not valid");
30 }
31}
32
34{
36
37 // Load map from DB:
38 for (const auto& cm : (*m_channelMapFromDB)) {
39 const int isl = cm.getISuperLayer();
40 const int il = cm.getILayer();
41 const int iw = cm.getIWire();
42 const int iBoard = cm.getBoardID();
43 const int iCh = cm.getBoardChannel();
44 const WireID wireId(isl, il, iw);
45 m_map[wireId.getEWire()] = std::pair<int, int>(iBoard, iCh);
46 }
47}
48
50{
51 return "Marks hits as background using ASIC-based filter.";
52}
53
54void AsicBackgroundDetector::apply(std::vector<CDCWireHit>& wireHits)
55{
56
57 std::map< std::pair<int, int>, std::vector<CDCWireHit*>> groupedByAsic;
58 for (CDCWireHit& wireHit : wireHits) {
59 auto eWire = wireHit.getWireID().getEWire();
60 B2ASSERT("Channel map NOT found for the channel", m_map.count(eWire) > 0);
61 auto board = m_map[eWire].first;
62 auto channel = m_map[eWire].second;
63 auto asicID = std::pair<int, int>(board, channel / 8); // ASIC are groups of 8 channels
64 groupedByAsic[asicID].push_back(&wireHit);
65 };
66 for (auto& asicList : groupedByAsic) {
67 applyAsicFilter(asicList.second);
68 };
69
70 return;
71}
72
73void AsicBackgroundDetector::exposeParameters(ModuleParamList* moduleParamList, const std::string& prefix)
74{
75 Super::exposeParameters(moduleParamList, prefix);
76 moduleParamList->addParameter(prefixed(prefix, "minimalHitNumberASIC"),
78 "Required number of hits per ASIC for background check",
80 moduleParamList->addParameter(prefixed(prefix, "deviationFromMedianTDCinASIC"),
82 "Flag hits as cross talk if TDC does not deviate from median more than this value",
84 moduleParamList->addParameter(prefixed(prefix, "minimalNSignalASIC"),
86 "Flag background hits only when ASIC contains no more than this number of signal hits",
88}
89
90void AsicBackgroundDetector::applyAsicFilter(std::vector<CDCWireHit*>& wireHits)
91{
92
93 if (wireHits.size() < m_minimal_hit_number) {
94 return;
95 };
96
97 if (wireHits.size() > 8) {
99 auto eWire = wireHits[0]->getWireID().getEWire();
100 auto board = m_map[eWire].first;
101
103 B2ERROR("Number of hits per asic should not exceed 8, observe too many hits."
104 << LogVar("nHits", wireHits.size())
105 << LogVar("Board ID", board));
107 } else {
108 B2WARNING("Number of hits per asic should not exceed 8, observe too many hits."
109 << LogVar("nHits", wireHits.size())
110 << LogVar("Board ID", board));
111 }
112
114 for (auto& hit : wireHits) {
115 (*hit)->setBackgroundFlag();
116 (*hit)->setTakenFlag();
117 }
118 return;
119 }
120
121 // compute median time:
122 std::vector<short> times;
123 for (auto& hit : wireHits) {
124 short tdc = hit->getHit()->getTDCCount();
125 times.push_back(tdc);
126 }
127 std::sort(times.begin(), times.end());
128 int mid = times.size() / 2;
129 double median = times.size() % 2 == 0 ? (times[mid] + times[mid - 1]) / 2 : times[mid];
130
131 size_t nbg = 0;
132 int adcOnMedian = 0;
133 int adcOffMedian = 0;
134 for (auto& hit : wireHits) {
135 int adc = hit->getHit()->getADCCount();
136 if (fabs(hit->getHit()->getTDCCount() - median) < m_deviation_from_median) {
137 nbg++;
138 if (adc > adcOnMedian) adcOnMedian = adc;
139 } else {
140 if (adc > adcOffMedian) adcOffMedian = adc;
141 }
142 }
143
144 if ((nbg < times.size()) // at least 1 hit with different TDC ("signal")
145 && (nbg > 1) // more than one candidate hits
146 && (nbg > times.size() - m_nsignal_max) // a few background hits
147 && (adcOnMedian < adcOffMedian) // triggered by large ADC "signal"
148 ) {
149
150 // mark hits too close to the median time as background:
151 for (auto& hit : wireHits) {
152 if (fabs(hit->getHit()->getTDCCount() - median) < m_deviation_from_median) {
153 (*hit)->setBackgroundFlag();
154 (*hit)->setTakenFlag();
155 }
156 }
157 }
158}
The Module parameter list class.
std::unique_ptr< DBArray< CDCChannelMap > > m_channelMapFromDB
Channel map retrieved from DB.
double m_deviation_from_median
distance from median TDC, to be considered as bg.
void beginRun() final
Reload channel map if needed.
size_t m_nsignal_max
max. number of signal-like hits in ASIC for background check
std::map< int, std::pair< int, int > > m_map
map from ewire to board/channel ID
std::string getDescription() final
Short description of the findlet.
size_t m_max_asic_error_messages
max. number of logged error messages for number of hits per ASIC check
void applyAsicFilter(std::vector< TrackingUtilities::CDCWireHit * > &wireHits)
Algorithm marking hit as background for each CDC ASIC.
virtual void apply(std::vector< TrackingUtilities::CDCWireHit > &wireHits) final
Main algorithm marking hit as background.
virtual void exposeParameters(ModuleParamList *moduleParamList, const std::string &prefix) final
Expose the parameters to a module.
size_t m_minimal_hit_number
min. number of hits in ASIC for background check
Class representing a hit wire in the central drift chamber.
Definition CDCWireHit.h:58
virtual void exposeParameters(ModuleParamList *moduleParamList, const std::string &prefix)
Class to identify a wire inside the CDC.
Definition WireID.h:34
unsigned short getEWire() const
Getter for encoded wire number.
Definition WireID.h:154
Class to store variables with their name which were sent to the logging service.
void addParameter(const std::string &name, T &paramVariable, const std::string &description, const T &defaultValue)
Adds a new parameter to the module list.
Abstract base class for different kinds of events.