CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_10/src/DPGAnalysis/SiStripTools/plugins/MultiplicityProducer.cc

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 //
00003 // Package:    MultiplicityProducer
00004 // Class:      MultiplicityProducer
00005 // 
00012 //
00013 // Original Author:  Andrea Venturi
00014 //         Created:  Fri Dec 04 2009
00015 //
00016 //
00017 
00018 
00019 // system include files
00020 #include <memory>
00021 #include <string>
00022 #include <map>
00023 #include <vector>
00024 
00025 // user include files
00026 #include "FWCore/Framework/interface/Frameworkfwd.h"
00027 #include "FWCore/Framework/interface/EDProducer.h"
00028 
00029 #include "FWCore/Framework/interface/Event.h"
00030 #include "FWCore/Framework/interface/MakerMacros.h"
00031 
00032 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00033 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00034 #include "FWCore/Utilities/interface/InputTag.h"
00035 
00036 #include "DataFormats/DetId/interface/DetId.h"
00037 #include "DataFormats/Common/interface/DetSetVectorNew.h"
00038 #include "DataFormats/Common/interface/DetSetVector.h"
00039 
00040 #include "DataFormats/SiStripCluster/interface/SiStripCluster.h"
00041 #include "DataFormats/SiStripDigi/interface/SiStripDigi.h"
00042 #include "DataFormats/SiPixelCluster/interface/SiPixelCluster.h"
00043 
00044 #include "DPGAnalysis/SiStripTools/interface/DetIdSelector.h"
00045 //
00046 // class decleration
00047 //
00048 template <class T>
00049 class MultiplicityProducer : public edm::EDProducer {
00050 
00051 public:
00052   explicit MultiplicityProducer(const edm::ParameterSet&);
00053   ~MultiplicityProducer();
00054   
00055 private:
00056   virtual void beginJob() ;
00057   virtual void produce(edm::Event&, const edm::EventSetup&);
00058   virtual void endJob() ;
00059   int multiplicity(typename T::const_iterator det) const;
00060   int detSetMultiplicity(typename T::const_iterator det) const;
00061 
00062       // ----------member data ---------------------------
00063 
00064   edm::InputTag m_collection;
00065   bool m_clustersize;
00066   std::map<unsigned int, std::string> m_subdets;
00067   std::map<unsigned int, DetIdSelector> m_subdetsels;
00068 
00069 };
00070 
00071 //
00072 // constants, enums and typedefs
00073 //
00074 
00075 
00076 //
00077 // static data member definitions
00078 //
00079 
00080 //
00081 // constructors and destructor
00082 //
00083 template <class T>
00084 MultiplicityProducer<T>::MultiplicityProducer(const edm::ParameterSet& iConfig):
00085   m_collection(iConfig.getParameter<edm::InputTag>("clusterdigiCollection")),
00086   m_clustersize(iConfig.getUntrackedParameter<bool>("withClusterSize",false)),
00087   m_subdets(),m_subdetsels()
00088 {
00089 
00090   produces<std::map<unsigned int,int> >();
00091 
00092    //now do what ever other initialization is needed
00093 
00094   std::vector<edm::ParameterSet> wantedsubds(iConfig.getParameter<std::vector<edm::ParameterSet> >("wantedSubDets"));
00095                                              
00096   for(std::vector<edm::ParameterSet>::iterator ps=wantedsubds.begin();ps!=wantedsubds.end();++ps) {
00097     m_subdets[ps->getParameter<unsigned int>("detSelection")] = ps->getParameter<std::string>("detLabel");
00098     m_subdetsels[ps->getParameter<unsigned int>("detSelection")] = 
00099       DetIdSelector(ps->getUntrackedParameter<std::vector<std::string> >("selection",std::vector<std::string>()));
00100   }
00101 }
00102 
00103 template <class T>
00104 MultiplicityProducer<T>::~MultiplicityProducer()
00105 {
00106  
00107    // do anything here that needs to be done at desctruction time
00108    // (e.g. close files, deallocate resources etc.)
00109 
00110 }
00111 
00112 
00113 //
00114 // member functions
00115 //
00116 
00117 // ------------ method called to produce the data  ------------
00118 template <class T>
00119 void
00120 MultiplicityProducer<T>::produce(edm::Event& iEvent, const edm::EventSetup& iSetup)
00121 {
00122 
00123   LogDebug("Multiplicity") << " Ready to loop";
00124 
00125   using namespace edm;
00126 
00127   std::auto_ptr<std::map<unsigned int,int> > mults(new std::map<unsigned int,int> );
00128   
00129   
00130   Handle<T> digis;
00131   iEvent.getByLabel(m_collection,digis);
00132   
00133   for(std::map<unsigned int,std::string>::const_iterator sdet=m_subdets.begin();sdet!=m_subdets.end();++sdet) { (*mults)[sdet->first]=0; }
00134 
00135   
00136   for(typename T::const_iterator det = digis->begin();det!=digis->end();++det) {
00137     
00138     //    if(m_subdets.find(0)!=m_subdets.end()) (*mults)[0]+= det->size();
00139     if(m_subdets.find(0)!=m_subdets.end()) (*mults)[0]+= multiplicity(det);
00140 
00141     DetId detid(det->detId());
00142     unsigned int subdet = detid.subdetId();
00143 
00144     //    if(m_subdets.find(subdet)!=m_subdets.end() && !m_subdetsels[subdet].isValid() ) (*mults)[subdet] += det->size();
00145     if(m_subdets.find(subdet)!=m_subdets.end() && !m_subdetsels[subdet].isValid() ) (*mults)[subdet] += multiplicity(det);
00146 
00147     for(std::map<unsigned int,DetIdSelector>::const_iterator detsel=m_subdetsels.begin();detsel!=m_subdetsels.end();++detsel) {
00148 
00149       //      if(detsel->second.isValid() && detsel->second.isSelected(detid)) (*mults)[detsel->first] += det->size();
00150       if(detsel->second.isValid() && detsel->second.isSelected(detid)) (*mults)[detsel->first] += multiplicity(det);
00151 
00152     }
00153 
00154   }
00155   
00156   
00157   for(std::map<unsigned int,int>::const_iterator it=mults->begin();it!=mults->end();++it) {
00158     LogDebug("Multiplicity") << " Found " << it->second << " digis/clusters in " << it->first << " " << m_subdets[it->first];
00159   }
00160   
00161   iEvent.put(mults);
00162   
00163 }
00164 
00165 // ------------ method called once each job just before starting event loop  ------------
00166 template <class T>
00167 void 
00168 MultiplicityProducer<T>::beginJob()
00169 {
00170 }
00171 
00172 // ------------ method called once each job just after ending the event loop  ------------
00173 template <class T>
00174 void 
00175 MultiplicityProducer<T>::endJob() {
00176 }
00177 
00178 template <class T>
00179 int
00180 MultiplicityProducer<T>::multiplicity(typename T::const_iterator det) const {
00181 
00182   int mult = 0;
00183   if(m_clustersize) {
00184 
00185 
00186     //    edm::LogInfo("multiplicitywithcustersize") << "sono qua: with size";
00187     mult = detSetMultiplicity(det);
00188 
00189   }
00190   else {
00191 
00192     mult = det->size();
00193     //    edm::LogInfo("multiplicitywithcustersize") << "sono qua senza size";
00194 
00195   }
00196   return mult;
00197 }
00198 
00199 
00200 template <class T>
00201 int
00202 MultiplicityProducer<T>::detSetMultiplicity(typename T::const_iterator det) const {
00203 
00204   return det->size();
00205 
00206 }
00207 
00208 
00209 template <>
00210 int 
00211 MultiplicityProducer<edmNew::DetSetVector<SiStripCluster> >::detSetMultiplicity(edmNew::DetSetVector<SiStripCluster>::const_iterator det) const {
00212 
00213   int mult = 0;
00214   
00215   for(edmNew::DetSet<SiStripCluster>::const_iterator clus=det->begin();clus!=det->end();++clus) {
00216 
00217     //    edm::LogInfo("multiplicitywithcustersize") << "sono qua";
00218     mult += clus->amplitudes().size();
00219 
00220 
00221 
00222   }
00223 
00224   return mult;
00225 
00226 }
00227 
00228 template <>
00229 int
00230 MultiplicityProducer<edmNew::DetSetVector<SiPixelCluster> >::detSetMultiplicity(edmNew::DetSetVector<SiPixelCluster>::const_iterator det) const {
00231 
00232   int mult = 0;
00233   
00234   for(edmNew::DetSet<SiPixelCluster>::const_iterator clus=det->begin();clus!=det->end();++clus) {
00235 
00236     mult += clus->size();
00237 
00238   }
00239 
00240   return mult;
00241 
00242 }
00243 
00244 //define this as a plug-in
00245 typedef MultiplicityProducer<edmNew::DetSetVector<SiStripCluster> > SiStripClusterMultiplicityProducer;
00246 typedef MultiplicityProducer<edmNew::DetSetVector<SiPixelCluster> > SiPixelClusterMultiplicityProducer;
00247 typedef MultiplicityProducer<edm::DetSetVector<SiStripDigi> > SiStripDigiMultiplicityProducer;
00248 
00249 
00250 DEFINE_FWK_MODULE(SiStripClusterMultiplicityProducer);
00251 DEFINE_FWK_MODULE(SiPixelClusterMultiplicityProducer);
00252 DEFINE_FWK_MODULE(SiStripDigiMultiplicityProducer);