BOSS 8.0.0
BESIII Offline Software System
Loading...
Searching...
No Matches
ResiAlign.cxx
Go to the documentation of this file.
4
5#include "GaudiKernel/Bootstrap.h"
6#include "GaudiKernel/IMessageSvc.h"
7#include "GaudiKernel/INTupleSvc.h"
8#include "GaudiKernel/IService.h"
9#include "GaudiKernel/ISvcLocator.h"
10#include "GaudiKernel/StatusCode.h"
11
12#include "EventModel/Event.h"
13#include "EventModel/EventHeader.h"
14#include "Identifier/Identifier.h"
15#include "Identifier/MdcID.h"
16#include "MdcRawEvent/MdcDigi.h"
17
18#include "TCanvas.h"
19#include "TF1.h"
20
21#include <cstring>
22#include <fstream>
23#include <iomanip>
24#include <iostream>
25#include <string>
26
27using namespace std;
28
30 // m_ndiv = 6;
31 m_ndiv = 12;
32 m_resiCut = 1.0;
33 for ( int i = 0; i < NEP; i++ ) m_npoint[i] = 0;
34
35 for ( int lay = 0; lay < LAYERNMAX; lay++ )
36 {
37 if ( lay < 8 )
38 {
39 m_docaMin[lay] = 0.6; // mm
40 m_docaMax[lay] = 4.8; // mm
41 }
42 else
43 {
44 m_docaMin[lay] = 0.8; // mm
45 m_docaMax[lay] = 6.4; // mm
46 }
47 }
48 for ( int lay = 0; lay < LAYERNMAX; lay++ )
49 {
50 if ( ( 0 == lay ) || ( 7 == lay ) || ( 8 == lay ) || ( 19 == lay ) || ( 20 == lay ) ||
51 ( 35 == lay ) || ( 36 == lay ) || ( 42 == lay ) )
52 m_layBound[lay] = true;
53 else m_layBound[lay] = false;
54 }
55
56 m_ncut1 = 0;
57 m_ncut2 = 0;
58 m_ncut3 = 0;
59 m_ncut4 = 0;
60 m_ncut5 = 0;
61 m_ncut6 = 0;
62 m_ncut7 = 0;
63 m_ncut8 = 0;
64 m_ncut9 = 0;
65 m_ncut10 = 0;
66 m_ncut11 = 0;
67 m_ncut12 = 0;
68 m_ncut13 = 0;
69}
70
72
74 delete m_hresAll;
75 delete m_hresInn;
76 delete m_hresStp;
77 delete m_hresOut;
78 for ( int lay = 0; lay < LAYERNMAX; lay++ )
79 {
80 delete m_hresLay[lay];
81 for ( int i = 0; i < 4; i++ ) delete m_hresLay_LR[lay][i];
82 }
83 for ( int i = 0; i < NEP; i++ ) delete m_gr[i];
84}
85
86void ResiAlign::initialize( TObjArray* hlist, IMdcGeomSvc* mdcGeomSvc,
87 IMdcCalibFunSvc* mdcFunSvc, IMdcUtilitySvc* mdcUtilitySvc ) {
88 IMessageSvc* msgSvc;
89 Gaudi::svcLocator()->service( "MessageSvc", msgSvc );
90 MsgStream log( msgSvc, "ResiAlign" );
91 log << MSG::INFO << "ResiAlign::initialize()" << endmsg;
92
93 m_hlist = hlist;
94 m_mdcGeomSvc = mdcGeomSvc;
95 m_mdcFunSvc = mdcFunSvc;
96 m_mdcUtilitySvc = mdcUtilitySvc;
97
98 double zeast;
99 for ( int lay = 0; lay < 43; lay++ )
100 {
101 zeast = m_mdcGeomSvc->Wire( lay, 0 )->Backward().z();
102 m_zrange[lay][1] = 2.0 * fabs( zeast ) / (double)m_ndiv;
103 m_zrange[lay][0] = -1.0 * m_zrange[lay][1];
104
105 m_radii[lay] = m_mdcGeomSvc->Layer( lay )->Radius();
106 }
107
108 for ( int wir = 0; wir < WIRENMAX; wir++ )
109 {
110 m_xe[wir] = m_mdcGeomSvc->Wire( wir )->Backward().x();
111 m_ye[wir] = m_mdcGeomSvc->Wire( wir )->Backward().y();
112 m_ze[wir] = m_mdcGeomSvc->Wire( wir )->Backward().z();
113 m_xw[wir] = m_mdcGeomSvc->Wire( wir )->Forward().x();
114 m_yw[wir] = m_mdcGeomSvc->Wire( wir )->Forward().y();
115 m_zw[wir] = m_mdcGeomSvc->Wire( wir )->Forward().z();
116 }
117
118 char hname[200];
119 int iEP;
120
121 INTupleSvc* ntupleSvc;
122 Gaudi::svcLocator()->service( "NTupleSvc", ntupleSvc );
123 for ( iEP = 0; iEP <= NEP; iEP++ )
124 {
125 if ( iEP < NEP ) sprintf( hname, "FILE137/align%02d", iEP );
126 else sprintf( hname, "FILE137/alignAll" );
127
128 NTuplePtr nt( ntupleSvc, hname );
129 if ( nt ) m_tuple[iEP] = nt;
130 else
131 {
132 m_tuple[iEP] = ntupleSvc->book( hname, CLID_ColumnWiseTuple, "align" );
133 if ( m_tuple[iEP] )
134 {
135 m_tuple[iEP]->addItem( "run", m_iRun[iEP] );
136 m_tuple[iEP]->addItem( "evt", m_iEvt[iEP] );
137 m_tuple[iEP]->addItem( "resi", m_resi[iEP] );
138 m_tuple[iEP]->addItem( "p", m_p[iEP] );
139 m_tuple[iEP]->addItem( "pt", m_pt[iEP] );
140 m_tuple[iEP]->addItem( "phi", m_phi[iEP] );
141 m_tuple[iEP]->addItem( "lay", m_lay[iEP] );
142 m_tuple[iEP]->addItem( "lr", m_lr[iEP] );
143 m_tuple[iEP]->addItem( "cel", m_cel[iEP] );
144 }
145 else { log << MSG::FATAL << "Cannot book N-tuple:" << long( m_tuple[iEP] ) << endmsg; }
146 }
147 }
148
149 m_hnTrk = new TH1F( "HNtrack", "", 10, -0.5, 9.5 );
150 m_hlist->Add( m_hnTrk );
151
152 m_hnHit = new TH1F( "HNhit", "", 100, -0.5, 99.5 );
153 m_hlist->Add( m_hnHit );
154
155 m_hlayHitmap = new TH1F( "Hitmap", "", 43, -0.5, 42.5 );
156 m_hlist->Add( m_hlayHitmap );
157
158 m_hresAll = new TH1F( "HResAllInc", "", 200, -1.0, 1.0 );
159 m_hlist->Add( m_hresAll );
160
161 m_hresInn = new TH1F( "HResInnInc", "", 200, -1.0, 1.0 );
162 m_hlist->Add( m_hresInn );
163
164 m_hresStp = new TH1F( "HResStpInc", "", 200, -1.0, 1.0 );
165 m_hlist->Add( m_hresStp );
166
167 m_hresOut = new TH1F( "HResOutInc", "", 200, -1.0, 1.0 );
168 m_hlist->Add( m_hresOut );
169
170 int lay;
171 for ( lay = 0; lay < LAYERNMAX; lay++ )
172 {
173 sprintf( hname, "Res_Layer%02d", lay );
174 m_hresLay[lay] = new TH1F( hname, "", 200, -1.0, 1.0 );
175 m_hlist->Add( m_hresLay[lay] );
176
177 for ( int i = 0; i < 4; i++ )
178 {
179 if ( 0 == i ) sprintf( hname, "Resi_Lay%02d_Up_L", lay );
180 else if ( 1 == i ) sprintf( hname, "Resi_Lay%02d_Up_R", lay );
181 else if ( 2 == i ) sprintf( hname, "Resi_Lay%02d_Dw_L", lay );
182 else sprintf( hname, "Resi_Lay%02d_Dw_R", lay );
183 m_hresLay_LR[lay][i] = new TH1F( hname, "", 200, -1.0, 1.0 );
184 m_hlist->Add( m_hresLay_LR[lay][i] );
185 }
186 }
187
188 for ( iEP = 0; iEP < NEP; iEP++ )
189 {
190 m_gr[iEP] = new TGraph();
191 sprintf( hname, "grResi%02d", iEP );
192 m_gr[iEP]->SetName( hname );
193 m_hlist->Add( m_gr[iEP] );
194 }
195 m_fevt.open( "evt.txt" );
196}
197
199 IMessageSvc* msgSvc;
200 Gaudi::svcLocator()->service( "MessageSvc", msgSvc );
201 MsgStream log( msgSvc, "ResiAlign" );
202 log << MSG::DEBUG << "ResiAlign::fillHist()" << endmsg;
203
204 bool esCutFg = event->getEsCutFlag();
205 if ( !esCutFg )
206 {
207 m_ncut1++;
208 return true;
209 }
210
211 int i = 0;
212 int k;
213
214 int trkStat;
215 double dr;
216 double phi0;
217 double kappa;
218 double dz;
219 double tanl;
220 double chisq;
221 double p;
222 double pt;
223
224 int nhits;
225 int lay;
226 int cel;
227 int wir;
228 int lr;
229 int iEnd;
230 int iEP;
231
232 double doca;
233 double resi;
234 double zhit;
235 double wphi;
236 double dphi;
237 double hitPhi;
238 double xx;
239 double yy;
240 double rr;
241 int stat;
242 MdcAliRecTrk* rectrk;
243 MdcAliRecHit* rechit;
244 int nhitlay;
245 bool fgHitLay[LAYERNMAX];
246
247 IDataProviderSvc* eventSvc = NULL;
248 Gaudi::svcLocator()->service( "EventDataSvc", eventSvc );
249 SmartDataPtr<Event::EventHeader> eventHeader( eventSvc, "/Event/EventHeader" );
250 if ( !eventHeader )
251 {
252 log << MSG::FATAL << "Could not find Event Header" << endmsg;
253 m_ncut3++;
254 return ( StatusCode::FAILURE );
255 }
256 int iEvt = eventHeader->eventNumber();
257 int iRun = eventHeader->runNumber();
258
259 int nTrk = event->getNTrk();
260 m_hnTrk->Fill( nTrk );
261 m_fevt << setw( 10 ) << iRun << setw( 10 ) << iEvt << setw( 10 ) << nTrk << endl;
262 if ( ( nTrk < m_param.nTrkCut[0] ) || ( nTrk > m_param.nTrkCut[1] ) )
263 {
264 m_ncut2++;
265 return true;
266 }
267
268 for ( i = 0; i < nTrk; i++ )
269 {
270 rectrk = event->getRecTrk( i );
271 nhits = rectrk->getNHits();
272 trkStat = rectrk->getStat();
273 // if (0 != trkStat) continue;
274
275 // dr cut
276 dr = rectrk->getDr();
277 if ( fabs( dr ) > m_param.drCut )
278 {
279 m_ncut4++;
280 continue;
281 }
282
283 phi0 = rectrk->getPhi0();
284 kappa = rectrk->getKappa();
285
286 // dz cut
287 dz = rectrk->getDz();
288 if ( fabs( dz ) > m_param.dzCut )
289 {
290 m_ncut5++;
291 continue;
292 }
293
294 for ( lay = 0; lay < LAYERNMAX; lay++ ) { fgHitLay[lay] = false; }
295
296 m_hnHit->Fill( nhits );
297 for ( k = 0; k < nhits; k++ )
298 {
299 rechit = rectrk->getRecHit( k );
300 lay = rechit->getLayid();
301 fgHitLay[lay] = true;
302 }
303
304 nhitlay = 0;
305 for ( lay = 0; lay < LAYERNMAX; lay++ )
306 {
307 if ( fgHitLay[lay] ) nhitlay++;
308 }
309 // cout << "hitlay " << nhitlay << endl;
310 if ( nhitlay < m_param.nHitLayCut )
311 {
312 m_ncut6++;
313 continue;
314 }
315
316 tanl = rectrk->getTanLamda();
317 chisq = rectrk->getChisq();
318 p = rectrk->getP();
319 pt = rectrk->getPt();
320
321 if ( ( fabs( pt ) < m_param.ptCut[0] ) || ( fabs( pt ) > m_param.ptCut[1] ) )
322 {
323 m_ncut7++;
324 continue;
325 }
326
327 HepVector helix = rectrk->getHelix();
328 for ( k = 0; k < nhits; k++ )
329 {
330 rechit = rectrk->getRecHit( k );
331 lay = rechit->getLayid();
332 cel = rechit->getCellid();
333 lr = rechit->getLR();
334 doca = rechit->getDocaInc();
335 zhit = rechit->getZhit();
336
337 stat = rechit->getStat();
338 if ( ( 1 == m_param.hitStatCut ) && ( 1 != stat ) )
339 {
340 m_ncut8++;
341 continue;
342 }
343
344 if ( 1 == m_param.resiType ) { resi = rechit->getResiExcLR(); }
345 else { resi = rechit->getResiIncLR(); }
346 resi *= -1.0;
347 // maqm if( (1==isnan(resi)) || (fabs(resi) > m_resiCut) ||
348 if ( ( 1 == std::isnan( resi ) ) || ( fabs( resi ) > m_resiCut ) ||
349 ( fabs( doca ) > m_docaMax[lay] ) || ( fabs( doca ) < m_docaMin[lay] ) )
350 {
351 m_ncut9++;
352 continue;
353 }
354
355 if ( m_param.fgAdjacLayerCut )
356 {
357 if ( 0 == lay )
358 {
359 if ( !fgHitLay[1] )
360 {
361 m_ncut10++;
362 continue;
363 }
364 }
365 else if ( 42 == lay )
366 {
367 if ( !fgHitLay[41] )
368 {
369 m_ncut11++;
370 continue;
371 }
372 }
373 else
374 {
375 if ( ( !fgHitLay[lay - 1] ) && ( !fgHitLay[lay + 1] ) )
376 {
377 m_ncut12++;
378 continue;
379 }
380 // for boundary layers
381 if ( m_param.fgBoundLayerCut && m_layBound[lay] &&
382 ( ( !fgHitLay[lay - 1] ) || ( !fgHitLay[lay + 1] ) ) )
383 {
384 m_ncut13++;
385 continue;
386 }
387 }
388 }
389 m_hlayHitmap->Fill( lay );
390
391 // fill alignment trees
392 if ( ( zhit < m_zrange[lay][0] ) || ( zhit > m_zrange[lay][1] ) )
393 {
394 wir = m_mdcGeomSvc->Wire( lay, cel )->Id();
395 xx = ( zhit - m_zw[wir] ) * ( m_xe[wir] - m_xw[wir] ) / ( m_ze[wir] - m_zw[wir] ) +
396 m_xw[wir];
397 yy = ( zhit - m_zw[wir] ) * ( m_ye[wir] - m_yw[wir] ) / ( m_ze[wir] - m_zw[wir] ) +
398 m_yw[wir];
399 rr = sqrt( ( xx * xx ) + ( yy * yy ) );
400 dphi = fabs( doca ) / m_radii[lay];
401
402 if ( yy >= 0 ) wphi = acos( xx / rr );
403 else wphi = PI2 - acos( xx / rr );
404 if ( 1 == lr ) hitPhi = wphi + dphi;
405 else hitPhi = wphi - dphi;
406 if ( hitPhi < 0 ) hitPhi += PI2;
407 else if ( hitPhi > PI2 ) hitPhi -= PI2;
408
409 if ( zhit < m_zrange[lay][0] ) iEnd = 0; // west
410 else iEnd = 1; // east
411 iEP = Alignment::getEpId( lay, iEnd );
412
413 m_iRun[iEP] = iRun;
414 m_iEvt[iEP] = iEvt;
415 m_resi[iEP] = resi;
416 m_p[iEP] = p;
417 m_pt[iEP] = pt;
418 m_phi[iEP] = hitPhi;
419 m_lay[iEP] = lay;
420 m_lr[iEP] = lr;
421 m_cel[iEP] = cel;
422 m_tuple[iEP]->write();
423
424 m_resi[NEP] = resi;
425 m_p[NEP] = p;
426 m_pt[NEP] = pt;
427 m_phi[NEP] = hitPhi;
428 m_lay[NEP] = lay;
429 m_lr[NEP] = lr;
430 m_cel[NEP] = cel;
431 m_tuple[NEP]->write();
432
433 m_hresAll->Fill( resi );
434 if ( lay < 8 ) m_hresInn->Fill( resi );
435 else if ( lay < 20 ) m_hresStp->Fill( resi );
436 else m_hresOut->Fill( resi );
437 m_hresLay[lay]->Fill( resi );
438 if ( 0 == lr )
439 {
440 if ( hitPhi < 3.14 ) m_hresLay_LR[lay][0]->Fill( resi ); // up L
441 else m_hresLay_LR[lay][2]->Fill( resi ); // down L
442 }
443 else
444 {
445 if ( hitPhi < 3.14 ) m_hresLay_LR[lay][1]->Fill( resi ); // up R
446 else m_hresLay_LR[lay][3]->Fill( resi ); // down R
447 }
448
449 m_gr[iEP]->SetPoint( m_npoint[iEP], hitPhi, resi );
450 m_npoint[iEP]++;
451 }
452 }
453 }
454
455 return true;
456}
457
459 IMessageSvc* msgSvc;
460 Gaudi::svcLocator()->service( "MessageSvc", msgSvc );
461 MsgStream log( msgSvc, "ResiAlign" );
462 log << MSG::INFO << "ResiAlign::updateConst()" << endmsg;
463 m_fevt.close();
464
465 int iEP;
466 double par[3];
467 double err[3];
468 double dx;
469 double dy;
470 double rz;
471 double rLayer[] = { 120.225, 205.0, 237.55, 270.175, 302.625, 334.775, 366.65, 500.0,
472 120.225, 205.0, 237.55, 270.175, 302.625, 334.775, 366.65, 500.0 };
473
474 TCanvas c1( "c1", "c1", 10, 10, 700, 500 );
475
476 TF1* fResPhi = new TF1( "fResPhi", funResi, 0, PI2, 3 );
477 fResPhi->SetParameter( 0, 0.0 );
478 fResPhi->SetParameter( 1, 0.0 );
479 fResPhi->SetParameter( 2, 0.0 );
480
481 for ( iEP = 0; iEP < NEP; iEP++ )
482 {
483 if ( ( m_gr[iEP]->GetN() ) > 500 )
484 {
485 m_gr[iEP]->Fit( "fResPhi", "V" );
486 par[0] = fResPhi->GetParameter( 0 );
487 par[1] = fResPhi->GetParameter( 1 );
488 par[2] = fResPhi->GetParameter( 2 );
489
490 err[0] = fResPhi->GetParError( 0 );
491 err[1] = fResPhi->GetParError( 1 );
492 err[2] = fResPhi->GetParError( 2 );
493
494 dx = -1.0 * par[1];
495 dy = par[2];
496 rz = par[0] / rLayer[iEP];
497
498 // assume the shift of the outer section is 0
499 if ( 7 == iEP || 15 == iEP )
500 {
501 dx = 0.0;
502 dy = 0.0;
503 rz = 0.0;
504 par[0] = 0.0;
505 par[1] = 0.0;
506 par[2] = 0.0;
507 }
508 alignPar->setDelDx( iEP, dx );
509 alignPar->setDelDy( iEP, dy );
510 alignPar->setDelRz( iEP, rz );
511
512 alignPar->setErrDx( iEP, err[1] );
513 alignPar->setErrDy( iEP, err[2] );
514 alignPar->setErrRz( iEP, err[0] / rLayer[iEP] );
515 }
516 }
517
518 cout << "TrackCut: cut1: " << m_ncut1 << ", cut2: " << m_ncut2 << ", cut3: " << m_ncut3
519 << ", cut4: " << m_ncut4 << ", cut5: " << m_ncut5 << ", cut6: " << m_ncut6
520 << ", cut7: " << m_ncut7 << endl;
521 cout << "HitCut: cut8: " << m_ncut8 << ", cut9: " << m_ncut9 << ", cut10: " << m_ncut10
522 << ", cut11: " << m_ncut11 << ", cut12: " << m_ncut12 << ", cut13: " << m_ncut13
523 << endl;
524
525 delete fResPhi;
526}
527
528Double_t ResiAlign::funResi( Double_t* x, Double_t* par ) {
529 Double_t val;
530 val = par[0] + par[1] * sin( x[0] ) + par[2] * cos( x[0] );
531 return val;
532}
sprintf(cut, "kal_costheta0_em>-0.93&&kal_costheta0_em<0.93&&kal_pxy0_em>=0.05+%d*0.1&&kal_" "pxy0_em<0.15+%d*0.1&&NGch>=2", j, j)
IMessageSvc * msgSvc()
int getCellid() const
int getLR() const
double getResiExcLR() const
int getLayid() const
double getZhit() const
int getStat() const
double getResiIncLR() const
double getDocaInc() const
double getPt() const
double getDz() const
double getDr() const
double getChisq() const
MdcAliRecHit * getRecHit(int index) const
double getKappa() const
int getStat() const
double getP() const
int getNHits() const
double getPhi0() const
HepVector getHelix() const
double getTanLamda() const
void setErrRz(int iEP, double val)
void setDelDy(int iEP, double val)
void setDelRz(int iEP, double val)
void setErrDx(int iEP, double val)
void setDelDx(int iEP, double val)
void setErrDy(int iEP, double val)
static Double_t funResi(double *x, double *par)
void initialize(TObjArray *hlist, IMdcGeomSvc *mdcGeomSvc, IMdcCalibFunSvc *mdcFunSvc, IMdcUtilitySvc *mdcUtilitySvc)
Definition ResiAlign.cxx:86
bool fillHist(MdcAliEvent *event)
void clear()
Definition ResiAlign.cxx:73
void updateConst(MdcAlignPar *alignPar)
const double PI2
Definition Alignment.h:39
const int LAYERNMAX
Definition Alignment.h:43
const int NEP
Definition Alignment.h:46
int getEpId(int lay, int iEnd)
Definition Alignment.cxx:18
const int WIRENMAX
Definition Alignment.h:42