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 //#define debugLog
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  TFile* hOutputFile ;
55  TTree* myTree;
56 
57  // Root tree members
60  struct myInfo{
62  void MyInfo() {
63  theMB0 = theMB1 = theMB2 = theMB3 = theMB4 = 0;
64  }
65  };
66  std::map<HcalDetId,myInfo> myMap;
69 };
70 
71 // constructors and destructor
72 
74  // get name of output file with histogramms
75  fOutputFileName = iConfig.getUntrackedParameter<std::string>("HistOutFile", "simOutput.root");
76  timeCut = iConfig.getUntrackedParameter<double>("TimeCut", 500);
77 
78  // get token names of modules, producing object collections
79  tok_evt_ = consumes<edm::HepMCProduct>(edm::InputTag("generator"));
80  tok_hcal_ = consumes<edm::PCaloHitContainer>(edm::InputTag("g4SimHits","HcalHits"));
81 
82 #ifdef debugLog
83  std::cout << "Use Time cut of " << timeCut << " ns and store o/p in "
84  << fOutputFileName << std::endl;
85 #endif
86 }
87 
89  // do anything here that needs to be done at desctruction time
90  // (e.g. close files, deallocate resources etc.)
91 }
92 
94 }
95 
96 void SimAnalyzerMinbias::endRun( const edm::Run& r, const edm::EventSetup& iSetup) {
97 }
98 
100  hOutputFile = new TFile( fOutputFileName.c_str(), "RECREATE" ) ;
101 
102  myTree = new TTree("SimJet","SimJet Tree");
103  myTree->Branch("mydet", &mydet, "mydet/I");
104  myTree->Branch("mysubd", &mysubd, "mysubd/I");
105  myTree->Branch("cells", &cells, "cells");
106  myTree->Branch("depth", &depth, "depth/I");
107  myTree->Branch("ieta", &ieta, "ieta/I");
108  myTree->Branch("iphi", &iphi, "iphi/I");
109  myTree->Branch("mom0_MB", &mom0_MB, "mom0_MB/F");
110  myTree->Branch("mom1_MB", &mom1_MB, "mom1_MB/F");
111  myTree->Branch("mom2_MB", &mom2_MB, "mom2_MB/F");
112  myTree->Branch("mom3_MB", &mom2_MB, "mom3_MB/F");
113  myTree->Branch("mom4_MB", &mom4_MB, "mom4_MB/F");
114 
115  myMap.clear();
116  return ;
117 }
118 
119 // EndJob
120 //
122 
123  cells = 0;
124  // std::cout << " Hello me here" << std::endl;
125  for (std::map<HcalDetId,myInfo>::const_iterator itr=myMap.begin();
126  itr != myMap.end(); ++itr) {
127  // std::cout << " Hello me here" << std::endl;
128  mysubd = itr->first.subdet();
129  depth = itr->first.depth();
130  iphi = itr->first.iphi();
131  ieta = itr->first.ieta();
132  myInfo info = itr->second;
133  if (info.theMB0 > 0) {
134  mom0_MB = info.theMB0;
135  mom1_MB = info.theMB1;
136  mom2_MB = info.theMB2;
137  mom3_MB = info.theMB3;
138  mom4_MB = info.theMB4;
139  cells++;
140 
141  edm::LogInfo("AnalyzerMB") << " Result= " << mysubd << " " << ieta << " "
142  << iphi << " mom0 " << mom0_MB << " mom1 "
143  << mom1_MB << " mom2 " << mom2_MB << " mom3 "
144  << mom3_MB << " mom4 " << mom4_MB;
145 #ifdef debugLog
146  std::cout << " Result= " << mysubd << " " << ieta << " "
147  << iphi << " mom0 " << mom0_MB << " mom1 "
148  << mom1_MB << " mom2 " << mom2_MB << " mom3 "
149  << mom3_MB << " mom4 " << mom4_MB << std::endl;
150 #endif
151  myTree->Fill();
152  }
153  }
154  edm::LogInfo("AnalyzerMB") << "cells " << cells;
155 #ifdef debugLog
156  std::cout << "cells " << cells << std::endl;
157 #endif
158  hOutputFile->cd();
159  myTree->Write();
160  hOutputFile->Write();
161  hOutputFile->Close() ;
162  return ;
163 }
164 
165 //
166 // member functions
167 //
168 
169 // ------------ method called to produce the data ------------
170 
172  const edm::EventSetup&) {
173 #ifdef debugLog
174  std::cout << " Start SimAnalyzerMinbias::analyze " << iEvent.id().run()
175  << ":" << iEvent.id().event() << std::endl;
176 #endif
177 
179  iEvent.getByToken(tok_evt_, evtMC);
180  if (!evtMC.isValid()) {
181  edm::LogInfo("AnalyzerMB") << "no HepMCProduct found";
182 #ifdef debugLog
183  std::cout << "no HepMCProduct found" << std::endl;
184 #endif
185  } else {
186  const HepMC::GenEvent * myGenEvent = evtMC->GetEvent();
187  edm::LogInfo("AnalyzerMB") << "Event with " << myGenEvent->particles_size()
188  << " particles + " << myGenEvent->vertices_size()
189  << " vertices";
190 #ifdef debugLog
191  std::cout << "Event with " << myGenEvent->particles_size()
192  << " particles + " << myGenEvent->vertices_size()
193  << " vertices" << std::endl;
194 #endif
195  }
196 
197 
199  iEvent.getByToken(tok_hcal_,hcalHits);
200  if (!hcalHits.isValid()) {
201  edm::LogInfo("AnalyzerMB") << "Error! can't get HcalHits product!";
202 #ifdef debugLog
203  std::cout << "Error! can't get HcalHits product!"
204  << std::endl;
205 #endif
206  return ;
207  }
208 
209  const edm::PCaloHitContainer * HitHcal = hcalHits.product () ;
210  std::map<HcalDetId,double> hitMap;
211  for (std::vector<PCaloHit>::const_iterator hcalItr = HitHcal->begin ();
212  hcalItr != HitHcal->end(); ++hcalItr) {
213  double time = hcalItr->time();
214  if (time < timeCut) {
215  double energyhit = hcalItr->energy();
216  HcalDetId hid = HcalDetId(hcalItr->id());
217  std::map<HcalDetId,double>::iterator itr1 = hitMap.find(hid);
218  if (itr1 == hitMap.end()) {
219  hitMap[hid] = 0;
220  itr1 = hitMap.find(hid);
221  }
222  itr1->second += energyhit;
223  }
224  }
225 #ifdef debugLog
226  std::cout << "extract information of " << hitMap.size() << " towers from "
227  << HitHcal->size() << " hits" << std::endl;
228 #endif
229 
230  for (std::map<HcalDetId,double>::const_iterator hcalItr=hitMap.begin();
231  hcalItr != hitMap.end(); ++hcalItr) {
232  HcalDetId hid = hcalItr->first;
233  double energyhit = hcalItr->second;
234  std::map<HcalDetId,myInfo>::iterator itr1 = myMap.find(hid);
235  if (itr1 == myMap.end()) {
236  myInfo info;
237  myMap[hid] = info;
238  itr1 = myMap.find(hid);
239  }
240  itr1->second.theMB0++;
241  itr1->second.theMB1 += energyhit;
242  itr1->second.theMB2 += (energyhit*energyhit);
243  itr1->second.theMB3 += (energyhit*energyhit*energyhit);
244  itr1->second.theMB4 += (energyhit*energyhit*energyhit*energyhit);
245 #ifdef debugLog
246  std::cout << "ID " << hid << " with energy " << energyhit << std::endl;
247 #endif
248  }
249 }
250 
251 //define this as a plug-in
254 
RunNumber_t run() const
Definition: EventID.h:39
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:457
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
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:75
edm::EDGetTokenT< edm::PCaloHitContainer > tok_hcal_
T const * product() const
Definition: Handle.h:81
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 &)
tuple cout
Definition: gather_cfg.py:121
std::map< HcalDetId, myInfo > myMap
Definition: Run.h:41
virtual void endRun(const edm::Run &r, const edm::EventSetup &iSetup)