Belle II Software  release-06-00-14
SPTC2GFTCConverterModule.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 #include <tracking/modules/spacePointCreator/SPTC2GFTCConverterModule.h>
10 #include <tracking/spacePointCreation/SpacePointTrackCand.h>
11 
12 #include <genfit/TrackCand.h>
13 
14 #include <framework/dataobjects/EventMetaData.h>
15 #include <framework/datastore/StoreArray.h>
16 #include <framework/datastore/StoreObjPtr.h>
17 
18 #include <framework/gearbox/Const.h>
19 
20 using namespace std;
21 using namespace Belle2;
22 
23 REG_MODULE(SPTC2GFTCConverter)
24 
26  Module()
27 {
28  setDescription("Module for converting SpacePointTrackCand to genfit::TrackCand.");
29  setPropertyFlags(c_ParallelProcessingCertified);
30 
31  addParam("SpacePointTCName", m_SPTCName, "Name of the container containing the SpacePointTrackCands to convert", string(""));
32  addParam("genfitTCName", m_genfitTCName, "Name of the container of the (output) genfit::TrackCands", string(""));
33  addParam("PXDClusters", m_PXDClustersName, "Name of the container of the PXD Clusters", string(""));
34  addParam("SVDClusters", m_SVDClustersName, "Name of the container of the SVD Clusters", string(""));
35 
36  initializeCounters(); // NOTE: they get initialized in initialize again!!
37 }
38 
39 void SPTC2GFTCConverterModule::initialize()
40 {
41  B2INFO("SPTC2GFTCConverter --------------- initialize() -------------------");
42  // initialize the counters
43  initializeCounters();
44 
45  // Register StoreArray<genfit::TrackCand> in the DataStore
46  StoreArray<genfit::TrackCand> genfitTCs(m_genfitTCName);
47  genfitTCs.registerInDataStore(m_genfitTCName, DataStore::c_ErrorIfAlreadyRegistered);
48 
49  StoreArray<SpacePointTrackCand> spTCs(m_SPTCName);
50  spTCs.isRequired(m_SPTCName);
51 
52  // Register Relation between the two StoreArrays
53  spTCs.registerRelationTo(genfitTCs);
54 
55  StoreArray<PXDCluster> PXDClusters(m_PXDClustersName); PXDClusters.isRequired(m_PXDClustersName);
56  StoreArray<SVDCluster> SVDClusters(m_SVDClustersName); SVDClusters.isRequired(m_SVDClustersName);
57 }
58 
59 void SPTC2GFTCConverterModule::event()
60 {
61  StoreObjPtr<EventMetaData> eventMetaData("EventMetaData", DataStore::c_Event);
62  const int eventCounter = eventMetaData->getEvent();
63  B2DEBUG(10, "SPTC2GFTCConverter::event() processing event " << eventCounter << " ----------");
64 
65  StoreArray<genfit::TrackCand> genfitTCs(m_genfitTCName);
66  StoreArray<SpacePointTrackCand> spacePointTCs(m_SPTCName);
67 
68  int nSPTCs = spacePointTCs.getEntries();
69  B2DEBUG(15, "Found " << nSPTCs << " SpacePointTrackCands in StoreArray " << spacePointTCs.getName());
70 
71  for (int iTC = 0; iTC < nSPTCs; ++iTC) {
72  const SpacePointTrackCand* trackCand = spacePointTCs[iTC];
73  m_SpacePointTCCtr += 1;
74 
75  genfit::TrackCand genfitTC;
76  B2DEBUG(20, "SpacePointTrackCand " << iTC << " contains " << trackCand->getNHits() << " SpacePoints");
77 
78  std::vector<const SpacePoint*> tcSpacePoints = trackCand->getHits();
79  std::vector<double> sortingParams = trackCand->getSortingParameters();
80 
81  // loop over all SpacePoints and look at their relations
82  for (unsigned int iTCSP = 0; iTCSP < tcSpacePoints.size(); ++iTCSP) {
83  const SpacePoint* aSP = tcSpacePoints[iTCSP];
84  double sortingParam = sortingParams[iTCSP];
85 
86  auto detType = aSP->getType();
87  int detID = detType == VXD::SensorInfoBase::SVD ? Const::SVD : Const::PXD;
88  vector<int> clusterInds;
89 
90  try {
91 
92  if (detType == VXD::SensorInfoBase::PXD) { clusterInds = getRelatedClusters<PXDCluster>(aSP, m_PXDClustersName); }
93  else if (detType == VXD::SensorInfoBase::SVD) { clusterInds = getRelatedClusters<SVDCluster>(aSP, m_SVDClustersName); }
94  else throw SpacePointTrackCand::UnsupportedDetType();
95 
96  } catch (std::runtime_error& anE) {
97  B2WARNING("Caught exception during creation of a genfit::TrackCand: " << anE.what());
98  m_skippedSPsCtr++;
99  continue; // with next SpacePoint
100  } catch (...) {
101  B2WARNING("Caught unknown exception during conversion from SPTC to GFTC!");
102  throw;
103  }
104 
105  for (int hitID : clusterInds) {
106  genfitTC.addHit(detID, hitID, -1, sortingParam);
107  B2DEBUG(60, "Added Cluster " << hitID << " with detID " << detID << " to genfit::TrackCand");
108  }
109  }
110 
111  // set other properties of TrackCand
112  genfitTC.set6DSeedAndPdgCode(trackCand->getStateSeed(), trackCand->getPdgCode());
113  genfitTC.setCovSeed(trackCand->getCovSeed());
114  genfitTC.setMcTrackId(trackCand->getMcTrackID());
115 
116  // add genfit::TrackCand to StoreArray
117  m_genfitTCCtr++;
118  if (LogSystem::Instance().isLevelEnabled(LogConfig::c_Debug, 150, PACKAGENAME())) genfitTC.Print(); // debug purposes
119  B2DEBUG(15, "genfit::TrackCand contains " << genfitTC.getNHits() << " TrackCandHits.");
120  genfit::TrackCand* newTC = genfitTCs.appendNew(genfitTC);
121  trackCand->addRelationTo(newTC);
122  B2DEBUG(15, "Added relation between SPTC " << trackCand->getArrayIndex() << " from Array " << trackCand->getArrayName() <<
123  " and GFTC.");
124  }
125 }
126 
127 void SPTC2GFTCConverterModule::terminate()
128 {
129  stringstream output;
130  output << "SPTC2GFTCConverter::terminate: got " << m_SpacePointTCCtr << " SpacePointTrackCands and created " << m_genfitTCCtr <<
131  " genfit::TrackCands";
132  if (m_skippedSPsCtr) output << ". " << m_skippedSPsCtr << " SpacePoints were skipped!";
133  B2INFO(output.str());
134 }
135 
136 // ========================================== GET RELATED CLUSTERS ================================================================
137 template<typename ClusterType>
138 std::vector<int> SPTC2GFTCConverterModule::getRelatedClusters(const Belle2::SpacePoint* spacePoint, const std::string& clusterNames)
139 {
140  std::vector<int> clusterInds;
141 
142  RelationVector<ClusterType> relatedClusters = spacePoint->getRelationsTo<ClusterType>(clusterNames);
143  if (relatedClusters.size() == 0) {
144  B2DEBUG(1, "Found no related Clusters for SpacePoint " << spacePoint->getArrayIndex() << " from Array " <<
145  spacePoint->getArrayName());
146  throw ClusterNotFound();
147  } else B2ASSERT("Too many clusters!", relatedClusters.size() < 3);
148 
149  for (const ClusterType& cluster : relatedClusters) {
150  clusterInds.push_back(cluster.getArrayIndex());
151  B2DEBUG(60, "Cluster " << cluster.getArrayIndex() << " from Array " << cluster.getArrayName() << " is related to SpacePoint " <<
152  spacePoint->getArrayIndex() << " from Array " << spacePoint->getArrayName());
153  }
154 
155  return clusterInds;
156 }
157 
158 void SPTC2GFTCConverterModule::initializeCounters()
159 {
160  m_SpacePointTCCtr = 0;
161  m_genfitTCCtr = 0;
162  m_skippedSPsCtr = 0;
163 }
Base class for Modules.
Definition: Module.h:72
Class for type safe access to objects that are referred to in relations.
size_t size() const
Get number of relations.
void addRelationTo(const RelationsInterface< BASE > *object, float weight=1.0, const std::string &namedRelation="") const
Add a relation from this object to another object (with caching).
std::string getArrayName() const
Get name of array this object is stored in, or "" if not found.
int getArrayIndex() const
Returns this object's array index (in StoreArray), or -1 if not found.
RelationVector< TO > getRelationsTo(const std::string &name="", const std::string &namedRelation="") const
Get the relations that point from this object to another store array.
Module for converting SpacePointTrackCands to genfit::SpacePointTrackCands.
Storage for (VXD) SpacePoint-based track candidates.
unsigned int getNHits() const
get the number of hits (space points) in the track candidate
int getPdgCode() const
get pdg code
int getMcTrackID() const
get the MC Track ID
const std::vector< const Belle2::SpacePoint * > & getHits() const
get hits (space points) of track candidate
const TMatrixDSym & getCovSeed() const
get the covariance matrix seed (6D).
const TVectorD & getStateSeed() const
get state seed as 6D vector
const std::vector< double > & getSortingParameters() const
get the sorting parameters
SpacePoint typically is build from 1 PXDCluster or 1-2 SVDClusters.
Definition: SpacePoint.h:42
Belle2::VXD::SensorInfoBase::SensorType getType() const
Return SensorType (PXD, SVD, ...) on which the SpacePoint lives.
Definition: SpacePoint.h:145
bool isRequired(const std::string &name="")
Ensure this array/object has been registered previously.
const std::string & getName() const
Return name under which the object is saved in the DataStore.
bool registerInDataStore(DataStore::EStoreFlags storeFlags=DataStore::c_WriteOut)
Register the object/array in the DataStore.
Accessor to arrays stored in the data store.
Definition: StoreArray.h:113
T * appendNew()
Construct a new T object at the end of the array.
Definition: StoreArray.h:246
int getEntries() const
Get the number of objects in the array.
Definition: StoreArray.h:216
bool registerRelationTo(const StoreArray< TO > &toArray, DataStore::EDurability durability=DataStore::c_Event, DataStore::EStoreFlags storeFlags=DataStore::c_WriteOut, const std::string &namedRelation="") const
Register a relation to the given StoreArray.
Definition: StoreArray.h:140
Type-safe access to single objects in the data store.
Definition: StoreObjPtr.h:95
Track candidate – seed values and indices.
Definition: TrackCand.h:69
void set6DSeedAndPdgCode(const TVectorD &state6D, const int pdgCode)
This function works the same as set6DSeed but instead of a charge hypothesis you can set a pdg code w...
Definition: TrackCand.cc:253
void Print(const Option_t *="") const
Write the content of all private attributes to the terminal.
Definition: TrackCand.cc:202
void setCovSeed(const TMatrixDSym &cov6D)
set the covariance matrix seed (6D).
Definition: TrackCand.h:175
void setMcTrackId(int i)
Set the MCT track id, for MC simulations.
Definition: TrackCand.h:151
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Definition: Module.h:650
Abstract base class for different kinds of events.