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