9#include <tracking/modules/spacePointCreator/PhaseSpaceAnalysisModule.h>
11#include <framework/dataobjects/EventMetaData.h>
12#include <framework/datastore/StoreObjPtr.h>
23 setDescription(
"Module for analysing the phase space covered by TrackCands (resp. their related MCParticles)");
25 std::vector<std::string> defaultNames = { std::string(
"") };
28 "If there are more 'containerNames' than 'trackCandTypes' the last type is assumed for all the names that cannot be matched"\
29 "one-to-one with a trackCandType. If there are as much names as types the i-th name is assumed to be of the i-th type");
32 "Use the first container as reference and only collect data for those MCParticle that"\
33 " cannot be found in the other containers.",
false);
35 std::vector<std::string> defaultRootFName = {
"PhaseSpaceAnalysis",
"RECREATE" };
37 " ('UPDATE' or 'RECREATE')", defaultRootFName);
47 B2INFO(
"PhaseSpaceAnalysis ------------------------ initialize --------------------------");
54 if (nTypes > nNames) { B2FATAL(
"Passed " << nTypes <<
" trackCandTypese but only " << nNames <<
" containerNames!"); }
56 for (
size_t iName = 0; iName < nNames; ++iName) {
57 size_t iType = iName < nTypes ? iName : nTypes - 1;
59 if (tcType.compare(std::string(
"GFTC")) != 0 && tcType.compare(std::string(
"SPTC")) != 0) {
60 B2FATAL(
"Found id " << tcType <<
" in 'trackCandTypes' but only 'GFTC' and 'SPTC' are allowed!");
64 if (tcType.compare(std::string(
"GFTC")) == 0) {
74 output +=
"'" +
id +
"' ";
76 B2FATAL(
"PhaseSpaceAnalysis::initialize() : rootFileName is set wrong: entries are: " << output);
93 const int eventCounter = eventMetaDataPtr->getEvent();
94 std::string arrayNames;
96 arrayNames +=
" " + name;
98 B2DEBUG(25,
"PhaseSpaceAnalysis::event(). Processing event " << eventCounter <<
" for StoreArray names :" << arrayNames);
101 B2DEBUG(25,
"Found " << nMCParticles <<
" MCParticles for this event");
103 std::vector<std::vector<int> > mcPartIds;
105 for (std::pair<boost::any, e_trackCandType> storeArray :
m_tcStoreArrays) {
107 if (storeArray.second ==
c_gftc) {
109 }
else if (storeArray.second ==
c_sptc) {
112 B2DEBUG(25, mcPartIds.back().size() <<
" MCParticles of this event were set for this container");
119 for (
size_t iTree = 0; iTree < mcPartIds.size(); ++iTree) {
120 B2DEBUG(25,
"Now collecting values for vector of MCParticle Ids " << iTree);
124 for (
int id : mcPartIds[iTree]) {
127 B2WARNING(
"Found a negative id in mcParticleId: " <<
id << \
128 ". It seems that it has not been set properly, I will skip this MC Particle");
133 if (mcParticle ==
nullptr) {
147 std::stringstream furtherInfo;
150 " nullptr pointers to MCParticles";
153 B2INFO(
"PhaseSpaceAnalysis::terminate(): Collected mcParticle info in " <<
m_PARAMcontainerNames.size() <<
" containers."\
154 " Collected information from " << nMCParts <<
" MCParticles and wrote them into " <<
m_treeNames.size() <<
" different trees."\
155 << furtherInfo.str());
158 std::stringstream output;
159 output <<
"tree-wise summary (no of mcParticles):";
160 for (
unsigned int ctr :
m_mcPartCtr) output <<
" " << ctr;
161 B2DEBUG(25, output.str());
167 for (TTree* tree :
m_treePtrs) { tree->Write(); }
175 B2DEBUG(25,
"initializing root file. fileName: " << fileName <<
", writeOption: " << writeOption);
176 m_rootFilePtr =
new TFile(fileName.c_str(), writeOption.c_str());
178 for (
size_t i = 0; i < treeNames.size(); ++i) {
179 TTree* tree =
new TTree(treeNames[i].c_str(),
"data of mc particles");
204 B2DEBUG(25,
"Collecting values for MCParticle " << mcParticle->
getArrayIndex());
206 const ROOT::Math::XYZVector momentum = mcParticle->
getMomentum();
207 rootVariables.
MomX.push_back(momentum.X());
208 rootVariables.
MomY.push_back(momentum.Y());
209 rootVariables.
MomZ.push_back(momentum.Z());
211 rootVariables.
pT.push_back(momentum.Rho());
212 rootVariables.
Eta.push_back(momentum.Eta());
214 B2DEBUG(25,
"XYZVector momentum: (" << momentum.X() <<
"," << momentum.Y() <<
"," << momentum.Z() << \
215 "). This leads to p_T = " << momentum.Rho() <<
" and eta = " << momentum.Eta());
217 const ROOT::Math::XYZVector vertex = mcParticle->
getVertex();
218 rootVariables.
VertX.push_back(vertex.Y());
219 rootVariables.
VertY.push_back(vertex.Y());
220 rootVariables.
VertZ.push_back(vertex.Z());
222 B2DEBUG(25,
"vertex: (" << vertex.X() <<
"," << vertex.Y() <<
"," << vertex.Z() <<
")");
226 rootVariables.
PDG.push_back(mcParticle->
getPDG());
227 rootVariables.
Mass.push_back(mcParticle->
getMass());
229 B2DEBUG(25,
"Charge " << mcParticle->
getCharge() <<
", PDG code" << mcParticle->
getPDG() <<
", Energy " << \
234template<
typename TrackCandType>
237 const int nTrackCands = trackCands.
getEntries();
238 B2DEBUG(25,
"In StoreArray " << trackCands.
getName() <<
": " << nTrackCands <<
" track candidates for this event");
239 std::vector<int> mcPartIds;
240 for (
int i = 0; i < nTrackCands; ++i) mcPartIds.push_back(trackCands[i]->getMcTrackId());
249 if (allIDs.size() < 2) {
250 B2DEBUG(25,
"There are no more than 1 vectors passed to getDiffIds. No comparison possible. Returning passed vector");
266 std::vector<std::vector<int> > diffIds;
267 std::vector<int> referenceIds = allIDs.at(0);
268 diffIds.push_back(referenceIds);
271 for (
size_t iVec = 1; iVec < allIDs.size(); ++iVec) {
272 std::vector<int> compareIds = allIDs[iVec];
273 diffIds.push_back(compareIds);
274 std::vector<int> notFoundIds;
275 for (
int id : referenceIds) {
276 if (find(compareIds.begin(), compareIds.end(),
id) == compareIds.end()) {
277 notFoundIds.push_back(
id);
280 diffIds.push_back(notFoundIds);
@ c_Event
Different object in each event, all objects/arrays are invalidated after event() function has been ca...
@ c_Debug
Debug: for code development.
static LogSystem & Instance()
Static method to get a reference to the LogSystem instance.
A Class to store the Monte Carlo particle information.
float getEnergy() const
Return particle energy in GeV.
float getMass() const
Return the particle mass in GeV.
ROOT::Math::XYZVector getVertex() const
Return production vertex position, shorthand for getProductionVertex().
int getArrayIndex() const
Get 0-based index of the particle in the corresponding MCParticle list.
float getCharge() const
Return the particle charge defined in TDatabasePDG.
int getPDG() const
Return PDG code of particle.
ROOT::Math::XYZVector getMomentum() const
Return momentum.
void setDescription(const std::string &description)
Sets the description of the module.
void initializeRootFile(std::string fileName, std::string writeOption, std::vector< std::string > treeNames)
create a root file with file name
std::vector< std::string > m_PARAMtrackCandTypes
types of the track candidates in containerNames
std::vector< unsigned int > m_mcPartCtr
container wise counter of TrackCands / MCParticles
void initialize() override
initialize
StoreArray< SpacePointTrackCand > m_SpacePointTrackCands
SpacePointTrackCands StoreArray.
RootVariables m_rootVariables
root variables used for collecting data event-wise
bool m_PARAMdiffAnalysis
take containers pair-wise and only analyze those particles that are found in one container but not in...
unsigned int m_noMcPartCtr
Counter for nullptr pointers to MC Particles (should always be zero.
void event() override
event
void terminate() override
terminate
std::vector< std::string > m_treeNames
names of the trees that are stored in the root file
StoreArray< genfit::TrackCand > m_GenfitTrackCands
Genfit::TrackCands StoreArray.
std::vector< std::string > m_PARAMrootFileName
name of the output root file name
unsigned int m_skippedTCsCtr
Counter for negative MC particle ids (should always be zero.
@ c_gftc
genfit::TrackCand
@ c_sptc
SpacePointTrackCand.
std::vector< TTree * > m_treePtrs
each container name gets its own tree in the root file
std::vector< std::string > m_PARAMcontainerNames
collection name of trackCands to be investigated
void initializeCounters(size_t nContainers)
initialize all counters to 0 to avoid undefined behavior
std::vector< std::vector< int > > getDiffIds(const std::vector< std::vector< int > > &allIDs)
the first vector in allIds is used as reference to which all other vectors of allIDs are compared.
void getValuesForRoot(Belle2::MCParticle *mcParticle, RootVariables &rootVariables)
collect the values for root output
std::vector< int > getMCParticleIDs(Belle2::StoreArray< TrackCandType > trackCands)
get all MCPartileIDs of tracks
TFile * m_rootFilePtr
Pointer to root file.
StoreArray< MCParticle > m_MCParticles
MCParticles StoreArray.
PhaseSpaceAnalysisModule()
Constructor.
std::vector< std::pair< boost::any, e_trackCandType > > m_tcStoreArrays
StoreArrays of the containers.
const std::string & getName() const
Return name under which the object is saved in the DataStore.
bool isRequired(const std::string &name="")
Ensure this array/object has been registered previously.
Accessor to arrays stored in the data store.
int getEntries() const
Get the number of objects in the array.
Type-safe access to single objects in the data store.
void addParam(const std::string &name, T ¶mVariable, const std::string &description, const T &defaultValue)
Adds a new parameter to the module.
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Abstract base class for different kinds of events.
helper class to have all RootVariables assembled in one container
std::vector< double > Mass
mc particle mass
std::vector< double > Eta
pseudo rapidity.
std::vector< double > MomY
y momentum
std::vector< double > VertZ
z position of vertex
std::vector< double > MomX
x momentum
std::vector< int > PDG
mc particle pdg code
std::vector< double > MomZ
z momentum
std::vector< double > VertY
y position of vertex
std::vector< double > VertX
x position of vertex
std::vector< double > Charge
mc particle charge
std::vector< double > pT
transverse momentum.
std::vector< double > Energy
mc particle energy