CMS 3D CMS Logo

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