CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
Vx3DHLTAnalyzer.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: Vx3DHLTAnalyzer
4 // Class: Vx3DHLTAnalyzer
5 //
13 //
14 // Original Author: Mauro Dinardo,28 S-020,+41227673777,
15 // Created: Tue Feb 23 13:15:31 CET 2010
16 // $Id: Vx3DHLTAnalyzer.cc,v 1.99 2010/10/11 20:38:12 rovere Exp $
17 
18 
20 
23 
29 
30 #include <TFitterMinuit.h>
31 
32 
33 using namespace std;
34 using namespace reco;
35 using namespace edm;
36 
37 
39 {
40  vertexCollection = edm::InputTag("pixelVertices");
41  debugMode = false;
42  nLumiReset = 1;
43  dataFromFit = true;
44  minNentries = 35;
45  xRange = 2.;
46  xStep = 0.001;
47  yRange = 2.;
48  yStep = 0.001;
49  zRange = 30.;
50  zStep = 0.05;
51  VxErrCorr = 1.58;
52  fileName = "BeamPixelResults.txt";
53 
54  vertexCollection = iConfig.getParameter<InputTag>("vertexCollection");
55  debugMode = iConfig.getParameter<bool>("debugMode");
56  nLumiReset = iConfig.getParameter<unsigned int>("nLumiReset");
57  dataFromFit = iConfig.getParameter<bool>("dataFromFit");
58  minNentries = iConfig.getParameter<unsigned int>("minNentries");
59  xRange = iConfig.getParameter<double>("xRange");
60  xStep = iConfig.getParameter<double>("xStep");
61  yRange = iConfig.getParameter<double>("yRange");
62  yStep = iConfig.getParameter<double>("yStep");
63  zRange = iConfig.getParameter<double>("zRange");
64  zStep = iConfig.getParameter<double>("zStep");
65  VxErrCorr = iConfig.getParameter<double>("VxErrCorr");
66  fileName = iConfig.getParameter<string>("fileName");
67 }
68 
69 
71 {
72 }
73 
74 
75 void Vx3DHLTAnalyzer::analyze(const Event& iEvent, const EventSetup& iSetup)
76 {
77  Handle<VertexCollection> Vx3DCollection;
78  iEvent.getByLabel(vertexCollection,Vx3DCollection);
79 
80  unsigned int i,j;
81  double det;
82  VertexType MyVertex;
83 
84  if (runNumber != iEvent.id().run())
85  {
86  reset("scratch");
87  runNumber = iEvent.id().run();
88 
89  if (debugMode == true)
90  {
91  stringstream debugFile;
92  string tmp(fileName);
93 
94  if (outputDebugFile.is_open() == true) outputDebugFile.close();
95  tmp.erase(strlen(fileName.c_str())-4,4);
96  debugFile << tmp.c_str() << "_Run" << iEvent.id().run() << ".txt";
97  outputDebugFile.open(debugFile.str().c_str(), ios::out);
98  outputDebugFile.close();
99  outputDebugFile.open(debugFile.str().c_str(), ios::app);
100  }
101  }
102  else if (beginTimeOfFit != 0)
103  {
104  totalHits += HitCounter(iEvent);
105 
106  for (vector<Vertex>::const_iterator it3DVx = Vx3DCollection->begin(); it3DVx != Vx3DCollection->end(); it3DVx++) {
107 
108  if ((it3DVx->isValid() == true) &&
109  (it3DVx->isFake() == false) &&
110  (it3DVx->ndof() >= minVxDoF) &&
111  (it3DVx->tracksSize() != 0))
112  {
113  for (i = 0; i < DIM; i++)
114  {
115  for (j = 0; j < DIM; j++)
116  {
117  MyVertex.Covariance[i][j] = it3DVx->covariance(i,j);
118  if (std::isnan(MyVertex.Covariance[i][j]) == true) break;
119  }
120  if (j != DIM) break;
121  }
122  det = std::fabs(MyVertex.Covariance[0][0])*(std::fabs(MyVertex.Covariance[1][1])*std::fabs(MyVertex.Covariance[2][2]) - MyVertex.Covariance[1][2]*MyVertex.Covariance[1][2]) -
123  MyVertex.Covariance[0][1]*(MyVertex.Covariance[0][1]*std::fabs(MyVertex.Covariance[2][2]) - MyVertex.Covariance[0][2]*MyVertex.Covariance[1][2]) +
124  MyVertex.Covariance[0][2]*(MyVertex.Covariance[0][1]*MyVertex.Covariance[1][2] - MyVertex.Covariance[0][2]*std::fabs(MyVertex.Covariance[1][1]));
125  if ((i == DIM) && (det > 0.))
126  {
127  MyVertex.x = it3DVx->x();
128  MyVertex.y = it3DVx->y();
129  MyVertex.z = it3DVx->z();
130  Vertices.push_back(MyVertex);
131  }
132  else if (internalDebug == true)
133  {
134  cout << "Vertex discarded !" << endl;
135  for (i = 0; i < DIM; i++)
136  for (j = 0; j < DIM; j++)
137  cout << "(i,j) --> " << i << "," << j << " --> " << MyVertex.Covariance[i][j] << endl;
138  }
139 
140  Vx_X->Fill(it3DVx->x());
141  Vx_Y->Fill(it3DVx->y());
142  Vx_Z->Fill(it3DVx->z());
143 
144  Vx_ZX->Fill(it3DVx->z(), it3DVx->x());
145  Vx_ZY->Fill(it3DVx->z(), it3DVx->y());
146  Vx_XY->Fill(it3DVx->x(), it3DVx->y());
147  }
148  }
149  }
150 }
151 
152 
154 {
156  iEvent.getByLabel("siPixelRecHits",rechitspixel);
157 
158  unsigned int counter = 0;
159 
160  for (SiPixelRecHitCollection::const_iterator j = rechitspixel->begin(); j != rechitspixel->end(); j++)
161  for (edmNew::DetSet<SiPixelRecHit>::const_iterator h = j->begin(); h != j->end(); h++) counter += h->cluster()->size();
162 
163  return counter;
164 }
165 
166 
167 char* Vx3DHLTAnalyzer::formatTime (const time_t& t)
168 {
169  static char ts[25];
170  strftime(ts, sizeof(ts), "%Y.%m.%d %H:%M:%S %Z", gmtime(&t));
171 
172  return ts;
173 }
174 
175 
176 void Gauss3DFunc(int& /*npar*/, double* /*gin*/, double& fval, double* par, int /*iflag*/)
177 {
178  double K[DIM][DIM]; // Covariance Matrix
179  double M[DIM][DIM]; // K^-1
180  double det;
181  double sumlog = 0.;
182 
183 // par[0] = K(0,0) --> Var[X]
184 // par[1] = K(1,1) --> Var[Y]
185 // par[2] = K(2,2) --> Var[Z]
186 // par[3] = K(0,1) = K(1,0) --> Cov[X,Y]
187 // par[4] = K(1,2) = K(2,1) --> Cov[Y,Z] --> dy/dz
188 // par[5] = K(0,2) = K(2,0) --> Cov[X,Z] --> dx/dz
189 // par[6] = mean x
190 // par[7] = mean y
191 // par[8] = mean z
192 
193  counterVx = 0;
194  for (unsigned int i = 0; i < Vertices.size(); i++)
195  {
197  (std::fabs(Vertices[i].z-zPos) <= maxLongLength))
198  {
199  if (considerVxCovariance == true)
200  {
201  K[0][0] = std::fabs(par[0]) + VxErrCorr*VxErrCorr * std::fabs(Vertices[i].Covariance[0][0]);
202  K[1][1] = std::fabs(par[1]) + VxErrCorr*VxErrCorr * std::fabs(Vertices[i].Covariance[1][1]);
203  K[2][2] = std::fabs(par[2]) + VxErrCorr*VxErrCorr * std::fabs(Vertices[i].Covariance[2][2]);
204  K[0][1] = K[1][0] = par[3] + VxErrCorr*VxErrCorr * Vertices[i].Covariance[0][1];
205  K[1][2] = K[2][1] = par[4]*(std::fabs(par[2])-std::fabs(par[1])) - par[5]*par[3] + VxErrCorr*VxErrCorr * Vertices[i].Covariance[1][2];
206  K[0][2] = K[2][0] = par[5]*(std::fabs(par[2])-std::fabs(par[0])) - par[4]*par[3] + VxErrCorr*VxErrCorr * Vertices[i].Covariance[0][2];
207  }
208  else
209  {
210  K[0][0] = std::fabs(par[0]);
211  K[1][1] = std::fabs(par[1]);
212  K[2][2] = std::fabs(par[2]);
213  K[0][1] = K[1][0] = par[3];
214  K[1][2] = K[2][1] = par[4]*(std::fabs(par[2])-std::fabs(par[1])) - par[5]*par[3];
215  K[0][2] = K[2][0] = par[5]*(std::fabs(par[2])-std::fabs(par[0])) - par[4]*par[3];
216  }
217 
218  det = K[0][0]*(K[1][1]*K[2][2] - K[1][2]*K[1][2]) -
219  K[0][1]*(K[0][1]*K[2][2] - K[0][2]*K[1][2]) +
220  K[0][2]*(K[0][1]*K[1][2] - K[0][2]*K[1][1]);
221 
222  M[0][0] = (K[1][1]*K[2][2] - K[1][2]*K[1][2]) / det;
223  M[1][1] = (K[0][0]*K[2][2] - K[0][2]*K[0][2]) / det;
224  M[2][2] = (K[0][0]*K[1][1] - K[0][1]*K[0][1]) / det;
225  M[0][1] = M[1][0] = (K[0][2]*K[1][2] - K[0][1]*K[2][2]) / det;
226  M[1][2] = M[2][1] = (K[0][2]*K[0][1] - K[1][2]*K[0][0]) / det;
227  M[0][2] = M[2][0] = (K[0][1]*K[1][2] - K[0][2]*K[1][1]) / det;
228 
229  sumlog += double(DIM)*std::log(2.*pi) + std::log(std::fabs(det)) +
230  (M[0][0]*(Vertices[i].x-par[6])*(Vertices[i].x-par[6]) +
231  M[1][1]*(Vertices[i].y-par[7])*(Vertices[i].y-par[7]) +
232  M[2][2]*(Vertices[i].z-par[8])*(Vertices[i].z-par[8]) +
233  2.*M[0][1]*(Vertices[i].x-par[6])*(Vertices[i].y-par[7]) +
234  2.*M[1][2]*(Vertices[i].y-par[7])*(Vertices[i].z-par[8]) +
235  2.*M[0][2]*(Vertices[i].x-par[6])*(Vertices[i].z-par[8]));
236 
237  counterVx++;
238  }
239  }
240 
241  fval = sumlog;
242 }
243 
244 
245 int Vx3DHLTAnalyzer::MyFit(vector<double>* vals)
246 {
247  // RETURN CODE:
248  // 0 == OK
249  // -2 == NO OK - not enough "minNentries"
250  // Any other number == NO OK
251  unsigned int nParams = 9;
252 
253  if ((vals != NULL) && (vals->size() == nParams*2))
254  {
255  double nSigmaXY = 100.;
256  double nSigmaZ = 100.;
257  double varFactor = 4./25.; // Take into account the difference between the RMS and sigma (RMS usually greater than sigma)
258  double parDistanceXY = 0.005; // Unit: [cm]
259  double parDistanceZ = 0.5; // Unit: [cm]
260  double parDistanceddZ = 1e-3; // Unit: [rad]
261  double parDistanceCxy = 1e-5; // Unit: [cm^2]
262  double bestEdm = 1e-1;
263 
264  const unsigned int trials = 4;
265  double largerDist[trials] = {0.1, 5., 10., 100.};
266 
267  double covxz,covyz,det;
268  double deltaMean;
269  int bestMovementX = 1;
270  int bestMovementY = 1;
271  int bestMovementZ = 1;
272  int goodData;
273 
274  double arglist[2];
275  double amin,errdef,edm;
276  int nvpar,nparx;
277 
278  vector<double>::const_iterator it = vals->begin();
279 
280  TFitterMinuit* Gauss3D = new TFitterMinuit(nParams);
281  if (internalDebug == true) Gauss3D->SetPrintLevel(3);
282  else Gauss3D->SetPrintLevel(0);
283  Gauss3D->SetFCN(Gauss3DFunc);
284  arglist[0] = 10000; // Max number of function calls
285  arglist[1] = 1e-9; // Tolerance on likelihood
286 
287  if (internalDebug == true) cout << "\n@@@ START FITTING @@@" << endl;
288 
289  // @@@ Fit at X-deltaMean | X | X+deltaMean @@@
290  bestEdm = 1.;
291  for (int i = 0; i < 3; i++)
292  {
293  deltaMean = (double(i)-1.)*std::sqrt((*(it+0))*varFactor);
294  if (internalDebug == true) cout << "deltaMean --> " << deltaMean << endl;
295 
296  Gauss3D->Clear();
297 
298  // arg3 - first guess of parameter value
299  // arg4 - step of the parameter
300  Gauss3D->SetParameter(0,"var x ", *(it+0)*varFactor, parDistanceXY*parDistanceXY, 0., 0.);
301  Gauss3D->SetParameter(1,"var y ", *(it+1)*varFactor, parDistanceXY*parDistanceXY, 0., 0.);
302  Gauss3D->SetParameter(2,"var z ", *(it+2), parDistanceZ*parDistanceZ, 0., 0.);
303  Gauss3D->SetParameter(3,"cov xy", *(it+3), parDistanceCxy, 0., 0.);
304  Gauss3D->SetParameter(4,"dydz ", *(it+4), parDistanceddZ, 0., 0.);
305  Gauss3D->SetParameter(5,"dxdz ", *(it+5), parDistanceddZ, 0., 0.);
306  Gauss3D->SetParameter(6,"mean x", *(it+6)+deltaMean, parDistanceXY, 0., 0.);
307  Gauss3D->SetParameter(7,"mean y", *(it+7), parDistanceXY, 0., 0.);
308  Gauss3D->SetParameter(8,"mean z", *(it+8), parDistanceZ, 0., 0.);
309 
310  // Set the central positions of the centroid for vertex rejection
311  xPos = Gauss3D->GetParameter(6);
312  yPos = Gauss3D->GetParameter(7);
313  zPos = Gauss3D->GetParameter(8);
314 
315  // Set dimensions of the centroid for vertex rejection
316  maxTransRadius = nSigmaXY * std::sqrt(std::fabs(Gauss3D->GetParameter(0)) + std::fabs(Gauss3D->GetParameter(1))) / 2.;
317  maxLongLength = nSigmaZ * std::sqrt(std::fabs(Gauss3D->GetParameter(2)));
318 
319  goodData = Gauss3D->ExecuteCommand("MIGRAD",arglist,2);
320  Gauss3D->GetStats(amin, edm, errdef, nvpar, nparx);
321 
322  if (counterVx < minNentries) goodData = -2;
323  else if (std::isnan(edm) == true) goodData = -1;
324  else for (unsigned int j = 0; j < nParams; j++) if (std::isnan(Gauss3D->GetParError(j)) == true) { goodData = -1; break; }
325  if (goodData == 0)
326  {
327  covyz = Gauss3D->GetParameter(4)*(std::fabs(Gauss3D->GetParameter(2))-std::fabs(Gauss3D->GetParameter(1))) - Gauss3D->GetParameter(5)*Gauss3D->GetParameter(3);
328  covxz = Gauss3D->GetParameter(5)*(std::fabs(Gauss3D->GetParameter(2))-std::fabs(Gauss3D->GetParameter(0))) - Gauss3D->GetParameter(4)*Gauss3D->GetParameter(3);
329 
330  det = std::fabs(Gauss3D->GetParameter(0)) * (std::fabs(Gauss3D->GetParameter(1))*std::fabs(Gauss3D->GetParameter(2)) - covyz*covyz) -
331  Gauss3D->GetParameter(3) * (Gauss3D->GetParameter(3)*std::fabs(Gauss3D->GetParameter(2)) - covxz*covyz) +
332  covxz * (Gauss3D->GetParameter(3)*covyz - covxz*std::fabs(Gauss3D->GetParameter(1)));
333  if (det < 0.) { goodData = -1; if (internalDebug == true) cout << "Negative determinant !" << endl; }
334  }
335 
336  if ((goodData == 0) && (std::fabs(edm) < bestEdm)) { bestEdm = edm; bestMovementX = i; }
337  }
338  if (internalDebug == true) cout << "Found bestMovementX --> " << bestMovementX << endl;
339 
340  // @@@ Fit at Y-deltaMean | Y | Y+deltaMean @@@
341  bestEdm = 1.;
342  for (int i = 0; i < 3; i++)
343  {
344  deltaMean = (double(i)-1.)*std::sqrt((*(it+1))*varFactor);
345  if (internalDebug == true)
346  {
347  cout << "deltaMean --> " << deltaMean << endl;
348  cout << "deltaMean X --> " << (double(bestMovementX)-1.)*std::sqrt((*(it+0))*varFactor) << endl;
349  }
350 
351  Gauss3D->Clear();
352 
353  // arg3 - first guess of parameter value
354  // arg4 - step of the parameter
355  Gauss3D->SetParameter(0,"var x ", *(it+0)*varFactor, parDistanceXY*parDistanceXY, 0., 0.);
356  Gauss3D->SetParameter(1,"var y ", *(it+1)*varFactor, parDistanceXY*parDistanceXY, 0., 0.);
357  Gauss3D->SetParameter(2,"var z ", *(it+2), parDistanceZ*parDistanceZ, 0., 0.);
358  Gauss3D->SetParameter(3,"cov xy", *(it+3), parDistanceCxy, 0., 0.);
359  Gauss3D->SetParameter(4,"dydz ", *(it+4), parDistanceddZ, 0., 0.);
360  Gauss3D->SetParameter(5,"dxdz ", *(it+5), parDistanceddZ, 0., 0.);
361  Gauss3D->SetParameter(6,"mean x", *(it+6)+(double(bestMovementX)-1.)*std::sqrt((*(it+0))*varFactor), parDistanceXY, 0., 0.);
362  Gauss3D->SetParameter(7,"mean y", *(it+7)+deltaMean, parDistanceXY, 0., 0.);
363  Gauss3D->SetParameter(8,"mean z", *(it+8), parDistanceZ, 0., 0.);
364 
365  // Set the central positions of the centroid for vertex rejection
366  xPos = Gauss3D->GetParameter(6);
367  yPos = Gauss3D->GetParameter(7);
368  zPos = Gauss3D->GetParameter(8);
369 
370  // Set dimensions of the centroid for vertex rejection
371  maxTransRadius = nSigmaXY * std::sqrt(std::fabs(Gauss3D->GetParameter(0)) + std::fabs(Gauss3D->GetParameter(1))) / 2.;
372  maxLongLength = nSigmaZ * std::sqrt(std::fabs(Gauss3D->GetParameter(2)));
373 
374  goodData = Gauss3D->ExecuteCommand("MIGRAD",arglist,2);
375  Gauss3D->GetStats(amin, edm, errdef, nvpar, nparx);
376 
377  if (counterVx < minNentries) goodData = -2;
378  else if (std::isnan(edm) == true) goodData = -1;
379  else for (unsigned int j = 0; j < nParams; j++) if (std::isnan(Gauss3D->GetParError(j)) == true) { goodData = -1; break; }
380  if (goodData == 0)
381  {
382  covyz = Gauss3D->GetParameter(4)*(std::fabs(Gauss3D->GetParameter(2))-std::fabs(Gauss3D->GetParameter(1))) - Gauss3D->GetParameter(5)*Gauss3D->GetParameter(3);
383  covxz = Gauss3D->GetParameter(5)*(std::fabs(Gauss3D->GetParameter(2))-std::fabs(Gauss3D->GetParameter(0))) - Gauss3D->GetParameter(4)*Gauss3D->GetParameter(3);
384 
385  det = std::fabs(Gauss3D->GetParameter(0)) * (std::fabs(Gauss3D->GetParameter(1))*std::fabs(Gauss3D->GetParameter(2)) - covyz*covyz) -
386  Gauss3D->GetParameter(3) * (Gauss3D->GetParameter(3)*std::fabs(Gauss3D->GetParameter(2)) - covxz*covyz) +
387  covxz * (Gauss3D->GetParameter(3)*covyz - covxz*std::fabs(Gauss3D->GetParameter(1)));
388  if (det < 0.) { goodData = -1; if (internalDebug == true) cout << "Negative determinant !" << endl; }
389  }
390 
391  if ((goodData == 0) && (std::fabs(edm) < bestEdm)) { bestEdm = edm; bestMovementY = i; }
392  }
393  if (internalDebug == true) cout << "Found bestMovementY --> " << bestMovementY << endl;
394 
395  // @@@ Fit at Z-deltaMean | Z | Z+deltaMean @@@
396  bestEdm = 1.;
397  for (int i = 0; i < 3; i++)
398  {
399  deltaMean = (double(i)-1.)*std::sqrt(*(it+2));
400  if (internalDebug == true)
401  {
402  cout << "deltaMean --> " << deltaMean << endl;
403  cout << "deltaMean X --> " << (double(bestMovementX)-1.)*std::sqrt((*(it+0))*varFactor) << endl;
404  cout << "deltaMean Y --> " << (double(bestMovementY)-1.)*std::sqrt((*(it+1))*varFactor) << endl;
405  }
406 
407  Gauss3D->Clear();
408 
409  // arg3 - first guess of parameter value
410  // arg4 - step of the parameter
411  Gauss3D->SetParameter(0,"var x ", *(it+0)*varFactor, parDistanceXY*parDistanceXY, 0., 0.);
412  Gauss3D->SetParameter(1,"var y ", *(it+1)*varFactor, parDistanceXY*parDistanceXY, 0., 0.);
413  Gauss3D->SetParameter(2,"var z ", *(it+2), parDistanceZ*parDistanceZ, 0., 0.);
414  Gauss3D->SetParameter(3,"cov xy", *(it+3), parDistanceCxy, 0., 0.);
415  Gauss3D->SetParameter(4,"dydz ", *(it+4), parDistanceddZ, 0., 0.);
416  Gauss3D->SetParameter(5,"dxdz ", *(it+5), parDistanceddZ, 0., 0.);
417  Gauss3D->SetParameter(6,"mean x", *(it+6)+(double(bestMovementX)-1.)*std::sqrt((*(it+0))*varFactor), parDistanceXY, 0., 0.);
418  Gauss3D->SetParameter(7,"mean y", *(it+7)+(double(bestMovementY)-1.)*std::sqrt((*(it+1))*varFactor), parDistanceXY, 0., 0.);
419  Gauss3D->SetParameter(8,"mean z", *(it+8)+deltaMean, parDistanceZ, 0., 0.);
420 
421  // Set the central positions of the centroid for vertex rejection
422  xPos = Gauss3D->GetParameter(6);
423  yPos = Gauss3D->GetParameter(7);
424  zPos = Gauss3D->GetParameter(8);
425 
426  // Set dimensions of the centroid for vertex rejection
427  maxTransRadius = nSigmaXY * std::sqrt(std::fabs(Gauss3D->GetParameter(0)) + std::fabs(Gauss3D->GetParameter(1))) / 2.;
428  maxLongLength = nSigmaZ * std::sqrt(std::fabs(Gauss3D->GetParameter(2)));
429 
430  goodData = Gauss3D->ExecuteCommand("MIGRAD",arglist,2);
431  Gauss3D->GetStats(amin, edm, errdef, nvpar, nparx);
432 
433  if (counterVx < minNentries) goodData = -2;
434  else if (std::isnan(edm) == true) goodData = -1;
435  else for (unsigned int j = 0; j < nParams; j++) if (std::isnan(Gauss3D->GetParError(j)) == true) { goodData = -1; break; }
436  if (goodData == 0)
437  {
438  covyz = Gauss3D->GetParameter(4)*(std::fabs(Gauss3D->GetParameter(2))-std::fabs(Gauss3D->GetParameter(1))) - Gauss3D->GetParameter(5)*Gauss3D->GetParameter(3);
439  covxz = Gauss3D->GetParameter(5)*(std::fabs(Gauss3D->GetParameter(2))-std::fabs(Gauss3D->GetParameter(0))) - Gauss3D->GetParameter(4)*Gauss3D->GetParameter(3);
440 
441  det = std::fabs(Gauss3D->GetParameter(0)) * (std::fabs(Gauss3D->GetParameter(1))*std::fabs(Gauss3D->GetParameter(2)) - covyz*covyz) -
442  Gauss3D->GetParameter(3) * (Gauss3D->GetParameter(3)*std::fabs(Gauss3D->GetParameter(2)) - covxz*covyz) +
443  covxz * (Gauss3D->GetParameter(3)*covyz - covxz*std::fabs(Gauss3D->GetParameter(1)));
444  if (det < 0.) { goodData = -1; if (internalDebug == true) cout << "Negative determinant !" << endl; }
445  }
446 
447  if ((goodData == 0) && (std::fabs(edm) < bestEdm)) { bestEdm = edm; bestMovementZ = i; }
448  }
449  if (internalDebug == true) cout << "Found bestMovementZ --> " << bestMovementZ << endl;
450 
451  Gauss3D->Clear();
452 
453  // @@@ FINAL FIT @@@
454  // arg3 - first guess of parameter value
455  // arg4 - step of the parameter
456  Gauss3D->SetParameter(0,"var x ", *(it+0)*varFactor, parDistanceXY*parDistanceXY, 0., 0.);
457  Gauss3D->SetParameter(1,"var y ", *(it+1)*varFactor, parDistanceXY*parDistanceXY, 0., 0.);
458  Gauss3D->SetParameter(2,"var z ", *(it+2), parDistanceZ*parDistanceZ, 0., 0.);
459  Gauss3D->SetParameter(3,"cov xy", *(it+3), parDistanceCxy, 0., 0.);
460  Gauss3D->SetParameter(4,"dydz ", *(it+4), parDistanceddZ, 0., 0.);
461  Gauss3D->SetParameter(5,"dxdz ", *(it+5), parDistanceddZ, 0., 0.);
462  Gauss3D->SetParameter(6,"mean x", *(it+6)+(double(bestMovementX)-1.)*std::sqrt((*(it+0))*varFactor), parDistanceXY, 0., 0.);
463  Gauss3D->SetParameter(7,"mean y", *(it+7)+(double(bestMovementY)-1.)*std::sqrt((*(it+1))*varFactor), parDistanceXY, 0., 0.);
464  Gauss3D->SetParameter(8,"mean z", *(it+8)+(double(bestMovementZ)-1.)*std::sqrt(*(it+2)), parDistanceZ, 0., 0.);
465 
466  // Set the central positions of the centroid for vertex rejection
467  xPos = Gauss3D->GetParameter(6);
468  yPos = Gauss3D->GetParameter(7);
469  zPos = Gauss3D->GetParameter(8);
470 
471  // Set dimensions of the centroid for vertex rejection
472  maxTransRadius = nSigmaXY * std::sqrt(std::fabs(Gauss3D->GetParameter(0)) + std::fabs(Gauss3D->GetParameter(1))) / 2.;
473  maxLongLength = nSigmaZ * std::sqrt(std::fabs(Gauss3D->GetParameter(2)));
474 
475  goodData = Gauss3D->ExecuteCommand("MIGRAD",arglist,2);
476  Gauss3D->GetStats(amin, edm, errdef, nvpar, nparx);
477 
478  if (counterVx < minNentries) goodData = -2;
479  else if (std::isnan(edm) == true) goodData = -1;
480  else for (unsigned int j = 0; j < nParams; j++) if (std::isnan(Gauss3D->GetParError(j)) == true) { goodData = -1; break; }
481  if (goodData == 0)
482  {
483  covyz = Gauss3D->GetParameter(4)*(std::fabs(Gauss3D->GetParameter(2))-std::fabs(Gauss3D->GetParameter(1))) - Gauss3D->GetParameter(5)*Gauss3D->GetParameter(3);
484  covxz = Gauss3D->GetParameter(5)*(std::fabs(Gauss3D->GetParameter(2))-std::fabs(Gauss3D->GetParameter(0))) - Gauss3D->GetParameter(4)*Gauss3D->GetParameter(3);
485 
486  det = std::fabs(Gauss3D->GetParameter(0)) * (std::fabs(Gauss3D->GetParameter(1))*std::fabs(Gauss3D->GetParameter(2)) - covyz*covyz) -
487  Gauss3D->GetParameter(3) * (Gauss3D->GetParameter(3)*std::fabs(Gauss3D->GetParameter(2)) - covxz*covyz) +
488  covxz * (Gauss3D->GetParameter(3)*covyz - covxz*std::fabs(Gauss3D->GetParameter(1)));
489  if (det < 0.) { goodData = -1; if (internalDebug == true) cout << "Negative determinant !" << endl; }
490  }
491 
492  // @@@ FIT WITH DIFFERENT PARAMETER DISTANCES@@@
493  // arg3 - first guess of parameter value
494  // arg4 - step of the parameter
495  for (unsigned int i = 0; i < trials; i++)
496  {
497  if ((goodData != 0) && (goodData != -2))
498  {
499  Gauss3D->Clear();
500 
501  if (internalDebug == true) cout << "FIT WITH DIFFERENT PARAMETER DISTANCES - STEP " << i+1 << endl;
502 
503  Gauss3D->SetParameter(0,"var x ", *(it+0)*varFactor, parDistanceXY*parDistanceXY * largerDist[i], 0, 0);
504  Gauss3D->SetParameter(1,"var y ", *(it+1)*varFactor, parDistanceXY*parDistanceXY * largerDist[i], 0, 0);
505  Gauss3D->SetParameter(2,"var z ", *(it+2), parDistanceZ*parDistanceZ * largerDist[i], 0, 0);
506  Gauss3D->SetParameter(3,"cov xy", *(it+3), parDistanceCxy * largerDist[i], 0, 0);
507  Gauss3D->SetParameter(4,"dydz ", *(it+4), parDistanceddZ * largerDist[i], 0, 0);
508  Gauss3D->SetParameter(5,"dxdz ", *(it+5), parDistanceddZ * largerDist[i], 0, 0);
509  Gauss3D->SetParameter(6,"mean x", *(it+6)+(double(bestMovementX)-1.)*std::sqrt((*(it+0))*varFactor), parDistanceXY * largerDist[i], 0, 0);
510  Gauss3D->SetParameter(7,"mean y", *(it+7)+(double(bestMovementY)-1.)*std::sqrt((*(it+1))*varFactor), parDistanceXY * largerDist[i], 0, 0);
511  Gauss3D->SetParameter(8,"mean z", *(it+8)+(double(bestMovementZ)-1.)*std::sqrt(*(it+2)), parDistanceZ * largerDist[i], 0, 0);
512 
513  // Set the central positions of the centroid for vertex rejection
514  xPos = Gauss3D->GetParameter(6);
515  yPos = Gauss3D->GetParameter(7);
516  zPos = Gauss3D->GetParameter(8);
517 
518  // Set dimensions of the centroid for vertex rejection
519  maxTransRadius = nSigmaXY * std::sqrt(std::fabs(Gauss3D->GetParameter(0)) + std::fabs(Gauss3D->GetParameter(1))) / 2.;
520  maxLongLength = nSigmaZ * std::sqrt(std::fabs(Gauss3D->GetParameter(2)));
521 
522  goodData = Gauss3D->ExecuteCommand("MIGRAD",arglist,2);
523  Gauss3D->GetStats(amin, edm, errdef, nvpar, nparx);
524 
525  if (counterVx < minNentries) goodData = -2;
526  else if (std::isnan(edm) == true) goodData = -1;
527  else for (unsigned int j = 0; j < nParams; j++) if (std::isnan(Gauss3D->GetParError(j)) == true) { goodData = -1; break; }
528  if (goodData == 0)
529  {
530  covyz = Gauss3D->GetParameter(4)*(std::fabs(Gauss3D->GetParameter(2))-std::fabs(Gauss3D->GetParameter(1))) - Gauss3D->GetParameter(5)*Gauss3D->GetParameter(3);
531  covxz = Gauss3D->GetParameter(5)*(std::fabs(Gauss3D->GetParameter(2))-std::fabs(Gauss3D->GetParameter(0))) - Gauss3D->GetParameter(4)*Gauss3D->GetParameter(3);
532 
533  det = std::fabs(Gauss3D->GetParameter(0)) * (std::fabs(Gauss3D->GetParameter(1))*std::fabs(Gauss3D->GetParameter(2)) - covyz*covyz) -
534  Gauss3D->GetParameter(3) * (Gauss3D->GetParameter(3)*std::fabs(Gauss3D->GetParameter(2)) - covxz*covyz) +
535  covxz * (Gauss3D->GetParameter(3)*covyz - covxz*std::fabs(Gauss3D->GetParameter(1)));
536  if (det < 0.) { goodData = -1; if (internalDebug == true) cout << "Negative determinant !" << endl; }
537  }
538  } else break;
539  }
540 
541  if (goodData == 0)
542  for (unsigned int i = 0; i < nParams; i++)
543  {
544  vals->operator[](i) = Gauss3D->GetParameter(i);
545  vals->operator[](i+nParams) = Gauss3D->GetParError(i);
546  }
547 
548  delete Gauss3D;
549  return goodData;
550  }
551 
552  return -1;
553 }
554 
555 
556 void Vx3DHLTAnalyzer::reset(string ResetType)
557 {
558  if (ResetType.compare("scratch") == 0)
559  {
560  runNumber = 0;
561  numberGoodFits = 0;
562  numberFits = 0;
563  lastLumiOfFit = 0;
564 
565  Vx_X->Reset();
566  Vx_Y->Reset();
567  Vx_Z->Reset();
568 
569  Vx_ZX->Reset();
570  Vx_ZY->Reset();
571  Vx_XY->Reset();
572 
573  mXlumi->Reset();
574  mYlumi->Reset();
575  mZlumi->Reset();
576 
577  sXlumi->Reset();
578  sYlumi->Reset();
579  sZlumi->Reset();
580 
581  dxdzlumi->Reset();
582  dydzlumi->Reset();
583 
584  hitCounter->Reset();
585  hitCountHistory->Reset();
586  goodVxCounter->Reset();
587  goodVxCountHistory->Reset();
588  fitResults->Reset();
589 
590  reportSummary->Fill(0.);
591  reportSummaryMap->Fill(0.5, 0.5, 0.);
592 
593  Vertices.clear();
594 
595  lumiCounter = 0;
596  lumiCounterHisto = 0;
597  totalHits = 0;
598  beginTimeOfFit = 0;
599  endTimeOfFit = 0;
600  beginLumiOfFit = 0;
601  endLumiOfFit = 0;
602  }
603  else if (ResetType.compare("whole") == 0)
604  {
605  Vx_X->Reset();
606  Vx_Y->Reset();
607  Vx_Z->Reset();
608 
609  Vx_ZX->Reset();
610  Vx_ZY->Reset();
611  Vx_XY->Reset();
612 
613  Vertices.clear();
614 
615  lumiCounter = 0;
616  lumiCounterHisto = 0;
617  totalHits = 0;
618  beginTimeOfFit = 0;
619  endTimeOfFit = 0;
620  beginLumiOfFit = 0;
621  endLumiOfFit = 0;
622  }
623  else if (ResetType.compare("partial") == 0)
624  {
625  Vx_X->Reset();
626  Vx_Y->Reset();
627  Vx_Z->Reset();
628 
629  Vertices.clear();
630 
631  lumiCounter = 0;
632  totalHits = 0;
633  beginTimeOfFit = 0;
634  endTimeOfFit = 0;
635  beginLumiOfFit = 0;
636  endLumiOfFit = 0;
637  }
638  else if (ResetType.compare("nohisto") == 0)
639  {
640  Vertices.clear();
641 
642  lumiCounter = 0;
643  lumiCounterHisto = 0;
644  totalHits = 0;
645  beginTimeOfFit = 0;
646  endTimeOfFit = 0;
647  beginLumiOfFit = 0;
648  endLumiOfFit = 0;
649  }
650  else if (ResetType.compare("hitCounter") == 0)
651  totalHits = 0;
652 }
653 
654 
655 void Vx3DHLTAnalyzer::writeToFile(vector<double>* vals,
656  edm::TimeValue_t BeginTimeOfFit,
657  edm::TimeValue_t EndTimeOfFit,
658  unsigned int BeginLumiOfFit,
659  unsigned int EndLumiOfFit,
660  int dataType)
661 {
662  stringstream BufferString;
663  BufferString.precision(5);
664 
665  outputFile.open(fileName.c_str(), ios::out);
666 
667  if ((outputFile.is_open() == true) && (vals != NULL) && (vals->size() == 8*2))
668  {
669  vector<double>::const_iterator it = vals->begin();
670 
671  outputFile << "Runnumber " << runNumber << endl;
672  outputFile << "BeginTimeOfFit " << formatTime(beginTimeOfFit >> 32) << " " << (beginTimeOfFit >> 32) << endl;
673  outputFile << "EndTimeOfFit " << formatTime(endTimeOfFit >> 32) << " " << (endTimeOfFit >> 32) << endl;
674  outputFile << "LumiRange " << beginLumiOfFit << " - " << endLumiOfFit << endl;
675  outputFile << "Type " << dataType << endl;
676  // 3D Vertexing with Pixel Tracks:
677  // Good data = Type 3
678  // Bad data = Type -1
679 
680  BufferString << *(it+0);
681  outputFile << "X0 " << BufferString.str().c_str() << endl;
682  BufferString.str("");
683 
684  BufferString << *(it+1);
685  outputFile << "Y0 " << BufferString.str().c_str() << endl;
686  BufferString.str("");
687 
688  BufferString << *(it+2);
689  outputFile << "Z0 " << BufferString.str().c_str() << endl;
690  BufferString.str("");
691 
692  BufferString << *(it+3);
693  outputFile << "sigmaZ0 " << BufferString.str().c_str() << endl;
694  BufferString.str("");
695 
696  BufferString << *(it+4);
697  outputFile << "dxdz " << BufferString.str().c_str() << endl;
698  BufferString.str("");
699 
700  BufferString << *(it+5);
701  outputFile << "dydz " << BufferString.str().c_str() << endl;
702  BufferString.str("");
703 
704  BufferString << *(it+6);
705  outputFile << "BeamWidthX " << BufferString.str().c_str() << endl;
706  BufferString.str("");
707 
708  BufferString << *(it+7);
709  outputFile << "BeamWidthY " << BufferString.str().c_str() << endl;
710  BufferString.str("");
711 
712  outputFile << "Cov(0,j) " << *(it+8) << " 0.0 0.0 0.0 0.0 0.0 0.0" << endl;
713  outputFile << "Cov(1,j) 0.0 " << *(it+9) << " 0.0 0.0 0.0 0.0 0.0" << endl;
714  outputFile << "Cov(2,j) 0.0 0.0 " << *(it+10) << " 0.0 0.0 0.0 0.0" << endl;
715  outputFile << "Cov(3,j) 0.0 0.0 0.0 " << *(it+11) << " 0.0 0.0 0.0" << endl;
716  outputFile << "Cov(4,j) 0.0 0.0 0.0 0.0 " << *(it+12) << " 0.0 0.0" << endl;
717  outputFile << "Cov(5,j) 0.0 0.0 0.0 0.0 0.0 " << *(it+13) << " 0.0" << endl;
718  outputFile << "Cov(6,j) 0.0 0.0 0.0 0.0 0.0 0.0 " << ((*(it+14)) + (*(it+15)) + 2.*std::sqrt((*(it+14))*(*(it+15)))) / 4. << endl;
719 
720  outputFile << "EmittanceX 0.0" << endl;
721  outputFile << "EmittanceY 0.0" << endl;
722  outputFile << "BetaStar 0.0" << endl;
723  }
724  outputFile.close();
725 
726  if ((debugMode == true) && (outputDebugFile.is_open() == true) && (vals != NULL) && (vals->size() == 8*2))
727  {
728  vector<double>::const_iterator it = vals->begin();
729 
730  outputDebugFile << "Runnumber " << runNumber << endl;
731  outputDebugFile << "BeginTimeOfFit " << formatTime(beginTimeOfFit >> 32) << " " << (beginTimeOfFit >> 32) << endl;
732  outputDebugFile << "EndTimeOfFit " << formatTime(endTimeOfFit >> 32) << " " << (endTimeOfFit >> 32) << endl;
733  outputDebugFile << "LumiRange " << beginLumiOfFit << " - " << endLumiOfFit << endl;
734  outputDebugFile << "Type " << dataType << endl;
735  // 3D Vertexing with Pixel Tracks:
736  // Good data = Type 3
737  // Bad data = Type -1
738 
739  BufferString << *(it+0);
740  outputDebugFile << "X0 " << BufferString.str().c_str() << endl;
741  BufferString.str("");
742 
743  BufferString << *(it+1);
744  outputDebugFile << "Y0 " << BufferString.str().c_str() << endl;
745  BufferString.str("");
746 
747  BufferString << *(it+2);
748  outputDebugFile << "Z0 " << BufferString.str().c_str() << endl;
749  BufferString.str("");
750 
751  BufferString << *(it+3);
752  outputDebugFile << "sigmaZ0 " << BufferString.str().c_str() << endl;
753  BufferString.str("");
754 
755  BufferString << *(it+4);
756  outputDebugFile << "dxdz " << BufferString.str().c_str() << endl;
757  BufferString.str("");
758 
759  BufferString << *(it+5);
760  outputDebugFile << "dydz " << BufferString.str().c_str() << endl;
761  BufferString.str("");
762 
763  BufferString << *(it+6);
764  outputDebugFile << "BeamWidthX " << BufferString.str().c_str() << endl;
765  BufferString.str("");
766 
767  BufferString << *(it+7);
768  outputDebugFile << "BeamWidthY " << BufferString.str().c_str() << endl;
769  BufferString.str("");
770 
771  outputDebugFile << "Cov(0,j) " << *(it+8) << " 0.0 0.0 0.0 0.0 0.0 0.0" << endl;
772  outputDebugFile << "Cov(1,j) 0.0 " << *(it+9) << " 0.0 0.0 0.0 0.0 0.0" << endl;
773  outputDebugFile << "Cov(2,j) 0.0 0.0 " << *(it+10) << " 0.0 0.0 0.0 0.0" << endl;
774  outputDebugFile << "Cov(3,j) 0.0 0.0 0.0 " << *(it+11) << " 0.0 0.0 0.0" << endl;
775  outputDebugFile << "Cov(4,j) 0.0 0.0 0.0 0.0 " << *(it+12) << " 0.0 0.0" << endl;
776  outputDebugFile << "Cov(5,j) 0.0 0.0 0.0 0.0 0.0 " << *(it+13) << " 0.0" << endl;
777  outputDebugFile << "Cov(6,j) 0.0 0.0 0.0 0.0 0.0 0.0 " << ((*(it+14)) + (*(it+15)) + 2.*std::sqrt((*(it+14))*(*(it+15)))) / 4. << endl;
778 
779  outputDebugFile << "EmittanceX 0.0" << endl;
780  outputDebugFile << "EmittanceY 0.0" << endl;
781  outputDebugFile << "BetaStar 0.0" << endl;
782  }
783 }
784 
785 
787  const EventSetup& iSetup)
788 {
789  if ((lumiCounter == 0) && (lumiBlock.luminosityBlock() > lastLumiOfFit))
790  {
791  beginTimeOfFit = lumiBlock.beginTime().value();
792  beginLumiOfFit = lumiBlock.luminosityBlock();
793  lumiCounter++;
794  lumiCounterHisto++;
795  }
796  else if ((lumiCounter != 0) && (lumiBlock.luminosityBlock() >= (beginLumiOfFit+lumiCounter))) { lumiCounter++; lumiCounterHisto++; }
797 }
798 
799 
801  const EventSetup& iSetup)
802 {
803  stringstream histTitle;
804  int goodData;
805  unsigned int nParams = 9;
806 
807  if ((lumiCounter%nLumiReset == 0) && (nLumiReset != 0) && (beginTimeOfFit != 0) && (runNumber != 0))
808  {
809  endTimeOfFit = lumiBlock.endTime().value();
810  endLumiOfFit = lumiBlock.luminosityBlock();
811  lastLumiOfFit = endLumiOfFit;
812  vector<double> vals;
813 
814  hitCounter->ShiftFillLast((double)totalHits, std::sqrt((double)totalHits), nLumiReset);
815 
816  if (lastLumiOfFit % prescaleHistory == 0)
817  {
818  hitCountHistory->getTH1()->SetBinContent(lastLumiOfFit, (double)totalHits);
819  hitCountHistory->getTH1()->SetBinError(lastLumiOfFit, std::sqrt((double)totalHits));
820  }
821 
822  if (dataFromFit == true)
823  {
824  vector<double> fitResults;
825 
826  fitResults.push_back(Vx_X->getTH1()->GetRMS()*Vx_X->getTH1()->GetRMS());
827  fitResults.push_back(Vx_Y->getTH1()->GetRMS()*Vx_Y->getTH1()->GetRMS());
828  fitResults.push_back(Vx_Z->getTH1()->GetRMS()*Vx_Z->getTH1()->GetRMS());
829  fitResults.push_back(0.0);
830  fitResults.push_back(0.0);
831  fitResults.push_back(0.0);
832  fitResults.push_back(Vx_X->getTH1()->GetMean());
833  fitResults.push_back(Vx_Y->getTH1()->GetMean());
834  fitResults.push_back(Vx_Z->getTH1()->GetMean());
835  for (unsigned int i = 0; i < nParams; i++) fitResults.push_back(0.0);
836 
837  goodData = MyFit(&fitResults);
838 
839  if (internalDebug == true)
840  {
841  cout << "goodData --> " << goodData << endl;
842  cout << "Used vertices --> " << counterVx << endl;
843  cout << "var x --> " << fitResults[0] << " +/- " << fitResults[0+nParams] << endl;
844  cout << "var y --> " << fitResults[1] << " +/- " << fitResults[1+nParams] << endl;
845  cout << "var z --> " << fitResults[2] << " +/- " << fitResults[2+nParams] << endl;
846  cout << "cov xy --> " << fitResults[3] << " +/- " << fitResults[3+nParams] << endl;
847  cout << "dydz --> " << fitResults[4] << " +/- " << fitResults[4+nParams] << endl;
848  cout << "dxdz --> " << fitResults[5] << " +/- " << fitResults[5+nParams] << endl;
849  cout << "mean x --> " << fitResults[6] << " +/- " << fitResults[6+nParams] << endl;
850  cout << "mean y --> " << fitResults[7] << " +/- " << fitResults[7+nParams] << endl;
851  cout << "mean z --> " << fitResults[8] << " +/- " << fitResults[8+nParams] << endl;
852  }
853 
854  if (goodData == 0)
855  {
856  vals.push_back(fitResults[6]);
857  vals.push_back(fitResults[7]);
858  vals.push_back(fitResults[8]);
859  vals.push_back(std::sqrt(std::fabs(fitResults[2])));
860  vals.push_back(fitResults[5]);
861  vals.push_back(fitResults[4]);
862  vals.push_back(std::sqrt(std::fabs(fitResults[0])));
863  vals.push_back(std::sqrt(std::fabs(fitResults[1])));
864 
865  vals.push_back(powf(fitResults[6+nParams],2.));
866  vals.push_back(powf(fitResults[7+nParams],2.));
867  vals.push_back(powf(fitResults[8+nParams],2.));
868  vals.push_back(powf(std::fabs(fitResults[2+nParams]) / (2.*std::sqrt(std::fabs(fitResults[2]))),2.));
869  vals.push_back(powf(fitResults[5+nParams],2.));
870  vals.push_back(powf(fitResults[4+nParams],2.));
871  vals.push_back(powf(std::fabs(fitResults[0+nParams]) / (2.*std::sqrt(std::fabs(fitResults[0]))),2.));
872  vals.push_back(powf(std::fabs(fitResults[1+nParams]) / (2.*std::sqrt(std::fabs(fitResults[1]))),2.));
873  }
874  else for (unsigned int i = 0; i < 8*2; i++) vals.push_back(0.0);
875 
876  fitResults.clear();
877  }
878  else
879  {
880  counterVx = Vx_X->getTH1F()->GetEntries();
881 
882  if (Vx_X->getTH1F()->GetEntries() >= minNentries)
883  {
884  goodData = 0;
885 
886  vals.push_back(Vx_X->getTH1F()->GetMean());
887  vals.push_back(Vx_Y->getTH1F()->GetMean());
888  vals.push_back(Vx_Z->getTH1F()->GetMean());
889  vals.push_back(Vx_Z->getTH1F()->GetRMS());
890  vals.push_back(0.0);
891  vals.push_back(0.0);
892  vals.push_back(Vx_X->getTH1F()->GetRMS());
893  vals.push_back(Vx_Y->getTH1F()->GetRMS());
894 
895  vals.push_back(powf(Vx_X->getTH1F()->GetMeanError(),2.));
896  vals.push_back(powf(Vx_Y->getTH1F()->GetMeanError(),2.));
897  vals.push_back(powf(Vx_Z->getTH1F()->GetMeanError(),2.));
898  vals.push_back(powf(Vx_Z->getTH1F()->GetRMSError(),2.));
899  vals.push_back(0.0);
900  vals.push_back(0.0);
901  vals.push_back(powf(Vx_X->getTH1F()->GetRMSError(),2.));
902  vals.push_back(powf(Vx_Y->getTH1F()->GetRMSError(),2.));
903  }
904  else
905  {
906  goodData = -2;
907  for (unsigned int i = 0; i < 8*2; i++) vals.push_back(0.0);
908  }
909  }
910 
911  // vals[0] = X0
912  // vals[1] = Y0
913  // vals[2] = Z0
914  // vals[3] = sigmaZ0
915  // vals[4] = dxdz
916  // vals[5] = dydz
917  // vals[6] = BeamWidthX
918  // vals[7] = BeamWidthY
919 
920  // vals[8] = err^2 X0
921  // vals[9] = err^2 Y0
922  // vals[10] = err^2 Z0
923  // vals[11] = err^2 sigmaZ0
924  // vals[12] = err^2 dxdz
925  // vals[13] = err^2 dydz
926  // vals[14] = err^2 BeamWidthX
927  // vals[15] = err^2 BeamWidthY
928 
929  // "goodData" CODE:
930  // 0 == OK --> Reset
931  // -2 == NO OK - not enough "minNentries" --> Wait for more lumisections
932  // Any other number == NO OK --> Reset
933 
934  numberFits++;
935  if (goodData == 0)
936  {
937  writeToFile(&vals, beginTimeOfFit, endTimeOfFit, beginLumiOfFit, endLumiOfFit, 3);
938  if ((internalDebug == true) && (outputDebugFile.is_open() == true)) outputDebugFile << "Used vertices: " << counterVx << endl;
939 
940  numberGoodFits++;
941 
942  histTitle << "Fitted Beam Spot [cm] (Lumi start: " << beginLumiOfFit << " - Lumi end: " << endLumiOfFit << ")";
943  if (lumiCounterHisto >= maxLumiIntegration) reset("whole");
944  else reset("partial");
945  }
946  else
947  {
948  writeToFile(&vals, beginTimeOfFit, endTimeOfFit, beginLumiOfFit, endLumiOfFit, -1);
949  if ((internalDebug == true) && (outputDebugFile.is_open() == true)) outputDebugFile << "Used vertices: " << counterVx << endl;
950 
951  if (goodData == -2)
952  {
953  histTitle << "Fitted Beam Spot [cm] (not enough statistics)";
954  if (lumiCounter >= maxLumiIntegration) reset("whole");
955  else reset("hitCounter");
956  }
957  else
958  {
959  histTitle << "Fitted Beam Spot [cm] (problems)";
960  if (lumiCounterHisto >= maxLumiIntegration) reset("whole");
961  else reset("partial");
962 
963  counterVx = 0;
964  }
965  }
966 
967  reportSummary->Fill(numberFits != 0 ? (double)numberGoodFits/(double)numberFits : 0.0);
968  reportSummaryMap->Fill(0.5, 0.5, numberFits != 0 ? (double)numberGoodFits/(double)numberFits : 0.0);
969 
970  fitResults->setAxisTitle(histTitle.str().c_str(), 1);
971 
972  fitResults->setBinContent(1, 9, vals[0]);
973  fitResults->setBinContent(1, 8, vals[1]);
974  fitResults->setBinContent(1, 7, vals[2]);
975  fitResults->setBinContent(1, 6, vals[3]);
976  fitResults->setBinContent(1, 5, vals[4]);
977  fitResults->setBinContent(1, 4, vals[5]);
978  fitResults->setBinContent(1, 3, vals[6]);
979  fitResults->setBinContent(1, 2, vals[7]);
980  fitResults->setBinContent(1, 1, counterVx);
981 
982  fitResults->setBinContent(2, 9, std::sqrt(vals[8]));
983  fitResults->setBinContent(2, 8, std::sqrt(vals[9]));
984  fitResults->setBinContent(2, 7, std::sqrt(vals[10]));
985  fitResults->setBinContent(2, 6, std::sqrt(vals[11]));
986  fitResults->setBinContent(2, 5, std::sqrt(vals[12]));
987  fitResults->setBinContent(2, 4, std::sqrt(vals[13]));
988  fitResults->setBinContent(2, 3, std::sqrt(vals[14]));
989  fitResults->setBinContent(2, 2, std::sqrt(vals[15]));
990  fitResults->setBinContent(2, 1, std::sqrt(counterVx));
991 
992  // Linear fit to the historical plots
993  TF1* myLinFit = new TF1("myLinFit", "[0] + [1]*x", mXlumi->getTH1()->GetXaxis()->GetXmin(), mXlumi->getTH1()->GetXaxis()->GetXmax());
994  myLinFit->SetLineColor(2);
995  myLinFit->SetLineWidth(2);
996  myLinFit->SetParName(0,"Intercept");
997  myLinFit->SetParName(1,"Slope");
998 
999  mXlumi->ShiftFillLast(vals[0], std::sqrt(vals[8]), nLumiReset);
1000  myLinFit->SetParameter(0, mXlumi->getTH1()->GetMean(2));
1001  myLinFit->SetParameter(1, 0.0);
1002  mXlumi->getTH1()->Fit("myLinFit","QR");
1003 
1004  mYlumi->ShiftFillLast(vals[1], std::sqrt(vals[9]), nLumiReset);
1005  myLinFit->SetParameter(0, mYlumi->getTH1()->GetMean(2));
1006  myLinFit->SetParameter(1, 0.0);
1007  mYlumi->getTH1()->Fit("myLinFit","QR");
1008 
1009  mZlumi->ShiftFillLast(vals[2], std::sqrt(vals[10]), nLumiReset);
1010  myLinFit->SetParameter(0, mZlumi->getTH1()->GetMean(2));
1011  myLinFit->SetParameter(1, 0.0);
1012  mZlumi->getTH1()->Fit("myLinFit","QR");
1013 
1014  sXlumi->ShiftFillLast(vals[6], std::sqrt(vals[14]), nLumiReset);
1015  myLinFit->SetParameter(0, sXlumi->getTH1()->GetMean(2));
1016  myLinFit->SetParameter(1, 0.0);
1017  sXlumi->getTH1()->Fit("myLinFit","QR");
1018 
1019  sYlumi->ShiftFillLast(vals[7], std::sqrt(vals[15]), nLumiReset);
1020  myLinFit->SetParameter(0, sYlumi->getTH1()->GetMean(2));
1021  myLinFit->SetParameter(1, 0.0);
1022  sYlumi->getTH1()->Fit("myLinFit","QR");
1023 
1024  sZlumi->ShiftFillLast(vals[3], std::sqrt(vals[11]), nLumiReset);
1025  myLinFit->SetParameter(0, sZlumi->getTH1()->GetMean(2));
1026  myLinFit->SetParameter(1, 0.0);
1027  sZlumi->getTH1()->Fit("myLinFit","QR");
1028 
1029  dxdzlumi->ShiftFillLast(vals[4], std::sqrt(vals[12]), nLumiReset);
1030  myLinFit->SetParameter(0, dxdzlumi->getTH1()->GetMean(2));
1031  myLinFit->SetParameter(1, 0.0);
1032  dxdzlumi->getTH1()->Fit("myLinFit","QR");
1033 
1034  dydzlumi->ShiftFillLast(vals[5], std::sqrt(vals[13]), nLumiReset);
1035  myLinFit->SetParameter(0, dydzlumi->getTH1()->GetMean(2));
1036  myLinFit->SetParameter(1, 0.0);
1037  dydzlumi->getTH1()->Fit("myLinFit","QR");
1038 
1039  goodVxCounter->ShiftFillLast((double)counterVx, std::sqrt((double)counterVx), nLumiReset);
1040  myLinFit->SetParameter(0, goodVxCounter->getTH1()->GetMean(2));
1041  myLinFit->SetParameter(1, 0.0);
1042  goodVxCounter->getTH1()->Fit("myLinFit","QR");
1043 
1044  if (lastLumiOfFit % prescaleHistory == 0)
1045  {
1046  goodVxCountHistory->getTH1()->SetBinContent(lastLumiOfFit, (double)counterVx);
1047  goodVxCountHistory->getTH1()->SetBinError(lastLumiOfFit, std::sqrt((double)counterVx));
1048  }
1049 
1050  delete myLinFit;
1051 
1052  vals.clear();
1053  }
1054  else if (nLumiReset == 0)
1055  {
1056  histTitle << "Fitted Beam Spot [cm] (no ongoing fits)";
1057  fitResults->setAxisTitle(histTitle.str().c_str(), 1);
1058  reportSummaryMap->Fill(0.5, 0.5, 1.0);
1059  hitCounter->ShiftFillLast(totalHits, std::sqrt(totalHits), 1);
1060  reset("nohisto");
1061  }
1062 }
1063 
1064 
1066 {
1067  DQMStore* dbe = 0;
1068  dbe = Service<DQMStore>().operator->();
1069 
1070  // ### Set internal variables ###
1071  nBinsHistoricalPlot = 80;
1072  nBinsWholeHistory = 3000; // Corresponds to about 20h of data taking: 20h * 60min * 60s / 23s per lumi-block = 3130
1073  // ##############################
1074 
1075  if ( dbe )
1076  {
1077  dbe->setCurrentFolder("BeamPixel");
1078 
1079  Vx_X = dbe->book1D("vertex x", "Primary Vertex X Coordinate Distribution", int(rint(xRange/xStep)), -xRange/2., xRange/2.);
1080  Vx_Y = dbe->book1D("vertex y", "Primary Vertex Y Coordinate Distribution", int(rint(yRange/yStep)), -yRange/2., yRange/2.);
1081  Vx_Z = dbe->book1D("vertex z", "Primary Vertex Z Coordinate Distribution", int(rint(zRange/zStep)), -zRange/2., zRange/2.);
1082  Vx_X->setAxisTitle("Primary Vertices X [cm]",1);
1083  Vx_X->setAxisTitle("Entries [#]",2);
1084  Vx_Y->setAxisTitle("Primary Vertices Y [cm]",1);
1085  Vx_Y->setAxisTitle("Entries [#]",2);
1086  Vx_Z->setAxisTitle("Primary Vertices Z [cm]",1);
1087  Vx_Z->setAxisTitle("Entries [#]",2);
1088 
1089  mXlumi = dbe->book1D("muX vs lumi", "\\mu_{x} vs. Lumisection", nBinsHistoricalPlot, 0.5, (double)nBinsHistoricalPlot+0.5);
1090  mYlumi = dbe->book1D("muY vs lumi", "\\mu_{y} vs. Lumisection", nBinsHistoricalPlot, 0.5, (double)nBinsHistoricalPlot+0.5);
1091  mZlumi = dbe->book1D("muZ vs lumi", "\\mu_{z} vs. Lumisection", nBinsHistoricalPlot, 0.5, (double)nBinsHistoricalPlot+0.5);
1092  mXlumi->setAxisTitle("Lumisection [#]",1);
1093  mXlumi->setAxisTitle("\\mu_{x} [cm]",2);
1094  mXlumi->getTH1()->SetOption("E1");
1095  mYlumi->setAxisTitle("Lumisection [#]",1);
1096  mYlumi->setAxisTitle("\\mu_{y} [cm]",2);
1097  mYlumi->getTH1()->SetOption("E1");
1098  mZlumi->setAxisTitle("Lumisection [#]",1);
1099  mZlumi->setAxisTitle("\\mu_{z} [cm]",2);
1100  mZlumi->getTH1()->SetOption("E1");
1101 
1102  sXlumi = dbe->book1D("sigmaX vs lumi", "\\sigma_{x} vs. Lumisection", nBinsHistoricalPlot, 0.5, (double)nBinsHistoricalPlot+0.5);
1103  sYlumi = dbe->book1D("sigmaY vs lumi", "\\sigma_{y} vs. Lumisection", nBinsHistoricalPlot, 0.5, (double)nBinsHistoricalPlot+0.5);
1104  sZlumi = dbe->book1D("sigmaZ vs lumi", "\\sigma_{z} vs. Lumisection", nBinsHistoricalPlot, 0.5, (double)nBinsHistoricalPlot+0.5);
1105  sXlumi->setAxisTitle("Lumisection [#]",1);
1106  sXlumi->setAxisTitle("\\sigma_{x} [cm]",2);
1107  sXlumi->getTH1()->SetOption("E1");
1108  sYlumi->setAxisTitle("Lumisection [#]",1);
1109  sYlumi->setAxisTitle("\\sigma_{y} [cm]",2);
1110  sYlumi->getTH1()->SetOption("E1");
1111  sZlumi->setAxisTitle("Lumisection [#]",1);
1112  sZlumi->setAxisTitle("\\sigma_{z} [cm]",2);
1113  sZlumi->getTH1()->SetOption("E1");
1114 
1115  dxdzlumi = dbe->book1D("dxdz vs lumi", "dX/dZ vs. Lumisection", nBinsHistoricalPlot, 0.5, (double)nBinsHistoricalPlot+0.5);
1116  dydzlumi = dbe->book1D("dydz vs lumi", "dY/dZ vs. Lumisection", nBinsHistoricalPlot, 0.5, (double)nBinsHistoricalPlot+0.5);
1117  dxdzlumi->setAxisTitle("Lumisection [#]",1);
1118  dxdzlumi->setAxisTitle("dX/dZ [rad]",2);
1119  dxdzlumi->getTH1()->SetOption("E1");
1120  dydzlumi->setAxisTitle("Lumisection [#]",1);
1121  dydzlumi->setAxisTitle("dY/dZ [rad]",2);
1122  dydzlumi->getTH1()->SetOption("E1");
1123 
1124  Vx_ZX = dbe->book2D("vertex zx", "Primary Vertex ZX Coordinate Distribution", int(rint(zRange/zStep/5.)), -zRange/2., zRange/2., int(rint(xRange/xStep/5.)), -xRange/2., xRange/2.);
1125  Vx_ZY = dbe->book2D("vertex zy", "Primary Vertex ZY Coordinate Distribution", int(rint(zRange/zStep/5.)), -zRange/2., zRange/2., int(rint(yRange/yStep/5.)), -yRange/2., yRange/2.);
1126  Vx_XY = dbe->book2D("vertex xy", "Primary Vertex XY Coordinate Distribution", int(rint(xRange/xStep/5.)), -xRange/2., xRange/2., int(rint(yRange/yStep/5.)), -yRange/2., yRange/2.);
1127  Vx_ZX->setAxisTitle("Primary Vertices Z [cm]",1);
1128  Vx_ZX->setAxisTitle("Primary Vertices X [cm]",2);
1129  Vx_ZX->setAxisTitle("Entries [#]",3);
1130  Vx_ZY->setAxisTitle("Primary Vertices Z [cm]",1);
1131  Vx_ZY->setAxisTitle("Primary Vertices Y [cm]",2);
1132  Vx_ZY->setAxisTitle("Entries [#]",3);
1133  Vx_XY->setAxisTitle("Primary Vertices X [cm]",1);
1134  Vx_XY->setAxisTitle("Primary Vertices Y [cm]",2);
1135  Vx_XY->setAxisTitle("Entries [#]",3);
1136 
1137  hitCounter = dbe->book1D("pixelHits vs lumi", "# Pixel-Hits vs. Lumisection", nBinsHistoricalPlot, 0.5, (double)nBinsHistoricalPlot+0.5);
1138  hitCounter->setAxisTitle("Lumisection [#]",1);
1139  hitCounter->setAxisTitle("Pixel-Hits [#]",2);
1140  hitCounter->getTH1()->SetOption("E1");
1141 
1142  hitCountHistory = dbe->book1D("hist pixelHits vs lumi", "History: # Pixel-Hits vs. Lumi", nBinsWholeHistory, 0.5, (double)nBinsWholeHistory+0.5);
1143  hitCountHistory->setAxisTitle("Lumisection [#]",1);
1144  hitCountHistory->setAxisTitle("Pixel-Hits [#]",2);
1145  hitCountHistory->getTH1()->SetOption("E1");
1146 
1147  goodVxCounter = dbe->book1D("good vertices vs lumi", "# Good vertices vs. Lumisection", nBinsHistoricalPlot, 0.5, (double)nBinsHistoricalPlot+0.5);
1148  goodVxCounter->setAxisTitle("Lumisection [#]",1);
1149  goodVxCounter->setAxisTitle("Good vertices [#]",2);
1150  goodVxCounter->getTH1()->SetOption("E1");
1151 
1152  goodVxCountHistory = dbe->book1D("hist good vx vs lumi", "History: # Good vx vs. Lumi", nBinsWholeHistory, 0.5, (double)nBinsWholeHistory+0.5);
1153  goodVxCountHistory->setAxisTitle("Lumisection [#]",1);
1154  goodVxCountHistory->setAxisTitle("Good vertices [#]",2);
1155  goodVxCountHistory->getTH1()->SetOption("E1");
1156 
1157  fitResults = dbe->book2D("fit results","Results of Beam Spot Fit", 2, 0., 2., 9, 0., 9.);
1158  fitResults->setAxisTitle("Fitted Beam Spot [cm]", 1);
1159  fitResults->setBinLabel(9, "X", 2);
1160  fitResults->setBinLabel(8, "Y", 2);
1161  fitResults->setBinLabel(7, "Z", 2);
1162  fitResults->setBinLabel(6, "\\sigma_{Z}", 2);
1163  fitResults->setBinLabel(5, "#frac{dX}{dZ}[rad]", 2);
1164  fitResults->setBinLabel(4, "#frac{dY}{dZ}[rad]", 2);
1165  fitResults->setBinLabel(3, "\\sigma_{X}", 2);
1166  fitResults->setBinLabel(2, "\\sigma_{Y}", 2);
1167  fitResults->setBinLabel(1, "Vertices", 2);
1168  fitResults->setBinLabel(1, "Value", 1);
1169  fitResults->setBinLabel(2, "Stat. Error", 1);
1170  fitResults->getTH1()->SetOption("text");
1171 
1172  dbe->setCurrentFolder("BeamPixel/EventInfo");
1173  reportSummary = dbe->bookFloat("reportSummary");
1174  reportSummary->Fill(0.);
1175  reportSummaryMap = dbe->book2D("reportSummaryMap","Pixel-Vertices Beam Spot: % Good Fits", 1, 0., 1., 1, 0., 1.);
1176  reportSummaryMap->Fill(0.5, 0.5, 0.);
1177  dbe->setCurrentFolder("BeamPixel/EventInfo/reportSummaryContents");
1178 
1179  // Convention for reportSummary and reportSummaryMap:
1180  // - 0% at the moment of creation of the histogram
1181  // - n% numberGoodFits / numberFits
1182  }
1183 
1184  // ### Set internal variables ###
1185  reset("scratch");
1186  prescaleHistory = 1;
1187  maxLumiIntegration = 15;
1188  minVxDoF = 4.;
1189  internalDebug = false;
1190  considerVxCovariance = true;
1191  pi = 3.141592653589793238;
1192  // ##############################
1193 }
1194 
1195 
1196 void Vx3DHLTAnalyzer::endJob() { reset("scratch"); }
1197 
1198 
1199 // Define this as a plug-in
RunNumber_t run() const
Definition: EventID.h:42
tuple arglist
Definition: fitWZ.py:38
T getParameter(std::string const &) const
int i
Definition: DBlmapReader.cc:9
boost::transform_iterator< IterHelp, const_IdIter > const_iterator
double maxLongLength
virtual char * formatTime(const time_t &t)
MonitorElement * book1D(const char *name, const char *title, int nchX, double lowX, double highX)
Book 1D histogram.
Definition: DQMStore.cc:519
double zPos
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
virtual int MyFit(std::vector< double > *vals)
#define NULL
Definition: scimark2.h:8
data_type const * const_iterator
Definition: DetSetNew.h:25
Timestamp const & beginTime() const
virtual unsigned int HitCounter(const edm::Event &iEvent)
virtual void writeToFile(std::vector< double > *vals, edm::TimeValue_t BeginTimeOfFit, edm::TimeValue_t EndTimeOfFit, unsigned int BeginLumiOfFit, unsigned int EndLumiOfFit, int dataType)
MonitorElement * bookFloat(const char *name)
Book float.
Definition: DQMStore.cc:456
void Fill(long long x)
double maxTransRadius
LuminosityBlockNumber_t luminosityBlock() const
bool considerVxCovariance
double xPos
virtual void endLuminosityBlock(const edm::LuminosityBlock &lumiBlock, const edm::EventSetup &iSetup)
Vx3DHLTAnalyzer(const edm::ParameterSet &)
int iEvent
Definition: GenABIO.cc:243
Definition: DDAxes.h:10
virtual void beginJob()
bool isnan(float x)
Definition: math.h:13
T sqrt(T t)
Definition: SSEVec.h:28
Timestamp const & endTime() const
virtual void analyze(const edm::Event &, const edm::EventSetup &)
int j
Definition: DBlmapReader.cc:9
double yPos
virtual void reset(std::string ResetType)
unsigned long long TimeValue_t
Definition: Timestamp.h:27
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
Definition: Event.h:359
double Covariance[DIM][DIM]
virtual void beginLuminosityBlock(const edm::LuminosityBlock &lumiBlock, const edm::EventSetup &iSetup)
tuple out
Definition: dbtoconf.py:99
TimeValue_t value() const
Definition: Timestamp.cc:72
Log< T >::type log(const T &t)
Definition: Log.h:22
#define DIM
static char * formatTime(const time_t t)
Definition: ELlog4cplus.cc:57
void Gauss3DFunc(int &, double *, double &fval, double *par, int)
std::vector< std::vector< double > > tmp
Definition: MVATrainer.cc:100
edm::EventID id() const
Definition: EventBase.h:56
std::vector< VertexType > Vertices
virtual void endJob()
unsigned int counterVx
tuple cout
Definition: gather_cfg.py:41
double pi
double VxErrCorr
MonitorElement * book2D(const char *name, const char *title, int nchX, double lowX, double highX, int nchY, double lowY, double highY)
Book 2D histogram.
Definition: DQMStore.cc:647
The Signals That Services Can Subscribe To This is based on ActivityRegistry h
Helper function to determine trigger accepts.
Definition: Activities.doc:4
void setAxisTitle(const std::string &title, int axis=1)
set x-, y- or z-axis title (axis=1, 2, 3 respectively)
void reset(double vett[256])
Definition: TPedValues.cc:11
void setCurrentFolder(const std::string &fullpath)
Definition: DQMStore.cc:237
const double par[8 *NPar][4]