Belle II Software  release-06-01-15
restofevent.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 #include <gtest/gtest.h>
9 #include "utilities/TestParticleFactory.h"
10 #include <analysis/dataobjects/Particle.h>
11 #include <analysis/VariableManager/Manager.h>
12 #include <analysis/dataobjects/RestOfEvent.h>
13 
14 #include <analysis/VariableManager/Utility.h>
15 #include <analysis/utility/PCmsLabTransform.h>
16 #include <analysis/utility/ReferenceFrame.h>
17 
18 #include <framework/datastore/StoreArray.h>
19 #include <framework/logging/Logger.h>
20 #include <mdst/dataobjects/Track.h>
21 #include <mdst/dataobjects/ECLCluster.h>
22 #include <mdst/dataobjects/KLMCluster.h>
23 #include <framework/gearbox/Gearbox.h>
24 #include <TLorentzVector.h>
25 
26 using namespace std;
27 using namespace Belle2;
28 using namespace Belle2::Variable;
29 namespace {
30  class ROETest : public ::testing::Test {
31  protected:
33  void SetUp() override
34  {
35 
36  DataStore::Instance().setInitializeActive(true);
37  StoreArray<ECLCluster> myECLClusters;
38  StoreArray<KLMCluster> myKLMClusters;
40  StoreArray<Track> myTracks;
41  StoreArray<Particle> myParticles;
43  StoreArray<PIDLikelihood> myPIDLikelihoods;
44  myECLClusters.registerInDataStore();
45  myKLMClusters.registerInDataStore();
46  myTFRs.registerInDataStore();
47  myTracks.registerInDataStore();
48  myParticles.registerInDataStore();
49  myROEs.registerInDataStore();
50  myPIDLikelihoods.registerInDataStore();
51  myParticles.registerRelationTo(myROEs);
52  myTracks.registerRelationTo(myPIDLikelihoods);
53  DataStore::Instance().setInitializeActive(false);
54 
56  TVector3 ipposition(0, 0, 0);
57  TLorentzVector ksmomentum(1, 0, 0, 3);
58  TVector3 ksposition(1.0, 0, 0);
59  //Creation of test particles:
60  //All daughters and mother particles have the same momenta within a decay
61  //In principle, this concept can be better developed if needed
62  auto* ksParticle = factory.produceParticle(string("^K_S0 -> ^pi+ ^pi-"), ksmomentum, ksposition);
63  TLorentzVector d0momentum(-2, 0, 0, 4);
64  auto* d0Particle = factory.produceParticle(string("^D0 -> ^K+ ^pi-"), d0momentum, ipposition);
65  TLorentzVector pi0momentum(-0.2, 0, 0, 1);
66  auto* pi0Particle = factory.produceParticle(string("^pi0 -> ^gamma ^gamma"), pi0momentum, ipposition);
67  TLorentzVector b0momentum(3, 0, 0, 5);
68  factory.produceParticle(string("^B0 -> [^K_S0 -> ^pi+ ^pi-] [^pi0 -> ^gamma ^gamma] ^gamma"), b0momentum, ipposition);
69 
70  RestOfEvent roe;
71  vector<const Particle*> roeParticles;
72  roeParticles.push_back(ksParticle->getDaughter(0));
73  roeParticles.push_back(ksParticle->getDaughter(1));
74  roeParticles.push_back(d0Particle->getDaughter(0));
75  roeParticles.push_back(d0Particle->getDaughter(1));
76  roeParticles.push_back(pi0Particle->getDaughter(0));
77  roeParticles.push_back(pi0Particle->getDaughter(1));
78  roe.addParticles(roeParticles);
79  roe.initializeMask("cutMask", "TestModule");
80  //Exclude K_S0 pions
81  std::shared_ptr<Variable::Cut> trackSelection = std::shared_ptr<Variable::Cut>(Variable::Cut::compile("p > 1.5"));
82  //Exclude pi0 gammas
83  std::shared_ptr<Variable::Cut> eclSelection = std::shared_ptr<Variable::Cut>(Variable::Cut::compile("p > 1"));
84  roe.updateMaskWithCuts("cutMask", trackSelection, eclSelection);
85  roe.initializeMask("excludeMask", "TestModule");
86  vector<const Particle*> excludeParticles = {ksParticle->getDaughter(1), d0Particle->getDaughter(0)};
87  roe.excludeParticlesFromMask("excludeMask", excludeParticles, Particle::EParticleSourceObject::c_Track, true);
88  roe.initializeMask("keepMask", "TestModule");
89  roe.excludeParticlesFromMask("keepMask", excludeParticles, Particle::EParticleSourceObject::c_Track, false);
90  roe.initializeMask("V0Mask", "TestModule");
91  roe.updateMaskWithCuts("V0Mask"); // No selection
92  //Add V0 to ROE mask:
93  roe.updateMaskWithV0("V0Mask", ksParticle);
94  myROEs.appendNew(roe);
95  roe.print();
96  }
97 
99  void TearDown() override
100  {
101  DataStore::Instance().reset();
102  }
103  };
104 
105  TEST_F(ROETest, hasParticle)
106  {
107  Gearbox& gearbox = Gearbox::getInstance();
108  gearbox.setBackends({std::string("file:")});
109  gearbox.close();
110  gearbox.open("geometry/Belle2.xml", false);
111  StoreArray<Particle> myParticles;
112  StoreArray<RestOfEvent> myROEs{};
113  const RestOfEvent* roe = myROEs[0];
114  EXPECT_TRUE(roe->hasParticle(myParticles[0])); // K_S0_pi0
115  EXPECT_TRUE(roe->hasParticle(myParticles[1])); // K_S0_pi1
116  EXPECT_FALSE(roe->hasParticle(myParticles[2])); // K_S0
117  EXPECT_TRUE(roe->hasParticle(myParticles[3])); // D0_K
118  EXPECT_TRUE(roe->hasParticle(myParticles[4])); // D0_pi
119  EXPECT_FALSE(roe->hasParticle(myParticles[5])); // D0
120  EXPECT_TRUE(roe->hasParticle(myParticles[6])); // pi0_gamma0
121  EXPECT_TRUE(roe->hasParticle(myParticles[7])); // pi0_gamma1
122  EXPECT_FALSE(roe->hasParticle(myParticles[8])); // pi0
123  EXPECT_FALSE(roe->hasParticle(myParticles[9])); // B0_K_S0_pi0
124  EXPECT_FALSE(roe->hasParticle(myParticles[10])); // B0_K_S0_pi1
125  EXPECT_FALSE(roe->hasParticle(myParticles[11])); // B0_pi0_gamma0
126  }
127 
128  TEST_F(ROETest, useROERecoilFrame)
129  {
130  Gearbox& gearbox = Gearbox::getInstance();
131  gearbox.setBackends({std::string("file:")});
132  gearbox.close();
133  gearbox.open("geometry/Belle2.xml", false);
134 
135  StoreArray<Particle> myParticles;
137  StoreObjPtr<RestOfEvent> myROEObject;
138  DataStore::Instance().setInitializeActive(true);
139  myROEObject.registerInDataStore(DataStore::c_DontWriteOut);
140  DataStore::Instance().setInitializeActive(false);
141  myParticles[14]->addRelationTo(myROEs[0]); // Add relation to B0
142  myROEObject.assign(myROEs[0]);
143 
145  // Recoil vector against all ROE particles
146  TLorentzVector pRecoil = T.getBeamFourMomentum() - myROEs[0]->get4Vector();
147  Particle tmp(pRecoil, 0);
148  RestFrame frame(&tmp);
149  //std::cout << "HER: " << T.getBeamParams().getHER()[0] << " LER: " << T.getBeamParams().getLER()[0] << std::endl;
150 
151  const Manager::Var* var = Manager::Instance().getVariable("useROERecoilFrame(p)");
152  ASSERT_NE(var, nullptr);
153  EXPECT_FLOAT_EQ(var->function(myParticles[5]), frame.getMomentum(myParticles[5]->get4Vector()).P()); // test on D0 in ROE
154  EXPECT_FLOAT_EQ(var->function(myParticles[14]), frame.getMomentum(myParticles[14]->get4Vector()).P()); // test on B0 on signal side
155  var = Manager::Instance().getVariable("useROERecoilFrame(E)");
156  ASSERT_NE(var, nullptr);
157  EXPECT_FLOAT_EQ(var->function(myParticles[5]), frame.getMomentum(myParticles[5]->get4Vector()).E()); // test on D0 in ROE
158  EXPECT_FLOAT_EQ(var->function(myParticles[14]), frame.getMomentum(myParticles[14]->get4Vector()).E()); // test on B0 on signal side
159 
160  DataStore::Instance().setInitializeActive(true);
161  DataStore::Instance().getEntry(myROEObject)->object = nullptr;
162  DataStore::Instance().setInitializeActive(false);
163  }
164 
165  TEST_F(ROETest, getParticles)
166  {
167  StoreArray<RestOfEvent> myROEs{};
168  const RestOfEvent* roe = myROEs[0];
169 
170  EXPECT_TRUE(roe->getParticles().size() == 6);
171  EXPECT_TRUE(roe->getPhotons().size() == 2);
172  EXPECT_TRUE(roe->getHadrons().size() == 0);
173  EXPECT_TRUE(roe->getChargedParticles().size() == 4);
174  EXPECT_TRUE(roe->getChargedParticles("", 321).size() == 1);
175  EXPECT_TRUE(roe->getChargedParticles("", 211).size() == 3);
176  }
177 
178  TEST_F(ROETest, updateMaskWithCuts)
179  {
180  StoreArray<Particle> myParticles;
181  StoreArray<RestOfEvent> myROEs{};
182  const RestOfEvent* roe = myROEs[0];
183 
184  EXPECT_FALSE(roe->hasParticle(myParticles[0], "cutMask")); // K_S0_pi0
185  EXPECT_FALSE(roe->hasParticle(myParticles[1], "cutMask")); // K_S0_pi0
186  EXPECT_TRUE(roe->hasParticle(myParticles[3], "cutMask")); // D0_K
187  EXPECT_TRUE(roe->hasParticle(myParticles[4], "cutMask")); // D0_pi
188  EXPECT_FALSE(roe->hasParticle(myParticles[6], "cutMask")); // pi0_gamma0
189  EXPECT_FALSE(roe->hasParticle(myParticles[7], "cutMask")); // pi0_gamma1
190  }
191  TEST_F(ROETest, excludeParticlesFromMask)
192  {
193  StoreArray<Particle> myParticles;
194  StoreArray<RestOfEvent> myROEs{};
195  const RestOfEvent* roe = myROEs[0];
196 
197  EXPECT_TRUE(roe->hasParticle(myParticles[0], "excludeMask")); // K_S0_pi0
198  EXPECT_FALSE(roe->hasParticle(myParticles[1], "excludeMask")); // K_S0_pi0
199  EXPECT_FALSE(roe->hasParticle(myParticles[3], "excludeMask")); // D0_K
200  EXPECT_TRUE(roe->hasParticle(myParticles[4], "excludeMask")); // D0_pi
201  EXPECT_TRUE(roe->hasParticle(myParticles[6], "excludeMask")); // pi0_gamma0
202  EXPECT_TRUE(roe->hasParticle(myParticles[7], "excludeMask")); // pi0_gamma1
203  // Inverted result with "!"
204  EXPECT_TRUE(!roe->hasParticle(myParticles[0], "keepMask")); // K_S0_pi0
205  EXPECT_FALSE(!roe->hasParticle(myParticles[1], "keepMask")); // K_S0_pi0
206  EXPECT_FALSE(!roe->hasParticle(myParticles[3], "keepMask")); // D0_K
207  EXPECT_TRUE(!roe->hasParticle(myParticles[4], "keepMask")); // D0_pi
208  // Photons not touched:
209  EXPECT_TRUE(roe->hasParticle(myParticles[6], "keepMask")); // pi0_gamma0
210  EXPECT_TRUE(roe->hasParticle(myParticles[7], "keepMask")); // pi0_gamma1
211  }
212  TEST_F(ROETest, updateMaskWithV0)
213  {
214  StoreArray<Particle> myParticles;
215  StoreArray<RestOfEvent> myROEs{};
216  const RestOfEvent* roe = myROEs[0];
217  // Has particle checks for daughters
218  EXPECT_TRUE(roe->hasParticle(myParticles[0], "V0Mask")); // K_S0_pi0
219  EXPECT_TRUE(roe->hasParticle(myParticles[1], "V0Mask")); // K_S0_pi0
220  EXPECT_TRUE(roe->hasParticle(myParticles[3], "V0Mask")); // D0_K
221  EXPECT_TRUE(roe->hasParticle(myParticles[4], "V0Mask")); // D0_pi
222  EXPECT_TRUE(roe->hasParticle(myParticles[6], "V0Mask")); // pi0_gamma0
223  EXPECT_TRUE(roe->hasParticle(myParticles[7], "V0Mask")); // pi0_gamma1
224  //Get all particles, including the K_S0 in the mask:
225  auto v0maskParticles = roe->getParticles("V0Mask", false);
226  //Get all particles, but substitute K_S0 FS daughters:
227  auto v0maskParticlesUnpacked = roe->getParticles("V0Mask", true);
228  B2INFO("packed size is " << v0maskParticles.size());
229  for (auto* particle : v0maskParticles) {
230  B2INFO("My pdg: " << particle->getPDGCode());
231  }
232  B2INFO("unpacked size is " << v0maskParticlesUnpacked.size());
233  for (auto* particle : v0maskParticlesUnpacked) {
234  B2INFO("My pdg: " << particle->getPDGCode());
235  }
236  EXPECT_FLOAT_EQ(v0maskParticles.size() , 5);
237  EXPECT_FLOAT_EQ(v0maskParticlesUnpacked.size() , 6);
238  }
239 } //
Singleton class responsible for loading detector parameters from an XML file.
Definition: Gearbox.h:34
Class to hold Lorentz transformations from/to CMS and boost vector.
TLorentzVector getBeamFourMomentum() const
Returns LAB four-momentum of e+e-.
Class to store reconstructed particles.
Definition: Particle.h:74
Rest frame of a particle.
This is a general purpose class for collecting reconstructed MDST data objects that are not used in r...
Definition: RestOfEvent.h:57
void initializeMask(const std::string &name, const std::string &origin="unknown")
Initialize new mask.
Definition: RestOfEvent.cc:135
bool hasParticle(const Particle *particle, const std::string &maskName="") const
Check if ROE has StoreArray index of given to the list of unused tracks in the event.
Definition: RestOfEvent.cc:125
void updateMaskWithCuts(const std::string &name, const std::shared_ptr< Variable::Cut > &trackCut=nullptr, const std::shared_ptr< Variable::Cut > &eclCut=nullptr, const std::shared_ptr< Variable::Cut > &klmCut=nullptr, bool updateExisting=false)
Update mask with cuts.
Definition: RestOfEvent.cc:180
void print(const std::string &maskName="", bool unpackComposite=true) const
Prints the contents of a RestOfEvent object to screen.
Definition: RestOfEvent.cc:368
std::vector< const Particle * > getChargedParticles(const std::string &maskName="", unsigned int pdg=0, bool unpackComposite=true) const
Get charged particles from ROE mask.
Definition: RestOfEvent.cc:109
std::vector< const Particle * > getHadrons(const std::string &maskName="", bool unpackComposite=true) const
Get hadrons from ROE mask.
Definition: RestOfEvent.cc:97
std::vector< const Particle * > getParticles(const std::string &maskName="", bool unpackComposite=true) const
Get all Particles from ROE mask.
Definition: RestOfEvent.cc:46
void addParticles(const std::vector< const Particle * > &particle)
Add StoreArray indices of given Particles to the list of unused particles in the event.
Definition: RestOfEvent.cc:25
void updateMaskWithV0(const std::string &name, const Particle *particleV0)
Update mask with composite particle.
Definition: RestOfEvent.cc:216
void excludeParticlesFromMask(const std::string &maskName, const std::vector< const Particle * > &particles, Particle::EParticleSourceObject listType, bool discard)
Update mask by keeping or excluding particles.
Definition: RestOfEvent.cc:147
std::vector< const Particle * > getPhotons(const std::string &maskName="", bool unpackComposite=true) const
Get photons from ROE mask.
Definition: RestOfEvent.cc:85
bool assign(TObject *object, bool replace=false)
Assign 'object' to this accessor.
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
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
This is a class, which generates DataStore particles, according to the provided decay string e....
const Belle2::Particle * produceParticle(const std::string &decayString, const TLorentzVector &momentum, const TVector3 &vertex)
Main method to produce particles.
TEST_F(GlobalLabelTest, LargeNumberOfTimeDependentParameters)
Test large number of time-dep params for registration and retrieval.
Definition: globalLabel.cc:72
Abstract base class for different kinds of events.
A variable returning a floating-point value for a given Particle.
Definition: Manager.h:133