CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
BeamSpotAnalyzer.cc
Go to the documentation of this file.
1 
15 // C++ standard
16 #include <string>
17 // CMS
21 
25 
26 #include "TMath.h"
27 
29 {
30  // get parameter
31  write2DB_ = iConfig.getParameter<edm::ParameterSet>("BSAnalyzerParameters").getParameter<bool>("WriteToDB");
32  runallfitters_ = iConfig.getParameter<edm::ParameterSet>("BSAnalyzerParameters").getParameter<bool>("RunAllFitters");
33  fitNLumi_ = iConfig.getParameter<edm::ParameterSet>("BSAnalyzerParameters").getUntrackedParameter<int>("fitEveryNLumi",-1);
34  resetFitNLumi_ = iConfig.getParameter<edm::ParameterSet>("BSAnalyzerParameters").getUntrackedParameter<int>("resetEveryNLumi",-1);
35  runbeamwidthfit_ = iConfig.getParameter<edm::ParameterSet>("BSAnalyzerParameters").getParameter<bool>("RunBeamWidthFit");
36 
37  theBeamFitter = new BeamFitter(iConfig);
43 
44  ftotalevents = 0;
45  ftmprun0 = ftmprun = -1;
46  countLumi_ = 0;
50 }
51 
52 
54 {
55  delete theBeamFitter;
56 }
57 
58 
59 void
61 {
62  ftotalevents++;
63  theBeamFitter->readEvent(iEvent);
64  ftmprun = iEvent.id().run();
65 
66 }
67 
68 
69 
70 void
72 {
73 }
74 
75 //--------------------------------------------------------
76 void
78  const edm::EventSetup& context) {
79 
80  const edm::TimeValue_t fbegintimestamp = lumiSeg.beginTime().value();
81  const std::time_t ftmptime = fbegintimestamp >> 32;
82 
83  if ( countLumi_ == 0 || (resetFitNLumi_ > 0 && countLumi_%resetFitNLumi_ == 0) ) {
84  ftmprun0 = lumiSeg.run();
85  ftmprun = ftmprun0;
87  refBStime[0] = ftmptime;
88  }
89 
90  countLumi_++;
91  //std::cout << "Lumi # " << countLumi_ << std::endl;
92  if ( ftmprun == previousRun_ ) {
93  if ( (previousLumi_ + 1) != int(lumiSeg.luminosityBlock()) )
94  edm::LogWarning("BeamSpotAnalyzer") << "LUMI SECTIONS ARE NOT SORTED!";
95  }
96 }
97 
98 //--------------------------------------------------------
99 void
101  const edm::EventSetup& iSetup) {
102 
103  //LogDebug("BeamSpotAnalyzer") <<
104  std::cout <<
105  "for lumis "<< beginLumiOfBSFit_ << " - " << endLumiOfBSFit_ << std::endl <<
106  "number of selected tracks = " << theBeamFitter->getNTracks() << std::endl;
107  std::cout << "number of selected PVs = " << theBeamFitter->getNPVs() << std::endl;
108  //std::cout << "number of selected PVs per bx: " << std::endl;
109  //theBeamFitter->getNPVsperBX();
110 
111  const edm::TimeValue_t fendtimestamp = lumiSeg.endTime().value();
112  const std::time_t fendtime = fendtimestamp >> 32;
113  refBStime[1] = fendtime;
114 
115  endLumiOfBSFit_ = lumiSeg.luminosityBlock();
117 
118  if ( fitNLumi_ == -1 && resetFitNLumi_ == -1 ) return;
119 
120  if (fitNLumi_ > 0 && countLumi_%fitNLumi_!=0) return;
121 
125 
126  std::pair<int,int> LSRange = theBeamFitter->getFitLSRange();
127 
130  std::cout << "\n RESULTS OF DEFAULT FIT " << std::endl;
131  std::cout << " for runs: " << ftmprun0 << " - " << ftmprun << std::endl;
132  std::cout << " for lumi blocks : " << LSRange.first << " - " << LSRange.second << std::endl;
133  std::cout << " lumi counter # " << countLumi_ << std::endl;
134  std::cout << bs << std::endl;
135  std::cout << "[BeamFitter] fit done. \n" << std::endl;
136  }
137  else { // Fill in empty beam spot if beamfit fails
138  reco::BeamSpot bs;
140  std::cout << "\n Empty Beam spot fit" << std::endl;
141  std::cout << " for runs: " << ftmprun0 << " - " << ftmprun << std::endl;
142  std::cout << " for lumi blocks : " << LSRange.first << " - " << LSRange.second << std::endl;
143  std::cout << " lumi counter # " << countLumi_ << std::endl;
144  std::cout << bs << std::endl;
145  std::cout << "[BeamFitter] fit failed \n" << std::endl;
146  //accumulate more events
147  // dissable this for the moment
148  //resetFitNLumi_ += 1;
149  //std::cout << "reset fitNLumi " << resetFitNLumi_ << std::endl;
150  }
151 
152  if (resetFitNLumi_ > 0 && countLumi_%resetFitNLumi_ == 0) {
153  std::vector<BSTrkParameters> theBSvector = theBeamFitter->getBSvector();
154  std::cout << "Total number of tracks accumulated = " << theBSvector.size() << std::endl;
155  std::cout << "Reset track collection for beam fit" <<std::endl;
161  countLumi_=0;
162  // reset counter to orginal
164  }
165 
166 }
167 
168 
169 void
171  std::cout << "\n-------------------------------------\n" << std::endl;
172  std::cout << "\n Total number of events processed: "<< ftotalevents << std::endl;
173  std::cout << "\n-------------------------------------\n\n" << std::endl;
174 
175  if ( fitNLumi_ == -1 && resetFitNLumi_ == -1 ) {
176 
178  reco::BeamSpot beam_default = theBeamFitter->getBeamSpot();
179  std::pair<int,int> LSRange = theBeamFitter->getFitLSRange();
180 
181  std::cout << "\n RESULTS OF DEFAULT FIT:" << std::endl;
182  std::cout << " for runs: " << ftmprun0 << " - " << ftmprun << std::endl;
183  std::cout << " for lumi blocks : " << LSRange.first << " - " << LSRange.second << std::endl;
184  std::cout << " lumi counter # " << countLumi_ << std::endl;
185  std::cout << beam_default << std::endl;
186 
187  if (write2DB_) {
188  std::cout << "\n-------------------------------------\n\n" << std::endl;
189  std::cout << " write results to DB..." << std::endl;
191  }
192 
193  if (runallfitters_) {
195  }
196 
197  }
198  if ((runbeamwidthfit_)){
200  reco::BeamSpot beam_width = theBeamFitter->getBeamWidth();
201  std::cout <<beam_width<< std::endl;
202  }
203 
204  else std::cout << "[BeamSpotAnalyzer] beamfit fails !!!" << std::endl;
205  }
206 
207  std::cout << "[BeamSpotAnalyzer] endJob done \n" << std::endl;
208 }
209 
210 //define this as a plug-in
int getNPVs()
Definition: BeamFitter.h:124
RunNumber_t run() const
Definition: EventID.h:42
T getParameter(std::string const &) const
virtual void beginLuminosityBlock(const edm::LuminosityBlock &lumiSeg, const edm::EventSetup &context)
int getNTracks()
Definition: BeamFitter.h:121
bool runPVandTrkFitter()
Definition: BeamFitter.cc:409
void write2DB()
Definition: BeamFitter.cc:739
void setRun(int run)
Definition: BeamFitter.h:119
std::vector< BSTrkParameters > getBSvector()
Definition: BeamFitter.h:91
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
bool runBeamWidthFitter()
Definition: BeamFitter.cc:590
void resetTrkVector()
Definition: BeamFitter.h:48
Timestamp const & beginTime() const
void readEvent(const edm::Event &iEvent)
Definition: BeamFitter.cc:223
reco::BeamSpot getBeamWidth()
Definition: BeamFitter.h:46
void setType(BeamType type)
set beam type
Definition: BeamSpot.h:132
virtual void analyze(const edm::Event &, const edm::EventSetup &)
LuminosityBlockNumber_t luminosityBlock() const
virtual void beginJob()
int iEvent
Definition: GenABIO.cc:243
Timestamp const & endTime() const
std::time_t refBStime[2]
virtual void endLuminosityBlock(const edm::LuminosityBlock &lumiSeg, const edm::EventSetup &c)
RunNumber_t run() const
void setFitLSRange(int ls0, int ls1)
Definition: BeamFitter.h:115
void resetLSRange()
Definition: BeamFitter.h:50
unsigned long long TimeValue_t
Definition: Timestamp.h:27
TimeValue_t value() const
Definition: Timestamp.cc:72
virtual void endJob()
void runAllFitter()
Definition: BeamFitter.cc:779
BeamFitter * theBeamFitter
edm::EventID id() const
Definition: EventBase.h:56
void resetPVFitter()
Definition: BeamFitter.h:64
BeamSpotAnalyzer(const edm::ParameterSet &)
void resetCutFlow()
Definition: BeamFitter.h:100
tuple cout
Definition: gather_cfg.py:121
void setRefTime(time_t t0, time_t t1)
Definition: BeamFitter.h:52
std::pair< int, int > getFitLSRange()
Definition: BeamFitter.h:112
reco::BeamSpot getBeamSpot()
Definition: BeamFitter.h:89
void resetRefTime()
Definition: BeamFitter.h:51