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