ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/ShallowTools/plugins/ShallowTree.cc
Revision: 1.1
Committed: Sat Jun 6 14:23:09 2009 UTC (15 years, 11 months ago) by bbetchar
Content type: text/plain
Branch: MAIN
CVS Tags: V03-00-02, V03-00-01, V03-00-00, HEAD
Log Message:
Move directories around

File Contents

# Content
1 #include "UserCode/ShallowTools/interface/ShallowTree.h"
2
3 #include "FWCore/Framework/interface/ConstProductRegistry.h"
4 #include "FWCore/Framework/interface/GroupSelector.h"
5 #include "FWCore/Framework/interface/GroupSelectorRules.h"
6 #include "DataFormats/Provenance/interface/Selections.h"
7
8 #include <map>
9 #include "boost/foreach.hpp"
10 #include <TBranch.h>
11
12 void ShallowTree::
13 analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup) {
14 BOOST_FOREACH( BranchConnector* connector, connectors)
15 connector->connect(iEvent);
16 tree->Fill();
17 }
18
19 template <class T>
20 void ShallowTree::TypedBranchConnector<T>::
21 connect(const edm::Event& iEvent) {
22 edm::Handle<T> handle_;
23 iEvent.getByLabel(ml, pin, handle_);
24 object_ = *handle_;
25 }
26
27 template <class T>
28 ShallowTree::TypedBranchConnector<T>::
29 TypedBranchConnector(edm::BranchDescription const* desc,
30 std::string t,
31 TTree * tree)
32 : ml( desc->moduleLabel() ),
33 pin( desc->productInstanceName() )
34 {
35 object_ptr_ = &object_;
36 std::string s=pin+t;
37 if(t!="") { tree->Branch(pin.c_str(), object_ptr_, s.c_str() );} //raw type
38 else { tree->Branch(pin.c_str(), &object_ptr_ );} //vector<type>
39 }
40
41 void ShallowTree::
42 beginJob(const edm::EventSetup&) {
43 tree = fs->make<TTree>("tree", "");
44
45 std::map<std::string, LEAFTYPE> leafmap;
46 leafmap["bool"] = BOOL; leafmap["bools"] = BOOL_V;
47 leafmap["short int"] = SHORT; leafmap["shorts"] = SHORT_V;
48 leafmap["ushort int"]= U_SHORT; leafmap["ushorts"] = U_SHORT_V;
49 leafmap["int"] = INT; leafmap["ints"] = INT_V;
50 leafmap["uint"] = U_INT; leafmap["uints"] = U_INT_V;
51 leafmap["float"] = FLOAT; leafmap["floats"] = FLOAT_V;
52 leafmap["double"] = DOUBLE; leafmap["doubles"] = DOUBLE_V;
53 leafmap["lint"] = LONG; leafmap["longs"] = LONG_V;
54 leafmap["ulint"] = U_LONG; leafmap["ulongs"] = U_LONG_V;
55
56 edm::Service<edm::ConstProductRegistry> reg;
57 edm::Selections allBranches = reg->allBranchDescriptions();
58 edm::GroupSelectorRules groupSelectorRules_(pset, "outputCommands", "ShallowTree");
59 edm::GroupSelector groupSelector_;
60 groupSelector_.initialize(groupSelectorRules_, allBranches);
61
62 std::set<std::string> branchnames;
63
64 BOOST_FOREACH( const edm::Selections::value_type& selection, allBranches) {
65 if(groupSelector_.selected(*selection)) {
66
67 //Check for duplicate branch names
68 if (branchnames.find( selection->productInstanceName()) != branchnames.end() ) {
69 throw edm::Exception(edm::errors::Configuration)
70 << "More than one branch named: "
71 << selection->productInstanceName() << std::endl
72 << "Exception thrown from ShallowTree::beginJob" << std::endl;
73 }
74 else {
75 branchnames.insert( selection->productInstanceName() );
76 }
77
78 //Create ShallowTree branch
79 switch(leafmap.find( selection->friendlyClassName() )->second) {
80 case BOOL : connectors.push_back( new TypedBranchConnector <bool> (selection, "/O", tree) ); break;
81 case BOOL_V : connectors.push_back( new TypedBranchConnector<std::vector <bool> >(selection, "", tree) ); break;
82 case INT : connectors.push_back( new TypedBranchConnector <int> (selection, "/I", tree) ); break;
83 case INT_V : connectors.push_back( new TypedBranchConnector<std::vector <int> >(selection, "", tree) ); break;
84 case U_INT : connectors.push_back( new TypedBranchConnector <unsigned int> (selection, "/i", tree) ); break;
85 case U_INT_V : connectors.push_back( new TypedBranchConnector<std::vector <unsigned int> >(selection, "", tree) ); break;
86 case SHORT : connectors.push_back( new TypedBranchConnector <short> (selection, "/S", tree) ); break;
87 case SHORT_V : connectors.push_back( new TypedBranchConnector<std::vector <short> >(selection, "", tree) ); break;
88 case U_SHORT : connectors.push_back( new TypedBranchConnector <unsigned short> (selection, "/s", tree) ); break;
89 case U_SHORT_V: connectors.push_back( new TypedBranchConnector<std::vector<unsigned short> >(selection, "", tree) ); break;
90 case FLOAT : connectors.push_back( new TypedBranchConnector <float> (selection, "/F", tree) ); break;
91 case FLOAT_V : connectors.push_back( new TypedBranchConnector<std::vector <float> >(selection, "", tree) ); break;
92 case DOUBLE : connectors.push_back( new TypedBranchConnector <double> (selection, "/D", tree) ); break;
93 case DOUBLE_V : connectors.push_back( new TypedBranchConnector<std::vector <double> >(selection, "", tree) ); break;
94 case LONG : connectors.push_back( new TypedBranchConnector <long> (selection, "/L", tree) ); break;
95 case LONG_V : connectors.push_back( new TypedBranchConnector<std::vector <long> >(selection, "", tree) ); break;
96 case U_LONG : connectors.push_back( new TypedBranchConnector <unsigned long> (selection, "/l", tree) ); break;
97 case U_LONG_V : connectors.push_back( new TypedBranchConnector<std::vector <unsigned long> >(selection, "", tree) ); break;
98 default:
99 {
100 std::string leafstring = "";
101 typedef std::pair<std::string, LEAFTYPE> pair_t;
102 BOOST_FOREACH( const pair_t& leaf, leafmap)
103 leafstring+= "\t" + leaf.first + "\n";
104
105 throw edm::Exception(edm::errors::Configuration)
106 << "class ShallowTree does not handle leaves of type " << selection->className() << " like\n"
107 << selection->friendlyClassName() << "_"
108 << selection->moduleLabel() << "_"
109 << selection->productInstanceName() << "_"
110 << selection->processName() << std::endl
111 << "Valid leaf types are (friendlyClassName):\n"
112 << leafstring
113 << "Exception thrown from ShallowTree::beginJob\n";
114 }
115 }
116 }
117 }
118 }
119