CMS 3D CMS Logo

TreeProducerCalibSimul.cc
Go to the documentation of this file.
1 // framework
11 
12 // for reconstruction
20 
21 // geometry
25 
26 // my include files
28 
29 // root includes
30 #include "TROOT.h"
31 #include "TSystem.h"
32 #include "TFile.h"
33 #include "TTree.h"
34 #include "TF1.h"
35 #include "TH1.h"
36 #include "TH2.h"
37 #include "TGraph.h"
38 #include "TStyle.h"
39 #include "TCanvas.h"
40 #include "TSelector.h"
41 #include "TApplication.h"
42 
43 // c++ includes
44 #include <string>
45 #include <stdio.h>
46 #include <sstream>
47 #include <iostream>
48 #include <unistd.h>
49 #include <fstream>
50 #include <math.h>
51 #include <memory>
52 #include <stdexcept>
53 
54 class TreeProducerCalibSimul : public edm::one::EDAnalyzer<edm::one::SharedResources> {
55 public:
57  ~TreeProducerCalibSimul() override = default;
58 
59  void analyze(const edm::Event&, const edm::EventSetup&) override;
60  void beginJob() override;
61  void endJob() override;
62 
63 private:
74  double posCluster_;
75 
76  std::unique_ptr<TreeMatrixCalib> myTree_;
77 
82 
86  int noHits_;
87  int noHodo_;
88  int noTdc_;
89  int noHeader_;
90 };
91 
92 // -------------------------------------------------
93 // contructor
95  usesResource(TFileService::kSharedResource);
96 
97  // now do what ever initialization is needed
98  xtalInBeam_ = iConfig.getUntrackedParameter<int>("xtalInBeam", -1000);
99  rootfile_ = iConfig.getUntrackedParameter<std::string>("rootfile", "mySimMatrixTree.root");
100  txtfile_ = iConfig.getUntrackedParameter<std::string>("txtfile", "mySimMatrixTree.txt");
101  EBRecHitCollection_ = iConfig.getParameter<std::string>("EBRecHitCollection");
102  RecHitProducer_ = iConfig.getParameter<std::string>("RecHitProducer");
103  hodoRecInfoCollection_ = iConfig.getParameter<std::string>("hodoRecInfoCollection");
104  hodoRecInfoProducer_ = iConfig.getParameter<std::string>("hodoRecInfoProducer");
105  tdcRecInfoCollection_ = iConfig.getParameter<std::string>("tdcRecInfoCollection");
106  tdcRecInfoProducer_ = iConfig.getParameter<std::string>("tdcRecInfoProducer");
107  eventHeaderCollection_ = iConfig.getParameter<std::string>("eventHeaderCollection");
108  eventHeaderProducer_ = iConfig.getParameter<std::string>("eventHeaderProducer");
109 
110  // summary
111  edm::LogVerbatim("GFlash") << "\nConstructor\n\nTreeProducerCalibSimul\nxtal in beam = " << xtalInBeam_;
112  edm::LogVerbatim("GFlash") << "Fetching hitCollection: " << EBRecHitCollection_.c_str() << " prod by "
113  << RecHitProducer_.c_str();
114  edm::LogVerbatim("GFlash") << "Fetching hodoCollection: " << hodoRecInfoCollection_.c_str() << " prod by "
115  << hodoRecInfoProducer_.c_str();
116  edm::LogVerbatim("GFlash") << "Fetching tdcCollection: " << tdcRecInfoCollection_.c_str() << " prod by "
117  << tdcRecInfoProducer_.c_str();
118  edm::LogVerbatim("GFlash") << "Fetching evHeaCollection: " << eventHeaderCollection_.c_str() << " prod by "
119  << eventHeaderProducer_.c_str() << "\n";
120 
121  tokEBRecHit_ = consumes<EBRecHitCollection>(edm::InputTag(RecHitProducer_, EBRecHitCollection_));
122  tokEcalHodo_ = consumes<EcalTBHodoscopeRecInfo>(edm::InputTag(hodoRecInfoProducer_, hodoRecInfoCollection_));
123  tokEcalTDC_ = consumes<EcalTBTDCRecInfo>(edm::InputTag(tdcRecInfoProducer_, tdcRecInfoCollection_));
124  tokEventHeader_ = consumes<EcalTBEventHeader>(edm::InputTag(eventHeaderProducer_));
125 }
126 
127 // ------------------------------------------------------
128 // initializations
130  edm::LogVerbatim("GFlash") << "\nBeginJob\n";
131 
132  // tree
133  myTree_ = std::make_unique<TreeMatrixCalib>(rootfile_.c_str());
134 
135  // counters
136  tot_events_ = 0;
137  tot_events_ok_ = 0;
138  noHits_ = 0;
139  noHodo_ = 0;
140  noTdc_ = 0;
141  noHeader_ = 0;
142 }
143 
144 // -------------------------------------------
145 // finalizing
147  edm::LogVerbatim("GFlash") << "\nEndJob\n";
148 
149  std::ofstream MyOut(txtfile_.c_str());
150  MyOut << "total events: " << tot_events_ << std::endl;
151  MyOut << "events skipped because of no hits: " << noHits_ << std::endl;
152  MyOut << "events skipped because of no hodos: " << noHodo_ << std::endl;
153  MyOut << "events skipped because of no tdc: " << noTdc_ << std::endl;
154  MyOut << "events skipped because of no header: " << noHeader_ << std::endl;
155  MyOut << "total OK events (passing the basic selection): " << tot_events_ok_ << std::endl;
156  MyOut.close();
157 }
158 
159 // -----------------------------------------------
160 // my analysis
162  // counting events
163  ++tot_events_;
164 
165  if (tot_events_ % 5000 == 0)
166  edm::LogVerbatim("GFlash") << "event " << tot_events_;
167 
168  // ---------------------------------------------------------------------
169  // taking what I need: hits
171 
172  // taking what I need: hodoscopes
173  const EcalTBHodoscopeRecInfo* recHodo = &iEvent.get(tokEcalHodo_);
174 
175  // taking what I need: tdc
176  const EcalTBTDCRecInfo* recTDC = &iEvent.get(tokEcalTDC_);
177 
178  // taking what I need: event header
179  const EcalTBEventHeader* evtHeader = &iEvent.get(tokEventHeader_);
180 
181  // checking everything is there and fine
182  if ((!EBRecHits) || (EBRecHits->size() == 0)) {
183  ++noHits_;
184  return;
185  }
186  if (!recTDC) {
187  ++noTdc_;
188  return;
189  }
190  if (!recHodo) {
191  ++noHodo_;
192  return;
193  }
194  if (!evtHeader) {
195  ++noHeader_;
196  return;
197  }
198  ++tot_events_ok_;
199 
200  // ---------------------------------------------------------------------
201  // info on the event
202  int run = -999;
203  int tbm = -999;
204  int event = evtHeader->eventNumber();
205 
206  // ---------------------------------------------------------------------
207  // xtal-in-beam
208  int nomXtalInBeam = -999;
209  int nextXtalInBeam = -999;
210 
211  EBDetId xtalInBeamId(1, xtalInBeam_, EBDetId::SMCRYSTALMODE);
212  if (xtalInBeamId == EBDetId(0)) {
213  return;
214  }
215  int mySupCry = xtalInBeamId.ic();
216  int mySupEta = xtalInBeamId.ieta();
217  int mySupPhi = xtalInBeamId.iphi();
218 
219  // ---------------------------------------------------------------------
220  // hodoscope information
221  double x = recHodo->posX();
222  double y = recHodo->posY();
223  double sx = recHodo->slopeX();
224  double sy = recHodo->slopeY();
225  double qx = recHodo->qualX();
226  double qy = recHodo->qualY();
227 
228  // ---------------------------------------------------------------------
229  // tdc information
230  double tdcOffset = recTDC->offset();
231 
232  // ---------------------------------------------------------------------
233  // Find EBDetId in a 7x7 Matrix
234  EBDetId Xtals7x7[49];
235  double energy[49];
236  int crystal[49];
237  int allMatrix = 1;
238  for (unsigned int icry = 0; icry < 49; icry++) {
239  unsigned int row = icry / 7;
240  unsigned int column = icry % 7;
241  Xtals7x7[icry] = EBDetId(xtalInBeamId.ieta() + column - 3, xtalInBeamId.iphi() + row - 3, EBDetId::ETAPHIMODE);
242 
243  if (Xtals7x7[icry].ism() == 1) {
244  energy[icry] = EBRecHits->find(Xtals7x7[icry])->energy();
245  crystal[icry] = Xtals7x7[icry].ic();
246  } else {
247  energy[icry] = -100.;
248  crystal[icry] = -100;
249  allMatrix = 0;
250  }
251  }
252 
253  // ---------------------------------------------------------------------
254  // Looking for the max energy crystal
255  double maxEne = -999.;
256  int maxEneCry = 9999;
257  for (int ii = 0; ii < 49; ii++) {
258  if (energy[ii] > maxEne) {
259  maxEne = energy[ii];
260  maxEneCry = crystal[ii];
261  }
262  }
263 
264  // Position reconstruction - skipped here
265  double Xcal = -999.;
266  double Ycal = -999.;
267 
268  // filling the tree
269  myTree_->fillInfo(run,
270  event,
271  mySupCry,
272  maxEneCry,
273  nomXtalInBeam,
274  nextXtalInBeam,
275  mySupEta,
276  mySupPhi,
277  tbm,
278  x,
279  y,
280  Xcal,
281  Ycal,
282  sx,
283  sy,
284  qx,
285  qy,
286  tdcOffset,
287  allMatrix,
288  energy,
289  crystal);
290  myTree_->store();
291 }
292 
295 
296 //define this as a plug-in
297 
static const std::string kSharedResource
Definition: TFileService.h:76
Log< level::Info, true > LogVerbatim
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
edm::EDGetTokenT< EcalTBTDCRecInfo > tokEcalTDC_
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
int iphi() const
get the crystal iphi
Definition: EBDetId.h:51
TreeProducerCalibSimul(const edm::ParameterSet &)
edm::EDGetTokenT< EcalTBEventHeader > tokEventHeader_
edm::EDGetTokenT< EcalTBHodoscopeRecInfo > tokEcalHodo_
int ieta() const
get the crystal ieta
Definition: EBDetId.h:49
T getUntrackedParameter(std::string const &, T const &) const
int eventNumber() const
Returns the event number.
int iEvent
Definition: GenABIO.cc:224
int ic() const
get ECAL/crystal number inside SM
Definition: EBDetId.cc:41
~TreeProducerCalibSimul() override=default
static const int ETAPHIMODE
Definition: EBDetId.h:158
ii
Definition: cuy.py:589
float offset() const
void analyze(const edm::Event &, const edm::EventSetup &) override
std::unique_ptr< TreeMatrixCalib > myTree_
static const int SMCRYSTALMODE
Definition: EBDetId.h:159
int ism(int ieta, int iphi)
Definition: EcalPyUtils.cc:49
Definition: event.py:1
edm::EDGetTokenT< EBRecHitCollection > tokEBRecHit_