CMS 3D CMS Logo

/data/doxygen/doxygen-1.7.3/gen/CMSSW_4_2_8/src/CalibTracker/SiStripCommon/plugins/ShallowTree.cc

Go to the documentation of this file.
00001 #include "CalibTracker/SiStripCommon/interface/ShallowTree.h"
00002 
00003 #include "FWCore/Framework/interface/ConstProductRegistry.h" 
00004 #include "FWCore/Framework/interface/GroupSelector.h"
00005 #include "FWCore/Framework/interface/GroupSelectorRules.h"
00006 #include "DataFormats/Provenance/interface/Selections.h"
00007 
00008 #include <map>
00009 #include "boost/foreach.hpp"
00010 #include <TBranch.h>
00011 
00012 void ShallowTree::
00013 analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup) {
00014   BOOST_FOREACH( BranchConnector* connector, connectors)
00015     connector->connect(iEvent);
00016   tree->Fill();
00017 }
00018 
00019 template <class T>
00020 void ShallowTree::TypedBranchConnector<T>::
00021 connect(const edm::Event& iEvent) {
00022   edm::Handle<T> handle_;
00023   iEvent.getByLabel(ml, pin, handle_);
00024   object_ = *handle_;
00025 }
00026 
00027 template <class T> 
00028 ShallowTree::TypedBranchConnector<T>::
00029 TypedBranchConnector(edm::BranchDescription const* desc, 
00030                      std::string t, 
00031                      TTree * tree)
00032   :  ml( desc->moduleLabel() ),  
00033      pin( desc->productInstanceName() )
00034 {
00035   object_ptr_ = &object_;  
00036   std::string s=pin+t;  
00037   if(t!="")  { tree->Branch(pin.c_str(),  object_ptr_, s.c_str() );}  //raw type
00038   else       { tree->Branch(pin.c_str(), &object_ptr_            );}  //vector<type>
00039 }
00040 
00041 void ShallowTree::
00042 beginJob() {
00043   tree = fs->make<TTree>("tree", ""); 
00044 
00045   std::map<std::string, LEAFTYPE> leafmap;
00046   leafmap["bool"]      = BOOL;       leafmap["bools"]     = BOOL_V;
00047   leafmap["short int"] = SHORT;      leafmap["shorts"]    = SHORT_V;
00048   leafmap["ushort int"]= U_SHORT;    leafmap["ushorts"]   = U_SHORT_V;
00049   leafmap["int"]       = INT;        leafmap["ints"]      = INT_V;
00050   leafmap["uint"]      = U_INT;      leafmap["uints"]     = U_INT_V;
00051   leafmap["float"]     = FLOAT;      leafmap["floats"]    = FLOAT_V;
00052   leafmap["double"]    = DOUBLE;     leafmap["doubles"]   = DOUBLE_V;
00053   leafmap["lint"]      = LONG;       leafmap["longs"]     = LONG_V;
00054   leafmap["ulint"]     = U_LONG;     leafmap["ulongs"]    = U_LONG_V;
00055   leafmap["char"]      = CHAR;       leafmap["chars"]     = CHAR_V;
00056   leafmap["uchar"]     = U_CHAR;     leafmap["uchars"]    = U_CHAR_V;
00057 
00058 
00059   edm::Service<edm::ConstProductRegistry> reg;
00060   edm::Selections allBranches = reg->allBranchDescriptions();
00061   edm::GroupSelectorRules groupSelectorRules_(pset, "outputCommands", "ShallowTree");
00062   edm::GroupSelector groupSelector_;
00063   groupSelector_.initialize(groupSelectorRules_, allBranches);
00064 
00065   std::set<std::string> branchnames;
00066 
00067   BOOST_FOREACH( const edm::Selections::value_type& selection, allBranches) {
00068     if(groupSelector_.selected(*selection)) {
00069 
00070       //Check for duplicate branch names
00071       if (branchnames.find( selection->productInstanceName()) != branchnames.end() ) {
00072         throw edm::Exception(edm::errors::Configuration)
00073           << "More than one branch named: "
00074           << selection->productInstanceName() << std::endl
00075           << "Exception thrown from ShallowTree::beginJob" << std::endl;
00076       }
00077       else {
00078         branchnames.insert( selection->productInstanceName() );
00079       }
00080 
00081       //Create ShallowTree branch
00082       switch(leafmap.find( selection->friendlyClassName() )->second) {
00083       case BOOL     :  connectors.push_back( new TypedBranchConnector                      <bool>  (selection, "/O", tree) ); break;
00084       case BOOL_V   :  connectors.push_back( new TypedBranchConnector<std::vector          <bool> >(selection,   "", tree) ); break;
00085       case INT      :  connectors.push_back( new TypedBranchConnector                       <int>  (selection, "/I", tree) ); break;
00086       case INT_V    :  connectors.push_back( new TypedBranchConnector<std::vector           <int> >(selection,   "", tree) ); break;
00087       case U_INT    :  connectors.push_back( new TypedBranchConnector              <unsigned int>  (selection, "/i", tree) ); break;
00088       case U_INT_V  :  connectors.push_back( new TypedBranchConnector<std::vector  <unsigned int> >(selection,   "", tree) ); break;
00089       case SHORT    :  connectors.push_back( new TypedBranchConnector                     <short>  (selection, "/S", tree) ); break;
00090       case SHORT_V  :  connectors.push_back( new TypedBranchConnector<std::vector         <short> >(selection,   "", tree) ); break;
00091       case U_SHORT  :  connectors.push_back( new TypedBranchConnector            <unsigned short>  (selection, "/s", tree) ); break;
00092       case U_SHORT_V:  connectors.push_back( new TypedBranchConnector<std::vector<unsigned short> >(selection,   "", tree) ); break;
00093       case FLOAT    :  connectors.push_back( new TypedBranchConnector                     <float>  (selection, "/F", tree) ); break;
00094       case FLOAT_V  :  connectors.push_back( new TypedBranchConnector<std::vector         <float> >(selection,   "", tree) ); break;
00095       case DOUBLE   :  connectors.push_back( new TypedBranchConnector                    <double>  (selection, "/D", tree) ); break;
00096       case DOUBLE_V :  connectors.push_back( new TypedBranchConnector<std::vector        <double> >(selection,   "", tree) ); break;
00097       case LONG     :  connectors.push_back( new TypedBranchConnector                      <long>  (selection, "/L", tree) ); break;
00098       case LONG_V   :  connectors.push_back( new TypedBranchConnector<std::vector          <long> >(selection,   "", tree) ); break;
00099       case U_LONG   :  connectors.push_back( new TypedBranchConnector             <unsigned long>  (selection, "/l", tree) ); break;
00100       case U_LONG_V :  connectors.push_back( new TypedBranchConnector<std::vector <unsigned long> >(selection,   "", tree) ); break;
00101       case CHAR     :  connectors.push_back( new TypedBranchConnector                      <char>  (selection, "/B", tree) ); break;
00102       case CHAR_V   :  connectors.push_back( new TypedBranchConnector<std::vector          <char> >(selection,   "", tree) ); break;
00103       case U_CHAR   :  connectors.push_back( new TypedBranchConnector             <unsigned char>  (selection, "/b", tree) ); break;
00104       case U_CHAR_V :  connectors.push_back( new TypedBranchConnector<std::vector <unsigned char> >(selection,   "", tree) ); break;
00105       default: 
00106         {
00107           std::string leafstring = "";
00108           typedef std::pair<std::string, LEAFTYPE> pair_t;
00109           BOOST_FOREACH( const pair_t& leaf, leafmap) 
00110             leafstring+= "\t" + leaf.first + "\n";
00111 
00112           throw edm::Exception(edm::errors::Configuration)
00113             << "class ShallowTree does not handle leaves of type " << selection->className() << " like\n"
00114             <<   selection->friendlyClassName()   << "_" 
00115             <<   selection->moduleLabel()         << "_" 
00116             <<   selection->productInstanceName() << "_"  
00117             <<   selection->processName()         << std::endl
00118             << "Valid leaf types are (friendlyClassName):\n"
00119             <<   leafstring
00120             << "Exception thrown from ShallowTree::beginJob\n";
00121         }
00122       }
00123     }
00124   }
00125 }
00126