Belle II Software  release-05-02-19
AsicBackgroundLibraryCreator.cc
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2016 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Sasha Glazov, tracking group *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 #include <tracking/trackFindingCDC/findlets/complete/AsicBackgroundLibraryCreator.h>
11 #include <tracking/trackFindingCDC/eventdata/hits/CDCWireHit.h>
12 #include <tracking/trackFindingCDC/eventdata/tracks/CDCTrack.h>
13 #include <tracking/trackFindingCDC/eventdata/trajectories/CDCTrajectory2D.h>
14 #include <tracking/trackFindingCDC/eventdata/trajectories/CDCTrajectorySZ.h>
15 #include <tracking/trackFindingCDC/topology/CDCWire.h>
16 #include <tracking/trackFindingCDC/utilities/StringManipulation.h>
17 #include <framework/core/ModuleParamList.templateDetails.h>
18 #include <cdc/dataobjects/CDCHit.h>
19 #include <framework/logging/Logger.h>
20 #include <iostream>
21 #include <TTree.h>
22 #include <TFile.h>
23 #include <TBranch.h>
24 using namespace Belle2;
25 using namespace TrackFindingCDC;
26 using std::vector;
27 using std::map;
28 using std::pair;
29 using std::sort;
30 using std::min;
31 using std::max;
32 
34 
35 float getDist2D(const TrackFindingCDC::CDCTrajectory3D& trajectory, const TrackFindingCDC::CDCWireHit* wireHit)
36 {
37  const TrackFindingCDC::CDCTrajectory2D& trajectory2D = trajectory.getTrajectory2D();
38  const TrackFindingCDC::CDCTrajectorySZ& trajectorySZ = trajectory.getTrajectorySZ();
39  TrackFindingCDC::Vector2D recoPos2D;
40  if (wireHit->isAxial()) {
41  recoPos2D = wireHit->reconstruct2D(trajectory2D);
42  } else {
43  const TrackFindingCDC::CDCWire& wire = wireHit->getWire();
44  const TrackFindingCDC::Vector2D& posOnXYPlane = wireHit->reconstruct2D(trajectory2D);
45  const double arcLength = trajectory2D.calcArcLength2D(posOnXYPlane);
46  const double z = trajectorySZ.mapSToZ(arcLength);
47  const TrackFindingCDC::Vector2D& wirePos2DAtZ = wire.getWirePos2DAtZ(z);
48  const TrackFindingCDC::Vector2D& recoPosOnTrajectory = trajectory2D.getClosest(wirePos2DAtZ);
49  const double driftLength = wireHit->getRefDriftLength();
50  TrackFindingCDC::Vector2D disp2D = recoPosOnTrajectory - wirePos2DAtZ;
51  disp2D.normalizeTo(driftLength);
52  recoPos2D = wirePos2DAtZ + disp2D;
53  }
54  const float distanceToHit = trajectory2D.getDist2D(recoPos2D);
55  return abs(distanceToHit);
56 }
57 
59 {
61  // database:
62  m_channelMapFromDB = std::make_unique<DBArray<CDCChannelMap>> ();
63 
64  if ((*m_channelMapFromDB).isValid()) {
65  B2DEBUG(29, "CDC Channel map is valid");
66  } else {
67  B2FATAL("CDC Channel map is not valid");
68  }
69 
70  // Library for writing
71 
72  auto leavesCreator = [this](TTree & tree) {
73  if (m_write_extra_vars) {
74  tree.Branch("Dist", &m_dist_signal, "DistSig/f:DistBg/f");
75  tree.Branch("ADC", &m_adc_sig, "ADC_Sig/S:ADC_bg/S");
76  tree.Branch("Track", &m_n_hit_track, "tr_nhit/S");
77  }
78  tree.Branch("Board", &m_board, "board/b");
79  tree.Branch("Channel", &m_channel, "channel/b");
80  tree.Branch("Nhit", &m_n_hit, "nhit/b");
81  tree.Branch("Asic", &m_asic_info[0],
82  "TDC0/S:ADC0/S:TOT0/S:TDC1/S:ADC1/S:TOT1/S:TDC2/S:ADC2/S:TOT2/S:TDC3/S:ADC3/S:TOT3/S:TDC4/S:ADC4/S:TOT4/S:TDC5/S:ADC5/S:TOT5/S:TDC6/S:ADC6/S:TOT6/S:TDC7/S:ADC7/S:TOT7/S");
83  return;
84  };
85 
86  m_recorder.reset(new Recorder(
87  leavesCreator
89  , "ASIC"));
90 }
91 
93 {
95  // Load map from DB:
96  for (const auto& cm : (*m_channelMapFromDB)) {
97  const int isl = cm.getISuperLayer();
98  const int il = cm.getILayer();
99  const int iw = cm.getIWire();
100  const int iBoard = cm.getBoardID();
101  const int iCh = cm.getBoardChannel();
102  const WireID wireId(isl, il, iw);
103  m_map[wireId.getEWire()] = std::pair<int, int>(iBoard, iCh);
104  }
105 }
106 
108 {
109  return "Finds suitable ASICs with a single hit attached to a track and uses them to create the library";
110 }
111 
112 void AsicBackgroundLibraryCreator::apply(const std::vector<CDCWireHit>& wireHits, const std::vector<CDCTrack>& tracks)
113 {
114 
115  map< pair<int, int>, vector<const CDCWireHit*>> groupedByAsic;
116  for (const CDCWireHit& wireHit : wireHits) {
117  auto eWire = wireHit.getWireID().getEWire();
118  B2ASSERT("Channel map NOT found for the channel", m_map.count(eWire) > 0);
119  auto board = m_map[eWire].first;
120  auto channel = m_map[eWire].second;
121  auto asicID = pair<int, int>(board, channel / 8); // ASIC are groups of 8 channels
122  groupedByAsic[asicID].push_back(&wireHit);
123  };
124  for (auto& [asicID, asicList] : groupedByAsic) {
125  selectAsic(asicList, tracks);
126  };
127 
128  return;
129 }
130 
131 void AsicBackgroundLibraryCreator::exposeParameters(ModuleParamList* moduleParamList, const std::string& prefix)
132 {
133  Super::exposeParameters(moduleParamList, prefix);
134  moduleParamList->addParameter(TrackFindingCDC::prefixed(prefix, "minimalHitNumberASIC"),
136  "Required number of hits per ASIC for library creation",
138 
139  moduleParamList->addParameter(TrackFindingCDC::prefixed(prefix, "AsicLibraryFileName"),
141  "ASIC library file name",
143 
144  moduleParamList->addParameter(TrackFindingCDC::prefixed(prefix, "maximalDistanceSignal"),
146  "maximal distance in cm from track to signal hit",
148 
149  moduleParamList->addParameter(TrackFindingCDC::prefixed(prefix, "minimalDistanceBackground"),
151  "minimal distance in cm from track to background hit",
153 
154  moduleParamList->addParameter(TrackFindingCDC::prefixed(prefix, "useAxialHitsOnly"),
156  "use axial layers only",
158 
159  moduleParamList->addParameter(TrackFindingCDC::prefixed(prefix, "writeExtraVars"),
161  "Write extra variables to the library",
163 
164  moduleParamList->addParameter(TrackFindingCDC::prefixed(prefix, "minimalHitsOnTrack"),
166  "Required number of hits on track for library creation",
168 
169  // set some defaults:
170  moduleParamList->getParameter<std::string>("inputTracks").setDefaultValue("CDCTrackVector");
171  moduleParamList->getParameter<std::string>("inputWireHits").setDefaultValue("CDCWireHitVector");
172 }
173 
175 {
176  m_recorder->write();
178 }
179 
180 void AsicBackgroundLibraryCreator::selectAsic(const std::vector<const CDCWireHit*>& wireHits, const std::vector<CDCTrack>& tracks)
181 {
182 
183  if (wireHits.size() < m_minimal_hit_number) {
184  return;
185  };
186 
187  if (wireHits.size() > 8) {
188  B2ERROR("Number of hits per asic should not exceed 8, observe too many hits." << LogVar("nHits", wireHits.size()));
190  return;
191  }
192 
193 
194  // count taken non-background hits:
195  int count = 0;
196  const CDCWireHit* signal = nullptr;
197  for (auto& hit : wireHits) {
198  if (!(*hit)->hasBackgroundFlag() && (*hit)->hasTakenFlag()) {
199  count += 1;
200  signal = hit;
201  }
202  }
203 
204  // require one and only one taken hit
205  if (count != 1) {
206  return;
207  }
208 
209 
210  // check if only axial hits are used:
211 
212  if ((!signal->isAxial()) && m_use_axial_hits_only) {
213  return;
214  }
215 
216  // find the track to which this hit belongs
217  const CDCTrack* signalTrack = nullptr;
218  for (auto& track : tracks) {
219  for (auto& hit : track) {
220  if (&hit.getWireHit() == signal) {
221  signalTrack = &track;
222  break;
223  }
224  }
225  if (signalTrack != nullptr) {
226  break;
227  }
228  }
229 
230  if (signalTrack == nullptr) {
231  B2DEBUG(29, "AsicBackgroundLibraryCreator::No track found for the signal hit");
232  return;
233  }
234 
235  m_n_hit_track = signalTrack->size();
236 
238 
239  // check the distance from the track to each signal hit
240  const auto& trajectory = signalTrack->getStartTrajectory3D();
241 
242  m_dist_bg = 1000.;
243 
244  for (auto& hit : wireHits) {
245 
246  const float dist = getDist2D(trajectory, hit);
247 
248 
249  if (hit == signal) {
250  m_dist_signal = dist;
251  if (dist > m_distance_signal_max) return;
252  } else {
253  m_dist_bg = min(m_dist_bg, dist);
254  if (dist < m_distance_background_min) return;
255  }
256  }
257 
258 
259  // Ok, passes all cuts !
260 
261  // reset the library entries
262  for (auto& channel : m_asic_info) {
263  channel.TDC = -1;
264  channel.ADC = -1;
265  channel.TOT = -1;
266  }
267 
268  // add to the library
269 
270  m_n_hit = wireHits.size();
271 
272  m_adc_max_bg = 0;
273  for (auto& hit : wireHits) {
274  auto eWire = hit->getWireID().getEWire();
275  auto channel = m_map[eWire].second;
276  auto asicCH = channel % 8;
277  m_asic_info[asicCH].ADC = hit->getHit()->getADCCount();
278  m_asic_info[asicCH].TDC = hit->getHit()->getTDCCount();
279  m_asic_info[asicCH].TOT = hit->getHit()->getTOT();
280 
281  if (hit != signal) {
282  m_adc_max_bg = max(m_adc_max_bg, m_asic_info[asicCH].ADC);
283  }
284  }
285  // also signal hit info
286  auto eWire = signal->getWireID().getEWire();
287  m_channel = m_map[eWire].second;
288  m_board = m_map[eWire].first;
289  m_adc_sig = signal->getHit()->getADCCount();
290 
291  // make sure that ADC of the signal is >= than ADC of the background:
292  if (m_adc_sig < m_adc_max_bg) return;
293 
294  // write out
295  m_recorder->capture();
296 }
Belle2::TrackFindingCDC::AsicBackgroundLibraryCreator::m_minimal_hits_on_track
size_t m_minimal_hits_on_track
min. number of hits on the track
Definition: AsicBackgroundLibraryCreator.h:97
Belle2::WireID
Class to identify a wire inside the CDC.
Definition: WireID.h:44
Belle2::TrackFindingCDC::AsicBackgroundLibraryCreator::exposeParameters
void exposeParameters(ModuleParamList *moduleParamList, const std::string &prefix) final
Expose the parameters to a module.
Definition: AsicBackgroundLibraryCreator.cc:131
Belle2::TrackFindingCDC::CDCWireHit::getWireID
const WireID & getWireID() const
Getter for the WireID of the wire the hit is located on.
Definition: CDCWireHit.h:193
Belle2::TrackFindingCDC::CDCTrack
Class representing a sequence of three dimensional reconstructed hits.
Definition: CDCTrack.h:51
Belle2::TrackFindingCDC::Vector2D::normalizeTo
double normalizeTo(const double toLength)
Normalizes the vector to the given length.
Definition: Vector2D.h:327
Belle2::TrackFindingCDC::AsicBackgroundLibraryCreator::m_n_hit_track
UShort_t m_n_hit_track
Number of hits on the track.
Definition: AsicBackgroundLibraryCreator.h:131
Belle2::TrackFindingCDC::Vector2D
A two dimensional vector which is equipped with functions for correct handeling of orientation relat...
Definition: Vector2D.h:37
Belle2::WireID::getEWire
unsigned short getEWire() const
Getter for encoded wire number.
Definition: WireID.h:164
Belle2::TrackFindingCDC::AsicBackgroundLibraryCreator::m_adc_max_bg
Short_t m_adc_max_bg
Max. ADC background.
Definition: AsicBackgroundLibraryCreator.h:128
Belle2::TrackFindingCDC::AsicBackgroundLibraryCreator::m_library_name
std::string m_library_name
output library name
Definition: AsicBackgroundLibraryCreator.h:78
Belle2::TrackFindingCDC::AsicBackgroundLibraryCreator::m_distance_background_min
double m_distance_background_min
minimal distance from track to background hit
Definition: AsicBackgroundLibraryCreator.h:87
Belle2::ModuleParamList::getParameter
ModuleParam< T > & getParameter(const std::string &name) const
Returns a reference to a parameter.
Definition: ModuleParamList.templateDetails.h:90
Belle2::TrackFindingCDC::AsicBackgroundLibraryCreator::m_distance_signal_max
double m_distance_signal_max
maximal distance from track to signal hit
Definition: AsicBackgroundLibraryCreator.h:84
Belle2::TrackFindingCDC::AsicBackgroundLibraryCreator::m_channelMapFromDB
std::unique_ptr< DBArray< CDCChannelMap > > m_channelMapFromDB
Channel map retrieved from DB.
Definition: AsicBackgroundLibraryCreator.h:72
Belle2::TrackFindingCDC::AsicBackgroundLibraryCreator::m_asic_info
asicChannel m_asic_info[8]
all 8 channels
Definition: AsicBackgroundLibraryCreator.h:105
Belle2::TrackFindingCDC::Recorder
Class to fill a tree from a set of variables.
Definition: Recorder.h:31
Belle2::TrackFindingCDC::CDCTrajectorySZ
Linear trajectory in sz space.
Definition: CDCTrajectorySZ.h:41
Belle2::TrackFindingCDC::CDCTrajectory2D::getClosest
Vector2D getClosest(const Vector2D &point) const
Calculates the closest approach on the trajectory to the given point.
Definition: CDCTrajectory2D.cc:173
Belle2::asicChannel::ADC
Short_t ADC
ADC info
Definition: CDCCrossTalkClasses.h:25
Belle2::TrackFindingCDC::CDCTrajectory2D
Particle trajectory as it is seen in xy projection represented as a circle.
Definition: CDCTrajectory2D.h:46
Belle2::TrackFindingCDC::CDCWire::getWirePos2DAtZ
Vector2D getWirePos2DAtZ(const double z) const
Gives the xy projected position of the wire at the given z coordinate.
Definition: CDCWire.h:194
Belle2::TrackFindingCDC::CompositeProcessingSignalListener::initialize
void initialize() override
Receive and dispatch signal before the start of the event processing.
Definition: CompositeProcessingSignalListener.cc:17
Belle2::TrackFindingCDC::CDCTrajectorySZ::mapSToZ
double mapSToZ(const double s=0) const
Translates the travel distance to the z coordinate.
Definition: CDCTrajectorySZ.h:65
Belle2::TrackFindingCDC::AsicBackgroundLibraryCreator::beginRun
void beginRun() final
Reload channel map if needed.
Definition: AsicBackgroundLibraryCreator.cc:92
Belle2::TrackFindingCDC::AsicBackgroundLibraryCreator::m_channel
UChar_t m_channel
signal channelID
Definition: AsicBackgroundLibraryCreator.h:111
Belle2::TrackFindingCDC::AsicBackgroundLibraryCreator::m_adc_sig
Short_t m_adc_sig
ADC of the signal.
Definition: AsicBackgroundLibraryCreator.h:125
Belle2::TrackFindingCDC::CDCTrajectory3D::getTrajectory2D
CDCTrajectory2D getTrajectory2D() const
Getter for the two dimensional trajectory.
Definition: CDCTrajectory3D.cc:336
Belle2::TrackFindingCDC::CDCTrajectory3D::getTrajectorySZ
CDCTrajectorySZ getTrajectorySZ() const
Getter for the sz trajectory.
Definition: CDCTrajectory3D.cc:343
Belle2::ModuleParamList::addParameter
void addParameter(const std::string &name, T &paramVariable, const std::string &description, const T &defaultValue)
Adds a new parameter to the module list.
Definition: ModuleParamList.templateDetails.h:38
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::TrackFindingCDC::Findlet< const CDCWireHit, const CDCTrack >::exposeParameters
virtual void exposeParameters(ModuleParamList *moduleParamList __attribute__((unused)), const std::string &prefix __attribute__((unused)))
Forward prefixed parameters of this findlet to the module parameter list.
Definition: Findlet.h:79
Belle2::TrackFindingCDC::CompositeProcessingSignalListener::beginRun
void beginRun() override
Receive and dispatch signal for the beginning of a new run.
Definition: CompositeProcessingSignalListener.cc:25
Belle2::asicChannel::TOT
Short_t TOT
Time over threshold.
Definition: CDCCrossTalkClasses.h:26
LogVar
Class to store variables with their name which were sent to the logging service.
Definition: LogVariableStream.h:24
Belle2::TrackFindingCDC::AsicBackgroundLibraryCreator::m_write_extra_vars
bool m_write_extra_vars
extra vars to the library
Definition: AsicBackgroundLibraryCreator.h:93
Belle2::TrackFindingCDC::AsicBackgroundLibraryCreator::m_dist_bg
float m_dist_bg
min. distance to non-linked hits
Definition: AsicBackgroundLibraryCreator.h:122
Belle2::TrackFindingCDC::AsicBackgroundLibraryCreator::terminate
void terminate() final
write out the library
Definition: AsicBackgroundLibraryCreator.cc:174
Belle2::TrackFindingCDC::AsicBackgroundLibraryCreator::m_use_axial_hits_only
bool m_use_axial_hits_only
use axial layers only
Definition: AsicBackgroundLibraryCreator.h:90
Belle2::TrackFindingCDC::CompositeProcessingSignalListener::terminate
void terminate() override
Receive and dispatch Signal for termination of the event processing.
Definition: CompositeProcessingSignalListener.cc:49
Belle2::TrackFindingCDC::AsicBackgroundLibraryCreator::m_recorder
std::unique_ptr< Recorder > m_recorder
Recorder for the root output.
Definition: AsicBackgroundLibraryCreator.h:100
Belle2::TrackFindingCDC::AsicBackgroundLibraryCreator::m_n_hit
UChar_t m_n_hit
For debuging, store also number of channels with hits.
Definition: AsicBackgroundLibraryCreator.h:114
Belle2::TrackFindingCDC::AsicBackgroundLibraryCreator::m_dist_signal
float m_dist_signal
Distance to signal hit.
Definition: AsicBackgroundLibraryCreator.h:119
Belle2::TrackFindingCDC::AsicBackgroundLibraryCreator::m_board
UChar_t m_board
signal boardID
Definition: AsicBackgroundLibraryCreator.h:108
Belle2::TrackFindingCDC::AsicBackgroundLibraryCreator::selectAsic
void selectAsic(const std::vector< const CDCWireHit * > &wireHits, const std::vector< CDCTrack > &tracks)
Algorithm to select suitable ASIC for library creation.
Definition: AsicBackgroundLibraryCreator.cc:180
Belle2::TrackFindingCDC::AsicBackgroundLibraryCreator::getDescription
std::string getDescription() final
Short description of the findlet.
Definition: AsicBackgroundLibraryCreator.cc:107
Belle2::TrackFindingCDC::CDCWire
Class representing a sense wire in the central drift chamber.
Definition: CDCWire.h:60
Belle2::TrackFindingCDC::CDCTrajectory2D::getDist2D
double getDist2D(const Vector2D &point) const
Calculates the distance from the point to the trajectory as seen from the xy projection.
Definition: CDCTrajectory2D.h:419
Belle2::TrackFindingCDC::AsicBackgroundLibraryCreator::m_map
std::map< int, std::pair< int, int > > m_map
map from ewire to board/channel ID
Definition: AsicBackgroundLibraryCreator.h:75
Belle2::TrackFindingCDC::CDCWireHit
Class representing a hit wire in the central drift chamber.
Definition: CDCWireHit.h:65
Belle2::ModuleParamList
The Module parameter list class.
Definition: ModuleParamList.h:46
Belle2::TrackFindingCDC::AsicBackgroundLibraryCreator::m_minimal_hit_number
size_t m_minimal_hit_number
min. number of hits in ASIC for background check
Definition: AsicBackgroundLibraryCreator.h:81
Belle2::TrackFindingCDC::CDCTrajectory2D::calcArcLength2D
double calcArcLength2D(const Vector2D &point) const
Calculate the travel distance from the start position of the trajectory.
Definition: CDCTrajectory2D.h:270
Belle2::TrackFindingCDC::AsicBackgroundLibraryCreator::apply
void apply(const std::vector< CDCWireHit > &wireHits, const std::vector< CDCTrack > &tracks) final
Main algorithm marking hit as background.
Definition: AsicBackgroundLibraryCreator.cc:112
Belle2::asicChannel::TDC
Short_t TDC
TDC info
Definition: CDCCrossTalkClasses.h:24
Belle2::TrackFindingCDC::CDCTrajectory3D
Particle full three dimensional trajectory.
Definition: CDCTrajectory3D.h:47
Belle2::TrackFindingCDC::AsicBackgroundLibraryCreator::initialize
void initialize() final
Access database here, open library for writing:
Definition: AsicBackgroundLibraryCreator.cc:58