CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
AlcaBeamSpotManager.cc
Go to the documentation of this file.
1 
11 #include <vector>
12 #include <math.h>
13 #include <limits.h>
14 
15 using namespace edm;
16 using namespace reco;
17 using namespace std;
18 
19 //--------------------------------------------------------------------------------------------------
21 }
22 
23 //--------------------------------------------------------------------------------------------------
25  beamSpotOutputBase_(iConfig.getParameter<ParameterSet>("AlcaBeamSpotHarvesterParameters").getUntrackedParameter<std::string>("BeamSpotOutputBase")),
26  beamSpotModuleName_(iConfig.getParameter<ParameterSet>("AlcaBeamSpotHarvesterParameters").getUntrackedParameter<std::string>("BeamSpotModuleName")),
27  beamSpotLabel_ (iConfig.getParameter<ParameterSet>("AlcaBeamSpotHarvesterParameters").getUntrackedParameter<std::string>("BeamSpotLabel"))
28 {
31  LogInfo("AlcaBeamSpotManager")
32  << "Output base: " << beamSpotOutputBase_
33  << std::endl;
34  reset();
35 }
36 
37 //--------------------------------------------------------------------------------------------------
39 }
40 
41 //--------------------------------------------------------------------------------------------------
43  beamSpotMap_.clear();
44 }
45 //--------------------------------------------------------------------------------------------------
47 
48  Handle<BeamSpot> beamSpotHandle;
49  iLumi.getByToken(beamSpotToken_, beamSpotHandle);
50 
51  if(beamSpotHandle.isValid()) { // check the product
52  beamSpotMap_[iLumi.luminosityBlock()] = *beamSpotHandle;
53  const BeamSpot* aBeamSpot = &beamSpotMap_[iLumi.luminosityBlock()];
54  aBeamSpot = beamSpotHandle.product();
55  LogInfo("AlcaBeamSpotManager")
56  << "Lumi: " << iLumi.luminosityBlock() << std::endl;
57  LogInfo("AlcaBeamSpotManager")
58  << *aBeamSpot << std::endl;
59  }
60  else {
61  LogInfo("AlcaBeamSpotManager")
62  << "Lumi: " << iLumi.luminosityBlock() << std::endl;
63  LogInfo("AlcaBeamSpotManager")
64  << " BS is not valid!" << std::endl;
65  }
66 
67 }
68 
69 //--------------------------------------------------------------------------------------------------
71  vector<bsMap_iterator> listToErase;
72  for(bsMap_iterator it=beamSpotMap_.begin(); it!=beamSpotMap_.end();it++){
73  if(it->second.type() != BeamSpot::Tracker){
74  listToErase.push_back(it);
75  }
76  }
77  for(vector<bsMap_iterator>::iterator it=listToErase.begin(); it !=listToErase.end(); it++){
78  beamSpotMap_.erase(*it);
79  }
80  if(beamSpotMap_.size() <= 1){
81  return;
82  }
83  //Return only if lumibased since the collapsing alghorithm requires the next and next to next lumi sections
84  else if(beamSpotMap_.size() == 2 && beamSpotOutputBase_ == "lumibased"){
85  return;
86  }
87  if(beamSpotOutputBase_ == "lumibased"){
88 // bsMap_iterator referenceBS = beamSpotMap_.begin();
89  bsMap_iterator firstBS = beamSpotMap_.begin();
90 // bsMap_iterator lastBS = beamSpotMap_.begin();
91  bsMap_iterator currentBS = beamSpotMap_.begin();
92  bsMap_iterator nextBS = ++beamSpotMap_.begin();
93  bsMap_iterator nextNextBS = ++(++(beamSpotMap_.begin()));
94 
95  map<LuminosityBlockNumber_t,BeamSpot> tmpBeamSpotMap_;
96  bool docreate = true;
97  bool endOfRun = false;//Added
98  bool docheck = true;//Added
99  bool foundShift = false;
100  long countlumi = 0;//Added
101  string tmprun = "";//Added
102  long maxNlumis = 60;//Added
103 // if weighted:
104 // maxNlumis = 999999999
105 
106 
107  unsigned int iteration = 0;
108  //while(nextNextBS!=beamSpotMap_.end()){
109  while(nextBS!=beamSpotMap_.end()){
110  LogInfo("AlcaBeamSpotManager")
111  << "Iteration: " << iteration << " size: " << beamSpotMap_.size() << "\n"
112  << "Lumi: " << currentBS->first << "\n"
113  << currentBS->second
114  << "\n" << nextBS->first << "\n" << nextBS->second
115  << endl;
116  if (nextNextBS!=beamSpotMap_.end())
117  LogInfo("AlcaBeamSpotManager")
118  << nextNextBS->first << "\n" << nextNextBS->second
119  << endl;
120 
121 
122  if(docreate){
123  firstBS = currentBS;
124  docreate = false;//Added
125  }
126  //if(iteration >= beamSpotMap_.size()-3){
127  if(iteration >= beamSpotMap_.size()-2){
128  LogInfo("AlcaBeamSpotManager")
129  << "Reached lumi " << currentBS->first
130  << " now close payload because end of data has been reached.";
131  docreate = true;
132  endOfRun = true;
133  }
134  // check we run over the same run
135 // if (ibeam->first.Run() != inextbeam->first.Run()){
136 // LogInfo("AlcaBeamSpotManager")
137 // << "close payload because end of run.";
138 // docreate = true;
139 // }
140  // check maximum lumi counts
141  if (countlumi == maxNlumis -1){
142  LogInfo("AlcaBeamSpotManager")
143  << "close payload because maximum lumi sections accumulated within run ";
144  docreate = true;
145  countlumi = 0;
146  }
147 // if weighted:
148 // docheck = False
149  // check offsets
150  if(docheck){
151  foundShift = false;
152  LogInfo("AlcaBeamSpotManager")
153  << "Checking checking!" << endl;
154  float limit = 0;
155  pair<float,float> adelta1;
156  pair<float,float> adelta2;
157  pair<float,float> adelta;
158  pair<float,float> adelta1dxdz;
159  pair<float,float> adelta2dxdz;
160  pair<float,float> adelta1dydz;
161  pair<float,float> adelta2dydz;
162  pair<float,float> adelta1widthX;
163  pair<float,float> adelta2widthX;
164  pair<float,float> adelta1widthY;
165  pair<float,float> adelta2widthY;
166  pair<float,float> adelta1z0;
167  pair<float,float> adelta1sigmaZ;
168 
169  // define minimum limit
170  float min_limit = 0.0025;
171 
172  // limit for x and y
173  limit = currentBS->second.BeamWidthX()/2.;
174  if(limit < min_limit){
175  limit = min_limit;
176  }
177 
178  //check movements in X
179  adelta1 = delta(currentBS->second.x0(), currentBS->second.x0Error(), nextBS->second.x0(), nextBS->second.x0Error());
180  adelta2 = pair<float,float>(0.,1.e9);
181  if (nextNextBS->second.type() != -1){
182  adelta2 = delta(nextBS->second.x0(), nextBS->second.x0Error(), nextNextBS->second.x0(), nextNextBS->second.x0Error());
183  }
184  bool deltaX = (deltaSig(adelta1.first,adelta1.second) > 3.5 && adelta1.first >= limit)?true:false;
185  if(iteration < beamSpotMap_.size()-2){
186  if( !deltaX && adelta1.first*adelta2.first > 0. && fabs(adelta1.first+adelta2.first) >= limit){
187  LogInfo("AlcaBeamSpotManager")
188  << " positive, " << (adelta1.first+adelta2.first) << " limit=" << limit << endl;
189  deltaX = true;
190  }
191  else if( deltaX && adelta1.first*adelta2.first < 0 && adelta2.first != 0 && fabs(adelta1.first/adelta2.first) > 0.33 && fabs(adelta1.first/adelta2.first) < 3){
192  LogInfo("AlcaBeamSpotManager")
193  << " negative, " << adelta1.first/adelta2.first << endl;
194  deltaX = false;
195  }
196  }
197 
198  //calculating all deltas
199  adelta1dxdz = delta(currentBS->second.dxdz(), currentBS->second.dxdzError(), nextBS->second.dxdz(), nextBS->second.dxdzError());
200  adelta2dxdz = pair<float,float>(0.,1.e9);
201  adelta1dydz = delta(currentBS->second.dydz(), currentBS->second.dydzError(), nextBS->second.dydz(), nextBS->second.dydzError());
202  adelta2dydz = pair<float,float>(0.,1.e9);
203  adelta1widthX = delta(currentBS->second.BeamWidthX(), currentBS->second.BeamWidthXError(), nextBS->second.BeamWidthX(), nextBS->second.BeamWidthXError());
204  adelta2widthX = pair<float,float>(0.,1.e9);
205  adelta1widthY = delta(currentBS->second.BeamWidthY(), currentBS->second.BeamWidthYError(), nextBS->second.BeamWidthY(), nextBS->second.BeamWidthYError());
206  adelta2widthY = pair<float,float>(0.,1.e9);
207  adelta1z0 = delta(currentBS->second.z0(), currentBS->second.z0Error(), nextBS->second.z0(), nextBS->second.z0Error());
208  adelta1sigmaZ = delta(currentBS->second.sigmaZ(), currentBS->second.sigmaZ0Error(), nextBS->second.sigmaZ(), nextBS->second.sigmaZ0Error());
209 
210  //check movements in Y
211  adelta1 = delta(currentBS->second.y0(), currentBS->second.y0Error(), nextBS->second.y0(), nextBS->second.y0Error());
212  adelta2 = pair<float,float>(0.,1.e9);
213  if( nextNextBS->second.type() != BeamSpot::Unknown){
214  adelta2 = delta(nextBS->second.y0(), nextBS->second.y0Error(), nextNextBS->second.y0(), nextNextBS->second.y0Error());
215  adelta2dxdz = delta(nextBS->second.dxdz(), nextBS->second.dxdzError(), nextNextBS->second.dxdz(), nextNextBS->second.dxdzError());
216  adelta2dydz = delta(nextBS->second.dydz(), nextBS->second.dydzError(), nextNextBS->second.dydz(), nextNextBS->second.dydzError());
217  adelta2widthX = delta(nextBS->second.BeamWidthX(), nextBS->second.BeamWidthXError(), nextNextBS->second.BeamWidthX(), nextNextBS->second.BeamWidthXError());
218  adelta2widthY = delta(nextBS->second.BeamWidthY(), nextBS->second.BeamWidthYError(), nextNextBS->second.BeamWidthY(), nextNextBS->second.BeamWidthYError());
219 
220  }
221  bool deltaY = (deltaSig(adelta1.first,adelta1.second) > 3.5 && adelta1.first >= limit)?true:false;
222  if(iteration < beamSpotMap_.size()-2){
223  if( !deltaY && adelta1.first*adelta2.first > 0. && fabs(adelta1.first+adelta2.first) >= limit){
224  LogInfo("AlcaBeamSpotManager")
225  << " positive, " << (adelta1.first+adelta2.first) << " limit=" << limit << endl;
226  deltaY = true;
227  }
228  else if( deltaY && adelta1.first*adelta2.first < 0 && adelta2.first != 0 && fabs(adelta1.first/adelta2.first) > 0.33 && fabs(adelta1.first/adelta2.first) < 3){
229  LogInfo("AlcaBeamSpotManager")
230  << " negative, " << adelta1.first/adelta2.first << endl;
231  deltaY = false;
232  }
233  }
234 
235  limit = currentBS->second.sigmaZ()/2.;
236  bool deltaZ = (deltaSig(adelta1z0.first,adelta1z0.second) > 3.5 && fabs(adelta1z0.first) >= limit)?true:false;
237  adelta = delta(currentBS->second.sigmaZ(), currentBS->second.sigmaZ0Error(), nextBS->second.sigmaZ(), nextBS->second.sigmaZ0Error());
238  bool deltasigmaZ = (deltaSig(adelta.first,adelta.second) > 5.0)?true:false;
239  bool deltadxdz = false;
240  bool deltadydz = false;
241  bool deltawidthX = false;
242  bool deltawidthY = false;
243 
244  if(iteration < beamSpotMap_.size()-2){
245 
246  adelta = delta(currentBS->second.dxdz(), currentBS->second.dxdzError(), nextBS->second.dxdz(), nextBS->second.dxdzError());
247  deltadxdz = (deltaSig(adelta.first,adelta.second) > 5.0)?true:false;
248  if(deltadxdz && (adelta1dxdz.first*adelta2dxdz.first) < 0 && adelta2dxdz.first != 0 && fabs(adelta1dxdz.first/adelta2dxdz.first) > 0.33 && fabs(adelta1dxdz.first/adelta2dxdz.first) < 3){
249  deltadxdz = false;
250  }
251 
252  adelta = delta(currentBS->second.dydz(), currentBS->second.dydzError(), nextBS->second.dydz(), nextBS->second.dydzError());
253  deltadydz = (deltaSig(adelta.first,adelta.second) > 5.0)?true:false;
254  if(deltadydz && (adelta1dydz.first*adelta2dydz.first) < 0 && adelta2dydz.first != 0 && fabs(adelta1dydz.first/adelta2dydz.first) > 0.33 && fabs(adelta1dydz.first/adelta2dydz.first) < 3){
255  deltadydz = false;
256  }
257 
258  adelta = delta(currentBS->second.BeamWidthX(), currentBS->second.BeamWidthXError(), nextBS->second.BeamWidthX(), nextBS->second.BeamWidthXError());
259  deltawidthX = (deltaSig(adelta.first,adelta.second) > 5.0)?true:false;
260  if(deltawidthX && (adelta1widthX.first*adelta2widthX.first) < 0 && adelta2widthX.first != 0 && fabs(adelta1widthX.first/adelta2widthX.first) > 0.33 && fabs(adelta1widthX.first/adelta2widthX.first) < 3){
261  deltawidthX = false;
262  }
263 
264  adelta = delta(currentBS->second.BeamWidthY(), currentBS->second.BeamWidthYError(), nextBS->second.BeamWidthY(), nextBS->second.BeamWidthYError());
265  deltawidthY = (deltaSig(adelta.first,adelta.second) > 5.0)?true:false;
266  if(deltawidthY && (adelta1widthY.first*adelta2widthY.first) < 0 && adelta2widthY.first != 0 && fabs(adelta1widthY.first/adelta2widthY.first) > 0.33 && fabs(adelta1widthY.first/adelta2widthY.first) < 3){
267  deltawidthY = false;
268  }
269 
270  }
271  if (deltaX || deltaY || deltaZ || deltasigmaZ || deltadxdz || deltadydz || deltawidthX || deltawidthY){
272  docreate = true;
273  foundShift = true;
274  LogInfo("AlcaBeamSpotManager")
275  << "close payload because of movement in"
276  << " X=" << deltaX
277  << ", Y=" << deltaY
278  << ", Z=" << deltaZ
279  << ", sigmaZ=" << deltasigmaZ
280  << ", dxdz=" << deltadxdz
281  << ", dydz=" << deltadydz
282  << ", widthX=" << deltawidthX
283  << ", widthY=" << deltawidthY
284  << endl;
285  }
286 
287  }
288  if(docreate){
289  if(foundShift){
290  tmpBeamSpotMap_[firstBS->first] = weight(firstBS,nextBS);
291  if (endOfRun){
292  //if we're here, then we need to found a shift in the last LS
293  //We already created a new IOV, now create one just for the last LS
294  tmpBeamSpotMap_[nextBS->first] = nextBS->second;
295  }
296  }
297  else if(!foundShift && !endOfRun){ //maxLS reached
298  tmpBeamSpotMap_[firstBS->first] = weight(firstBS,nextBS);
299  }
300  else { // end of run with no shift detectred in last LS
301  tmpBeamSpotMap_[firstBS->first] = weight(firstBS,beamSpotMap_.end());
302  }
303  firstBS = nextBS;
304  countlumi = 0;
305 
306  }
307  //tmprun = currentBS->second.Run
308  ++countlumi;
309 
310  currentBS = nextBS;
311  nextBS = nextNextBS;
312  nextNextBS++;
313  ++iteration;
314  }
315  beamSpotMap_.clear();
316  beamSpotMap_ = tmpBeamSpotMap_;
317  }
318  else if(beamSpotOutputBase_ == "runbased"){
319  BeamSpot aBeamSpot = weight(beamSpotMap_.begin(),beamSpotMap_.end());
320  LuminosityBlockNumber_t firstLumi = beamSpotMap_.begin()->first;
321  beamSpotMap_.clear();
322  beamSpotMap_[firstLumi] = aBeamSpot;
323  }
324  else{
325  LogInfo("AlcaBeamSpotManager")
326  << "Unrecognized BeamSpotOutputBase parameter: " << beamSpotOutputBase_
327  << endl;
328  }
329 }
330 
331 //--------------------------------------------------------------------------------------------------
333  const bsMap_iterator& end){
334  double x,xError = 0;
335  double y,yError = 0;
336  double z,zError = 0;
337  double sigmaZ,sigmaZError = 0;
338  double dxdz,dxdzError = 0;
339  double dydz,dydzError = 0;
340  double widthX,widthXError = 0;
341  double widthY,widthYError = 0;
342  LogInfo("AlcaBeamSpotManager")
343  << "Weighted BeamSpot will span lumi "
344  << begin->first << " to " << end->first
345  << endl;
346 
348  for(bsMap_iterator it=begin; it!=end; it++){
349  weight(x , xError , it->second.x0() , it->second.x0Error());
350  weight(y , yError , it->second.y0() , it->second.y0Error());
351  weight(z , zError , it->second.z0() , it->second.z0Error());
352  weight(sigmaZ, sigmaZError, it->second.sigmaZ() , it->second.sigmaZ0Error());
353  weight(dxdz , dxdzError , it->second.dxdz() , it->second.dxdzError());
354  weight(dydz , dydzError , it->second.dydz() , it->second.dydzError());
355  weight(widthX, widthXError, it->second.BeamWidthX(), it->second.BeamWidthXError());
356  weight(widthY, widthYError, it->second.BeamWidthY(), it->second.BeamWidthYError());
357  if(it->second.type() == BeamSpot::Tracker){
358  type = BeamSpot::Tracker;
359  }
360  }
361  BeamSpot::Point bsPosition(x,y,z);
363  error(0,0) = xError*xError;
364  error(1,1) = yError*yError;
365  error(2,2) = zError*zError;
366  error(3,3) = sigmaZError*sigmaZError;
367  error(4,4) = dxdzError*dxdzError;
368  error(5,5) = dydzError*dydzError;
369  error(6,6) = widthXError*widthXError;
370  BeamSpot weightedBeamSpot(bsPosition,sigmaZ,dxdz,dydz,widthX,error,type);
371  weightedBeamSpot.setBeamWidthY(widthY);
372  LogInfo("AlcaBeamSpotManager")
373  << "Weighted BeamSpot will be:" <<'\n'
374  << weightedBeamSpot
375  << endl;
376  return weightedBeamSpot;
377 }
378 
379 //--------------------------------------------------------------------------------------------------
380 void AlcaBeamSpotManager::weight(double& mean,double& meanError,const double& val,const double& valError){
381  double tmpError = 0;
382  if (meanError < 1e-8){
383  tmpError = 1/(valError*valError);
384  mean = val*tmpError;
385  }
386  else{
387  tmpError = 1/(meanError*meanError) + 1/(valError*valError);
388  mean = mean/(meanError*meanError) + val/(valError*valError);
389  }
390  mean = mean/tmpError;
391  meanError = sqrt(1/tmpError);
392 }
393 
394 //--------------------------------------------------------------------------------------------------
395 pair<float,float> AlcaBeamSpotManager::delta(const float& x, const float& xError, const float& nextX, const float& nextXError){
396  return pair<float,float>(x - nextX, sqrt(pow(xError,2) + pow(nextXError,2)) );
397 }
398 
399 //--------------------------------------------------------------------------------------------------
400 float AlcaBeamSpotManager::deltaSig(const float& num, const float& den){
401  if(den != 0){
402  return fabs(num/den);
403  }
404  else{
405  return float(LONG_MAX);
406  }
407 }
408 
type
Definition: HCALResponse.h:21
math::Error< dimension >::type CovarianceMatrix
Definition: BeamSpot.h:31
BeamType
beam spot flags
Definition: BeamSpot.h:26
std::string beamSpotOutputBase_
edm::InputTag beamSpotTag_
edm::EDGetTokenT< reco::BeamSpot > beamSpotToken_
bool getByToken(EDGetToken token, Handle< PROD > &result) const
std::map< edm::LuminosityBlockNumber_t, reco::BeamSpot >::iterator bsMap_iterator
math::XYZPoint Point
point in the space
Definition: BeamSpot.h:29
unsigned int LuminosityBlockNumber_t
Definition: EventID.h:31
float float float z
std::string beamSpotModuleName_
LuminosityBlockNumber_t luminosityBlock() const
tuple iteration
Definition: align_cfg.py:5
void setBeamWidthY(double v)
Definition: BeamSpot.h:109
T sqrt(T t)
Definition: SSEVec.h:48
reco::BeamSpot weight(const bsMap_iterator &begin, const bsMap_iterator &end)
virtual ~AlcaBeamSpotManager(void)
#define end
Definition: vmac.h:37
bool isValid() const
Definition: HandleBase.h:76
tuple tmprun
Definition: ntuplemaker.py:242
void readLumi(const edm::LuminosityBlock &)
T const * product() const
Definition: Handle.h:81
#define begin
Definition: vmac.h:30
Definition: DDAxes.h:10
std::pair< float, float > delta(const float &x, const float &xError, const float &nextX, const float &nextXError)
Power< A, B >::type pow(const A &a, const B &b)
Definition: Power.h:40
std::map< edm::LuminosityBlockNumber_t, reco::BeamSpot > beamSpotMap_
float deltaSig(const float &num, const float &den)