9 #include <tracking/modules/spacePointCreator/PhaseSpaceAnalysisModule.h>
11 #include <genfit/TrackCand.h>
12 #include <tracking/spacePointCreation/SpacePointTrackCand.h>
14 #include <framework/datastore/StoreArray.h>
15 #include <framework/dataobjects/EventMetaData.h>
16 #include <framework/datastore/StoreObjPtr.h>
31 setDescription(
"Module for analysing the phase space covered by TrackCands (resp. their related MCParticles)");
33 std::vector<std::string> defaultNames = { std::string(
"") };
34 addParam(
"containerNames", m_PARAMcontainerNames,
"Collection names of trackCands to be analized.", defaultNames);
35 addParam(
"trackCandTypes", m_PARAMtrackCandTypes,
"Either 'GFTC' for genfit::TrackCand or 'SPTC' for SpacePointTrackCand." \
36 "If there are more 'containerNames' than 'trackCandTypes' the last type is assumed for all the names that cannot be matched"\
37 "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");
39 addParam(
"diffAnalysis", m_PARAMdiffAnalysis,
40 "Use the first container as reference and only collect data for those MCParticle that"\
41 " cannot be found in the other containers.",
false);
43 std::vector<std::string> defaultRootFName = {
"PhaseSpaceAnalysis",
"RECREATE" };
44 addParam(
"rootFileName", m_PARAMrootFileName,
"Name of the output root file without '.root' file ending + write mode"\
45 " ('UPDATE' or 'RECREATE')", defaultRootFName);
48 m_rootFilePtr =
nullptr;
49 initializeCounters(0);
53 void PhaseSpaceAnalysisModule::initialize()
55 B2INFO(
"PhaseSpaceAnalysis ------------------------ initialize --------------------------");
60 size_t nNames = m_PARAMcontainerNames.size();
61 size_t nTypes = m_PARAMtrackCandTypes.size();
63 if (nTypes > nNames) { B2FATAL(
"Passed " << nTypes <<
" trackCandTypese but only " << nNames <<
" containerNames!"); }
65 for (
size_t iName = 0; iName < nNames; ++iName) {
66 size_t iType = iName < nTypes ? iName : nTypes - 1;
67 string tcType = m_PARAMtrackCandTypes.at(iType);
68 if (tcType.compare(
string(
"GFTC")) != 0 && tcType.compare(
string(
"SPTC")) != 0) {
69 B2FATAL(
"Found id " << tcType <<
" in 'trackCandTypes' but only 'GFTC' and 'SPTC' are allowed!");
72 string contName = m_PARAMcontainerNames.at(iName);
73 if (tcType.compare(
string(
"GFTC")) == 0) {
76 m_tcStoreArrays.push_back(make_pair(trackCands, c_gftc));
80 m_tcStoreArrays.push_back(make_pair(trackCands, c_sptc));
82 if (m_PARAMrootFileName.size() != 2 || (m_PARAMrootFileName[1] !=
"UPDATE" && m_PARAMrootFileName[1] !=
"RECREATE")) {
84 for (
string id : m_PARAMrootFileName) {
85 output +=
"'" +
id +
"' ";
87 B2FATAL(
"PhaseSpaceAnalysis::initialize() : rootFileName is set wrong: entries are: " << output);
90 m_treeNames.push_back(contName);
91 if (m_PARAMdiffAnalysis && iName != 0) m_treeNames.push_back(contName +
"_diff");
94 initializeRootFile(m_PARAMrootFileName[0] +
string(
".root"), m_PARAMrootFileName[1], m_treeNames);
95 initializeCounters(m_treeNames.size());
100 void PhaseSpaceAnalysisModule::event()
104 const int eventCounter = eventMetaDataPtr->getEvent();
106 for (
string name : m_PARAMcontainerNames) {
107 arrayNames +=
" " + name;
109 B2DEBUG(10,
"PhaseSpaceAnalysis::event(). Processing event " << eventCounter <<
" for StoreArray names :" << arrayNames);
112 const int nMCParticles = mcParticles.
getEntries();
113 B2DEBUG(25,
"Found " << nMCParticles <<
" MCParticles for this event");
115 vector<vector<int> > mcPartIds;
117 for (pair<boost::any, e_trackCandType> storeArray : m_tcStoreArrays) {
119 if (storeArray.second == c_gftc) {
121 }
else if (storeArray.second == c_sptc) {
124 B2DEBUG(25, mcPartIds.back().size() <<
" MCParticles of this event were set for this container");
127 if (m_PARAMdiffAnalysis) { mcPartIds = getDiffIds(mcPartIds); }
131 for (
size_t iTree = 0; iTree < mcPartIds.size(); ++iTree) {
132 B2DEBUG(25,
"Now collecting values for vector of MCParticle Ids " << iTree);
133 m_mcPartCtr[iTree]++;
136 for (
int id : mcPartIds[iTree]) {
138 B2WARNING(
"Found a negative id in mcParticleId: " <<
id << \
139 ". It seems that it has not been set properly, I will skip this MC Particle");
144 if (mcParticle ==
nullptr) {
148 getValuesForRoot(mcParticle, m_rootVariables);
150 m_treePtrs[iTree]->Fill();
156 void PhaseSpaceAnalysisModule::terminate()
158 stringstream furtherInfo;
159 if (m_skippedTCsCtr || m_noMcPartCtr) {
160 furtherInfo <<
" There were " << m_skippedTCsCtr <<
" negative mcParticle IDs and " << m_noMcPartCtr <<
161 " nullptr pointers to MCParticles";
163 unsigned int nMCParts = accumulate(m_mcPartCtr.begin(), m_mcPartCtr.end(), 0);
164 B2INFO(
"PhaseSpaceAnalysis::terminate(): Collected mcParticle info in " << m_PARAMcontainerNames.size() <<
" containers."\
165 " Collected information from " << nMCParts <<
" MCParticles and wrote them into " << m_treeNames.size() <<
" different trees."\
166 << furtherInfo.str());
168 if (LogSystem::Instance().isLevelEnabled(LogConfig::c_Debug, 1, PACKAGENAME())) {
170 output <<
"tree-wise summary (no of mcParticles):";
171 for (
unsigned int ctr : m_mcPartCtr) output <<
" " << ctr;
172 B2DEBUG(1, output.str());
176 if (m_rootFilePtr !=
nullptr) {
178 for (TTree* tree : m_treePtrs) { tree->Write(); }
179 m_rootFilePtr->Close();
184 void PhaseSpaceAnalysisModule::initializeRootFile(std::string fileName, std::string writeOption, std::vector<std::string> treeNames)
186 B2DEBUG(10,
"initializing root file. fileName: " << fileName <<
", writeOption: " << writeOption);
187 m_rootFilePtr =
new TFile(fileName.c_str(), writeOption.c_str());
189 for (
size_t i = 0; i < treeNames.size(); ++i) {
190 TTree* tree =
new TTree(treeNames[i].c_str(),
"data of mc particles");
191 tree->Branch(
"momentum_X", &m_rootVariables.MomX);
192 tree->Branch(
"momentum_Y", &m_rootVariables.MomY);
193 tree->Branch(
"momentum_Z", &m_rootVariables.MomZ);
195 tree->Branch(
"eta", &m_rootVariables.Eta);
196 tree->Branch(
"p_T", &m_rootVariables.pT);
198 tree->Branch(
"vertex_X", &m_rootVariables.VertX);
199 tree->Branch(
"vertex_Y", &m_rootVariables.VertY);
200 tree->Branch(
"vertex_Z", &m_rootVariables.VertZ);
202 tree->Branch(
"energy", &m_rootVariables.Energy);
203 tree->Branch(
"mass", &m_rootVariables.Mass);
205 tree->Branch(
"charge", &m_rootVariables.Charge);
206 tree->Branch(
"pdg_code", &m_rootVariables.PDG);
208 m_treePtrs.push_back(tree);
215 B2DEBUG(100,
"Collecting values for MCParticle " << mcParticle->
getArrayIndex());
217 const TVector3 momentum = mcParticle->
getMomentum();
218 rootVariables.
MomX.push_back(momentum.X());
219 rootVariables.
MomY.push_back(momentum.Y());
220 rootVariables.
MomZ.push_back(momentum.Z());
222 rootVariables.
pT.push_back(momentum.Pt());
223 rootVariables.
Eta.push_back(momentum.Eta());
225 B2DEBUG(499,
"TVector3 momentum: (" << momentum.X() <<
"," << momentum.Y() <<
"," << momentum.Z() << \
226 "). This leads to p_T = " << momentum.Pt() <<
" and eta = " << momentum.Eta());
228 const TVector3 vertex = mcParticle->
getVertex();
229 rootVariables.
VertX.push_back(vertex.Y());
230 rootVariables.
VertY.push_back(vertex.Y());
231 rootVariables.
VertZ.push_back(vertex.Z());
233 B2DEBUG(499,
"TVector3 vertex: (" << vertex.X() <<
"," << vertex.Y() <<
"," << vertex.Z() <<
")");
237 rootVariables.
PDG.push_back(mcParticle->
getPDG());
238 rootVariables.
Mass.push_back(mcParticle->
getMass());
240 B2DEBUG(499,
"Charge " << mcParticle->
getCharge() <<
", PDG code" << mcParticle->
getPDG() <<
", Energy " << \
245 template<
typename TrackCandType>
248 const int nTrackCands = trackCands.
getEntries();
249 B2DEBUG(25,
"In StoreArray " << trackCands.
getName() <<
": " << nTrackCands <<
" track candidates for this event");
250 std::vector<int> mcPartIds;
251 for (
int i = 0; i < nTrackCands; ++i) mcPartIds.push_back(trackCands[i]->getMcTrackId());
258 std::vector<std::vector<int> > PhaseSpaceAnalysisModule::getDiffIds(
const std::vector<std::vector<int> >& allIDs)
260 if (allIDs.size() < 2) {
261 B2DEBUG(50,
"There are no more than 1 vectors passed to getDiffIds. No comparison possible. Returning passed vector");
277 vector<vector<int> > diffIds;
278 vector<int> referenceIds = allIDs.at(0);
279 diffIds.push_back(referenceIds);
282 for (
size_t iVec = 1; iVec < allIDs.size(); ++iVec) {
283 vector<int> compareIds = allIDs[iVec];
284 diffIds.push_back(compareIds);
285 vector<int> notFoundIds;
286 for (
int id : referenceIds) {
287 if (find(compareIds.begin(), compareIds.end(),
id) == compareIds.end()) {
288 notFoundIds.push_back(
id);
291 diffIds.push_back(notFoundIds);
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.
int getArrayIndex() const
Get 0-based index of the particle in the corresponding MCParticle list.
TVector3 getVertex() const
Return production vertex position, shorthand for getProductionVertex().
TVector3 getMomentum() const
Return momentum.
float getCharge() const
Return the particle charge defined in TDatabasePDG.
int getPDG() const
Return PDG code of particle.
Module for analysing the phase space of genfit::TrackCand(s) and SpacePointTrackCand(s) NOTE: this is...
bool isRequired(const std::string &name="")
Ensure this array/object has been registered previously.
const std::string & getName() const
Return name under which the object is saved in the DataStore.
int getEntries() const
Get the number of objects in the array.
Type-safe access to single objects in the data store.
#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