10 #include <tracking/modules/spacePointCreator/PhaseSpaceAnalysisModule.h>
12 #include <genfit/TrackCand.h>
13 #include <tracking/spacePointCreation/SpacePointTrackCand.h>
15 #include <framework/datastore/StoreArray.h>
16 #include <framework/dataobjects/EventMetaData.h>
17 #include <framework/datastore/StoreObjPtr.h>
32 setDescription(
"Module for analysing the phase space covered by TrackCands (resp. their related MCParticles)");
34 std::vector<std::string> defaultNames = { std::string(
"") };
35 addParam(
"containerNames", m_PARAMcontainerNames,
"Collection names of trackCands to be analized.", defaultNames);
36 addParam(
"trackCandTypes", m_PARAMtrackCandTypes,
"Either 'GFTC' for genfit::TrackCand or 'SPTC' for SpacePointTrackCand." \
37 "If there are more 'containerNames' than 'trackCandTypes' the last type is assumed for all the names that cannot be matched"\
38 "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");
40 addParam(
"diffAnalysis", m_PARAMdiffAnalysis,
41 "Use the first container as reference and only collect data for those MCParticle that"\
42 " cannot be found in the other containers.",
false);
44 std::vector<std::string> defaultRootFName = {
"PhaseSpaceAnalysis",
"RECREATE" };
45 addParam(
"rootFileName", m_PARAMrootFileName,
"Name of the output root file without '.root' file ending + write mode"\
46 " ('UPDATE' or 'RECREATE')", defaultRootFName);
50 initializeCounters(0);
54 void PhaseSpaceAnalysisModule::initialize()
56 B2INFO(
"PhaseSpaceAnalysis ------------------------ initialize --------------------------");
59 mcParticles.isRequired();
61 size_t nNames = m_PARAMcontainerNames.size();
62 size_t nTypes = m_PARAMtrackCandTypes.size();
64 if (nTypes > nNames) { B2FATAL(
"Passed " << nTypes <<
" trackCandTypese but only " << nNames <<
" containerNames!"); }
66 for (
size_t iName = 0; iName < nNames; ++iName) {
67 size_t iType = iName < nTypes ? iName : nTypes - 1;
68 string tcType = m_PARAMtrackCandTypes.at(iType);
69 if (tcType.compare(
string(
"GFTC")) != 0 && tcType.compare(
string(
"SPTC")) != 0) {
70 B2FATAL(
"Found id " << tcType <<
" in 'trackCandTypes' but only 'GFTC' and 'SPTC' are allowed!");
73 string contName = m_PARAMcontainerNames.at(iName);
74 if (tcType.compare(
string(
"GFTC")) == 0) {
76 trackCands.isRequired();
77 m_tcStoreArrays.push_back(make_pair(trackCands, c_gftc));
80 trackCands.isRequired();
81 m_tcStoreArrays.push_back(make_pair(trackCands, c_sptc));
83 if (m_PARAMrootFileName.size() != 2 || (m_PARAMrootFileName[1] !=
"UPDATE" && m_PARAMrootFileName[1] !=
"RECREATE")) {
86 for (
string id : m_PARAMrootFileName) { 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();
107 for (
string name : m_PARAMcontainerNames) { arrayNames +=
" " + name; }
108 B2DEBUG(10,
"PhaseSpaceAnalysis::event(). Processing event " << eventCounter <<
" for StoreArray names :" << arrayNames);
111 const int nMCParticles = mcParticles.
getEntries();
112 B2DEBUG(25,
"Found " << nMCParticles <<
" MCParticles for this event");
114 vector<vector<int> > mcPartIds;
116 for (pair<boost::any, e_trackCandType> storeArray : m_tcStoreArrays) {
118 if (storeArray.second == c_gftc) {
120 }
else if (storeArray.second == c_sptc) {
123 B2DEBUG(25, mcPartIds.back().size() <<
" MCParticles of this event were set for this container");
126 if (m_PARAMdiffAnalysis) { mcPartIds = getDiffIds(mcPartIds); }
130 for (
size_t iTree = 0; iTree < mcPartIds.size(); ++iTree) {
131 B2DEBUG(25,
"Now collecting values for vector of MCParticle Ids " << iTree);
132 m_mcPartCtr[iTree]++;
135 for (
int id : mcPartIds[iTree]) {
137 B2WARNING(
"Found a negative id in mcParticleId: " <<
id << \
138 ". It seems that it has not been set properly, I will skip this MC Particle");
143 if (mcParticle == NULL) {
147 getValuesForRoot(mcParticle, m_rootVariables);
149 m_treePtrs[iTree]->Fill();
155 void PhaseSpaceAnalysisModule::terminate()
157 stringstream furtherInfo;
158 if (m_skippedTCsCtr || m_noMcPartCtr) {
159 furtherInfo <<
" There were " << m_skippedTCsCtr <<
" negative mcParticle IDs and " << m_noMcPartCtr <<
160 " NULL pointers to MCParticles";
162 unsigned int nMCParts = accumulate(m_mcPartCtr.begin(), m_mcPartCtr.end(), 0);
163 B2INFO(
"PhaseSpaceAnalysis::terminate(): Collected mcParticle info in " << m_PARAMcontainerNames.size() <<
" containers."\
164 " Collected information from " << nMCParts <<
" MCParticles and wrote them into " << m_treeNames.size() <<
" different trees."\
165 << furtherInfo.str());
167 if (LogSystem::Instance().isLevelEnabled(LogConfig::c_Debug, 1, PACKAGENAME())) {
169 output <<
"tree-wise summary (no of mcParticles):";
170 for (
unsigned int ctr : m_mcPartCtr) output <<
" " << ctr;
171 B2DEBUG(1, output.str());
175 if (m_rootFilePtr != NULL) {
177 for (TTree* tree : m_treePtrs) { tree->Write(); }
178 m_rootFilePtr->Close();
183 void PhaseSpaceAnalysisModule::initializeRootFile(std::string fileName, std::string writeOption, std::vector<std::string> treeNames)
185 B2DEBUG(10,
"initializing root file. fileName: " << fileName <<
", writeOption: " << writeOption);
186 m_rootFilePtr =
new TFile(fileName.c_str(), writeOption.c_str());
188 for (
size_t i = 0; i < treeNames.size(); ++i) {
189 TTree* tree =
new TTree(treeNames[i].c_str(),
"data of mc particles");
190 tree->Branch(
"momentum_X", &m_rootVariables.MomX);
191 tree->Branch(
"momentum_Y", &m_rootVariables.MomY);
192 tree->Branch(
"momentum_Z", &m_rootVariables.MomZ);
194 tree->Branch(
"eta", &m_rootVariables.Eta);
195 tree->Branch(
"p_T", &m_rootVariables.pT);
197 tree->Branch(
"vertex_X", &m_rootVariables.VertX);
198 tree->Branch(
"vertex_Y", &m_rootVariables.VertY);
199 tree->Branch(
"vertex_Z", &m_rootVariables.VertZ);
201 tree->Branch(
"energy", &m_rootVariables.Energy);
202 tree->Branch(
"mass", &m_rootVariables.Mass);
204 tree->Branch(
"charge", &m_rootVariables.Charge);
205 tree->Branch(
"pdg_code", &m_rootVariables.PDG);
207 m_treePtrs.push_back(tree);
214 B2DEBUG(100,
"Collecting values for MCParticle " << mcParticle->
getArrayIndex());
216 const TVector3 momentum = mcParticle->
getMomentum();
217 rootVariables.
MomX.push_back(momentum.X());
218 rootVariables.
MomY.push_back(momentum.Y());
219 rootVariables.
MomZ.push_back(momentum.Z());
221 rootVariables.
pT.push_back(momentum.Pt());
222 rootVariables.
Eta.push_back(momentum.Eta());
224 B2DEBUG(499,
"TVector3 momentum: (" << momentum.X() <<
"," << momentum.Y() <<
"," << momentum.Z() << \
225 "). This leads to p_T = " << momentum.Pt() <<
" and eta = " << momentum.Eta());
227 const TVector3 vertex = mcParticle->
getVertex();
228 rootVariables.
VertX.push_back(vertex.Y());
229 rootVariables.
VertY.push_back(vertex.Y());
230 rootVariables.
VertZ.push_back(vertex.Z());
232 B2DEBUG(499,
"TVector3 vertex: (" << vertex.X() <<
"," << vertex.Y() <<
"," << vertex.Z() <<
")");
236 rootVariables.
PDG.push_back(mcParticle->
getPDG());
237 rootVariables.
Mass.push_back(mcParticle->
getMass());
239 B2DEBUG(499,
"Charge " << mcParticle->
getCharge() <<
", PDG code" << mcParticle->
getPDG() <<
", Energy " << \
244 template<
typename TrackCandType>
247 const int nTrackCands = trackCands.
getEntries();
248 B2DEBUG(25,
"In StoreArray " << trackCands.getName() <<
": " << nTrackCands <<
" track candidates for this event");
249 std::vector<int> mcPartIds;
250 for (
int i = 0; i < nTrackCands; ++i) mcPartIds.push_back(trackCands[i]->getMcTrackId());
257 std::vector<std::vector<int> > PhaseSpaceAnalysisModule::getDiffIds(
const std::vector<std::vector<int> >& allIDs)
259 if (allIDs.size() < 2) {
260 B2DEBUG(50,
"There are no more than 1 vectors passed to getDiffIds. No comparison possible. Returning passed vector");
276 vector<vector<int> > diffIds;
277 vector<int> referenceIds = allIDs.at(0);
278 diffIds.push_back(referenceIds);
281 for (
size_t iVec = 1; iVec < allIDs.size(); ++iVec) {
282 vector<int> compareIds = allIDs[iVec];
283 diffIds.push_back(compareIds);
284 vector<int> notFoundIds;
285 for (
int id : referenceIds) {
286 if (find(compareIds.begin(), compareIds.end(),
id) == compareIds.end()) {
287 notFoundIds.push_back(
id);
290 diffIds.push_back(notFoundIds);