CMS 3D CMS Logo

ShallowTree.cc
Go to the documentation of this file.
2 
6 
7 #include <map>
8 #include <TBranch.h>
9 
11  tree_ = fs_->make<TTree>("tree", "");
12 
13  std::map<std::string, LEAFTYPE> leafmap;
14  leafmap["bool"] = BOOL; leafmap["bools"] = BOOL_V;
15  leafmap["short int"] = SHORT; leafmap["shorts"] = SHORT_V;
16  leafmap["ushort int"]= U_SHORT; leafmap["ushorts"] = U_SHORT_V;
17  leafmap["int"] = INT; leafmap["ints"] = INT_V;
18  leafmap["uint"] = U_INT; leafmap["uints"] = U_INT_V;
19  leafmap["float"] = FLOAT; leafmap["floats"] = FLOAT_V;
20  leafmap["double"] = DOUBLE; leafmap["doubles"] = DOUBLE_V;
21  leafmap["lint"] = LONG; leafmap["longs"] = LONG_V;
22  leafmap["ulint"] = U_LONG; leafmap["ulongs"] = U_LONG_V;
23  leafmap["char"] = CHAR; leafmap["chars"] = CHAR_V;
24  leafmap["uchar"] = U_CHAR; leafmap["uchars"] = U_CHAR_V;
25 
26 
28  auto allBranches = reg->allBranchDescriptions();
29  edm::ProductSelectorRules productSelectorRules_(iConfig, "outputCommands", "ShallowTree");
30  edm::ProductSelector productSelector_;
31  productSelector_.initialize(productSelectorRules_, allBranches);
32 
33  std::set<std::string> branchnames;
34 
35  for( auto const& selection : allBranches) {
36  if(productSelector_.selected(*selection)) {
37 
38  //Check for duplicate branch names
39  if (branchnames.find( selection->productInstanceName()) != branchnames.end() ) {
41  << "More than one branch named: "
42  << selection->productInstanceName() << std::endl
43  << "Exception thrown from ShallowTree::ShallowTree" << std::endl;
44  }
45  else {
46  branchnames.insert( selection->productInstanceName() );
47  }
48 
49  //Create ShallowTree branch
50  switch(leafmap.find( selection->friendlyClassName() )->second) {
51  case BOOL : connectors_.push_back( new TypedBranchConnector <bool> (selection, "/O", tree_) ); eat <bool> (selection); break;
52  case BOOL_V : connectors_.push_back( new TypedBranchConnector<std::vector <bool> >(selection, "", tree_) ); eat<std::vector <bool> >(selection); break;
53  case INT : connectors_.push_back( new TypedBranchConnector <int> (selection, "/I", tree_) ); eat <int> (selection); break;
54  case INT_V : connectors_.push_back( new TypedBranchConnector<std::vector <int> >(selection, "", tree_) ); eat<std::vector <int> >(selection); break;
55  case U_INT : connectors_.push_back( new TypedBranchConnector <unsigned int> (selection, "/i", tree_) ); eat <unsigned int> (selection); break;
56  case U_INT_V : connectors_.push_back( new TypedBranchConnector<std::vector <unsigned int> >(selection, "", tree_) ); eat<std::vector <unsigned int> >(selection); break;
57  case SHORT : connectors_.push_back( new TypedBranchConnector <short> (selection, "/S", tree_) ); eat <short> (selection); break;
58  case SHORT_V : connectors_.push_back( new TypedBranchConnector<std::vector <short> >(selection, "", tree_) ); eat<std::vector <short> >(selection); break;
59  case U_SHORT : connectors_.push_back( new TypedBranchConnector <unsigned short> (selection, "/s", tree_) ); eat <unsigned short> (selection); break;
60  case U_SHORT_V: connectors_.push_back( new TypedBranchConnector<std::vector<unsigned short> >(selection, "", tree_) ); eat<std::vector<unsigned short> >(selection); break;
61  case FLOAT : connectors_.push_back( new TypedBranchConnector <float> (selection, "/F", tree_) ); eat <float> (selection); break;
62  case FLOAT_V : connectors_.push_back( new TypedBranchConnector<std::vector <float> >(selection, "", tree_) ); eat<std::vector <float> >(selection); break;
63  case DOUBLE : connectors_.push_back( new TypedBranchConnector <double> (selection, "/D", tree_) ); eat <double> (selection); break;
64  case DOUBLE_V : connectors_.push_back( new TypedBranchConnector<std::vector <double> >(selection, "", tree_) ); eat<std::vector <double> >(selection); break;
65  case LONG : connectors_.push_back( new TypedBranchConnector <long> (selection, "/L", tree_) ); eat <long> (selection); break;
66  case LONG_V : connectors_.push_back( new TypedBranchConnector<std::vector <long> >(selection, "", tree_) ); eat<std::vector <long> >(selection); break;
67  case U_LONG : connectors_.push_back( new TypedBranchConnector <unsigned long> (selection, "/l", tree_) ); eat <unsigned long> (selection); break;
68  case U_LONG_V : connectors_.push_back( new TypedBranchConnector<std::vector <unsigned long> >(selection, "", tree_) ); eat<std::vector <unsigned long> >(selection); break;
69  case CHAR : connectors_.push_back( new TypedBranchConnector <char> (selection, "/B", tree_) ); eat <char> (selection); break;
70  case CHAR_V : connectors_.push_back( new TypedBranchConnector<std::vector <char> >(selection, "", tree_) ); eat<std::vector <char> >(selection); break;
71  case U_CHAR : connectors_.push_back( new TypedBranchConnector <unsigned char> (selection, "/b", tree_) ); eat <unsigned char> (selection); break;
72  case U_CHAR_V : connectors_.push_back( new TypedBranchConnector<std::vector <unsigned char> >(selection, "", tree_) ); eat<std::vector <unsigned char> >(selection); break;
73  default:
74  {
75  std::string leafstring = "";
76  typedef std::pair<std::string, LEAFTYPE> pair_t;
77  for( const pair_t& leaf: leafmap) {
78  leafstring+= "\t" + leaf.first + "\n";
79  }
80 
82  << "class ShallowTree does not handle leaves of type " << selection->className() << " like\n"
83  << selection->friendlyClassName() << "_"
84  << selection->moduleLabel() << "_"
85  << selection->productInstanceName() << "_"
86  << selection->processName() << std::endl
87  << "Valid leaf types are (friendlyClassName):\n"
88  << leafstring
89  << "Exception thrown from ShallowTree::ShallowTree\n";
90  }
91  }
92  }
93  }
94 }
95 
96 
97 void ShallowTree::
98 analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup) {
99  for( BranchConnector* connector: connectors_) {
100  connector->connect(iEvent);
101  }
102  tree_->Fill();
103 }
104 
105 template <class T>
108  edm::Handle<T> handle_;
109  iEvent.getByLabel(ml, pin, handle_);
110  object_ = *handle_;
111 }
112 
113 template <class T>
116  std::string t,
117  TTree * tree)
118  : ml( desc->moduleLabel() ),
119  pin( desc->productInstanceName() )
120 {
121  object_ptr_ = &object_;
122  std::string s=pin+t;
123  if(t!="") { tree->Branch(pin.c_str(), object_ptr_, s.c_str() );} //raw type
124  else { tree->Branch(pin.c_str(), &object_ptr_ );} //vector<type>
125 }
126 
127 void ShallowTree::
129 }
130 
bool selected(BranchDescription const &desc) const
selection
main part
Definition: corrVsCorr.py:98
TypedBranchConnector(edm::BranchDescription const *, std::string, TTree *)
Definition: ShallowTree.cc:115
T * make(const Args &...args) const
make new ROOT object
Definition: TFileService.h:64
void analyze(const edm::Event &, const edm::EventSetup &) override
Definition: ShallowTree.cc:98
U second(std::pair< T, U > const &p)
int iEvent
Definition: GenABIO.cc:230
edm::Service< TFileService > fs_
Definition: ShallowTree.h:62
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
Definition: Event.h:464
ShallowTree(const edm::ParameterSet &iConfig)
Definition: ShallowTree.cc:10
void beginJob() override
Definition: ShallowTree.cc:128
TTree * tree_
Definition: ShallowTree.h:63
void connect(const edm::Event &) override
Definition: ShallowTree.cc:107
void initialize(ProductSelectorRules const &rules, std::vector< BranchDescription const * > const &branchDescriptions)
Definition: tree.py:1
std::vector< BranchDescription const * > allBranchDescriptions() const
std::vector< BranchConnector * > connectors_
Definition: ShallowTree.h:64