1 |
#include "TopQuarkAnalysis/TopRefTuple/interface/TreeMaker.h"
|
2 |
#include "TopQuarkAnalysis/TopRefTuple/interface/fTypes.h"
|
3 |
|
4 |
#include "FWCore/Framework/interface/ConstProductRegistry.h"
|
5 |
#include "FWCore/Framework/interface/GroupSelector.h"
|
6 |
#include "FWCore/Framework/interface/GroupSelectorRules.h"
|
7 |
#include "DataFormats/Provenance/interface/Selections.h"
|
8 |
|
9 |
#include "boost/foreach.hpp"
|
10 |
#include "TInterpreter.h"
|
11 |
|
12 |
void TreeMaker::
|
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 TreeMaker::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 |
TreeMaker::TypedBranchConnector<T>::
|
29 |
TypedBranchConnector(edm::BranchDescription const* desc,
|
30 |
TTree * tree,
|
31 |
std::string t,
|
32 |
std::string inc)
|
33 |
: ml( desc->moduleLabel() ),
|
34 |
pin( desc->productInstanceName() )
|
35 |
{
|
36 |
object_ptr_ = &object_;
|
37 |
std::string name = (pin=="") ? ml : pin;
|
38 |
if(t.size() && t[0]=='/') { tree->Branch( name.c_str(), object_ptr_, (name+t).c_str() );} //raw type
|
39 |
else if(t=="") { tree->Branch( name.c_str(), &object_ptr_ );} //implied by type
|
40 |
else { gInterpreter->GenerateDictionary(t.c_str(),inc.c_str());
|
41 |
tree->Branch( name.c_str(), t.c_str(), &object_ptr_ );} //specified by string
|
42 |
}
|
43 |
|
44 |
void TreeMaker::
|
45 |
beginJob() {
|
46 |
tree = fs->make<TTree>("tree", "");
|
47 |
|
48 |
edm::Service<edm::ConstProductRegistry> reg;
|
49 |
edm::Selections allBranches = reg->allBranchDescriptions();
|
50 |
edm::GroupSelectorRules groupSelectorRules_(pset, "outputCommands", "TreeMaker");
|
51 |
edm::GroupSelector groupSelector_;
|
52 |
groupSelector_.initialize(groupSelectorRules_, allBranches);
|
53 |
|
54 |
std::map<std::string,fTypes::LEAFTYPE> dict = fTypes::dict();
|
55 |
std::set<std::string> branchnames;
|
56 |
BOOST_FOREACH( const edm::Selections::value_type& selection, allBranches) {
|
57 |
if(groupSelector_.selected(*selection)) {
|
58 |
|
59 |
//Check for duplicate branch names
|
60 |
const std::string name = selection->productInstanceName()==""? selection->moduleLabel() : selection->productInstanceName();
|
61 |
if (branchnames.find(name) != branchnames.end() )
|
62 |
throw edm::Exception(edm::errors::Configuration)
|
63 |
<< "More than one branch named: " << name << std::endl
|
64 |
<< "Exception thrown from TreeMaker::beginJob" << std::endl;
|
65 |
else
|
66 |
branchnames.insert( selection->productInstanceName() );
|
67 |
|
68 |
//Create TreeMaker branch
|
69 |
switch(dict.find( selection->friendlyClassName() )->second) {
|
70 |
#define EXPAND(enumT,typeT,charT,incT) case fTypes::enumT : connectors.push_back( new TypedBranchConnector<typeT >(selection,tree,charT,incT)); break
|
71 |
EXPAND( BOOL, bool, "/O","");
|
72 |
EXPAND( INT, int, "/I","");
|
73 |
EXPAND( U_INT, unsigned, "/i","");
|
74 |
EXPAND( SHORT, short, "/S","");
|
75 |
EXPAND(U_SHORT, unsigned short, "/s","");
|
76 |
EXPAND( FLOAT, float, "/F","");
|
77 |
EXPAND( DOUBLE, double, "/D","");
|
78 |
EXPAND( LONG, long, "/L","");
|
79 |
EXPAND( U_LONG, unsigned long, "/l","");
|
80 |
|
81 |
EXPAND( STRING, std::string, "", "");
|
82 |
EXPAND( POINTD, fTypes::dPoint, "", "");
|
83 |
EXPAND( VECTORD, fTypes::dVector, "", "");
|
84 |
EXPAND(LORENTZVD, fTypes::dXYZLorentzV, "", "");
|
85 |
EXPAND(LORENTZVP, fTypes::dPolarLorentzV, "", "");
|
86 |
|
87 |
EXPAND( BOOL_V, std::vector< bool>, "", "");
|
88 |
EXPAND( INT_V, std::vector< int>, "", "");
|
89 |
EXPAND( U_INT_V, std::vector< unsigned>, "", "");
|
90 |
EXPAND( SHORT_V, std::vector< short>, "", "");
|
91 |
EXPAND(U_SHORT_V, std::vector<unsigned short>, "", "");
|
92 |
EXPAND( FLOAT_V, std::vector< float>, "", "");
|
93 |
EXPAND( DOUBLE_V, std::vector< double>, "", "");
|
94 |
EXPAND( LONG_V, std::vector< long>, "", "");
|
95 |
EXPAND( U_LONG_V, std::vector< unsigned long>, "", "");
|
96 |
|
97 |
EXPAND( POINTD_V, std::vector <fTypes::dPoint>, "", "");
|
98 |
EXPAND( VECTORD_V, std::vector <fTypes::dVector>, "", "");
|
99 |
EXPAND(LORENTZVD_V, std::vector <fTypes::dXYZLorentzV>, "", "");
|
100 |
EXPAND(LORENTZVP_V, std::vector<fTypes::dPolarLorentzV>, "vector<ROOT::Math::LorentzVector<ROOT::Math::PtEtaPhiM4D<Double32_t> > >","vector;Math/LorentzVector.h");
|
101 |
|
102 |
EXPAND( STRING_BOOL_M, fTypes::mapStringBool , "", "");
|
103 |
EXPAND( STRING_INT_M, fTypes::mapStringInt , "", "");
|
104 |
#undef EXPAND
|
105 |
default:
|
106 |
{
|
107 |
std::string leafstring = "";
|
108 |
typedef std::pair<std::string, fTypes::LEAFTYPE> pair_t;
|
109 |
BOOST_FOREACH( const pair_t& leaf, dict)
|
110 |
leafstring+= "\t" + leaf.first + "\n";
|
111 |
|
112 |
throw edm::Exception(edm::errors::Configuration)
|
113 |
<< "class TreeMaker 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 TreeMaker::beginJob\n";
|
121 |
}
|
122 |
}
|
123 |
}
|
124 |
}
|
125 |
}
|
126 |
|