Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 11:59:47

0001 #include "ShallowTree.h"
0002 
0003 #include "FWCore/Framework/interface/ConstProductRegistry.h"
0004 #include "FWCore/Framework/interface/ProductSelector.h"
0005 #include "FWCore/Framework/interface/ProductSelectorRules.h"
0006 
0007 #include <map>
0008 #include <TBranch.h>
0009 
0010 ShallowTree::ShallowTree(const edm::ParameterSet& iConfig) {
0011   usesResource(TFileService::kSharedResource);
0012 
0013   //int compSettings= iConfig.getParameter<int>("CompressionSettings",-1);
0014   int compSettings = iConfig.getUntrackedParameter<int>("CompressionSettings", -1);
0015   if (compSettings > 0)
0016     fs_->file().SetCompressionSettings(compSettings);
0017   tree_ = fs_->make<TTree>("tree", "");
0018 
0019   std::map<std::string, LEAFTYPE> leafmap;
0020   leafmap["bool"] = BOOL;
0021   leafmap["bools"] = BOOL_V;
0022   leafmap["short int"] = SHORT;
0023   leafmap["shorts"] = SHORT_V;
0024   leafmap["ushort int"] = U_SHORT;
0025   leafmap["ushorts"] = U_SHORT_V;
0026   leafmap["int"] = INT;
0027   leafmap["ints"] = INT_V;
0028   leafmap["uint"] = U_INT;
0029   leafmap["uints"] = U_INT_V;
0030   leafmap["float"] = FLOAT;
0031   leafmap["floats"] = FLOAT_V;
0032   leafmap["double"] = DOUBLE;
0033   leafmap["doubles"] = DOUBLE_V;
0034   leafmap["lint"] = LONG;
0035   leafmap["longs"] = LONG_V;
0036   leafmap["ulint"] = U_LONG;
0037   leafmap["ulongs"] = U_LONG_V;
0038   leafmap["char"] = CHAR;
0039   leafmap["chars"] = CHAR_V;
0040   leafmap["uchar"] = U_CHAR;
0041   leafmap["uchars"] = U_CHAR_V;
0042 
0043   edm::Service<edm::ConstProductRegistry> reg;
0044   auto allBranches = reg->allBranchDescriptions();
0045   edm::ProductSelectorRules productSelectorRules_(iConfig, "outputCommands", "ShallowTree");
0046   edm::ProductSelector productSelector_;
0047   productSelector_.initialize(productSelectorRules_, allBranches);
0048 
0049   std::set<std::string> branchnames;
0050 
0051   for (auto const& selection : allBranches) {
0052     if (productSelector_.selected(*selection)) {
0053       //Check for duplicate branch names
0054       if (branchnames.find(selection->productInstanceName()) != branchnames.end()) {
0055         throw edm::Exception(edm::errors::Configuration)
0056             << "More than one branch named: " << selection->productInstanceName() << std::endl
0057             << "Exception thrown from ShallowTree::ShallowTree" << std::endl;
0058       } else {
0059         branchnames.insert(selection->productInstanceName());
0060       }
0061 
0062       //Create ShallowTree branch
0063       switch (leafmap.find(selection->friendlyClassName())->second) {
0064         case BOOL:
0065           connectors_.push_back(new TypedBranchConnector<bool>(selection, "/O", tree_));
0066           eat<bool>(selection);
0067           break;
0068         case BOOL_V:
0069           connectors_.push_back(new TypedBranchConnector<std::vector<bool> >(selection, "", tree_));
0070           eat<std::vector<bool> >(selection);
0071           break;
0072         case INT:
0073           connectors_.push_back(new TypedBranchConnector<int>(selection, "/I", tree_));
0074           eat<int>(selection);
0075           break;
0076         case INT_V:
0077           connectors_.push_back(new TypedBranchConnector<std::vector<int> >(selection, "", tree_));
0078           eat<std::vector<int> >(selection);
0079           break;
0080         case U_INT:
0081           connectors_.push_back(new TypedBranchConnector<unsigned int>(selection, "/i", tree_));
0082           eat<unsigned int>(selection);
0083           break;
0084         case U_INT_V:
0085           connectors_.push_back(new TypedBranchConnector<std::vector<unsigned int> >(selection, "", tree_));
0086           eat<std::vector<unsigned int> >(selection);
0087           break;
0088         case SHORT:
0089           connectors_.push_back(new TypedBranchConnector<short>(selection, "/S", tree_));
0090           eat<short>(selection);
0091           break;
0092         case SHORT_V:
0093           connectors_.push_back(new TypedBranchConnector<std::vector<short> >(selection, "", tree_));
0094           eat<std::vector<short> >(selection);
0095           break;
0096         case U_SHORT:
0097           connectors_.push_back(new TypedBranchConnector<unsigned short>(selection, "/s", tree_));
0098           eat<unsigned short>(selection);
0099           break;
0100         case U_SHORT_V:
0101           connectors_.push_back(new TypedBranchConnector<std::vector<unsigned short> >(selection, "", tree_));
0102           eat<std::vector<unsigned short> >(selection);
0103           break;
0104         case FLOAT:
0105           connectors_.push_back(new TypedBranchConnector<float>(selection, "/F", tree_));
0106           eat<float>(selection);
0107           break;
0108         case FLOAT_V:
0109           connectors_.push_back(new TypedBranchConnector<std::vector<float> >(selection, "", tree_));
0110           eat<std::vector<float> >(selection);
0111           break;
0112         case DOUBLE:
0113           connectors_.push_back(new TypedBranchConnector<double>(selection, "/D", tree_));
0114           eat<double>(selection);
0115           break;
0116         case DOUBLE_V:
0117           connectors_.push_back(new TypedBranchConnector<std::vector<double> >(selection, "", tree_));
0118           eat<std::vector<double> >(selection);
0119           break;
0120         case LONG:
0121           connectors_.push_back(new TypedBranchConnector<long>(selection, "/L", tree_));
0122           eat<long>(selection);
0123           break;
0124         case LONG_V:
0125           connectors_.push_back(new TypedBranchConnector<std::vector<long> >(selection, "", tree_));
0126           eat<std::vector<long> >(selection);
0127           break;
0128         case U_LONG:
0129           connectors_.push_back(new TypedBranchConnector<unsigned long>(selection, "/l", tree_));
0130           eat<unsigned long>(selection);
0131           break;
0132         case U_LONG_V:
0133           connectors_.push_back(new TypedBranchConnector<std::vector<unsigned long> >(selection, "", tree_));
0134           eat<std::vector<unsigned long> >(selection);
0135           break;
0136         case CHAR:
0137           connectors_.push_back(new TypedBranchConnector<char>(selection, "/B", tree_));
0138           eat<char>(selection);
0139           break;
0140         case CHAR_V:
0141           connectors_.push_back(new TypedBranchConnector<std::vector<char> >(selection, "", tree_));
0142           eat<std::vector<char> >(selection);
0143           break;
0144         case U_CHAR:
0145           connectors_.push_back(new TypedBranchConnector<unsigned char>(selection, "/b", tree_));
0146           eat<unsigned char>(selection);
0147           break;
0148         case U_CHAR_V:
0149           connectors_.push_back(new TypedBranchConnector<std::vector<unsigned char> >(selection, "", tree_));
0150           eat<std::vector<unsigned char> >(selection);
0151           break;
0152         default: {
0153           std::string leafstring = "";
0154           typedef std::pair<std::string, LEAFTYPE> pair_t;
0155           for (const auto& leaf : leafmap) {
0156             leafstring += "\t" + leaf.first + "\n";
0157           }
0158 
0159           throw edm::Exception(edm::errors::Configuration)
0160               << "class ShallowTree does not handle leaves of type " << selection->className() << " like\n"
0161               << selection->friendlyClassName() << "_" << selection->moduleLabel() << "_"
0162               << selection->productInstanceName() << "_" << selection->processName() << std::endl
0163               << "Valid leaf types are (friendlyClassName):\n"
0164               << leafstring << "Exception thrown from ShallowTree::ShallowTree\n";
0165         }
0166       }
0167     }
0168   }
0169 }
0170 
0171 void ShallowTree::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup) {
0172   for (BranchConnector* connector : connectors_) {
0173     connector->connect(iEvent);
0174   }
0175   tree_->Fill();
0176 }
0177 
0178 template <class T>
0179 void ShallowTree::TypedBranchConnector<T>::connect(const edm::Event& iEvent) {
0180   edm::Handle<T> handle_;
0181   iEvent.getByLabel(ml, pin, handle_);
0182   object_ = *handle_;
0183 }
0184 
0185 template <class T>
0186 ShallowTree::TypedBranchConnector<T>::TypedBranchConnector(edm::BranchDescription const* desc,
0187                                                            std::string t,
0188                                                            TTree* tree)
0189     : ml(desc->moduleLabel()), pin(desc->productInstanceName()) {
0190   object_ptr_ = &object_;
0191   std::string s = pin + t;
0192   if (!t.empty()) {
0193     tree->Branch(pin.c_str(), object_ptr_, s.c_str());
0194   }  //raw type
0195   else {
0196     tree->Branch(pin.c_str(), &object_ptr_);
0197   }  //vector<type>
0198 }