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