Belle II Software development
BtubeCreatorModule.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 <analysis/modules/BtubeCreator/BtubeCreatorModule.h>
10
11// framework aux
12#include <framework/gearbox/Unit.h>
13#include <framework/gearbox/Const.h>
14#include <framework/logging/Logger.h>
15
16// dataobjects
17#include <analysis/dataobjects/Particle.h>
18#include <analysis/DecayDescriptor/ParticleListName.h>
19
20// utilities
21#include <analysis/utility/PCmsLabTransform.h>
22#include <analysis/utility/ParticleCopy.h>
23
24// rave
25#include <analysis/VertexFitting/RaveInterface/RaveSetup.h>
26#include <analysis/VertexFitting/RaveInterface/RaveVertexFitter.h>
27
28// Magnetic field
29#include <framework/geometry/BFieldManager.h>
30
31#include <TMath.h>
32#include <Eigen/Core>
33
34using namespace std;
35using namespace Belle2;
36
37//-----------------------------------------------------------------
38// Register the Module
39//-----------------------------------------------------------------
40REG_MODULE(BtubeCreator);
41
42//-----------------------------------------------------------------
43// Implementation
44//-----------------------------------------------------------------
45
47 m_Bfield(0)
48{
49 // Set module properties
50 setDescription("BtubeCreator : This module creates Btube object, an object geometrically similar to a very long ellipsoid along a particular direction and can be used as a constraint in vertexing B particles. Upsilon4S decays to two Bs. the user selects one B with a ^ in decaystring. The order of daughters in reconstructdecay of Upsilon4S and this decaystring should be same. Using selected B as reference, the module calculates the direction in which the other B should fly and constructs a Btube object along that direction. In semi-leptonic analyses, the selected B is usually the fully reconstructed one, while for time dependent studies, the selected B is usually signal B which can be partially reconstructed");
52
53 // Parameter definitions
54 addParam("listName", m_listName, "name of mother particle list", string(""));
55 addParam("confidenceLevel", m_confidenceLevel, "Confidence level to accept the fit.", 0.001);
56 addParam("decayString", m_decayString,
57 "decay string of the mother particle, the selected daughter specifies which daughter will be used as reference to create Btube",
58 string(""));
59 addParam("associateBtubeToBselected", m_associateBtubeToBselected,
60 "whether to associate the Btube with the selected B",
61 false);
62 addParam("verbosity", m_verbose, "print statements", true);
63}
64
66{
67
68 m_plist.isRequired(m_listName);
69
70 // magnetic field
71 m_Bfield = BFieldManager::getFieldInTesla(ROOT::Math::XYZVector(0, 0, 0)).Z();
72
73 m_BeamSpotCenter = m_beamSpotDB->getIPPosition();
74 m_beamSpotCov.ResizeTo(3, 3);
75 m_beamSpotCov = m_beamSpotDB->getCovVertex();
76 B2INFO("BtubeCreator : magnetic field = " << m_Bfield);
77
78 m_tubeArray.registerInDataStore();
80
81 bool valid = m_decaydescriptor.init(m_decayString);
82 if (!valid)
83 B2ERROR("BtubeCreatorModule: invalid decay string" << m_decayString);
84 int nProducts = m_decaydescriptor.getNDaughters();
85 if (nProducts != 2)
86 B2ERROR("BtubeCreatorModule: decay string should contain only two daughters");
87}
88
90{
92
93 std::vector<unsigned int> toRemove;
94 unsigned int n = m_plist->getListSize();
95
96 for (unsigned i = 0; i < n; i++) {
97 Particle* particle = m_plist->getParticle(i);
98
99 std::vector<Particle*> daughtervec = particle->getDaughters();
100 std::vector<const Particle*> selParticles = m_decaydescriptor.getSelectionParticles(particle);
101 if (selParticles.size() > 1) {
102 B2FATAL("Please select only one daughter which will be used as reference to create Btube");
103 }
104
105 int selectindex = 1;
106 if ((daughtervec.at(0))->getArrayIndex() == (selParticles.at(0))->getArrayIndex()) selectindex = 0 ;
107
108 Particle* tubecreatorB = const_cast<Particle*>(particle->getDaughter(selectindex));
109 Particle* otherB = const_cast<Particle*>(particle->getDaughter(1 - selectindex));
110
111 if ((tubecreatorB->getVertexErrorMatrix()(2, 2)) == 0.0) {
112 B2FATAL("Please perform a vertex fit of the selected B before calling this module");
113 }
114
115 //make a copy of tubecreatorB so as not to modify the original object
116
117 Particle* tubecreatorBCopy = ParticleCopy::copyParticle(tubecreatorB);
118
119 if (m_verbose) {
120 B2DEBUG(10, "tubecreator B decay vertex: ");
121 B2DEBUG(10, "{" << std::fixed << std::setprecision(20) << tubecreatorBCopy->getVertex().X() << "," << std::fixed <<
122 std::setprecision(
123 20) << tubecreatorBCopy->getVertex().Y() << "," << std::fixed << std::setprecision(20) << tubecreatorBCopy->getVertex().Z() << "}");
124 }
125
126 bool ok0 = doVertexFit(tubecreatorBCopy);
127
128 if (ok0) {
129 if (tubecreatorBCopy->getPValue() < m_confidenceLevel) {
130 toRemove.push_back(particle->getArrayIndex());
131 } else {
132 particle->setVertex(tubecreatorBCopy->getVertex());
133 particle->setMomentumVertexErrorMatrix(tubecreatorBCopy->getMomentumVertexErrorMatrix());
134
135 tubecreatorB->writeExtraInfo("prod_vtx_x", tubecreatorBCopy->getVertex().X());
136 tubecreatorB->writeExtraInfo("prod_vtx_y", tubecreatorBCopy->getVertex().Y());
137 tubecreatorB->writeExtraInfo("prod_vtx_z", tubecreatorBCopy->getVertex().Z());
138 tubecreatorB->writeExtraInfo("prod_vtx_cov00", tubecreatorBCopy->getVertexErrorMatrix()(0, 0));
139 tubecreatorB->writeExtraInfo("prod_vtx_cov01", tubecreatorBCopy->getVertexErrorMatrix()(0, 1));
140 tubecreatorB->writeExtraInfo("prod_vtx_cov02", tubecreatorBCopy->getVertexErrorMatrix()(0, 2));
141 tubecreatorB->writeExtraInfo("prod_vtx_cov10", tubecreatorBCopy->getVertexErrorMatrix()(1, 0));
142 tubecreatorB->writeExtraInfo("prod_vtx_cov11", tubecreatorBCopy->getVertexErrorMatrix()(1, 1));
143 tubecreatorB->writeExtraInfo("prod_vtx_cov12", tubecreatorBCopy->getVertexErrorMatrix()(1, 2));
144 tubecreatorB->writeExtraInfo("prod_vtx_cov20", tubecreatorBCopy->getVertexErrorMatrix()(2, 0));
145 tubecreatorB->writeExtraInfo("prod_vtx_cov21", tubecreatorBCopy->getVertexErrorMatrix()(2, 1));
146 tubecreatorB->writeExtraInfo("prod_vtx_cov22", tubecreatorBCopy->getVertexErrorMatrix()(2, 2));
147
148 tubecreatorB->writeExtraInfo("Px_after_avf", (tubecreatorBCopy->get4Vector()).Px());
149 tubecreatorB->writeExtraInfo("Py_after_avf", (tubecreatorBCopy->get4Vector()).Py());
150 tubecreatorB->writeExtraInfo("Pz_after_avf", (tubecreatorBCopy->get4Vector()).Pz());
151 tubecreatorB->writeExtraInfo("E_after_avf", (tubecreatorBCopy->get4Vector()).E());
152
153 Eigen::Matrix<double, 3, 1> tubecreatorBOriginpos(tubecreatorBCopy->getVertex().X(), tubecreatorBCopy->getVertex().Y(),
154 tubecreatorBCopy->getVertex().Z());
155 ROOT::Math::PxPyPzEVector v4Final = tubecreatorBCopy->get4Vector();
157 ROOT::Math::PxPyPzEVector vec = T.rotateLabToCms() * v4Final;
158 ROOT::Math::PxPyPzEVector vecNew(-1 * vec.Px(), -1 * vec.Py(), -1 * vec.Pz(), vec.E());
159 ROOT::Math::PxPyPzEVector v4FinalNew = T.rotateCmsToLab() * vecNew;
160
161 if (m_verbose) {
162 B2DEBUG(10, "beamspot center :");
163 B2DEBUG(10, "{" << std::fixed << std::setprecision(20) << m_BeamSpotCenter.X() << "," << std::fixed << std::setprecision(
164 20) << m_BeamSpotCenter.Y() << "," << std::fixed << std::setprecision(20) << m_BeamSpotCenter.Z() << "}");
165 B2DEBUG(10, "beamspot cov :");
166
167 B2DEBUG(10, "{" << std::fixed << std::setprecision(20) << m_beamSpotCov(0,
168 0) << "," << std::fixed << std::setprecision(20) << m_beamSpotCov(0,
169 1) << "," << std::fixed << std::setprecision(20) << m_beamSpotCov(0, 2) << "},");
170 B2DEBUG(10, "{" << std::fixed << std::setprecision(20) << m_beamSpotCov(1,
171 0) << "," << std::fixed << std::setprecision(20) << m_beamSpotCov(1,
172 1) << "," << std::fixed << std::setprecision(20) << m_beamSpotCov(1, 2) << "},");
173 B2DEBUG(10, "{" << std::fixed << std::setprecision(20) << m_beamSpotCov(2,
174 0) << "," << std::fixed << std::setprecision(20) << m_beamSpotCov(2,
175 1) << "," << std::fixed << std::setprecision(20) << m_beamSpotCov(2, 2) << "}");
176 }
177 TMatrixFSym pp = (tubecreatorBCopy->getMomentumErrorMatrix()).GetSub(0, 2, 0, 2, "S");
178 double pe = tubecreatorBCopy->getMomentumErrorMatrix()(2, 2);
179 TMatrixFSym pv = tubecreatorBCopy->getVertexErrorMatrix();
180
181 // start rotation
182
183 double theta = v4FinalNew.Theta();
184 double phi = v4FinalNew.Phi();
185
186 double st = TMath::Sin(theta);
187 double ct = TMath::Cos(theta);
188 double sp = TMath::Sin(phi);
189 double cp = TMath::Cos(phi);
190
191 TMatrix r2z(3, 3); r2z(2, 2) = 1;
192 r2z(0, 0) = cp; r2z(0, 1) = -1 * sp;
193 r2z(1, 0) = sp; r2z(1, 1) = cp;
194
195 TMatrix r2y(3, 3); r2y(1, 1) = 1;
196 r2y(0, 0) = ct; r2y(0, 2) = st;
197 r2y(2, 0) = -1 * st; r2y(2, 2) = ct;
198
199 TMatrix r2(3, 3); r2.Mult(r2z, r2y);
200 TMatrix r2t(3, 3); r2t.Transpose(r2);
201
202 TMatrix longerror(3, 3); longerror(2, 2) = 1000;
203 TMatrix longerror_temp(3, 3); longerror_temp.Mult(r2, longerror);
204 TMatrix longerrorRotated(3, 3); longerrorRotated.Mult(longerror_temp, r2t);
205
206 TMatrix pvNew(3, 3);
207 pvNew += pv;
208 pvNew += longerrorRotated;
209
210 TMatrixFSym errNew(7);
211 errNew.SetSub(0, 0, pp);
212 errNew.SetSub(4, 4, pvNew);
213 errNew(3, 3) = pe;
214
215 TMatrixFSym tubeMat(3);
216 tubeMat.SetSub(0, 0, pvNew);
217
218 TMatrixFSym tubeMatCenterError(3);
219 tubeMatCenterError.SetSub(0, 0, pv);
220
221 if (m_verbose) {
222 B2DEBUG(10, "B origin error matrix : ");
223 B2DEBUG(10, "{" << std::fixed << std::setprecision(20) << pv(0, 0) << "," << std::fixed << std::setprecision(20) << pv(0,
224 1) << "," << std::fixed << std::setprecision(20) << pv(0, 2) << "},");
225 B2DEBUG(10, "{" << std::fixed << std::setprecision(20) << pv(1, 0) << "," << std::fixed << std::setprecision(20) << pv(1,
226 1) << "," << std::fixed << std::setprecision(20) << pv(1, 2) << "},");
227 B2DEBUG(10, "{" << std::fixed << std::setprecision(20) << pv(2, 0) << "," << std::fixed << std::setprecision(20) << pv(2,
228 1) << "," << std::fixed << std::setprecision(20) << pv(2, 2) << "}");
229
230 B2DEBUG(10, "B tube error matrix : ");
231 B2DEBUG(10, "{" << std::fixed << std::setprecision(20) << pvNew(0, 0) << "," << std::fixed << std::setprecision(20) << pvNew(0,
232 1) << "," << std::fixed << std::setprecision(20) << pvNew(0, 2) << "},");
233 B2DEBUG(10, "{" << std::fixed << std::setprecision(20) << pvNew(1, 0) << "," << std::fixed << std::setprecision(20) << pvNew(1,
234 1) << "," << std::fixed << std::setprecision(20) << pvNew(1, 2) << "},");
235 B2DEBUG(10, "{" << std::fixed << std::setprecision(20) << pvNew(2, 0) << "," << std::fixed << std::setprecision(20) << pvNew(2,
236 1) << "," << std::fixed << std::setprecision(20) << pvNew(2, 2) << "}");
237
238 B2DEBUG(10, "B origin ");
239 B2DEBUG(10, "{" << std::fixed << std::setprecision(20) << tubecreatorBCopy->getVertex().X() << "," << std::fixed <<
240 std::setprecision(
241 20) << tubecreatorBCopy->getVertex().Y() << "," << std::fixed << std::setprecision(20) << tubecreatorBCopy->getVertex().Z() << "}");
242 }
243
244 tubecreatorBCopy->setMomentumVertexErrorMatrix(errNew);
245
246 Btube* tubeconstraint = m_tubeArray.appendNew(Btube());
248 tubecreatorB->addRelationTo(tubeconstraint);
249 } else {
250 otherB->addRelationTo(tubeconstraint);
251 }
252
253 tubeconstraint->setTubeCenter(tubecreatorBOriginpos);
254 tubeconstraint->setTubeMatrix(tubeMat);
255 tubeconstraint->setTubeCenterErrorMatrix(tubeMatCenterError);
256
258 addextrainfos(tubecreatorB, tubecreatorBCopy, pvNew, v4FinalNew);
259 } else {
260 addextrainfos(otherB, tubecreatorBCopy, pvNew, v4FinalNew);
261 }
262 }
263 }
264 if (!ok0) toRemove.push_back(particle->getArrayIndex());
265 }
266 m_plist->removeParticles(toRemove);
267
269}
270
272{
274
276 rsg.addTrack(mother);
277 int nvert = rsg.fit("avf");
278
279 if (nvert == 1) {
280 rsg.updateDaughters();
281 double pValue = rsg.getPValue();
282 mother->setPValue(pValue);
283 } else {return false;}
284 return true;
285}
286
287void BtubeCreatorModule::addextrainfos(Particle* daughter, Particle* copy, TMatrix mat, ROOT::Math::PxPyPzEVector TLV)
288{
289 daughter->writeExtraInfo("TubePosX", copy->getVertex().X());
290 daughter->writeExtraInfo("TubePosY", copy->getVertex().Y());
291 daughter->writeExtraInfo("TubePosZ", copy->getVertex().Z());
292
293 daughter->writeExtraInfo("TubeCov00", mat(0, 0));
294 daughter->writeExtraInfo("TubeCov01", mat(0, 1));
295 daughter->writeExtraInfo("TubeCov02", mat(0, 2));
296 daughter->writeExtraInfo("TubeCov10", mat(1, 0));
297 daughter->writeExtraInfo("TubeCov11", mat(1, 1));
298 daughter->writeExtraInfo("TubeCov12", mat(1, 2));
299 daughter->writeExtraInfo("TubeCov20", mat(2, 0));
300 daughter->writeExtraInfo("TubeCov21", mat(2, 1));
301 daughter->writeExtraInfo("TubeCov22", mat(2, 2));
302
303 daughter->writeExtraInfo("TubeDirX", (TLV.Px() / TLV.P()));
304 daughter->writeExtraInfo("TubeDirY", (TLV.Py() / TLV.P()));
305 daughter->writeExtraInfo("TubeDirZ", (TLV.Pz() / TLV.P()));
306 daughter->writeExtraInfo("TubeB_p_estimated", TLV.P());
307}
static ROOT::Math::XYZVector getFieldInTesla(const ROOT::Math::XYZVector &pos)
return the magnetic field at a given position in Tesla.
TMatrixDSym m_beamSpotCov
Beam spot covariance matrix.
virtual void initialize() override
declare data store elements
StoreArray< Btube > m_tubeArray
the (output) array of Btube objects
virtual void event() override
process event
std::string m_decayString
specifies which daughter particles will be used as reference to create Btube
std::string m_listName
name of particle list
ROOT::Math::XYZVector m_BeamSpotCenter
Beam spot position.
BtubeCreatorModule()
Constructor: Sets the description, the properties and the parameters of the module.
DBObjPtr< BeamSpot > m_beamSpotDB
Beam spot database object.
bool doVertexFit(Particle *p)
Main steering routine.
double m_confidenceLevel
required fit confidence level
DecayDescriptor m_decaydescriptor
Decay descriptor of decays to look for.
void addextrainfos(Particle *daughter, Particle *copy, TMatrix mat, ROOT::Math::PxPyPzEVector TLV)
fills extrainfos to the particle
double m_Bfield
magnetic field from data base
StoreObjPtr< ParticleList > m_plist
the input particle list
bool m_verbose
run fit with a lot of B2INFOs for debugging
bool m_associateBtubeToBselected
whether to associate the Btube with the selected B
For each MCParticle with hits in the CDC, this class stores some summarising information on those hit...
Definition Btube.h:22
void setTubeCenterErrorMatrix(const TMatrixFSym &tubecentererrormatrix)
Sets Btube Center Error Matrix.
Definition Btube.h:54
void setTubeCenter(const Eigen::Matrix< double, 3, 1 > &tubecenter)
Sets Btube Center.
Definition Btube.h:41
void setTubeMatrix(const TMatrixFSym &tubematrix)
Sets Btube Matrix.
Definition Btube.h:49
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
Class to hold Lorentz transformations from/to CMS and boost vector.
Class to store reconstructed particles.
Definition Particle.h:76
TMatrixFSym getVertexErrorMatrix() const
Returns the 3x3 position error sub-matrix.
Definition Particle.cc:478
void writeExtraInfo(const std::string &name, const double value)
Sets the user defined extraInfo.
Definition Particle.cc:1393
double getPValue() const
Returns chi^2 probability of fit if done or -1.
Definition Particle.h:687
ROOT::Math::XYZVector getVertex() const
Returns vertex position (POCA for charged, IP for neutral FS particles)
Definition Particle.h:651
ROOT::Math::PxPyPzEVector get4Vector() const
Returns Lorentz vector.
Definition Particle.h:567
TMatrixFSym getMomentumErrorMatrix() const
Returns the 4x4 momentum error matrix.
Definition Particle.cc:466
void setMomentumVertexErrorMatrix(const TMatrixFSym &errMatrix)
Sets 7x7 error matrix.
Definition Particle.cc:424
void setPValue(double pValue)
Sets chi^2 probability of fit.
Definition Particle.h:377
TMatrixFSym getMomentumVertexErrorMatrix() const
Returns 7x7 error matrix.
Definition Particle.cc:451
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).
Accessor to arrays stored in the data store.
Definition StoreArray.h:113
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
static void initialize(int verbosity=1, double MagneticField=1.5)
Set everything up so everything needed for vertex fitting is there.
Definition RaveSetup.cc:35
static RaveSetup * getInstance()
get the pointer to the instance to get/set any of options stored in RaveSetup
Definition RaveSetup.h:40
void setBeamSpot(const ROOT::Math::XYZVector &beamSpot, const TMatrixDSym &beamSpotCov)
The beam spot position and covariance is known you can set it here so that and a vertex in the beam s...
Definition RaveSetup.cc:74
void reset()
frees memory allocated by initialize().
Definition RaveSetup.cc:60
The RaveVertexFitter class is part of the RaveInterface together with RaveSetup.
int fit(std::string options="default")
do the vertex fit with all tracks previously added with the addTrack or addMother function.
void addTrack(const Particle *const aParticlePtr)
add a track (in the format of a Particle) to set of tracks that should be fitted to a vertex
void updateDaughters()
update the Daughters particles
double getPValue(VecSize vertexId=0) const
get the p value of the fitted vertex.
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
Particle * copyParticle(const Particle *original)
Function takes argument Particle and creates a copy of it and copies of all its (grand-)^n-daughters.
Abstract base class for different kinds of events.
STL namespace.