Belle II Software development
ECLCRFinderModule.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
9/* Own header. */
10#include <ecl/modules/eclCRFinder/ECLCRFinderModule.h>
11
12/* ECL headeers. */
13#include <ecl/dataobjects/ECLCalDigit.h>
14#include <ecl/dataobjects/ECLConnectedRegion.h>
15#include <ecl/geometry/ECLNeighbours.h>
16
17/* Basf2 headers. */
18#include <framework/gearbox/Unit.h>
19#include <framework/logging/Logger.h>
20#include <mdst/dataobjects/EventLevelClusteringInfo.h>
21
22/* C++ headers. */
23#include <algorithm>
24#include <iostream>
25#include <array>
26
27// NAMESPACE(S)
28using namespace Belle2;
29
30//-----------------------------------------------------------------
31// Register the Module
32//-----------------------------------------------------------------
33REG_MODULE(ECLCRFinder);
34REG_MODULE(ECLCRFinderPureCsI);
35
36//-----------------------------------------------------------------
37// Implementation
38//-----------------------------------------------------------------
41{
42 // Set description
43 setDescription("ECLCRFinderModule");
44
45 // Parallel processing certification.
47
48 // Add module parameters.
49 addParam("energyCut0", m_energyCut[0], "Seed energy cut.", 20.0 * Belle2::Unit::MeV);
50 addParam("energyCut1", m_energyCut[1], "Growth energy cut.", 20.0 * Belle2::Unit::MeV);
51 addParam("energyCut2", m_energyCut[2], "Digit energy cut.", 0.5 * Belle2::Unit::MeV);
52 addParam("timeCut0", m_timeCut[0], "Seed time cut (negative values for residual cut).", 400.);
53 addParam("timeCut1", m_timeCut[1], "Growth time cut (negative values for residual cut).", 400.);
54 addParam("timeCut2", m_timeCut[2], "Digit time cut (negative values for residual cut).", 99999.);
55 addParam("timeCut0maxEnergy", m_timeCut_maxEnergy[0], "Time cut is only applied below this energy for seed crystals.",
56 99999.0 * Belle2::Unit::MeV);
57 addParam("timeCut1maxEnergy", m_timeCut_maxEnergy[1], "Time cut is only applied below this energy for growth crystals.",
58 99999.0 * Belle2::Unit::MeV);
59 addParam("timeCut2maxEnergy", m_timeCut_maxEnergy[2], "Time cut is only applied below this energy for digits.",
60 0.0 * Belle2::Unit::MeV);
61 addParam("mapType0", m_mapType[0], "Map type for seed crystals.", std::string("N"));
62 addParam("mapType1", m_mapType[1], "Map type for growth crystals.", std::string("N"));
63 addParam("mapPar0", m_mapPar[0],
64 "Map parameter for seed crystals (radius (type=R), integer (for type=N) or fraction (for type=MC)).", 1.0);
65 addParam("mapPar1", m_mapPar[1],
66 "Map parameter for growth crystals (radius (type=R), integer (for type=N) or fraction (for type=MC)).", 1.0);
67 addParam("skipFailedTimeFitDigits", m_skipFailedTimeFitDigits, "Digits with failed fits are skipped when checking timing cuts.", 0);
68 addParam("useParametersFromDatabase", m_useParametersFromDatabase, "get energy and time cuts from payload", true);
69
70}
71
76
78{
79 B2DEBUG(200, "ECLCRFinderModule::initialize()");
80
81 // Register dataobjects.
85
86 // Register relations.
87 m_eclConnectedRegions.registerRelationTo(m_eclCalDigits);
88
89 // Initialize neighbour maps.
90 m_neighbourMaps.resize(2);
93
94 // Resize the vectors
95 m_cellIdToCheckVec.resize(8737);
96 m_cellIdToSeedVec.resize(8737);
97 m_cellIdToGrowthVec.resize(8737);
98 m_cellIdToDigitVec.resize(8737);
99 m_cellIdToTempCRIdVec.resize(8737);
100 m_calDigitStoreArrPosition.resize(8737);
101
102}
103
104//-----------------------------------------------------------------
105//..By default, get the energy and time thresholds from the
106// eclClusteringParameters dbOject
108{
110 std::array<double, 3> CRF_energyCut = m_eclClusteringParameters->getCRFEnergyCut();
111 m_energyCut[0] = CRF_energyCut[0];
112 m_energyCut[1] = CRF_energyCut[1];
113 m_energyCut[2] = CRF_energyCut[2];
114
115 std::array<double, 3> CRF_timeCut = m_eclClusteringParameters->getCRFTimeCut();
116 m_timeCut[0] = CRF_timeCut[0];
117 m_timeCut[1] = CRF_timeCut[1];
118 m_timeCut[2] = CRF_timeCut[2];
119
120 std::array<double, 3> CRF_timeCutMaxEnergy = m_eclClusteringParameters->getCRFTimeCutMaxEnergy();
121 m_timeCut_maxEnergy[0] = CRF_timeCutMaxEnergy[0];
122 m_timeCut_maxEnergy[1] = CRF_timeCutMaxEnergy[1];
123 m_timeCut_maxEnergy[2] = CRF_timeCutMaxEnergy[2];
124 }
125
126 // Check user inputs: [2]: digit, [1]: growth, [0]: seed
127 // overall energy thresholds
128 if (std::isless(m_energyCut[0], m_energyCut[1])) B2FATAL("ECLCRFinderModule::beginRun(): m_energyCut[0]=" << m_energyCut[0] <<
129 " must be larger or equal than m_energyCut[1]=" << m_energyCut[1]);
130 if (std::isless(m_energyCut[1], m_energyCut[2])) B2FATAL("ECLCRFinderModule::beginRun(): m_energyCut[1]=" << m_energyCut[1] <<
131 " must be larger or equal than m_energyCut[2]=" << m_energyCut[2]);
132 // timing threshold (can depend on energy, but we make the check here even stronger by checking that the timing is looser without checking the timing energy range)
133 if (std::isgreater(m_timeCut[0], m_timeCut[1]))
134 B2FATAL("ECLCRFinderModule::beginRun(): m_timeCut[0] must be less or equal than m_timeCut[1].");
135 if (std::isgreater(m_timeCut[1], m_timeCut[2]))
136 B2FATAL("ECLCRFinderModule::beginRun(): m_timeCut[1] must be less or equal than m_timeCut[2].");
137}
138
139
140//-----------------------------------------------------------------
141
143{
144 B2DEBUG(200, "ECLCRFinderModule::event()");
145
146 // Reset the vector(s).
147 std::fill(m_cellIdToCheckVec.begin(), m_cellIdToCheckVec.end(), 0);
148 std::fill(m_cellIdToSeedVec.begin(), m_cellIdToSeedVec.end(), 0);
149 std::fill(m_cellIdToGrowthVec.begin(), m_cellIdToGrowthVec.end(), 0);
150 std::fill(m_cellIdToDigitVec.begin(), m_cellIdToDigitVec.end(), 0);
151 std::fill(m_cellIdToTempCRIdVec.begin(), m_cellIdToTempCRIdVec.end(), 0);
152
153 // Fill a vector that can be used to map cellid -> store array position
154 std::fill(m_calDigitStoreArrPosition.begin(), m_calDigitStoreArrPosition.end(), -1);
155 for (int i = 0; i < m_eclCalDigits.getEntries(); i++) {
156 m_calDigitStoreArrPosition[m_eclCalDigits[i]->getCellId()] = i;
157 }
158
159 // Clear the map(s).
160 m_cellIdToTempCRIdMap.clear();
161
162 //-------------------------------------------------------
163 // fill digits into maps
164 for (const auto& eclCalDigit : m_eclCalDigits) {
165 const double energy = eclCalDigit.getEnergy();
166 const double time = eclCalDigit.getTime();
167 const double timeresolution = eclCalDigit.getTimeResolution();
168 const int cellid = eclCalDigit.getCellId();
169 const bool fitfailed = eclCalDigit.isFailedFit();
170
171 double timeresidual = 999.;
172 if (!fitfailed and fabs(timeresolution) > 1e-9) {
173 timeresidual = time / timeresolution;
174 }
175
176 // Negative timecut is interpreted as cut on time residual, positive cut as cut on the time!
177 // Start filling all crystals to a map. Growth and seed crystals are strict subsets.
178 if (std::isgreaterequal(energy, m_energyCut[2])) {
179 if (fitfailed > 0 and m_skipFailedTimeFitDigits > 0) continue;
180 if (!fitfailed
181 and energy < m_timeCut_maxEnergy[2]) { //check timing cuts only if we have a good fit and if the energy is below threshold
182 if (m_timeCut[2] > 1e-9 and fabs(time) > m_timeCut[2]) continue;
183 if (m_timeCut[2] < -1e-9 and fabs(timeresidual) > fabs(m_timeCut[2])) continue;
184 }
185 m_cellIdToDigitVec[cellid] = 1;
186 B2DEBUG(250, "ECLCRFinderModule::event(), adding 'all digit' cellid = " << cellid << " " << energy << " " << time << " " <<
187 timeresidual);
188
189 // check growth only if they already passed the digit check
190 if (std::isgreaterequal(energy, m_energyCut[1])) {
191 if (!fitfailed
192 and energy < m_timeCut_maxEnergy[1]) { //check timing cuts only if we have a good fit and if the energy is below threshold
193 if (m_timeCut[1] > 1e-9 and fabs(time) > m_timeCut[1]) continue;
194 if (m_timeCut[1] < -1e-9 and fabs(timeresidual) > fabs(m_timeCut[1])) continue;
195 }
196 m_cellIdToGrowthVec[cellid] = 1;
197 B2DEBUG(250, "ECLCRFinderModule::event(), adding 'growth digit' cellid = " << cellid << " " << energy << " " << time << " " <<
198 timeresidual);
199
200
201 // check seed only if they already passed the growth check
202 if (std::isgreaterequal(energy, m_energyCut[0])) {
203 if (!fitfailed
204 and energy < m_timeCut_maxEnergy[0]) { //check timing cuts only if we have a good fit and if the energy is below threshold
205 if (m_timeCut[0] > 1e-9 and fabs(time) > m_timeCut[0]) continue;
206 if (m_timeCut[0] < -1e-9 and fabs(timeresidual) > fabs(m_timeCut[0])) continue;
207 }
208 m_cellIdToSeedVec[cellid] = 1;
209 B2DEBUG(250, "ECLCRFinderModule::event(), adding 'seed digit' cellid = " << cellid << " " << energy << " " << time << " " <<
210 timeresidual);
211 } // end seed
212 } //end growth
213 }// end digit
214 }//end filling maps
215
216 // we start with seed crystals A and attach all growth crystals B
217 std::vector<std::vector<int>> connectedRegions_AB = getConnectedRegions(m_cellIdToSeedVec, m_cellIdToGrowthVec, 0);
218 std::vector<int> connectedRegions_AB_flattened = flattenVector(connectedRegions_AB);
219 std::vector<int> AB = oneHotVector(connectedRegions_AB_flattened, m_cellIdToSeedVec.size());
220
221 // Check if any of the growth crystals could grow to other growth crystals
222 std::vector<std::vector<int>> connectedRegions_ABB = getConnectedRegions(AB, m_cellIdToGrowthVec, 0);
223 std::vector<int> connectedRegions_ABB_flattened = flattenVector(connectedRegions_ABB);
224 std::vector<int> ABB = oneHotVector(connectedRegions_ABB_flattened, AB.size());
225
226 // and finally: attach all normal digits
227 std::vector<std::vector<int>> connectedRegions_ABBC = getConnectedRegions(ABB, m_cellIdToDigitVec, 0);
228
229 //final step: merge all CRs that share at least one crystal
230 std::vector<std::set<int>> connectedRegionsMerged_ABBC_sets = mergeVectorsUsingSets(connectedRegions_ABBC);
231
232 // Create CRs and add relations to digits.
233 unsigned int connectedRegionID = 0;
234 for (const auto& xcr : connectedRegionsMerged_ABBC_sets) {
235
236 // Append to store array
237 const auto aCR = m_eclConnectedRegions.appendNew();
238
239 // Set CR ID
240 aCR->setCRId(connectedRegionID);
241 connectedRegionID++;
242
243 // Add all digits
244 for (int x : xcr) {
245 const int pos = m_calDigitStoreArrPosition[x];
246 aCR->addRelationTo(m_eclCalDigits[pos], 1.0);
247 }
248 }
249
250}
251
253{
254 B2DEBUG(200, "ECLCRFinderModule::endRun()");
255}
256
257
259{
260 B2DEBUG(200, "ECLCRFinderModule::terminate()");
261 for (unsigned int i = 0; i < m_neighbourMaps.size(); i++) {
262 if (m_neighbourMaps[i]) delete m_neighbourMaps[i];
263 }
264
265}
266
267bool ECLCRFinderModule::areNeighbours(const int cellid1, const int cellid2, const int maptype)
268{
269 for (const auto& neighbour : m_neighbourMaps[maptype]->getNeighbours(cellid1)) {
270 if (neighbour == cellid2) return true;
271 }
272 return false;
273}
274
275std::vector<int> ECLCRFinderModule::flattenVector(std::vector<std::vector<int>>& A)
276{
277 std::vector<int> C;
278 for (const auto& B : A) {
279 C.insert(C.end(), B.begin(), B.end());
280 }
281 std::sort(C.begin(), C.end());
282 C.erase(std::unique(C.begin(), C.end()), C.end());
283 return C;
284}
285
286std::vector<int> ECLCRFinderModule::oneHotVector(std::vector<int>& A, const int n)
287{
288 std::vector<int> C(n, 0);
289 for (int x : A) {
290 if (x >= 0 && x < n) {
291 C[x] = 1;
292 }
293 }
294 return C;
295}
296
297std::vector<std::set<int>> ECLCRFinderModule::mergeVectorsUsingSets(std::vector<std::vector<int>>& A)
298{
299
300 // Make empty list of sets "output"
301 std::vector< std::set<int> > output;
302
303 for (auto& vec : A) {
304 std::set<int> s(vec.begin(), vec.end());
305
306 //Check whether element intersects with any in output
307 for (auto it = output.begin(); it != output.end();) {
308 std::set<int> intersect;
309 std::set_intersection(it->begin(), it->end(), s.begin(), s.end(),
310 std::inserter(intersect, intersect.begin()));
311
312 if (!intersect.empty()) {
313 s.insert(it->begin(), it->end());
314 it = output.erase(it);
315 } else ++it;
316 }
317 output.push_back(s);
318 }
319
320 return output;
321}
322
323std::vector<std::vector<int>> ECLCRFinderModule::getConnectedRegions(const std::vector<int>& A, const std::vector<int>& B,
324 const int maptype)
325{
326 std::vector<std::vector<int>> connectedRegions;
327
328 for (unsigned int i = 0; i < A.size(); ++i) {
329 if (A[i] > 0) {
330 std::vector<int> region;
331 region.push_back(i);
332
333 for (unsigned int j = 0; j < B.size(); ++j) {
334 if (B[j] > 0 && areNeighbours(i, j, maptype)) {
335 region.push_back(j);
336 }
337 }
338
339 std::sort(region.begin(), region.end());
340 region.erase(unique(region.begin(), region.end()), region.end());
341 connectedRegions.push_back(region);
342 }
343 }
344
345 return connectedRegions;
346}
double m_mapPar[2]
Parameters for neighbour maps.
virtual const char * eventLevelClusteringInfoName() const
Name to be used for default option: EventLevelClusteringInfo.
bool areNeighbours(const int cellid1, const int cellid2, const int maptype)
Check if two crystals are neighbours.
std::vector< int > m_cellIdToDigitVec
cellid -> above threshold digits.
virtual ~ECLCRFinderModule()
Destructor.
StoreArray< ECLConnectedRegion > m_eclConnectedRegions
Store array: ECLConnectedRegion.
double m_timeCut[3]
Time cut for seed, neighbours, ...
std::vector< int > oneHotVector(std::vector< int > &A, const int n)
Convert vector of cell ids to 0/1 vectors from 1-8737.
std::vector< int > m_cellIdToGrowthVec
cellid -> growth digits.
virtual void initialize() override
Initialize.
std::map< int, int > m_cellIdToTempCRIdMap
cellid -> temporary CR.
double m_energyCut[3]
Energy cut for seed, neighbours, ...
std::vector< int > m_cellIdToCheckVec
Digit vectors.
virtual void event() override
Event.
std::vector< int > m_cellIdToSeedVec
cellid -> seed digit.
virtual void endRun() override
End run.
std::vector< int > flattenVector(std::vector< std::vector< int > > &A)
Convert vector of vectors to one long vector.
virtual void terminate() override
Terminate (close ROOT files here if you have opened any).
std::vector< int > m_cellIdToTempCRIdVec
Connected Region map.
int m_skipFailedTimeFitDigits
Handling of digits with failed time fits.
std::vector< int > m_calDigitStoreArrPosition
vector (ECLElementNumbers::c_NCrystals + 1 entries) with cell id to store array positions
virtual void beginRun() override
Begin.
std::vector< std::vector< int > > getConnectedRegions(const std::vector< int > &A, const std::vector< int > &B, const int maptype)
Get all connected regions.
std::vector< std::set< int > > mergeVectorsUsingSets(std::vector< std::vector< int > > &A)
Find all lists of cell-ids that share at least one cell.
DBObjPtr< ECLClusteringParameters > m_eclClusteringParameters
ECLClusteringParameters payload for parameters.
bool m_useParametersFromDatabase
get energy and time cuts from payload
virtual const char * eclConnectedRegionArrayName() const
Name to be used for default option: ECLConnectedRegions.
std::vector< ECL::ECLNeighbours * > m_neighbourMaps
Neighbour maps.
virtual const char * eclCalDigitArrayName() const
Name to be used for default or PureCsI option: ECLCalDigits.
std::string m_mapType[2]
Neighbour map types.
double m_timeCut_maxEnergy[3]
Time cut is only applied below this energy, ...
StoreObjPtr< EventLevelClusteringInfo > m_eventLevelClusteringInfo
Store object pointer: EventLevelClusteringInfo.
StoreArray< ECLCalDigit > m_eclCalDigits
Store array: ECLCalDigit.
Class to get the neighbours for a given cell id.
void setDescription(const std::string &description)
Sets the description of the module.
Definition Module.cc:214
void setPropertyFlags(unsigned int propertyFlags)
Sets the flags for the module properties.
Definition Module.cc:208
Module()
Constructor.
Definition Module.cc:30
@ c_ParallelProcessingCertified
This module can be run in parallel processing mode safely (All I/O must be done through the data stor...
Definition Module.h:80
static const double MeV
[megaelectronvolt]
Definition Unit.h:114
void addParam(const std::string &name, T &paramVariable, const std::string &description, const T &defaultValue)
Adds a new parameter to the module.
Definition Module.h:559
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Definition Module.h:649
int intersect(const TRGCDCLpar &lp1, const TRGCDCLpar &lp2, CLHEP::HepVector &v1, CLHEP::HepVector &v2)
intersection
Definition Lpar.cc:249
Abstract base class for different kinds of events.