CMS 3D CMS Logo

SimAnalyzerMinbias.cc
Go to the documentation of this file.
1 // system include files
2 #include <memory>
3 #include <string>
4 #include <iostream>
5 #include <fstream>
6 #include <sstream>
7 #include <vector>
8 #include <map>
9 
10 // user include files
20 
25 
31 
33 
34 #include "TFile.h"
35 #include "TTree.h"
36 
37 // class declaration
39 
40 public:
41  explicit SimAnalyzerMinbias(const edm::ParameterSet&);
43  virtual void analyze(const edm::Event&, const edm::EventSetup&);
44  virtual void beginJob() ;
45  virtual void endJob() ;
46  virtual void beginRun(const edm::Run& r, const edm::EventSetup& iSetup);
47  virtual void endRun(const edm::Run& r, const edm::EventSetup& iSetup);
48 
49 private:
50 
51  // ----------member data ---------------------------
53  double timeCut_;
54  TTree* myTree_;
55 
56  // Root tree members
59  struct myInfo{
61  myInfo() {
62  theMB0 = theMB1 = theMB2 = theMB3 = theMB4 = 0;
63  }
64  };
65  std::map<HcalDetId,myInfo> myMap_;
68 };
69 
70 // constructors and destructor
71 
73 
74  timeCut_ = iConfig.getUntrackedParameter<double>("TimeCut", 500);
75 
76  // get token names of modules, producing object collections
77  tok_evt_ = consumes<edm::HepMCProduct>(edm::InputTag("generator"));
78  tok_hcal_ = consumes<edm::PCaloHitContainer>(edm::InputTag("g4SimHits","HcalHits"));
79 
80  edm::LogInfo("AnalyzerMB") << "Use Time cut of " << timeCut_ << " ns";
81 }
82 
84  // do anything here that needs to be done at desctruction time
85  // (e.g. close files, deallocate resources etc.)
86 }
87 
89 }
90 
92 }
93 
95 
96  myTree_ = fs_->make<TTree>("SimJet","SimJet Tree");
97  myTree_->Branch("mydet", &mydet, "mydet/I");
98  myTree_->Branch("mysubd", &mysubd, "mysubd/I");
99  myTree_->Branch("cells", &cells, "cells");
100  myTree_->Branch("depth", &depth, "depth/I");
101  myTree_->Branch("ieta", &ieta, "ieta/I");
102  myTree_->Branch("iphi", &iphi, "iphi/I");
103  myTree_->Branch("mom0_MB", &mom0_MB, "mom0_MB/F");
104  myTree_->Branch("mom1_MB", &mom1_MB, "mom1_MB/F");
105  myTree_->Branch("mom2_MB", &mom2_MB, "mom2_MB/F");
106  myTree_->Branch("mom3_MB", &mom3_MB, "mom3_MB/F");
107  myTree_->Branch("mom4_MB", &mom4_MB, "mom4_MB/F");
108 
109  myMap_.clear();
110 }
111 
112 // EndJob
113 //
115 
116  cells = 0;
117  for (std::map<HcalDetId,myInfo>::const_iterator itr=myMap_.begin();
118  itr != myMap_.end(); ++itr) {
119  mysubd = itr->first.subdet();
120  depth = itr->first.depth();
121  iphi = itr->first.iphi();
122  ieta = itr->first.ieta();
123  myInfo info = itr->second;
124  if (info.theMB0 > 0) {
125  mom0_MB = info.theMB0;
126  mom1_MB = info.theMB1;
127  mom2_MB = info.theMB2;
128  mom3_MB = info.theMB3;
129  mom4_MB = info.theMB4;
130  cells++;
131 
132  edm::LogInfo("AnalyzerMB") << " Result= " << mysubd << " " << ieta << " "
133  << iphi << " mom0 " << mom0_MB << " mom1 "
134  << mom1_MB << " mom2 " << mom2_MB << " mom3 "
135  << mom3_MB << " mom4 " << mom4_MB;
136  myTree_->Fill();
137  }
138  }
139  edm::LogInfo("AnalyzerMB") << "cells " << cells;
140 }
141 
142 //
143 // member functions
144 //
145 
146 // ------------ method called to produce the data ------------
147 
149  const edm::EventSetup&) {
150  edm::LogInfo("AnalyzerMB") << " Start SimAnalyzerMinbias::analyze "
151  << iEvent.id().run() << ":" <<iEvent.id().event();
152 
153 
155  iEvent.getByToken(tok_evt_, evtMC);
156  if (!evtMC.isValid()) {
157  edm::LogWarning("AnalyzerMB") << "no HepMCProduct found";
158  } else {
159  const HepMC::GenEvent * myGenEvent = evtMC->GetEvent();
160  edm::LogInfo("AnalyzerMB") << "Event with " << myGenEvent->particles_size()
161  << " particles + " <<myGenEvent->vertices_size()
162  << " vertices";
163  }
164 
165 
167  iEvent.getByToken(tok_hcal_,hcalHits);
168  if (!hcalHits.isValid()) {
169  edm::LogWarning("AnalyzerMB") << "Error! can't get HcalHits product!";
170  return ;
171  }
172 
173  const edm::PCaloHitContainer * HitHcal = hcalHits.product () ;
174  std::map<HcalDetId,double> hitMap;
175  for (std::vector<PCaloHit>::const_iterator hcalItr = HitHcal->begin();
176  hcalItr != HitHcal->end(); ++hcalItr) {
177  double time = hcalItr->time();
178  if (time < timeCut_) {
179  double energyhit = hcalItr->energy();
180  HcalDetId hid = HcalDetId(hcalItr->id());
181  std::map<HcalDetId,double>::iterator itr1 = hitMap.find(hid);
182  if (itr1 == hitMap.end()) {
183  hitMap[hid] = 0;
184  itr1 = hitMap.find(hid);
185  }
186  itr1->second += energyhit;
187  }
188  }
189  edm::LogInfo("AnalyzerMB") << "extract information of " << hitMap.size()
190  << " towers from " << HitHcal->size() << " hits";
191 
192  for (std::map<HcalDetId,double>::const_iterator hcalItr=hitMap.begin();
193  hcalItr != hitMap.end(); ++hcalItr) {
194  HcalDetId hid = hcalItr->first;
195  double energyhit = hcalItr->second;
196  std::map<HcalDetId,myInfo>::iterator itr1 = myMap_.find(hid);
197  if (itr1 == myMap_.end()) {
198  myInfo info;
199  myMap_[hid] = info;
200  itr1 = myMap_.find(hid);
201  }
202  itr1->second.theMB0++;
203  itr1->second.theMB1 += energyhit;
204  itr1->second.theMB2 += (energyhit*energyhit);
205  itr1->second.theMB3 += (energyhit*energyhit*energyhit);
206  itr1->second.theMB4 += (energyhit*energyhit*energyhit*energyhit);
207  edm::LogInfo("AnalyzerMB") << "ID " << hid << " with energy " << energyhit;
208  }
209 }
210 
211 //define this as a plug-in
214 
RunNumber_t run() const
Definition: EventID.h:39
std::map< HcalDetId, myInfo > myMap_
EventNumber_t event() const
Definition: EventID.h:41
T getUntrackedParameter(std::string const &, T const &) const
std::vector< PCaloHit > PCaloHitContainer
static const TGPicture * info(bool iBackgroundIsBlack)
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:579
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
T * make(const Args &...args) const
make new ROOT object
Definition: TFileService.h:64
return((rh^lh)&mask)
virtual void beginJob()
edm::EDGetTokenT< edm::HepMCProduct > tok_evt_
int iEvent
Definition: GenABIO.cc:230
bool isValid() const
Definition: HandleBase.h:74
edm::EDGetTokenT< edm::PCaloHitContainer > tok_hcal_
const HepMC::GenEvent * GetEvent() const
Definition: HepMCProduct.h:38
T const * product() const
Definition: Handle.h:81
edm::Service< TFileService > fs_
virtual void beginRun(const edm::Run &r, const edm::EventSetup &iSetup)
edm::EventID id() const
Definition: EventBase.h:60
SimAnalyzerMinbias(const edm::ParameterSet &)
virtual void analyze(const edm::Event &, const edm::EventSetup &)
Definition: Run.h:44
virtual void endRun(const edm::Run &r, const edm::EventSetup &iSetup)