Belle II Software  release-08-01-10
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 
11 #include <framework/dataobjects/EventMetaData.h>
12 #include <framework/datastore/StoreObjPtr.h>
13 
14 #include <framework/gearbox/Const.h>
15 
16 using namespace Belle2;
17 
18 REG_MODULE(SPTC2GFTCConverter);
19 
21  Module()
22 {
23  setDescription("Module for converting SpacePointTrackCand to genfit::TrackCand.");
25 
26  addParam("SpacePointTCName", m_SPTCName, "Name of the container containing the SpacePointTrackCands to convert", std::string(""));
27  addParam("genfitTCName", m_genfitTCName, "Name of the container of the (output) genfit::TrackCands", std::string(""));
28  addParam("PXDClusters", m_PXDClustersName, "Name of the container of the PXD Clusters", std::string(""));
29  addParam("SVDClusters", m_SVDClustersName, "Name of the container of the SVD Clusters", std::string(""));
30 
31  initializeCounters(); // NOTE: they get initialized in initialize again!!
32 }
33 
35 {
36  B2INFO("SPTC2GFTCConverter --------------- initialize() -------------------");
37  // initialize the counters
39 
40  // Register StoreArray<genfit::TrackCand> in the DataStore
42 
44 
45  // Register Relation between the two StoreArrays
47 
48 // StoreArray<PXDCluster> m_PXDClusters(m_PXDClustersName); m_PXDClusters.isRequired(m_PXDClustersName);
49 // StoreArray<SVDCluster> m_SVDClusters(m_SVDClustersName); m_SVDClusters.isRequired(m_SVDClustersName);
50 }
51 
53 {
54  StoreObjPtr<EventMetaData> eventMetaData("EventMetaData", DataStore::c_Event);
55  const int eventCounter = eventMetaData->getEvent();
56  B2DEBUG(20, "SPTC2GFTCConverter::event() processing event " << eventCounter << " ----------");
57 
58  int nSPTCs = m_SpacePointTrackCands.getEntries();
59  B2DEBUG(20, "Found " << nSPTCs << " SpacePointTrackCands in StoreArray " << m_SpacePointTrackCands.getName());
60 
61  for (int iTC = 0; iTC < nSPTCs; ++iTC) {
62  const SpacePointTrackCand* trackCand = m_SpacePointTrackCands[iTC];
63  m_SpacePointTCCtr += 1;
64 
65  genfit::TrackCand genfitTC;
66  B2DEBUG(20, "SpacePointTrackCand " << iTC << " contains " << trackCand->getNHits() << " SpacePoints");
67 
68  std::vector<const SpacePoint*> tcSpacePoints = trackCand->getHits();
69  std::vector<double> sortingParams = trackCand->getSortingParameters();
70 
71  // loop over all SpacePoints and look at their relations
72  for (unsigned int iTCSP = 0; iTCSP < tcSpacePoints.size(); ++iTCSP) {
73  const SpacePoint* aSP = tcSpacePoints[iTCSP];
74  double sortingParam = sortingParams[iTCSP];
75 
76  auto detType = aSP->getType();
77  int detID = detType == VXD::SensorInfoBase::SVD ? Const::SVD : Const::PXD;
78  std::vector<int> clusterInds;
79 
80  try {
81 
82  if (detType == VXD::SensorInfoBase::PXD) { clusterInds = getRelatedClusters<PXDCluster>(aSP, m_PXDClustersName); }
83  else if (detType == VXD::SensorInfoBase::SVD) { clusterInds = getRelatedClusters<SVDCluster>(aSP, m_SVDClustersName); }
84  else throw SpacePointTrackCand::UnsupportedDetType();
85 
86  } catch (std::runtime_error& anE) {
87  B2WARNING("Caught exception during creation of a genfit::TrackCand: " << anE.what());
89  continue; // with next SpacePoint
90  } catch (...) {
91  B2WARNING("Caught unknown exception during conversion from SPTC to GFTC!");
92  throw;
93  }
94 
95  for (int hitID : clusterInds) {
96  genfitTC.addHit(detID, hitID, -1, sortingParam);
97  B2DEBUG(29, "Added Cluster " << hitID << " with detID " << detID << " to genfit::TrackCand");
98  }
99  }
100 
101  // set other properties of TrackCand
102  genfitTC.set6DSeedAndPdgCode(trackCand->getStateSeed(), trackCand->getPdgCode());
103  genfitTC.setCovSeed(trackCand->getCovSeed());
104  genfitTC.setMcTrackId(trackCand->getMcTrackID());
105 
106  // add genfit::TrackCand to StoreArray
107  m_genfitTCCtr++;
108  if (LogSystem::Instance().isLevelEnabled(LogConfig::c_Debug, 150, PACKAGENAME())) genfitTC.Print(); // debug purposes
109  B2DEBUG(22, "genfit::TrackCand contains " << genfitTC.getNHits() << " TrackCandHits.");
111  trackCand->addRelationTo(newTC);
112  B2DEBUG(22, "Added relation between SPTC " << trackCand->getArrayIndex() << " from Array " << trackCand->getArrayName() <<
113  " and GFTC.");
114  }
115 }
116 
118 {
119  std::stringstream output;
120  output << "SPTC2GFTCConverter::terminate: got " << m_SpacePointTCCtr << " SpacePointTrackCands and created " << m_genfitTCCtr <<
121  " genfit::TrackCands";
122  if (m_skippedSPsCtr) output << ". " << m_skippedSPsCtr << " SpacePoints were skipped!";
123  B2INFO(output.str());
124 }
125 
126 // ========================================== GET RELATED CLUSTERS ================================================================
127 template<typename ClusterType>
128 std::vector<int> SPTC2GFTCConverterModule::getRelatedClusters(const Belle2::SpacePoint* spacePoint, const std::string& clusterNames)
129 {
130  std::vector<int> clusterInds;
131 
132  RelationVector<ClusterType> relatedClusters = spacePoint->getRelationsTo<ClusterType>(clusterNames);
133  if (relatedClusters.size() == 0) {
134  B2DEBUG(20, "Found no related Clusters for SpacePoint " << spacePoint->getArrayIndex() << " from Array " <<
135  spacePoint->getArrayName());
136  throw ClusterNotFound();
137  } else B2ASSERT("Too many clusters!", relatedClusters.size() < 3);
138 
139  for (const ClusterType& cluster : relatedClusters) {
140  clusterInds.push_back(cluster.getArrayIndex());
141  B2DEBUG(29, "Cluster " << cluster.getArrayIndex() << " from Array " << cluster.getArrayName() << " is related to SpacePoint " <<
142  spacePoint->getArrayIndex() << " from Array " << spacePoint->getArrayName());
143  }
144 
145  return clusterInds;
146 }
147 
149 {
150  m_SpacePointTCCtr = 0;
151  m_genfitTCCtr = 0;
152  m_skippedSPsCtr = 0;
153 }
@ c_ErrorIfAlreadyRegistered
If the object/array was already registered, produce an error (aborting initialisation).
Definition: DataStore.h:72
@ c_Event
Different object in each event, all objects/arrays are invalidated after event() function has been ca...
Definition: DataStore.h:59
@ c_Debug
Debug: for code development.
Definition: LogConfig.h:26
static LogSystem & Instance()
Static method to get a reference to the LogSystem instance.
Definition: LogSystem.cc:31
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
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.
unsigned int m_skippedSPsCtr
Counter for SpacePoints that were not converted.
void initialize() override
initialize module
StoreArray< SpacePointTrackCand > m_SpacePointTrackCands
SpacePointTrackCands StoreArray.
std::string m_SVDClustersName
SVD Clusters collection name.
void event() override
event: convert SpacePointTrackCand to genfit::TrackCand
void terminate() override
terminate: some summary information on the processed events
unsigned int m_genfitTCCtr
Counter for genfit::TrackCands that were actually created by the module.
std::vector< int > getRelatedClusters(const Belle2::SpacePoint *spacePoint, const std::string &clusterNames="ALL")
get all related Clusters to a SpacePoint
std::string m_genfitTCName
genfit::TrackCand collection name
StoreArray< genfit::TrackCand > m_GenfitTrackCands
genfit::TrackCands StoreArray
std::string m_PXDClustersName
PXD Clusters collection name.
std::string m_SPTCName
SpacePointTrackCand collection name.
unsigned int m_SpacePointTCCtr
Counter for SpacePointTrackCands presented to the module.
void initializeCounters()
reset counters to 0 to avoid indeterministic behaviour
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.
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:96
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
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
Abstract base class for different kinds of events.