CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
ShallowTree.cc
Go to the documentation of this file.
1 #include "ShallowTree.h"
2 
6 
7 #include <map>
8 #include <TBranch.h>
9 
11  usesResource(TFileService::kSharedResource);
12 
13  //int compSettings= iConfig.getParameter<int>("CompressionSettings",-1);
14  int compSettings = iConfig.getUntrackedParameter<int>("CompressionSettings", -1);
15  if (compSettings > 0)
16  fs_->file().SetCompressionSettings(compSettings);
17  tree_ = fs_->make<TTree>("tree", "");
18 
19  std::map<std::string, LEAFTYPE> leafmap;
20  leafmap["bool"] = BOOL;
21  leafmap["bools"] = BOOL_V;
22  leafmap["short int"] = SHORT;
23  leafmap["shorts"] = SHORT_V;
24  leafmap["ushort int"] = U_SHORT;
25  leafmap["ushorts"] = U_SHORT_V;
26  leafmap["int"] = INT;
27  leafmap["ints"] = INT_V;
28  leafmap["uint"] = U_INT;
29  leafmap["uints"] = U_INT_V;
30  leafmap["float"] = FLOAT;
31  leafmap["floats"] = FLOAT_V;
32  leafmap["double"] = DOUBLE;
33  leafmap["doubles"] = DOUBLE_V;
34  leafmap["lint"] = LONG;
35  leafmap["longs"] = LONG_V;
36  leafmap["ulint"] = U_LONG;
37  leafmap["ulongs"] = U_LONG_V;
38  leafmap["char"] = CHAR;
39  leafmap["chars"] = CHAR_V;
40  leafmap["uchar"] = U_CHAR;
41  leafmap["uchars"] = U_CHAR_V;
42 
44  auto allBranches = reg->allBranchDescriptions();
45  edm::ProductSelectorRules productSelectorRules_(iConfig, "outputCommands", "ShallowTree");
46  edm::ProductSelector productSelector_;
47  productSelector_.initialize(productSelectorRules_, allBranches);
48 
49  std::set<std::string> branchnames;
50 
51  for (auto const& selection : allBranches) {
52  if (productSelector_.selected(*selection)) {
53  //Check for duplicate branch names
54  if (branchnames.find(selection->productInstanceName()) != branchnames.end()) {
56  << "More than one branch named: " << selection->productInstanceName() << std::endl
57  << "Exception thrown from ShallowTree::ShallowTree" << std::endl;
58  } else {
59  branchnames.insert(selection->productInstanceName());
60  }
61 
62  //Create ShallowTree branch
63  switch (leafmap.find(selection->friendlyClassName())->second) {
64  case BOOL:
66  eat<bool>(selection);
67  break;
68  case BOOL_V:
69  connectors_.push_back(new TypedBranchConnector<std::vector<bool> >(selection, "", tree_));
70  eat<std::vector<bool> >(selection);
71  break;
72  case INT:
74  eat<int>(selection);
75  break;
76  case INT_V:
77  connectors_.push_back(new TypedBranchConnector<std::vector<int> >(selection, "", tree_));
78  eat<std::vector<int> >(selection);
79  break;
80  case U_INT:
82  eat<unsigned int>(selection);
83  break;
84  case U_INT_V:
85  connectors_.push_back(new TypedBranchConnector<std::vector<unsigned int> >(selection, "", tree_));
86  eat<std::vector<unsigned int> >(selection);
87  break;
88  case SHORT:
90  eat<short>(selection);
91  break;
92  case SHORT_V:
93  connectors_.push_back(new TypedBranchConnector<std::vector<short> >(selection, "", tree_));
94  eat<std::vector<short> >(selection);
95  break;
96  case U_SHORT:
98  eat<unsigned short>(selection);
99  break;
100  case U_SHORT_V:
101  connectors_.push_back(new TypedBranchConnector<std::vector<unsigned short> >(selection, "", tree_));
102  eat<std::vector<unsigned short> >(selection);
103  break;
104  case FLOAT:
106  eat<float>(selection);
107  break;
108  case FLOAT_V:
109  connectors_.push_back(new TypedBranchConnector<std::vector<float> >(selection, "", tree_));
110  eat<std::vector<float> >(selection);
111  break;
112  case DOUBLE:
114  eat<double>(selection);
115  break;
116  case DOUBLE_V:
117  connectors_.push_back(new TypedBranchConnector<std::vector<double> >(selection, "", tree_));
118  eat<std::vector<double> >(selection);
119  break;
120  case LONG:
121  connectors_.push_back(new TypedBranchConnector<long>(selection, "/L", tree_));
122  eat<long>(selection);
123  break;
124  case LONG_V:
125  connectors_.push_back(new TypedBranchConnector<std::vector<long> >(selection, "", tree_));
126  eat<std::vector<long> >(selection);
127  break;
128  case U_LONG:
130  eat<unsigned long>(selection);
131  break;
132  case U_LONG_V:
133  connectors_.push_back(new TypedBranchConnector<std::vector<unsigned long> >(selection, "", tree_));
134  eat<std::vector<unsigned long> >(selection);
135  break;
136  case CHAR:
137  connectors_.push_back(new TypedBranchConnector<char>(selection, "/B", tree_));
138  eat<char>(selection);
139  break;
140  case CHAR_V:
141  connectors_.push_back(new TypedBranchConnector<std::vector<char> >(selection, "", tree_));
142  eat<std::vector<char> >(selection);
143  break;
144  case U_CHAR:
146  eat<unsigned char>(selection);
147  break;
148  case U_CHAR_V:
149  connectors_.push_back(new TypedBranchConnector<std::vector<unsigned char> >(selection, "", tree_));
150  eat<std::vector<unsigned char> >(selection);
151  break;
152  default: {
153  std::string leafstring = "";
154  typedef std::pair<std::string, LEAFTYPE> pair_t;
155  for (const auto& leaf : leafmap) {
156  leafstring += "\t" + leaf.first + "\n";
157  }
158 
160  << "class ShallowTree does not handle leaves of type " << selection->className() << " like\n"
161  << selection->friendlyClassName() << "_" << selection->moduleLabel() << "_"
162  << selection->productInstanceName() << "_" << selection->processName() << std::endl
163  << "Valid leaf types are (friendlyClassName):\n"
164  << leafstring << "Exception thrown from ShallowTree::ShallowTree\n";
165  }
166  }
167  }
168  }
169 }
170 
172  for (BranchConnector* connector : connectors_) {
173  connector->connect(iEvent);
174  }
175  tree_->Fill();
176 }
177 
178 template <class T>
180  edm::Handle<T> handle_;
181  iEvent.getByLabel(ml, pin, handle_);
182  object_ = *handle_;
183 }
184 
185 template <class T>
187  std::string t,
188  TTree* tree)
189  : ml(desc->moduleLabel()), pin(desc->productInstanceName()) {
190  object_ptr_ = &object_;
191  std::string s = pin + t;
192  if (!t.empty()) {
193  tree->Branch(pin.c_str(), object_ptr_, s.c_str());
194  } //raw type
195  else {
196  tree->Branch(pin.c_str(), &object_ptr_);
197  } //vector<type>
198 }
static const std::string kSharedResource
Definition: TFileService.h:76
T getUntrackedParameter(std::string const &, T const &) const
bool selected(BranchDescription const &desc) const
selection
main part
Definition: corrVsCorr.py:100
TypedBranchConnector(edm::BranchDescription const *, std::string, TTree *)
Definition: ShallowTree.cc:186
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:171
U second(std::pair< T, U > const &p)
int iEvent
Definition: GenABIO.cc:224
edm::Service< TFileService > fs_
Definition: ShallowTree.h:61
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
Definition: Event.h:500
ShallowTree(const edm::ParameterSet &iConfig)
Definition: ShallowTree.cc:10
TFile & file() const
return opened TFile
Definition: TFileService.h:37
TTree * tree_
Definition: ShallowTree.h:62
void connect(const edm::Event &) override
Definition: ShallowTree.cc:179
void initialize(ProductSelectorRules const &rules, std::vector< BranchDescription const * > const &branchDescriptions)
std::vector< BranchDescription const * > allBranchDescriptions() const
std::vector< BranchConnector * > connectors_
Definition: ShallowTree.h:63