CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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
18 
23 
29 
31 
32 #include "TFile.h"
33 #include "TTree.h"
34 
35 // class declaration
37 
38 public:
39  explicit SimAnalyzerMinbias(const edm::ParameterSet&);
41  virtual void analyze(const edm::Event&, const edm::EventSetup&);
42  virtual void beginJob() ;
43  virtual void endJob() ;
44 
45 private:
46 
47  // ----------member data ---------------------------
49  double timeCut_;
50  TFile* hOutputFile_;
51  TTree* myTree_;
52 
53  // Root tree members
56  struct myInfo{
58  void MyInfo() {
59  theMB0 = theMB1 = theMB2 = theMB3 = theMB4 = 0;
60  }
61  };
62  std::map<HcalDetId,myInfo> myMap_;
65 };
66 
67 // constructors and destructor
68 
70  // get name of output file with histogramms
71  fOutputFileName_= iConfig.getUntrackedParameter<std::string>("HistOutFile", "simOutput.root");
72  timeCut_ = iConfig.getUntrackedParameter<double>("TimeCut", 500);
73 
74  // get token names of modules, producing object collections
75  tok_evt_ = consumes<edm::HepMCProduct>(edm::InputTag("generatorSmeared"));
76  tok_hcal_ = consumes<edm::PCaloHitContainer>(edm::InputTag("g4SimHits","HcalHits"));
77 
78  edm::LogInfo("AnalyzerMB") << "Use Time cut of " << timeCut_
79  << " ns and store o/p in " << fOutputFileName_;
80 }
81 
83  // do anything here that needs to be done at desctruction time
84  // (e.g. close files, deallocate resources etc.)
85 }
86 
88  hOutputFile_ = new TFile( fOutputFileName_.c_str(), "RECREATE" );
89 
90  myTree_ = new TTree("SimJet","SimJet Tree");
91  myTree_->Branch("mydet", &mydet, "mydet/I");
92  myTree_->Branch("mysubd", &mysubd, "mysubd/I");
93  myTree_->Branch("cells", &cells, "cells");
94  myTree_->Branch("depth", &depth, "depth/I");
95  myTree_->Branch("ieta", &ieta, "ieta/I");
96  myTree_->Branch("iphi", &iphi, "iphi/I");
97  myTree_->Branch("mom0_MB", &mom0_MB, "mom0_MB/F");
98  myTree_->Branch("mom1_MB", &mom1_MB, "mom1_MB/F");
99  myTree_->Branch("mom2_MB", &mom2_MB, "mom2_MB/F");
100  myTree_->Branch("mom3_MB", &mom2_MB, "mom3_MB/F");
101  myTree_->Branch("mom4_MB", &mom4_MB, "mom4_MB/F");
102 
103  myMap_.clear();
104 }
105 
106 // EndJob
107 //
109 
110  cells = 0;
111  for (std::map<HcalDetId,myInfo>::const_iterator itr=myMap_.begin();
112  itr != myMap_.end(); ++itr) {
113  mysubd = itr->first.subdet();
114  depth = itr->first.depth();
115  iphi = itr->first.iphi();
116  ieta = itr->first.ieta();
117  myInfo info = itr->second;
118  if (info.theMB0 > 0) {
119  mom0_MB = info.theMB0;
120  mom1_MB = info.theMB1;
121  mom2_MB = info.theMB2;
122  mom3_MB = info.theMB3;
123  mom4_MB = info.theMB4;
124  cells++;
125 
126  edm::LogInfo("AnalyzerMB") << " Result= " << mysubd << " " << ieta << " "
127  << iphi << " mom0 " << mom0_MB << " mom1 "
128  << mom1_MB << " mom2 " << mom2_MB << " mom3 "
129  << mom3_MB << " mom4 " << mom4_MB;
130  myTree_->Fill();
131  }
132  }
133  edm::LogInfo("AnalyzerMB") << "cells " << cells;
134  hOutputFile_->cd();
135  myTree_->Write();
136  hOutputFile_->Write();
137  hOutputFile_->Close() ;
138 }
139 
140 //
141 // member functions
142 //
143 
144 // ------------ method called to produce the data ------------
145 
147  const edm::EventSetup&) {
148 
149  edm::LogInfo("AnalyzerMB") << " Start SimAnalyzerMinbias::analyze "
150  << iEvent.id().run() << ":" << iEvent.id().event();
151 
153  iEvent.getByToken(tok_evt_, evtMC);
154  if (!evtMC.isValid()) {
155  edm::LogInfo("AnalyzerMB") << "no HepMCProduct found";
156  } else {
157  const HepMC::GenEvent * myGenEvent = evtMC->GetEvent();
158  edm::LogInfo("AnalyzerMB") << "Event with " << myGenEvent->particles_size()
159  << " particles + " << myGenEvent->vertices_size()
160  << " vertices";
161  }
162 
163 
165  iEvent.getByToken(tok_hcal_,hcalHits);
166  if (!hcalHits.isValid()) {
167  edm::LogWarning("AnalyzerMB") << "Error! can't get HcalHits product!";
168  return;
169  }
170 
171  const edm::PCaloHitContainer * HitHcal = hcalHits.product () ;
172  std::map<HcalDetId,double> hitMap;
173  for (std::vector<PCaloHit>::const_iterator hcalItr = HitHcal->begin();
174  hcalItr != HitHcal->end(); ++hcalItr) {
175  double time = hcalItr->time();
176  if (time < timeCut_) {
177  double energyhit = hcalItr->energy();
178  HcalDetId hid = HcalDetId(hcalItr->id());
179  std::map<HcalDetId,double>::iterator itr1 = hitMap.find(hid);
180  if (itr1 == hitMap.end()) {
181  hitMap[hid] = 0;
182  itr1 = hitMap.find(hid);
183  }
184  itr1->second += energyhit;
185  }
186  }
187  edm::LogInfo("AnalyzerMB") << "extract information of " << hitMap.size()
188  << " towers from " << HitHcal->size() << " hits";
189 
190  for (std::map<HcalDetId,double>::const_iterator hcalItr=hitMap.begin();
191  hcalItr != hitMap.end(); ++hcalItr) {
192  HcalDetId hid = hcalItr->first;
193  double energyhit = hcalItr->second;
194  std::map<HcalDetId,myInfo>::iterator itr1 = myMap_.find(hid);
195  if (itr1 == myMap_.end()) {
196  myInfo info;
197  myMap_[hid] = info;
198  itr1 = myMap_.find(hid);
199  }
200  itr1->second.theMB0++;
201  itr1->second.theMB1 += energyhit;
202  itr1->second.theMB2 += (energyhit*energyhit);
203  itr1->second.theMB3 += (energyhit*energyhit*energyhit);
204  itr1->second.theMB4 += (energyhit*energyhit*energyhit*energyhit);
205  edm::LogInfo("AnalyzerMB") << "ID " << hid << " with energy " << energyhit;
206  }
207 }
208 
209 //define this as a plug-in
212 
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:464
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
virtual void beginJob()
edm::EDGetTokenT< edm::HepMCProduct > tok_evt_
int iEvent
Definition: GenABIO.cc:230
bool isValid() const
Definition: HandleBase.h:75
edm::EDGetTokenT< edm::PCaloHitContainer > tok_hcal_
std::string fOutputFileName_
T const * product() const
Definition: Handle.h:81
edm::EventID id() const
Definition: EventBase.h:60
SimAnalyzerMinbias(const edm::ParameterSet &)
virtual void analyze(const edm::Event &, const edm::EventSetup &)