CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch12/src/DataFormats/Candidate/src/NamedCompositeCandidate.cc

Go to the documentation of this file.
00001 // $Id: NamedCompositeCandidate.cc,v 1.4 2008/07/22 06:07:44 llista Exp $
00002 #include "DataFormats/Candidate/interface/NamedCompositeCandidate.h"
00003 #include "FWCore/Utilities/interface/Exception.h"
00004 
00005 #include <iostream> 
00006 
00007 using namespace reco;
00008 
00009 NamedCompositeCandidate::NamedCompositeCandidate(std::string name, 
00010                                                  const NamedCompositeCandidate::role_collection & roles,
00011                                                  const Candidate & c) :
00012   CompositeCandidate(c),
00013   name_(name),
00014   roles_(roles)
00015 {
00016 
00017   // Check if there are the same number of daughters and roles
00018   int N1 = roles_.size();
00019   int N2 = numberOfDaughters();
00020 
00021   if ( N1 != N2 ) {
00022     throw cms::Exception("InvalidReference")
00023       << "NamedCompositeCandidate constructor: Number of roles and daughters differ, this is an error. Name = " << name << "\n";
00024   }
00025 }
00026 
00027 NamedCompositeCandidate::~NamedCompositeCandidate() { clearDaughters(); clearRoles(); }
00028 
00029 NamedCompositeCandidate * NamedCompositeCandidate::clone() const { return new NamedCompositeCandidate( * this ); }
00030 
00031 void NamedCompositeCandidate::applyRoles()
00032 {
00033 
00034   // Check if there are the same number of daughters and roles
00035   int N1 = roles_.size();
00036   int N2 = numberOfDaughters();
00037   if ( N1 != N2 ) {
00038     throw cms::Exception("InvalidReference")
00039       << "NamedCompositeCandidate::applyRoles : Number of roles and daughters differ, this is an error.\n";
00040   }
00041   // Set up the daughter roles
00042   for ( int i = 0 ; i < N1; ++i ) {
00043     std::string role = roles_[i];
00044     Candidate * c = CompositeCandidate::daughter( i );
00045 
00046     NamedCompositeCandidate * c1 = dynamic_cast<NamedCompositeCandidate *>(c);
00047     if ( c1 != 0 ) {
00048       c1->setName( role );
00049     }
00050   }
00051 }
00052 
00053 Candidate * NamedCompositeCandidate::daughter(const std::string& s ) 
00054 {
00055   int ret = -1;
00056   int i = 0, N = roles_.size();
00057   bool found = false;
00058   for ( ; i < N && !found; ++i ) {
00059     if ( s == roles_[i] ) {
00060       found = true;
00061       ret = i;
00062     }
00063   }
00064 
00065   if ( ret < 0 ) {
00066     throw cms::Exception("InvalidReference")
00067       << "NamedCompositeCandidate::daughter: Cannot find role " << s << "\n";
00068   }
00069   
00070   return daughter(ret);
00071 }
00072 
00073 const Candidate * NamedCompositeCandidate::daughter(const std::string& s ) const 
00074 {
00075   int ret = -1;
00076   int i = 0, N = roles_.size();
00077   bool found = false;
00078   for ( ; i < N && !found; ++i ) {
00079     if ( s == roles_[i] ) {
00080       found = true;
00081       ret = i;
00082     }
00083   }
00084 
00085   if ( ret < 0 ) {
00086     throw cms::Exception("InvalidReference")
00087       << "NamedCompositeCandidate::daughter: Cannot find role " << s << "\n";
00088   }
00089   
00090   return daughter(ret);
00091 }
00092 
00093 void NamedCompositeCandidate::addDaughter( const Candidate & cand, const std::string& s )
00094 {
00095 
00096   role_collection::iterator begin = roles_.begin(), end = roles_.end();
00097   bool isFound = ( find( begin, end, s) != end );
00098   if ( isFound ) {
00099     throw cms::Exception("InvalidReference")
00100       << "NamedCompositeCandidate::addDaughter: Already have role with name " << s 
00101       << ", please clearDaughters, or use a new name\n";
00102   }
00103 
00104   roles_.push_back( s );
00105   std::auto_ptr<Candidate> c( cand.clone() );
00106   NamedCompositeCandidate * c1 =  dynamic_cast<NamedCompositeCandidate*>(&*c);
00107   if ( c1 != 0 ) {
00108     c1->setName( s );
00109   }
00110   CompositeCandidate::addDaughter( c );
00111 }
00112 
00113 void NamedCompositeCandidate::addDaughter( std::auto_ptr<Candidate> cand, const std::string& s )
00114 {
00115   role_collection::iterator begin = roles_.begin(), end = roles_.end();
00116   bool isFound = ( find( begin, end, s) != end );
00117   if ( isFound ) {
00118     throw cms::Exception("InvalidReference")
00119       << "NamedCompositeCandidate::addDaughter: Already have role with name " << s 
00120       << ", please clearDaughters, or use a new name\n";
00121   }
00122 
00123   roles_.push_back( s );
00124   NamedCompositeCandidate * c1 = dynamic_cast<NamedCompositeCandidate*>(&*cand);
00125   if ( c1 != 0 ) {
00126     c1->setName( s );
00127   }
00128   CompositeCandidate::addDaughter( cand );
00129 }
00130 
00131