Belle II Software  release-08-00-10
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 // THIS MODULE
10 #include <ecl/modules/eclCRFinder/ECLCRFinderModule.h>
11 
12 // FRAMEWORK
13 #include <framework/core/Environment.h>
14 #include <framework/gearbox/Unit.h>
15 #include <framework/logging/LogConfig.h>
16 #include <framework/logging/Logger.h>
17 
18 //ECL
19 #include <ecl/geometry/ECLNeighbours.h>
20 #include <ecl/dataobjects/ECLCalDigit.h>
21 #include <ecl/dataobjects/ECLConnectedRegion.h>
22 
23 // MDST
24 #include <mdst/dataobjects/EventLevelClusteringInfo.h>
25 
26 // C++
27 #include <algorithm>
28 #include <iostream>
29 
30 // NAMESPACE(S)
31 using namespace Belle2;
32 
33 //-----------------------------------------------------------------
34 // Register the Module
35 //-----------------------------------------------------------------
36 REG_MODULE(ECLCRFinder);
37 REG_MODULE(ECLCRFinderPureCsI);
38 
39 //-----------------------------------------------------------------
40 // Implementation
41 //-----------------------------------------------------------------
42 ECLCRFinderModule::ECLCRFinderModule() : Module(), m_eclCalDigits(eclCalDigitArrayName()),
43  m_eclConnectedRegions(eclConnectedRegionArrayName()), m_eventLevelClusteringInfo(eventLevelClusteringInfoName())
44 {
45  // Set description
46  setDescription("ECLCRFinderModule");
47 
48  // Parallel processing certification.
50 
51  // Add module parameters.
52  addParam("energyCut0", m_energyCut[0], "Seed energy cut.", 10.0 * Belle2::Unit::MeV);
53  addParam("energyCut1", m_energyCut[1], "Growth energy cut.", 10.0 * Belle2::Unit::MeV);
54  addParam("energyCut2", m_energyCut[2], "Digit energy cut.", 0.5 * Belle2::Unit::MeV);
55  addParam("timeCut0", m_timeCut[0], "Seed time cut (negative values for residual cut).", 99999.);
56  addParam("timeCut1", m_timeCut[1], "Growth time cut (negative values for residual cut).", 99999.);
57  addParam("timeCut2", m_timeCut[2], "Digit time cut (negative values for residual cut).", 99999.);
58  addParam("timeCut0maxEnergy", m_timeCut_maxEnergy[0], "Time cut is only applied below this energy for seed crystals.",
59  0.0 * Belle2::Unit::MeV);
60  addParam("timeCut1maxEnergy", m_timeCut_maxEnergy[1], "Time cut is only applied below this energy for growth crystals.",
61  0.0 * Belle2::Unit::MeV);
62  addParam("timeCut2maxEnergy", m_timeCut_maxEnergy[2], "Time cut is only applied below this energy for digits.",
63  0.0 * Belle2::Unit::MeV);
64  addParam("mapType0", m_mapType[0], "Map type for seed crystals.", std::string("N"));
65  addParam("mapType1", m_mapType[1], "Map type for growth crystals.", std::string("N"));
66  addParam("mapPar0", m_mapPar[0],
67  "Map parameter for seed crystals (radius (type=R), integer (for type=N) or fraction (for type=MC)).", 1.0);
68  addParam("mapPar1", m_mapPar[1],
69  "Map parameter for growth crystals (radius (type=R), integer (for type=N) or fraction (for type=MC)).", 1.0);
70  addParam("skipFailedTimeFitDigits", m_skipFailedTimeFitDigits, "Digits with failed fits are skipped when checking timing cuts.", 0);
71 
72 }
73 
75 {
76  ;
77 }
78 
80 {
81  B2DEBUG(200, "ECLCRFinderModule::initialize()");
82 
83  // Register dataobjects.
84  m_eclCalDigits.registerInDataStore(eclCalDigitArrayName());
87 
88  // Register relations.
89  m_eclConnectedRegions.registerRelationTo(m_eclCalDigits);
90 
91  // Check user inputs: [2]: digit, [1]: growth, [0]: seed
92  // overall energy thresholds
93  if (std::isless(m_energyCut[0], m_energyCut[1])) B2FATAL("ECLCRFinderModule::initialize(): m_energyCut[0]=" << m_energyCut[0] <<
94  " must be larger or equal than m_energyCut[1]=" << m_energyCut[1]);
95  if (std::isless(m_energyCut[1], m_energyCut[2])) B2FATAL("ECLCRFinderModule::initialize(): m_energyCut[1]=" << m_energyCut[1] <<
96  " must be larger or equal than m_energyCut[2]=" << m_energyCut[2]);
97 
98  // 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)
99  if (std::isgreater(m_timeCut[0], m_timeCut[1]))
100  B2FATAL("ECLCRFinderModule::initialize(): m_timeCut[0] must be less or equal than m_timeCut[1].");
101  if (std::isgreater(m_timeCut[1], m_timeCut[2]))
102  B2FATAL("ECLCRFinderModule::initialize(): m_timeCut[1] must be less or equal than m_timeCut[2].");
103 
104  // Initialize neighbour maps.
105  m_neighbourMaps.resize(2);
108 
109  // Resize the vectors
110  m_cellIdToCheckVec.resize(8737);
111  m_cellIdToSeedVec.resize(8737);
112  m_cellIdToGrowthVec.resize(8737);
113  m_cellIdToDigitVec.resize(8737);
114  m_cellIdToTempCRIdVec.resize(8737);
115  m_calDigitStoreArrPosition.resize(8737);
116 
117 }
118 
120 {
121 
122 }
123 
125 {
126  B2DEBUG(200, "ECLCRFinderModule::event()");
127 
128  // Reset the vector(s).
129  std::fill(m_cellIdToCheckVec.begin(), m_cellIdToCheckVec.end(), 0);
130  std::fill(m_cellIdToSeedVec.begin(), m_cellIdToSeedVec.end(), 0);
131  std::fill(m_cellIdToGrowthVec.begin(), m_cellIdToGrowthVec.end(), 0);
132  std::fill(m_cellIdToDigitVec.begin(), m_cellIdToDigitVec.end(), 0);
133  std::fill(m_cellIdToTempCRIdVec.begin(), m_cellIdToTempCRIdVec.end(), 0);
134 
135  // Fill a vector that can be used to map cellid -> store array position
136  std::fill(m_calDigitStoreArrPosition.begin(), m_calDigitStoreArrPosition.end(), -1);
137  for (int i = 0; i < m_eclCalDigits.getEntries(); i++) {
138  m_calDigitStoreArrPosition[m_eclCalDigits[i]->getCellId()] = i;
139  }
140 
141  // Clear the map(s).
142  m_cellIdToTempCRIdMap.clear();
143 
144  //-------------------------------------------------------
145  // fill digits into maps
146  for (const auto& eclCalDigit : m_eclCalDigits) {
147  const double energy = eclCalDigit.getEnergy();
148  const double time = eclCalDigit.getTime();
149  const double timeresolution = eclCalDigit.getTimeResolution();
150  const int cellid = eclCalDigit.getCellId();
151  const bool fitfailed = eclCalDigit.isFailedFit();
152 
153  double timeresidual = 999.;
154  if (!fitfailed and fabs(timeresolution) > 1e-9) {
155  timeresidual = time / timeresolution;
156  }
157 
158  // Negative timecut is interpreted as cut on time residual, positive cut as cut on the timeresolution!
159  // Start filling all crystals to a map. Growth and seed crystals are strict subsets.
160  if (std::isgreaterequal(energy, m_energyCut[2])) {
161  if (fitfailed > 0 and m_skipFailedTimeFitDigits > 0) continue;
162  if (!fitfailed
163  and energy < m_timeCut_maxEnergy[2]) { //check timing cuts only if we have a good fit and if the energy is below threshold
164  if (m_timeCut[2] > 1e-9 and fabs(timeresolution) > m_timeCut[2]) continue;
165  if (m_timeCut[2] < -1e-9 and fabs(timeresidual) > fabs(m_timeCut[2])) continue;
166  }
167  m_cellIdToDigitVec[cellid] = 1;
168  B2DEBUG(250, "ECLCRFinderModule::event(), adding 'all digit' with cellid = " << cellid);
169 
170  // check growth only if they already passed the digit check
171  if (std::isgreaterequal(energy, m_energyCut[1])) {
172  if (!fitfailed
173  and energy < m_timeCut_maxEnergy[1]) { //check timing cuts only if we have a good fit and if the energy is below threshold
174  if (m_timeCut[1] > 1e-9 and fabs(timeresolution) > m_timeCut[1]) continue;
175  if (m_timeCut[1] < -1e-9 and fabs(timeresidual) > fabs(m_timeCut[1])) continue;
176  }
177  m_cellIdToGrowthVec[cellid] = 1;
178  B2DEBUG(250, "ECLCRFinderModule::event(), adding 'growth digit' with cellid = " << cellid);
179 
180  // check seed only if they already passed the growth check
181  if (std::isgreaterequal(energy, m_energyCut[0])) {
182  if (!fitfailed
183  and energy < m_timeCut_maxEnergy[0]) { //check timing cuts only if we have a good fit and if the energy is below threshold
184  if (m_timeCut[0] > 1e-9 and fabs(timeresolution) > m_timeCut[0]) continue;
185  if (m_timeCut[0] < -1e-9 and fabs(timeresidual) > fabs(m_timeCut[0])) continue;
186  }
187  }
188  m_cellIdToSeedVec[cellid] = 1;
189  B2DEBUG(250, "ECLCRFinderModule::event(), adding 'seed digit' with cellid = " << cellid);
190  } //end growth
191  }// end digit
192  }//end filling maps
193 
194  // we start with seed crystals A and attach all growth crystals B
195  std::vector<std::vector<int>> connectedRegions_AB = getConnectedRegions(m_cellIdToSeedVec, m_cellIdToGrowthVec, 0);
196  std::vector<int> connectedRegions_AB_flattened = flattenVector(connectedRegions_AB);
197  std::vector<int> AB = oneHotVector(connectedRegions_AB_flattened, m_cellIdToSeedVec.size());
198 
199  // Check if any of the growth crystals could grow to other growth crystals
200  std::vector<std::vector<int>> connectedRegions_ABB = getConnectedRegions(AB, m_cellIdToGrowthVec, 0);
201  std::vector<int> connectedRegions_ABB_flattened = flattenVector(connectedRegions_ABB);
202  std::vector<int> ABB = oneHotVector(connectedRegions_ABB_flattened, AB.size());
203 
204  // and finally: attach all normal digits
205  std::vector<std::vector<int>> connectedRegions_ABBC = getConnectedRegions(ABB, m_cellIdToDigitVec, 0);
206 
207  //final step: merge all CRs that share at least one crystal
208  std::vector<std::set<int>> connectedRegionsMerged_ABBC_sets = mergeVectorsUsingSets(connectedRegions_ABBC);
209 
210  // Create CRs and add relations to digits.
211  unsigned int connectedRegionID = 0;
212  for (const auto& xcr : connectedRegionsMerged_ABBC_sets) {
213 
214  // Append to store array
215  const auto aCR = m_eclConnectedRegions.appendNew();
216 
217  // Set CR ID
218  aCR->setCRId(connectedRegionID);
219  connectedRegionID++;
220 
221  // Add all digits
222  for (int x : xcr) {
223  const int pos = m_calDigitStoreArrPosition[x];
224  aCR->addRelationTo(m_eclCalDigits[pos], 1.0);
225  }
226  }
227 
228 }
229 
231 {
232  B2DEBUG(200, "ECLCRFinderModule::endRun()");
233 }
234 
235 
237 {
238  B2DEBUG(200, "ECLCRFinderModule::terminate()");
239  for (unsigned int i = 0; i < m_neighbourMaps.size(); i++) {
240  if (m_neighbourMaps[i]) delete m_neighbourMaps[i];
241  }
242 
243 }
244 
245 bool ECLCRFinderModule::areNeighbours(const int cellid1, const int cellid2, const int maptype)
246 {
247  for (const auto& neighbour : m_neighbourMaps[maptype]->getNeighbours(cellid1)) {
248  if (neighbour == cellid2) return true;
249  }
250  return false;
251 }
252 
253 std::vector<int> ECLCRFinderModule::flattenVector(std::vector<std::vector<int>>& A)
254 {
255  std::vector<int> C;
256  for (const auto& B : A) {
257  C.insert(C.end(), B.begin(), B.end());
258  }
259  std::sort(C.begin(), C.end());
260  C.erase(std::unique(C.begin(), C.end()), C.end());
261  return C;
262 }
263 
264 std::vector<int> ECLCRFinderModule::oneHotVector(std::vector<int>& A, const int n)
265 {
266  std::vector<int> C(n, 0);
267  for (int x : A) {
268  if (x >= 0 && x < n) {
269  C[x] = 1;
270  }
271  }
272  return C;
273 }
274 
275 std::vector<std::set<int>> ECLCRFinderModule::mergeVectorsUsingSets(std::vector<std::vector<int>>& A)
276 {
277 
278  // Make empty list of sets "output"
279  std::vector< std::set<int> > output;
280 
281  for (auto& vec : A) {
282  std::set<int> s(vec.begin(), vec.end());
283 
284  //Check whether element intersects with any in output
285  for (auto it = output.begin(); it != output.end();) {
286  std::set<int> intersect;
287  std::set_intersection(it->begin(), it->end(), s.begin(), s.end(),
288  std::inserter(intersect, intersect.begin()));
289 
290  if (!intersect.empty()) {
291  s.insert(it->begin(), it->end());
292  it = output.erase(it);
293  } else ++it;
294  }
295  output.push_back(s);
296  }
297 
298  return output;
299 }
300 
301 std::vector<std::vector<int>> ECLCRFinderModule::getConnectedRegions(const std::vector<int>& A, const std::vector<int>& B,
302  const int maptype)
303 {
304  std::vector<std::vector<int>> connectedRegions;
305 
306  for (unsigned int i = 0; i < A.size(); ++i) {
307  if (A[i] > 0) {
308  std::vector<int> region;
309  region.push_back(i);
310 
311  for (unsigned int j = 0; j < B.size(); ++j) {
312  if (B[j] > 0 && areNeighbours(i, j, maptype)) {
313  region.push_back(j);
314  }
315  }
316 
317  std::sort(region.begin(), region.end());
318  region.erase(unique(region.begin(), region.end()), region.end());
319  connectedRegions.push_back(region);
320  }
321  }
322 
323  return connectedRegions;
324 }
double m_mapPar[2]
Parameters for neighbour maps.
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.
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< int > flattenVector(std::vector< std::vector< int >> &A)
Convert vector of vectors to one long vector.
virtual const char * eventLevelClusteringInfoName() const
Name to be used for default option: EventLevelClusteringInfo.
std::vector< std::set< int > > mergeVectorsUsingSets(std::vector< std::vector< int >> &A)
Find all lists of cell-ids that share at least one cell.
std::vector< ECL::ECLNeighbours * > m_neighbourMaps
Neighbour maps.
std::string m_mapType[2]
Neighbour map types.
double m_timeCut_maxEnergy[3]
Time cut is only applied below this energy, ...
virtual const char * eclCalDigitArrayName() const
Name to be used for default or PureCsI option: ECLCalDigits.
StoreObjPtr< EventLevelClusteringInfo > m_eventLevelClusteringInfo
Store object pointer: EventLevelClusteringInfo.
StoreArray< ECLCalDigit > m_eclCalDigits
Store array: ECLCalDigit.
virtual const char * eclConnectedRegionArrayName() const
Name to be used for default option: ECLConnectedRegions.
Class to get the neighbours for a given cell id.
Definition: ECLNeighbours.h:25
Base class for Modules.
Definition: Module.h:72
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
@ 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
REG_MODULE(arichBtest)
Register the Module.
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:560
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.