CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
ShallowTree.cc
Go to the documentation of this file.
2 
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>
22  edm::Handle<T> handle_;
23  iEvent.getByLabel(ml, pin, handle_);
24  object_ = *handle_;
25 }
26 
27 template <class T>
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::
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  leafmap["char"] = CHAR; leafmap["chars"] = CHAR_V;
56  leafmap["uchar"] = U_CHAR; leafmap["uchars"] = U_CHAR_V;
57 
58 
60  edm::Selections allBranches = reg->allBranchDescriptions();
61  edm::GroupSelectorRules groupSelectorRules_(pset, "outputCommands", "ShallowTree");
62  edm::GroupSelector groupSelector_;
63  groupSelector_.initialize(groupSelectorRules_, allBranches);
64 
65  std::set<std::string> branchnames;
66 
67  BOOST_FOREACH( const edm::Selections::value_type& selection, allBranches) {
68  if(groupSelector_.selected(*selection)) {
69 
70  //Check for duplicate branch names
71  if (branchnames.find( selection->productInstanceName()) != branchnames.end() ) {
73  << "More than one branch named: "
74  << selection->productInstanceName() << std::endl
75  << "Exception thrown from ShallowTree::beginJob" << std::endl;
76  }
77  else {
78  branchnames.insert( selection->productInstanceName() );
79  }
80 
81  //Create ShallowTree branch
82  switch(leafmap.find( selection->friendlyClassName() )->second) {
83  case BOOL : connectors.push_back( new TypedBranchConnector <bool> (selection, "/O", tree) ); break;
84  case BOOL_V : connectors.push_back( new TypedBranchConnector<std::vector <bool> >(selection, "", tree) ); break;
85  case INT : connectors.push_back( new TypedBranchConnector <int> (selection, "/I", tree) ); break;
86  case INT_V : connectors.push_back( new TypedBranchConnector<std::vector <int> >(selection, "", tree) ); break;
87  case U_INT : connectors.push_back( new TypedBranchConnector <unsigned int> (selection, "/i", tree) ); break;
88  case U_INT_V : connectors.push_back( new TypedBranchConnector<std::vector <unsigned int> >(selection, "", tree) ); break;
89  case SHORT : connectors.push_back( new TypedBranchConnector <short> (selection, "/S", tree) ); break;
90  case SHORT_V : connectors.push_back( new TypedBranchConnector<std::vector <short> >(selection, "", tree) ); break;
91  case U_SHORT : connectors.push_back( new TypedBranchConnector <unsigned short> (selection, "/s", tree) ); break;
92  case U_SHORT_V: connectors.push_back( new TypedBranchConnector<std::vector<unsigned short> >(selection, "", tree) ); break;
93  case FLOAT : connectors.push_back( new TypedBranchConnector <float> (selection, "/F", tree) ); break;
94  case FLOAT_V : connectors.push_back( new TypedBranchConnector<std::vector <float> >(selection, "", tree) ); break;
95  case DOUBLE : connectors.push_back( new TypedBranchConnector <double> (selection, "/D", tree) ); break;
96  case DOUBLE_V : connectors.push_back( new TypedBranchConnector<std::vector <double> >(selection, "", tree) ); break;
97  case LONG : connectors.push_back( new TypedBranchConnector <long> (selection, "/L", tree) ); break;
98  case LONG_V : connectors.push_back( new TypedBranchConnector<std::vector <long> >(selection, "", tree) ); break;
99  case U_LONG : connectors.push_back( new TypedBranchConnector <unsigned long> (selection, "/l", tree) ); break;
100  case U_LONG_V : connectors.push_back( new TypedBranchConnector<std::vector <unsigned long> >(selection, "", tree) ); break;
101  case CHAR : connectors.push_back( new TypedBranchConnector <char> (selection, "/B", tree) ); break;
102  case CHAR_V : connectors.push_back( new TypedBranchConnector<std::vector <char> >(selection, "", tree) ); break;
103  case U_CHAR : connectors.push_back( new TypedBranchConnector <unsigned char> (selection, "/b", tree) ); break;
104  case U_CHAR_V : connectors.push_back( new TypedBranchConnector<std::vector <unsigned char> >(selection, "", tree) ); break;
105  default:
106  {
107  std::string leafstring = "";
108  typedef std::pair<std::string, LEAFTYPE> pair_t;
109  BOOST_FOREACH( const pair_t& leaf, leafmap)
110  leafstring+= "\t" + leaf.first + "\n";
111 
113  << "class ShallowTree does not handle leaves of type " << selection->className() << " like\n"
114  << selection->friendlyClassName() << "_"
115  << selection->moduleLabel() << "_"
116  << selection->productInstanceName() << "_"
117  << selection->processName() << std::endl
118  << "Valid leaf types are (friendlyClassName):\n"
119  << leafstring
120  << "Exception thrown from ShallowTree::beginJob\n";
121  }
122  }
123  }
124  }
125 }
126 
std::vector< BranchDescription const * > Selections
Definition: Selections.h:10
virtual void analyze(const edm::Event &, const edm::EventSetup &)
Definition: ShallowTree.cc:13
virtual void beginJob()
Definition: ShallowTree.cc:42
virtual void connect(const edm::Event &)=0
selection
main part
Definition: corrVsCorr.py:98
TypedBranchConnector(edm::BranchDescription const *, std::string, TTree *)
Definition: ShallowTree.cc:29
edm::ParameterSet pset
Definition: ShallowTree.h:61
U second(std::pair< T, U > const &p)
int iEvent
Definition: GenABIO.cc:243
tuple leaf
Definition: Node.py:62
TTree * tree
Definition: ShallowTree.h:59
Container::value_type value_type
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
Definition: Event.h:356
void connect(const edm::Event &)
Definition: ShallowTree.cc:21
edm::Service< TFileService > fs
Definition: ShallowTree.h:58
std::vector< BranchConnector * > connectors
Definition: ShallowTree.h:60
bool selected(BranchDescription const &desc) const
T * make() const
make new ROOT object
std::vector< BranchDescription const * > allBranchDescriptions() const
void initialize(GroupSelectorRules const &rules, std::vector< BranchDescription const * > const &branchDescriptions)