HGCal Test Beam  03a93d6239a951948e06fb3ef8dae4cbdebfad30
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
Pedestals.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: HGCal/Calibration
4 // Class: Pedestals
5 //
6 /**\class Pedestals Pedestals.cc HGCal/Pedestals/plugins/Pedestals.cc
7 
8  Description: [one line class summary]
9 
10  Implementation:
11  [Notes on implementation]
12 */
13 //
14 // Original Author: Shervin Nourbakhsh
15 // Created: Sun, 03 Apr 2016 09:12:17 GMT
16 //
17 //
18 
19 
20 // system include files
21 #include <memory>
22 
23 // user include files
24 #include "FWCore/Framework/interface/Frameworkfwd.h"
25 #include "FWCore/Framework/interface/one/EDAnalyzer.h"
26 
27 #include "FWCore/Framework/interface/Event.h"
28 #include "FWCore/Framework/interface/MakerMacros.h"
29 
30 #include "FWCore/ParameterSet/interface/ParameterSet.h"
31 
32 
33 #include "HGCal/DataFormats/interface/HGCalTBDigiCollections.h"
34 #include "HGCal/CondObjects/interface/HGCalCondObjects.h"
35 #include "HGCal/CondObjects/interface/HGCalCondObjectTextIO.h"
36 #include "HGCal/CondObjects/interface/HGCalTBNumberingScheme.h"
37 
38 #include <cassert>
39 
40 //
41 // class declaration
42 //
43 /// \todo calculated and define pedestals for all gains
44 
45 // If the analyzer does not use TFileService, please remove
46 // the template argument to the base class so the class inherits
47 // from edm::one::EDAnalyzer<> and also remove the line from
48 // constructor "usesResource("TFileService");"
49 // This will improve performance in multithreaded jobs.
50 
51 class Pedestals : public edm::one::EDAnalyzer<edm::one::SharedResources>
52 {
53 public:
54  explicit Pedestals(const edm::ParameterSet&);
55  ~Pedestals();
56 
57  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
58 
59 
60 private:
61  virtual void beginJob() override;
62  virtual void analyze(const edm::Event&, const edm::EventSetup&) override;
63  virtual void endJob() override;
64 
65  // ----------member data ---------------------------
66 
67  edm::EDGetTokenT<HGCalTBDigiCollection> _digisToken;
68 
69  typedef struct {
70  float sum;
71  float sum2;
72  unsigned int n;
73  } pedestalSum_t;
74 
75  typedef std::map< HGCalTBDetId, pedestalSum_t > pedestalMap_t;
76  pedestalMap_t pedestals;
77 
78 };
79 
80 //
81 // constants, enums and typedefs
82 //
83 
84 //
85 // static data member definitions
86 //
87 
88 //
89 // constructors and destructor
90 //
91 Pedestals::Pedestals(const edm::ParameterSet& iConfig) :
92  _digisToken(consumes<HGCalTBDigiCollection>(iConfig.getParameter<edm::InputTag>("digiCollection")))
93 {
94  //now do what ever initialization is needed
95  usesResource("TFileService");
96 
97 }
98 
99 
100 Pedestals::~Pedestals()
101 {
102 
103  // do anything here that needs to be done at desctruction time
104  // (e.g. close files, deallocate resources etc.)
105 
106 }
107 
108 
109 //
110 // member functions
111 //
112 
113 // ------------ method called for each event ------------
114 void
115 Pedestals::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup)
116 {
117  using namespace edm;
118 
119  edm::Handle<HGCalTBDigiCollection> digisHandle;
120  iEvent.getByToken(_digisToken, digisHandle);
121 
124 
126  HGCalElectronicsMap emap;
127 
128  condIO.load("CondObjects/data/map_FNAL_2.txt", emap);
129 
130  for(auto digi_itr = digisHandle->begin(); digi_itr != digisHandle->end(); ++digi_itr) {
131 
132  SKIROC2DataFrame digi(*digi_itr);
133  HGCalTBDetId detId = digi.detid();
134 
135  unsigned int nSamples = digi.samples();
136 
137  /**** WARNING \todo multiple samples are not yet allowed */
138  nSamples = 1;
139  /**** */
140 
141  pedestalMap_t::iterator thisPedestal_itr = pedestals.find(detId);
142  if(thisPedestal_itr == pedestals.end()) {
143  pedestalSum_t empty_ped;
144  empty_ped.sum = 0.;
145  empty_ped.n = 0;
146 
147  pedestals[detId] = empty_ped;
148  thisPedestal_itr = pedestals.find(detId);
149  assert(thisPedestal_itr != pedestals.end());
150  }
151 
152  // now sum the values from different events
153  for(unsigned int iSample = 0; iSample < nSamples; ++iSample) {
154  thisPedestal_itr->second.sum += digi[iSample].adcLow();
155  thisPedestal_itr->second.sum2 += digi[iSample].adcLow() * digi[iSample].adcLow();
156  ++(thisPedestal_itr->second.n);
157 #ifdef DEBUG
158  std::cout << "[PEDESTAL PRODUCER: digi]" << *digi_itr << std::endl;
159  std::cout << " ** " << thisPedestal_itr->second.sum << std::endl;
160 #endif
161 
162  }
163  }
164 
165  // now I have the pedestal average, I have to print them
166  for(const auto pedestal : pedestals) {
167  float pedestal_mean = pedestal.second.sum / pedestal.second.n;
168  pedestals_cond.set(pedestal.first, pedestal_mean);
169  noise_cond.set(pedestal.first, sqrt(pedestal.second.sum2 / pedestal.second.n - (pedestal_mean * pedestal_mean)));
170  }
171  condIO.store("newPedestals.txt", pedestals_cond);
172  condIO.store("newNoise.txt", noise_cond);
173 
174 }
175 
176 
177 // ------------ method called once each job just before starting event loop ------------
178 void
179 Pedestals::beginJob()
180 {
181 }
182 
183 // ------------ method called once each job just after ending the event loop ------------
184 void
185 Pedestals::endJob()
186 {
187 }
188 
189 // ------------ method fills 'descriptions' with the allowed parameters for the module ------------
190 void
191 Pedestals::fillDescriptions(edm::ConfigurationDescriptions& descriptions)
192 {
193  //The following says we do not know what parameters are allowed so do no validation
194  // Please change this to state exactly what you do use, even if it is no parameters
195  edm::ParameterSetDescription desc;
196  desc.setUnknown();
197  descriptions.addDefault(desc);
198 }
199 
200 //define this as a plug-in
201 DEFINE_FWK_MODULE(Pedestals);
DEFINE_FWK_MODULE(Pedestals)
static const HGCalCondObjectNumberingScheme * scheme()
provides the conversion between electronics Id to DetId