Skip to content
Snippets Groups Projects

Draft: Optimise the FIND_DECAY and FIND_MCDECAY functors

Open Abhijit Mathad requested to merge AM_ThorFindDecay into 2024-patches
6 files
+ 964
931
Compare changes
  • Side-by-side
  • Inline
Files
6
@@ -38,7 +38,7 @@ namespace Decay {
@@ -38,7 +38,7 @@ namespace Decay {
/// constructors
/// constructors
Node();
Node();
Node( const std::string& node_name );
Node( const std::string& node_name );
Node( const int& pid, const bool& hasCC, const bool& isMarked );
Node( int pid, bool hasCC, bool isMarked );
Node( const detail::ptr_of_ptrv1& particle );
Node( const detail::ptr_of_ptrv1& particle );
Node( const detail::ptr_of_ptrv1mc& mcparticle );
Node( const detail::ptr_of_ptrv1mc& mcparticle );
@@ -81,14 +81,18 @@ namespace Decay {
@@ -81,14 +81,18 @@ namespace Decay {
friend bool operator!=( const Node& lhs, const Node& rhs );
friend bool operator!=( const Node& lhs, const Node& rhs );
/// non-templated class implementation, comparaison between a Particle and a Node
/// non-templated class implementation, comparaison between a Particle and a Node
template <class PARTICLE>
template <class PARTICLE>
friend bool operator==( const Node& lhs, const PARTICLE* const rhs );
friend bool operator==( const Node& lhs, const PARTICLE* const rhs ) {
 
return lhs.m_pid == rhs->particleID().pid();
 
}
template <class PARTICLE>
template <class PARTICLE>
friend bool operator!=( const Node& lhs, const PARTICLE* const rhs ) {
friend bool operator!=( const Node& lhs, const PARTICLE* const rhs ) {
return !( lhs == rhs );
return !( lhs == rhs );
}
}
/// non-templated class implementation, comparaison between a Particle and a Node
/// non-templated class implementation, comparaison between a Particle and a Node
template <class PARTICLE>
template <class PARTICLE>
friend bool operator==( const PARTICLE* const lhs, const Node& rhs );
friend bool operator==( const PARTICLE* const lhs, const Node& rhs ) {
 
return rhs == lhs;
 
}
template <class PARTICLE>
template <class PARTICLE>
friend bool operator!=( const PARTICLE* const lhs, const Node& rhs ) {
friend bool operator!=( const PARTICLE* const lhs, const Node& rhs ) {
return !( lhs == rhs );
return !( lhs == rhs );
@@ -122,208 +126,4 @@ namespace Decay {
@@ -122,208 +126,4 @@ namespace Decay {
};
};
} // namespace Decay
} // namespace Decay
namespace Decay {
Node::Node() : m_pid{0}, m_hasCC{false}, m_isMarked{false} {}
Node::Node( const std::string& node_name ) {
detail::tuple_nodeprop_t tuple_nodeprops = pruneNodeName( node_name );
m_pid = getPidFromName( std::get<0>( tuple_nodeprops ) );
m_hasCC = std::get<1>( tuple_nodeprops );
m_isMarked = std::get<2>( tuple_nodeprops );
}
Node::Node( const detail::ptr_of_ptrv1& particle )
: m_pid{( *particle.get() )->particleID().pid()}, m_hasCC{false}, m_isMarked{false} {}
Node::Node( const detail::ptr_of_ptrv1mc& mcparticle )
: m_pid{( *mcparticle.get() )->particleID().pid()}, m_hasCC{false}, m_isMarked{false} {}
// constructor for Node with pid, hasCC and isMarked
Node::Node( const int& pid, const bool& hasCC, const bool& isMarked )
: m_pid{pid}, m_hasCC{hasCC}, m_isMarked{isMarked} {}
Node::Node( const Node& node ) { // copy constructor
m_pid = node.m_pid;
m_hasCC = node.m_hasCC;
m_isMarked = node.m_isMarked;
}
Node& Node::operator=( const Node& node ) { // copy assignment operator
if ( &node == this ) return *this;
m_pid = node.m_pid;
m_hasCC = node.m_hasCC;
m_isMarked = node.m_isMarked;
return *this;
}
Node Node::getChargeConjugate( bool reversehasCC ) const { // get charge conjugated node
// make a copy of the current node
Node node_cc{*this};
// set the pid of the antiparticle
node_cc.setpid( getAntiParticlePid( m_pid ) );
// reverse the hasCC flag (if requested) as it can help with
// the recursive calling of operator==
if ( reversehasCC ) node_cc.reversehasCC();
return node_cc;
}
int Node::getAntiParticlePid( const int& pid ) const {
// get the particle property of the node
const auto* part_prop = m_ppSvc->find( LHCb::ParticleID( pid ) ); // ptr to LHCb::ParticleProperty
if ( !part_prop )
throw GaudiException{"Could not interpret the particle ID " + std::to_string( pid ) +
". Please check your descriptor.",
"Decay::Node::getChargeConjugate()", StatusCode::FAILURE};
// get antiparticle pid of the current node
const auto* part_prop_cc = part_prop->antiParticle();
if ( !part_prop_cc )
throw GaudiException{"Could not find antiparticle of the particle with ID " + std::to_string( pid ) +
". Please check your descriptor.",
"Decay::Node::getChargeConjugate()", StatusCode::FAILURE};
return ( part_prop_cc->pid() ).pid();
}
bool Node::checkisMarked( const std::string& node_name ) const {
// output true if the expression has a "^"
return std::regex_search( node_name, std::regex{"\\^"} ); // matches a hat
};
bool Node::checkhasCC( const std::string& node_name ) const {
// output true if the expression has "CC"
return std::regex_search( node_name, std::regex{"C{2}"} ); // matches 2 C's in a row
}
/// returns a vector of node possibilities based on the CC property of the Node
std::vector<Node> Node::getPossibilities() const {
// make a empty vector of nodes
std::vector<Node> vectorOfNodes;
// set hasCC property to false for all possibilities because we want vector of "simple" nodes
bool hasCC{false};
if ( m_hasCC ) {
// if has cc reserve two nodes in memory
vectorOfNodes.reserve( 2 );
vectorOfNodes.emplace_back( m_pid, hasCC, m_isMarked );
vectorOfNodes.emplace_back( getAntiParticlePid( m_pid ), hasCC, m_isMarked );
return vectorOfNodes;
}
vectorOfNodes.reserve( 1 );
vectorOfNodes.emplace_back( m_pid, hasCC, m_isMarked );
return vectorOfNodes;
}
detail::tuple_nodeprop_t Node::pruneNodeName( const std::string& node_name ) const { // TODO: make sure this
// is well
// documented
std::string pruned_node_name;
bool hasCC = checkhasCC( node_name );
bool isMarked = checkisMarked( node_name );
// cleans the nodes from operators and returns the particle name only
if ( hasCC && isMarked ) { // if node has self CC and is marked
std::regex expression( "\\^" // hat
"\\s*\\[" // any whitespaces and a square bracket
"\\s*(\\S+)\\s*" // any whitespace then one or more non-whitespace character(s)
// that is matched "(\\S+)" then any whitespace
"\\]\\s*CC" // closing square bracket then any whitespace and then a CC
);
std::smatch what;
std::regex_search( node_name, what, expression );
pruned_node_name = what.str( 1 );
} else if ( hasCC && !isMarked ) { // if node has self CC
std::regex expression( "\\[" // square bracket
"\\s*(\\S+)\\s*" // any whitespace then one or more non-whitespace character(s)
// that is matched "(\\S+)" then any whitespace
"\\]\\s*CC" // closing square bracket then any whitespace and then a CC
);
std::smatch what;
std::regex_search( node_name, what, expression );
pruned_node_name = what.str( 1 );
} else if ( !hasCC && isMarked ) { // if node is marked
std::regex expression( "\\^" // hat
"\\s*(\\S+)\\s*" // any whitespace then one or more non-whitespace character(s)
// that is matched "(\\S+)" then any whitespace
);
std::smatch what;
std::regex_search( node_name, what, expression );
pruned_node_name = what.str( 1 );
} else {
std::regex expression( "^\\s*" // any initial whitespaces
"(\\S+)" // matches particle name
"\\s*$" // any final whitespaces
);
std::smatch what;
std::regex_search( node_name, what, expression );
pruned_node_name = what.str( 1 );
}
return std::make_tuple( pruned_node_name, hasCC, isMarked );
}
int Node::getPidFromName( const std::string& name ) const {
const auto* part_prop = m_ppSvc->find( name ); // ptr to LHCb::ParticleProperty
if ( !part_prop )
throw GaudiException{"Could not interpret the node name " + name + ". Please check your descriptor.",
"Decay::Node::getPidFromName()", StatusCode::FAILURE};
return ( part_prop->pid() ).pid();
}
std::string Node::getNameFromPid( const int& pid ) const {
const auto* part_prop = m_ppSvc->find( LHCb::ParticleID( pid ) ); // ptr to LHCb::ParticleProperty
if ( !part_prop )
throw GaudiException{"Could not interpret the particle ID " + std::to_string( pid ) +
". Please check your descriptor.",
"Decay::Node::getNameFromPid()", StatusCode::FAILURE};
return part_prop->name();
}
std::ostream& operator<<( std::ostream& os, const Node& node ) {
if ( node.m_hasCC ) {
if ( node.m_isMarked )
os << "^[" << node.name() << "]CC";
else
os << "[" << node.name() << "]CC";
} else if ( node.m_isMarked )
os << "^" << node.name();
else
os << node.name();
// MsgStream log( m_msgSvc.get(), "Decay::Node" );
// log << MSG::INFO << "Node: Defining boost regex: " << endmsg;
return os;
}
bool operator==( const Node& lhs, const Node& rhs ) {
if ( lhs.m_pid == rhs.m_pid ) return true;
bool reversehasCC = true;
if ( lhs.m_hasCC ) {
// Return a copy of this node with charged conjugate node and hasCC flag set to false
// hasCC flag has to be false otherwise stuck in infinite loop
Node lhs_cc{lhs.getChargeConjugate( reversehasCC )};
return rhs == lhs_cc;
} else if ( rhs.m_hasCC ) {
// Return a copy of this node with charged conjugate node and hasCC flag set to false
// hasCC flag has to be false otherwise stuck in infinite loop
Node rhs_cc{rhs.getChargeConjugate( reversehasCC )};
return lhs == rhs_cc;
}
return false;
}
bool operator!=( const Node& lhs, const Node& rhs ) { return !( lhs == rhs ); }
template <class PARTICLE>
bool operator==( const Node& lhs, const PARTICLE* const rhs ) {
if ( lhs.m_pid == ( rhs->particleID() ).pid() ) return true;
return false;
}
template <class PARTICLE>
bool operator==( const PARTICLE* const lhs, const Node& rhs ) {
if ( ( lhs->particleID() ).pid() == rhs.m_pid ) return true;
return false;
}
} // namespace Decay
#endif // NODE_H
#endif // NODE_H
\ No newline at end of file
Loading