BOSS 7.0.1
BESIII Offline Software System
Loading...
Searching...
No Matches
HoughValidUpdate.cxx
Go to the documentation of this file.
1#include "MdcHoughFinder/HoughValidUpdate.h"
2#include "GaudiKernel/MsgStream.h"
3#include "GaudiKernel/AlgFactory.h"
4#include "GaudiKernel/ISvcLocator.h"
5#include "GaudiKernel/SmartDataPtr.h"
6#include "GaudiKernel/IDataProviderSvc.h"
7#include "GaudiKernel/PropertyMgr.h"
8#include "GaudiKernel/IService.h"
9#include "EventModel/EventHeader.h"
10#include "McTruth/DecayMode.h"
11#include "McTruth/McParticle.h"
12#include "MdcRawEvent/MdcDigi.h"
13#include "Identifier/Identifier.h"
14#include "Identifier/MdcID.h"
15
16
17#include "EvTimeEvent/RecEsTime.h"
18#include "RawDataProviderSvc/MdcRawDataProvider.h"
19#include "McTruth/MdcMcHit.h"
20#include "MdcRecEvent/RecMdcTrack.h"
21#include "MdcRecEvent/RecMdcHit.h"
22#include "TrkBase/TrkFit.h"
23#include "TrkBase/TrkHitList.h"
24#include "TrkBase/TrkExchangePar.h"
25#include "TrkFitter/TrkHelixMaker.h"
26#include "TrkFitter/TrkCircleMaker.h"
27#include "TrkFitter/TrkLineMaker.h"
28#include "TrkFitter/TrkHelixFitter.h"
29#include "MdcxReco/Mdcxprobab.h"
30#include "MdcData/MdcHit.h"
31#include "MdcData/MdcRecoHitOnTrack.h"
32#include "MdcTrkRecon/MdcTrack.h"
33#include "MdcGeom/EntranceAngle.h"
34#include "TrackUtil/Helix.h"
35#include "GaudiKernel/IPartPropSvc.h"
36#include "MdcRecoUtil/Pdt.h"
37#include "RawEvent/RawDataUtil.h"
38#include "TrkBase/TrkErrCode.h"
39#include "MdcPrintSvc/MdcPrintSvc.h"
40
41#include "TTree.h"
42#include "TF1.h"
43#include "TGraph.h"
44#include "iomanip"
45
46using namespace std;
47using namespace Event;
48
49/////////////////////////////////////////////////////////////////////////////
50
51HoughValidUpdate::HoughValidUpdate(const std::string& name, ISvcLocator* pSvcLocator) :
52 Algorithm(name, pSvcLocator)
53{
54 // Declare the properties
55 declareProperty("drawTuple", m_drawTuple=0);
56 declareProperty("useMcInfo", m_useMcInfo=0);
57 declareProperty("method", m_method=0);
58 declareProperty("debug", m_debug = 0);
59 declareProperty("data", m_data= 0);
60 declareProperty("binx", m_binx= 100);
61 declareProperty("biny", m_biny= 200);
62 declareProperty("maxrho", m_maxrho= 0.3);
63 declareProperty("peakCellNumX", m_peakCellNumX);
64 declareProperty("peakCellNumY", m_peakCellNumY);
65 declareProperty("ndev1", m_ndev1= 1);
66 declareProperty("ndev2", m_ndev2= 5);
67 declareProperty("f_hit_pro", m_hit_pro= 0.4);
68 declareProperty("pdtFile", m_pdtFile = "pdt.table");
69 declareProperty("pickHits", m_pickHits = true); // ??
70 declareProperty("pid", m_pid = 0);
71 declareProperty("combineTracking",m_combineTracking = false);
72 declareProperty("getDigiFlag", m_getDigiFlag = 0);
73 declareProperty("maxMdcDigi", m_maxMdcDigi = 0);
74 declareProperty("keepBadTdc", m_keepBadTdc = 0);
75 declareProperty("dropHot", m_dropHot= 0);
76 declareProperty("keepUnmatch", m_keepUnmatch = 0);
77 declareProperty("minMdcDigi", m_minMdcDigi = 0);
78 declareProperty("setnpeak_fit", m_setpeak_fit= -1);
79 declareProperty("Q", m_q= 0);
80 declareProperty("eventcut", m_eventcut= -1);
81 declareProperty("rhocut", m_rhocut= -1.);
82 declareProperty("mcpar", m_mcpar= 0); //if use par truth
83 declareProperty("fillTruth", m_fillTruth= 0);
84 declareProperty("removeBadTrack", m_removeBadTrack= false);
85 declareProperty("dropTrkDrCut", m_dropTrkDrCut= 1.);
86 declareProperty("dropTrkDzCut", m_dropTrkDzCut= 10.);
87 declareProperty("dropTrkPtCut", m_dropTrkPtCut= 99999.);
88 declareProperty("dropTrkChi2Cut", m_dropTrkChi2Cut = 99999.);
89 declareProperty("dropTrkChi2NdfCut", m_dropTrkChi2NdfCut = 99999.);
90 declareProperty("qualityFactor", m_qualityFactor= 3.);
91 declareProperty("dropTrkNhitCut", m_dropTrkNhitCut= 9);
92}
93
95
96 //Initailize MdcDetector
97 m_gm = MdcDetector::instance(0);
98 if(NULL == m_gm) return StatusCode::FAILURE;
99
100 return StatusCode::SUCCESS;
101}
102
103// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
105
106 MsgStream log(msgSvc(), name());
107 log << MSG::INFO << "in initialize()" << endreq;
108
109 StatusCode sc;
110 //for(int i=0;i<43;i++) TrkHelixFitter::nSigmaCut[i] = m_helixHitsSigma[i];
111
112 IPartPropSvc* p_PartPropSvc;
113 static const bool CREATEIFNOTTHERE(true);
114 sc = service("PartPropSvc", p_PartPropSvc, CREATEIFNOTTHERE);
115 if (!sc.isSuccess() || 0 == p_PartPropSvc) {
116 log << MSG::ERROR << " Could not initialize PartPropSvc" << endreq;
117 return sc;
118 }
119 m_particleTable = p_PartPropSvc->PDT();
120
121 // RawData
122 IRawDataProviderSvc* irawDataProviderSvc;
123 sc = service ("RawDataProviderSvc", irawDataProviderSvc);
124 m_rawDataProviderSvc = dynamic_cast<RawDataProviderSvc*> (irawDataProviderSvc);
125 if ( sc.isFailure() ){
126 log << MSG::FATAL << name()<<" Could not load RawDataProviderSvc!" << endreq;
127 return StatusCode::FAILURE;
128
129 }
130
131 // Geometry
132 IMdcGeomSvc* imdcGeomSvc;
133 sc = service ("MdcGeomSvc", imdcGeomSvc);
134 m_mdcGeomSvc = dynamic_cast<MdcGeomSvc*> (imdcGeomSvc);
135 if ( sc.isFailure() ){
136 log << MSG::FATAL << "Could not load MdcGeoSvc!" << endreq;
137 return StatusCode::FAILURE;
138 }
139
140 // MdcPrintSvc
141 IMdcPrintSvc* iMdcPrintSvc;
142 sc = service ("MdcPrintSvc", iMdcPrintSvc);
143 m_mdcPrintSvc= dynamic_cast<MdcPrintSvc*> (iMdcPrintSvc);
144 if ( sc.isFailure() ){
145 log << MSG::FATAL << "Could not load MdcPrintSvc!" << endreq;
146 return StatusCode::FAILURE;
147 }
148
149 sc = service ("MagneticFieldSvc",m_pIMF);
150 if(sc!=StatusCode::SUCCESS) {
151 log << MSG::ERROR << "Unable to open Magnetic field service"<<endreq;
152 }
153 m_bfield = new BField(m_pIMF);
154 log << MSG::INFO << "field z = "<<m_bfield->bFieldNominal()<< endreq;
155
156 m_context = new TrkContextEv(m_bfield);
157
158 Pdt::readMCppTable(m_pdtFile);
159
160 //Get MdcCalibFunSvc
161 IMdcCalibFunSvc* imdcCalibSvc;
162 sc = service ("MdcCalibFunSvc", imdcCalibSvc);
163 m_mdcCalibFunSvc = dynamic_cast<MdcCalibFunSvc*>(imdcCalibSvc);
164 if ( sc.isFailure() ){
165 log << MSG::FATAL << "Could not load MdcCalibFunSvc!" << endreq;
166 return StatusCode::FAILURE;
167 }
168
169 //initialize ntuplehit
170 if(m_drawTuple){
171 NTuplePtr nt1(ntupleSvc(), "HoughValidUpdate/hit");
172 if ( nt1 ){
173 ntuplehit = nt1;
174 } else {
175 ntuplehit = ntupleSvc()->book("HoughValidUpdate/hit", CLID_ColumnWiseTuple, "hit");
176 if(ntuplehit){
177 ntuplehit->addItem("eventNum", m_eventNum);
178 ntuplehit->addItem("runNum", m_runNum);
179 ntuplehit->addItem("nHitMc", m_nHit_Mc,0, 6796);
180 ntuplehit->addItem("nHitAxialMc", m_nHitAxial_Mc);
181 ntuplehit->addItem("layerMc", m_nHit_Mc, m_layer_Mc);
182 ntuplehit->addItem("cellMc", m_nHit_Mc, m_cell_Mc);
183
184 ntuplehit->addItem("nTrkMC", m_nTrkMC,0,10);
185 ntuplehit->addItem("pidTruth", m_nTrkMC,m_pidTruth);
186 ntuplehit->addItem("costaTruth", m_nTrkMC,m_costaTruth);
187 ntuplehit->addItem("ptTruth", m_nTrkMC,m_ptTruth);
188 ntuplehit->addItem("pzTruth", m_nTrkMC,m_pzTruth);
189 ntuplehit->addItem("pTruth", m_nTrkMC,m_pTruth);
190 ntuplehit->addItem("qTruth", m_nTrkMC,m_qTruth);
191 ntuplehit->addItem("drTruth", m_nTrkMC,m_drTruth);
192 ntuplehit->addItem("phiTruth", m_nTrkMC,m_phi0Truth);
193 ntuplehit->addItem("omegaTruth", m_nTrkMC,m_omegaTruth);
194 ntuplehit->addItem("dzTruth", m_nTrkMC,m_dzTruth);
195 ntuplehit->addItem("tanlTruth", m_nTrkMC,m_tanl_mc);
196
197 ntuplehit->addItem("nHit", m_nHit,0, 6796);
198 ntuplehit->addItem("nHitDigi", m_nHitDigi);
199 ntuplehit->addItem("nHitAxial", m_nHitAxial);
200 ntuplehit->addItem("nHitStereo", m_nHitStereo);
201 ntuplehit->addItem("layer", m_nHit, m_layer);
202 ntuplehit->addItem("cell", m_nHit, m_cell);
203 ntuplehit->addItem("x_east", m_nHit, m_x_east);
204 ntuplehit->addItem("y_east", m_nHit, m_y_east);
205 ntuplehit->addItem("z_east", m_nHit, m_z_east);
206 ntuplehit->addItem("x_west", m_nHit, m_x_west);
207 ntuplehit->addItem("y_west", m_nHit, m_y_west);
208 ntuplehit->addItem("z_west", m_nHit, m_z_west);
209 ntuplehit->addItem("x", m_nHit, m_x);
210 ntuplehit->addItem("y", m_nHit, m_y);
211 ntuplehit->addItem("z", m_nHit, m_z);
212 ntuplehit->addItem("u", m_nHit, m_u);
213 ntuplehit->addItem("v", m_nHit, m_v);
214 ntuplehit->addItem("p", m_nHit, m_p);
215 ntuplehit->addItem("slant", m_nHit, m_slant);
216 ntuplehit->addItem("layerNhit", 43, m_layerNhit);
217 ntuplehit->addItem("layerMax", m_layer_max);
218 ntuplehit->addItem("l_truth", m_nHit, m_ltruth);
219 ntuplehit->addItem("z_truth", m_nHit, m_ztruth);
220 ntuplehit->addItem("z_postruth", m_nHit, m_postruth);
221 ntuplehit->addItem("digi_truth", m_nHit, m_digi_truth);
222 ntuplehit->addItem("e_delta", m_e_delta);
223 //hough map
224 ntuplehit->addItem("nCross", m_nCross, 0, 125000);
225 ntuplehit->addItem("rho", m_nCross, m_rho);
226 ntuplehit->addItem("theta", m_nCross, m_theta);
227 ntuplehit->addItem("xybinNum", m_xybinNum, 0,100000);
228 ntuplehit->addItem("xybin", m_xybinNum, m_xybin);
229 ntuplehit->addItem("xsigma", m_xsigma);
230 ntuplehit->addItem("ysigma", m_ysigma);
231
232 ntuplehit->addItem("npeak_1", m_npeak_1);
233 ntuplehit->addItem("npeak_2", m_npeak_2);
234 ntuplehit->addItem("trackFailure", m_trackFailure);
235 ntuplehit->addItem("npeak_after_tracking", m_npeak_after_tracking, 0,1000);
236 ntuplehit->addItem("nHitSelect", m_npeak_after_tracking, m_nHitSelect);
237 ntuplehit->addItem("nHitAxialSelect", m_npeak_after_tracking, m_nHitAxialSelect);
238 ntuplehit->addItem("nHitStereoSelect", m_npeak_after_tracking, m_nHitStereoSelect);
239 ntuplehit->addItem("naxiallose", m_npeak_after_tracking, m_axiallose);
240 ntuplehit->addItem("nstereolose", m_npeak_after_tracking, m_stereolose);
241
242 ntuplehit->addItem("trackNum_Mc", m_trackNum_Mc, 0,100);
243 ntuplehit->addItem("trackNum", m_trackNum, 0,100);
244 ntuplehit->addItem("trackNum_fit", m_trackNum_fit, 0,100);
245 ntuplehit->addItem("trackNum_tds", m_trackNum_tds, 0,100);
246 ntuplehit->addItem("circleCenterX", m_trackNum, m_x_circle);
247 ntuplehit->addItem("circleCenterY", m_trackNum, m_y_circle);
248 ntuplehit->addItem("circleR", m_trackNum, m_r);
249 ntuplehit->addItem("chis_pt", m_trackNum, m_chis_pt);
250 ntuplehit->addItem("pt_rec", m_trackNum, m_pt);
251
252 ntuplehit->addItem("nHitStereo_use",m_nHitStereo_use,0,1000);
253 ntuplehit->addItem("l_Calcu_Left", m_nHitStereo_use, m_lCalcuLeft);
254 ntuplehit->addItem("l_Calcu_Right", m_nHitStereo_use, m_lCalcuRight);
255 ntuplehit->addItem("z_Calcu_Left", m_nHitStereo_use, m_zCalcuLeft);
256 ntuplehit->addItem("z_Calcu_Right", m_nHitStereo_use, m_zCalcuRight);
257 ntuplehit->addItem("z_Calcu", m_nHitStereo_use, m_zCalcu);
258 ntuplehit->addItem("l_Calcu", m_nHitStereo_use, m_lCalcu);
259 ntuplehit->addItem("z_delta", m_nHitStereo_use, m_delta_z);
260 ntuplehit->addItem("l_delta", m_nHitStereo_use, m_delta_l);
261 ntuplehit->addItem("amb", m_nHitStereo_use, m_amb);
262 ntuplehit->addItem("amb_no_match", m_ambig_no_match);
263 ntuplehit->addItem("amb_no_match_pro", m_ambig_no_match_propotion);
264 ntuplehit->addItem("tanl_Cal", m_tanl_Cal);
265 ntuplehit->addItem("z0_Cal", m_z0_Cal);
266
267 ntuplehit->addItem("pt2_rec_final", m_trackNum_fit, m_pt2_rec_final);
268 ntuplehit->addItem("p_rec_final", m_trackNum_fit, m_p_rec_final);
269 ntuplehit->addItem("d0_final", m_trackNum_fit, m_d0_final);
270 ntuplehit->addItem("phi0_final", m_trackNum_fit, m_phi0_final);
271 ntuplehit->addItem("omega_final", m_trackNum_fit, m_omega_final);
272 ntuplehit->addItem("z0_final", m_trackNum_fit, m_z0_final);
273 ntuplehit->addItem("tanl_final", m_trackNum_fit, m_tanl_final);
274 ntuplehit->addItem("chis_3d_final", m_trackNum_fit, m_chis_3d_final);
275 ntuplehit->addItem("Q_final", m_trackNum_fit, m_Q);
276
277 ntuplehit->addItem("pt2_rec_tds", m_trackNum_tds, m_pt2_rec_tds);
278 ntuplehit->addItem("p_rec_tds", m_trackNum_tds, m_p_rec_tds);
279 ntuplehit->addItem("d0_tds", m_trackNum_tds, m_d0_tds);
280 ntuplehit->addItem("phi0_tds", m_trackNum_tds, m_phi0_tds);
281 ntuplehit->addItem("omega_tds", m_trackNum_tds, m_omega_tds);
282 ntuplehit->addItem("z0_tds", m_trackNum_tds, m_z0_tds);
283 ntuplehit->addItem("tanl_tds", m_trackNum_tds, m_tanl_tds);
284 ntuplehit->addItem("Q_tds", m_trackNum_tds, m_Q_tds);
285
286
287 ntuplehit->addItem("nhitdis0", m_nhitdis0, 0,2000);
288 ntuplehit->addItem("nhitdis1", m_nhitdis1, 0,2000);
289 ntuplehit->addItem("nhitdis2", m_nhitdis2, 0,2000);
290 ntuplehit->addItem("hitdis0", m_nhitdis0, m_hit_dis0);
291 ntuplehit->addItem("hitdis1", m_nhitdis1, m_hit_dis1);
292 ntuplehit->addItem("hitdis2", m_nhitdis2, m_hit_dis2);
293 ntuplehit->addItem("hitdis1_3d", m_nhitdis1, m_hit_dis1_3d);
294 ntuplehit->addItem("hitdis2_3d", m_nhitdis2, m_hit_dis2_3d);
295 ntuplehit->addItem("track_use_intrk1", m_track_use_intrk1);
296 ntuplehit->addItem("noise_intrk1", m_noise_intrk1);
297 ntuplehit->addItem("decay", m_decay);
298 ntuplehit->addItem("outputTrk", m_outputTrk, 0,100);
299 ntuplehit->addItem("hitOnTrk", m_outputTrk, m_hitOnTrk);
300
301
302 } else { log << MSG::ERROR << "Cannot book tuple HoughValidUpdate/hough" << endmsg;
303 return StatusCode::FAILURE;
304 }
305 }
306 }
307
308 if(m_debug>2) TrkHelixFitter::m_debug = true;
309
310 return StatusCode::SUCCESS;
311
312
313}
314
315
316// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
318
319 cout.precision(6);
320 MsgStream log(msgSvc(), name());
321 log << MSG::INFO << "in execute()" << endreq;
322
323 //event start time
324 SmartDataPtr<RecEsTimeCol> evTimeCol(eventSvc(),"/Event/Recon/RecEsTimeCol");
325 if (!evTimeCol) {
326 log << MSG::WARNING<< "Could not retrieve RecEsTimeCol , use t0=0" << endreq;
327 m_bunchT0=0.;
328 }else{
329 RecEsTimeCol::iterator iter_evt = evTimeCol->begin();
330 if (iter_evt != evTimeCol->end()){
331 m_bunchT0 = (*iter_evt)->getTest()*1.e-9;//m_bunchT0-s, getTest-ns
332 }
333 }
334
335 // Get the event header, print out event and run number
336 SmartDataPtr<Event::EventHeader> eventHeader(eventSvc(),"/Event/EventHeader");
337 if (!eventHeader) {
338 log << MSG::FATAL << "Could not find Event Header" << endreq;
339 return( StatusCode::FAILURE);
340 }
341
342 DataObject *aTrackCol;
343 DataObject *aRecHitCol;
344 SmartIF<IDataManagerSvc> dataManSvc(eventSvc());
345 if(!m_combineTracking){
346 eventSvc()->findObject("/Event/Recon/RecMdcTrackCol",aTrackCol);
347 if(aTrackCol != NULL) {
348 dataManSvc->clearSubTree("/Event/Recon/RecMdcTrackCol");
349 eventSvc()->unregisterObject("/Event/Recon/RecMdcTrackCol");
350 }
351 eventSvc()->findObject("/Event/Recon/RecMdcHitCol",aRecHitCol);
352 if(aRecHitCol != NULL) {
353 dataManSvc->clearSubTree("/Event/Recon/RecMdcHitCol");
354 eventSvc()->unregisterObject("/Event/Recon/RecMdcHitCol");
355 }
356 }
357
358 RecMdcTrackCol* trackList;
359 eventSvc()->findObject("/Event/Recon/RecMdcTrackCol",aTrackCol);
360 if (aTrackCol) {
361 trackList = dynamic_cast<RecMdcTrackCol*> (aTrackCol);
362 }else{
363 trackList = new RecMdcTrackCol;
364 StatusCode sc = eventSvc()->registerObject(EventModel::Recon::RecMdcTrackCol, trackList);
365 if(!sc.isSuccess()) {
366 log << MSG::FATAL << " Could not register RecMdcTrack collection" <<endreq;
367 return StatusCode::FAILURE;
368 }
369 }
370
371 RecMdcHitCol* hitList;
372 eventSvc()->findObject("/Event/Recon/RecMdcHitCol",aRecHitCol);
373 if (aRecHitCol) {
374 hitList = dynamic_cast<RecMdcHitCol*> (aRecHitCol);
375 }else{
376 hitList = new RecMdcHitCol;
377 StatusCode sc = eventSvc()->registerObject(EventModel::Recon::RecMdcHitCol, hitList);
378 if(!sc.isSuccess()) {
379 log << MSG::FATAL << " Could not register RecMdcHit collection" <<endreq;
380 return StatusCode::FAILURE;
381 }
382 }
383
384 //remove bad quality or low pt track(s)
385 if(m_removeBadTrack) {
386 vector<RecMdcTrack*> vec_trackList;
387 if( m_debug>0 ) cout<<"PATTSF collect "<<trackList->size()<<" track. "<<endl;
388 if(trackList->size()!=0){
389 RecMdcTrackCol::iterator iter_pat = trackList->begin();
390 for(;iter_pat!=trackList->end();iter_pat++){
391 double d0=(*iter_pat)->helix(0);
392 double kap = (*iter_pat)->helix(2);
393 double pt = 0.00001;
394 if(fabs(kap)>0.00001) pt = fabs(1./kap);
395 double dz=(*iter_pat)->helix(4);
396 double chi2=(*iter_pat)->chi2();
397 if( m_debug>0) cout<<"d0: "<<d0<<" z0: "<<dz<<" pt "<<pt<<" chi2 "<<chi2<<endl;
398 if(fabs(d0)>m_dropTrkDrCut || fabs(dz)>m_dropTrkDzCut || chi2>m_dropTrkChi2Cut || pt>m_dropTrkPtCut) {
399 vec_trackList.push_back(*iter_pat);
400 //remove hits on track
401 HitRefVec rechits = (*iter_pat)->getVecHits();
402 HitRefVec::iterator hotIter = rechits.begin();
403 while (hotIter!=rechits.end()) {
404 if(m_debug>0) cout <<"remove hit " << (*hotIter)->getId() <<endl;
405 hitList->remove(*hotIter);
406 hotIter++;
407 }
408 trackList->remove(*iter_pat);
409 iter_pat--;
410 }
411 // if( m_debug>0 ) cout<<"after delete trackcol size : "<<trackList->size()<<endl;
412 }
413 } else {
414 if(m_debug>0) cout<<" PATTSF find 0 track. "<<endl;
415 }
416 }//end of remove bad quality high pt track
417
418 int nTdsTk=trackList->size();
419
420
421 t_eventNum=eventHeader->eventNumber();
422 t_runNum=eventHeader->runNumber();
423 if( m_drawTuple ){
424 m_eventNum=t_eventNum;
425 m_runNum=t_runNum;
426 }
427 if(m_debug>0) cout<<" begin event :"<<t_eventNum<<endl;
428 if(t_eventNum<=m_eventcut) {
429 cout<<" eventNum cut "<<t_eventNum<<endl;
430 return StatusCode::SUCCESS;
431 }
432 log << MSG::INFO << "HoughValidUpdate: retrieved event: " << eventHeader->eventNumber() << " run: " << eventHeader->runNumber() << endreq;
433
434 vector<double> vec_u;
435 vector<double> vec_v;
436 vector<double> vec_p;
437 vector<double> vec_x_east;
438 vector<double> vec_y_east;
439 vector<double> vec_z_east;
440 vector<double> vec_x_west;
441 vector<double> vec_y_west;
442 vector<double> vec_z_west;
443 vector<double> vec_z_Mc;
444 vector<double> vec_x;
445 vector<double> vec_y;
446 vector<double> vec_z;
447 vector<int> vec_layer;
448 vector<int> vec_wire;
449 vector<int> vec_slant;
450 vector<double> vec_zStereo;
451 vector<double> l;
452
453 vector<int> vec_layer_Mc;
454 vector<int> vec_wire_Mc;
455 vector<int> vec_hit_Mc;
456 vector<double> vec_ztruth_Mc;
457 vector<double> vec_flt_truth_mc;
458 vector<double> vec_pos_flag;
459 vector<double> vec_phit_MC;
460
461 // get mc particle digi
462 int mc_particle=GetMcInfo();
463 if(m_debug>2) cout<<"MC particle : "<<mc_particle<<endl;
464
465 // retrieve Mdc truth hits
466 if(m_fillTruth ==1 && m_useMcInfo) {
467 int digiId_Mc=0;
468 int nHitAxial_Mc=0;
469 SmartDataPtr<Event::MdcMcHitCol> mcMdcMcHitCol(eventSvc(),"/Event/MC/MdcMcHitCol");
470 if (!mcMdcMcHitCol) {
471 log << MSG::INFO << "Could not find MdcMcHit" << endreq;
472 }else{
473 Event::MdcMcHitCol::iterator iterMcHit = mcMdcMcHitCol->begin();
474
475 for(; iterMcHit != mcMdcMcHitCol->end(); ++iterMcHit,digiId_Mc++) {
476 Identifier mdcid = (*iterMcHit)->identify();
477 int layerId_Mc = MdcID::layer(mdcid);
478 int wireId_Mc = MdcID::wire(mdcid);
479 double hittemp=layerId_Mc*1000+wireId_Mc;
480 double mc_hit_distance =(*iterMcHit)->getDriftDistance();
481 double z_mc = (*iterMcHit)->getPositionZ()/10.;//mm 2 cm
482 double flt_truth=(z_mc-dz_mc)/tanl_mc;
483 double pos_flag=(*iterMcHit)->getPositionFlag();
484
485 // cout<<"flag: "<<(*iterMcHit)->getPositionFlag()<<endl;
486 if( (*iterMcHit)->getPositionFlag()==-999 ){
487 // cout<<"track Id: "<<(*iterMcHit)->getTrackIndex()<<endl;
488 double px= (*iterMcHit)->getPositionX();
489 double py= (*iterMcHit)->getPositionY();
490 double pz= (*iterMcHit)->getPositionZ();
491 double pxyz = sqrt(px*px+py*py+pz*pz);
492 vec_phit_MC.push_back(pxyz);
493 // cout<<"PX: "<<px<<endl;
494 // cout<<"PY: "<<py<<endl;
495 // cout<<"PT: "<<sqrt(px*px+py*py)<<endl;
496 // cout<<"PZ: "<<pz<<endl;
497 // cout<<"PXYZ: "<<pxyz<<endl;
498 // cout<<endl;
499 }
500
501 // cout<<"( MC: " <<layerId_Mc<<","<<wireId_Mc<<"):"<<endl;
502 vec_layer_Mc.push_back(layerId_Mc);
503 vec_wire_Mc.push_back(wireId_Mc);
504 vec_hit_Mc.push_back(hittemp);
505 vec_ztruth_Mc.push_back(z_mc);
506 vec_flt_truth_mc.push_back(flt_truth);
507 vec_pos_flag.push_back(pos_flag);
508
509 if(m_debug>5) {
510 cout<<"(" <<layerId_Mc<<","<<wireId_Mc<<"):"<<endl;
511 cout<<" hit_distance : "<<mc_hit_distance<<endl;
512 cout<<" position flag : "<<pos_flag<<endl;
513 cout<<" hit_z_mc : "<<z_mc<<endl;
514 cout<<" flt_truth : "<<flt_truth<<endl;
515 }
516
517 if(m_data==0){
518 if ((layerId_Mc>=8&&layerId_Mc<=19)||(layerId_Mc>=36)) {
519 nHitAxial_Mc++;}
520 const MdcGeoWire *geowir =m_mdcGeomSvc->Wire(layerId_Mc,wireId_Mc);
521 HepPoint3D eastP = geowir->Backward()/10.0;//mm 2 cm
522 HepPoint3D westP = geowir->Forward() /10.0;//mm 2 cm
523
524 double x = (*iterMcHit)->getPositionX()/10.;//mm 2 cm
525 double y = (*iterMcHit)->getPositionY()/10.;//mm 2 cm
526 double z = (*iterMcHit)->getPositionZ()/10.;//mm 2 cm
527 double u=CFtrans(x,y);
528 double v=CFtrans(y,x);
529 double p=sqrt(u*u+v*v);
530
531 vec_x_east.push_back(eastP.x());
532 vec_y_east.push_back(eastP.y());
533 vec_z_east.push_back(eastP.z());
534 vec_x_west.push_back(westP.x());
535 vec_y_west.push_back(westP.y());
536 vec_z_west.push_back(westP.z());
537 vec_x.push_back(x);
538 vec_y.push_back(y);
539 vec_z.push_back(z);
540 vec_u.push_back(u);
541 vec_v.push_back(v);
542 vec_p.push_back(p);
543 vec_slant.push_back( SlantId(layerId_Mc) );
544
545 m_x_east[digiId_Mc]=eastP.x();
546 m_y_east[digiId_Mc]=eastP.y();
547 m_z_east[digiId_Mc]=eastP.z();
548 m_x_west[digiId_Mc]=westP.x();
549 m_y_west[digiId_Mc]=westP.y();
550 m_z_west[digiId_Mc]=westP.z();
551 m_layer[digiId_Mc]=layerId_Mc;
552 m_cell[digiId_Mc]=wireId_Mc;
553 m_x[digiId_Mc]=x;
554 m_y[digiId_Mc]=y;
555 m_z[digiId_Mc]=z;
556 m_u[digiId_Mc]=u;
557 m_v[digiId_Mc]=v;
558 m_p[digiId_Mc]=p;
559 m_slant[digiId_Mc]=SlantId(layerId_Mc);
560 }
561 }
562 }
563 m_nHit_Mc=digiId_Mc;
564 m_nHitAxial_Mc=nHitAxial_Mc;
565 if(0==m_data) {m_nHit=digiId_Mc; m_nHitAxial=nHitAxial_Mc;}
566 }
567
568 // retrieve Mdc digi vector form RawDataProviderSvc
569 vector<int> vec_hit;
570 vector<int> vec_track_index;
571 set<int> hit_noise; //1
572 uint32_t getDigiFlag = 0;
573 if(m_dropHot || m_combineTracking)getDigiFlag |= MdcRawDataProvider::b_dropHot;
574 if(m_keepBadTdc) getDigiFlag |= MdcRawDataProvider::b_keepBadTdc;
575 if(m_keepUnmatch) getDigiFlag |= MdcRawDataProvider::b_keepUnmatch;
576
577 MdcDigiVec mdcDigiVec = m_rawDataProviderSvc->getMdcDigiVec(getDigiFlag);
578 if(m_debug>0) cout<<"MdcDigiVec: "<<mdcDigiVec.size()<<endl;
579 MdcDigiVec::iterator iter1 = mdcDigiVec.begin();
580 int digiId = 0;
581 int nHitAxial = 0;
582 for (;iter1 != mdcDigiVec.end(); iter1++, digiId++) {
583 Identifier mdcId= (*iter1)->identify();
584 int layerId = MdcID::layer(mdcId);
585 int wireId = MdcID::wire(mdcId);
586 double hittemp=layerId*1000+wireId;
587 const MdcGeoWire *geowir =m_mdcGeomSvc->Wire(layerId,wireId);
588 HepPoint3D eastP = geowir->Backward()/10.0;// cm
589 HepPoint3D westP = geowir->Forward() /10.0;// cm
590 if ((layerId>=8&&layerId<=19)||(layerId>=36)) nHitAxial++;
591
592 int track_index=(*iter1)->getTrackIndex();
593 if( track_index>=1000 ) track_index-=1000;
594 if( track_index<0 ) hit_noise.insert(hittemp);
595 int tchannal=(*iter1)->getTimeChannel();
596 int qchannal=(*iter1)->getChargeChannel();
597 if( tchannal!=qchannal && m_debug>0 ) cout<<"("<<layerId<<","<<wireId<<")"<< 0<<endl;
598 if( m_debug>3 ) cout<<"track_index in digi: "<<"("<<layerId<<","<<wireId<<" "<< track_index<<endl;
599 if( m_debug>3 ) cout<<"event: "<<t_eventNum<<" "<<"layer: "<<layerId<<" "<<"wireId: "<<wireId<<" "<<"zeast: "<<eastP.z()<<" "<<"zwest: "<<westP.z()<<" "<<endl;
600
601 //conformal trans:
602 double x =(eastP.x()+westP.x())/2.;
603 double y =(eastP.y()+westP.y())/2.;
604 double u=CFtrans(x,y);
605 double v=CFtrans(y,x);
606 if(m_debug>3) cout<<"digiId: "<<digiId<<" layer: "<<layerId<<" "<<"wireId: "<<wireId<<" "<<"x: "<<x<<" "<<"y: "<<y<<" "<< "u: "<<u<<"v: "<<v<<endl;
607
608 vec_track_index.push_back(track_index);
609 vec_layer.push_back(layerId);
610 vec_wire.push_back(wireId);
611 vec_hit.push_back(hittemp);
612 vec_x_east.push_back(eastP.x());
613 vec_y_east.push_back(eastP.y());
614 vec_z_east.push_back(eastP.z());
615 vec_x_west.push_back(westP.x());
616 vec_y_west.push_back(westP.y());
617 vec_z_west.push_back(westP.z());
618 vec_x.push_back(x);
619 vec_y.push_back(y);
620 vec_p.push_back(sqrt(u*u+v*v));
621 vec_u.push_back(u);
622 vec_v.push_back(v);
623 vec_slant.push_back( SlantId(layerId) );
624
625 if(m_drawTuple){
626 m_x_east[digiId]=eastP.x();
627 m_y_east[digiId]=eastP.y();
628 m_z_east[digiId]=eastP.z();
629 m_x_west[digiId]=westP.x();
630 m_y_west[digiId]=westP.y();
631 m_z_west[digiId]=westP.z();
632 m_layer[digiId]=layerId;
633 m_cell[digiId]=wireId;
634 m_x[digiId]=(vec_x_east[digiId]+vec_x_west[digiId])/2;
635 m_y[digiId]=(vec_y_east[digiId]+vec_y_west[digiId])/2;
636 m_u[digiId]=u;
637 m_v[digiId]=v;
638 m_p[digiId]=sqrt(u*u+v*v);
639 m_slant[digiId]=SlantId( layerId );
640 m_layerNhit[layerId]++;
641 }
642 }
643 if(m_drawTuple){
644 m_nHit=digiId;
645 m_nHitAxial=nHitAxial;
646 m_nHitStereo=m_nHit-m_nHitAxial;
647 }
648 if(m_debug>0) cout<<"Hit digi number: "<<digiId<<endl;
649 if(digiId<5||nHitAxial<3) {
650 if(m_drawTuple) m_trackFailure=1;
651 if(m_drawTuple) ntuplehit->write();
652 if(m_debug>0) cout<<"not enough hit "<<endl;
653 return StatusCode::SUCCESS;
654 }
655
656 //correspond mc_fltLenth with digiHit
657 vector<int> vec_digitruth(digiId,0);
658 vector<double> vec_flt_truth(digiId,999.);
659 vector<double> vec_ztruth(digiId,999.);
660 vector<double> vec_posflag_truth(digiId,999.);
661 if(m_useMcInfo){
662 int if_exit_delta_e=0;
663 int nhit_digi=0;
664 for(int iHit=0;iHit<digiId;iHit++){
665 vector<int>::iterator iter_ihit = find( vec_hit_Mc.begin(),vec_hit_Mc.end(),vec_hit[iHit] );
666 if( iter_ihit==vec_hit_Mc.end() ) {
667 vec_digitruth[iHit]=0;
668 if(m_drawTuple) m_digi_truth[iHit]=0;
669 if_exit_delta_e=1;
670 }
671 else {
672 vec_digitruth[iHit]=1;
673 m_digi_truth[iHit]=1;
674 nhit_digi++;
675 }
676 for(int iHit_Mc=0;iHit_Mc<vec_flt_truth_mc.size();iHit_Mc++){
677 if(vec_hit[iHit]==vec_hit_Mc[iHit_Mc]) {
678 vec_flt_truth[iHit]=vec_flt_truth_mc[iHit_Mc];
679 vec_ztruth[iHit]=vec_ztruth_Mc[iHit_Mc];
680 vec_posflag_truth[iHit]=vec_pos_flag[iHit_Mc];
681 if(m_drawTuple){
682 m_ztruth[iHit]=vec_ztruth_Mc[iHit_Mc];
683 m_ltruth[iHit]=vec_flt_truth_mc[iHit_Mc];
684 m_postruth[iHit]=vec_pos_flag[iHit_Mc];
685 }
686 }
687 }
688 if(m_debug>3) cout<<" hitPos: "<<"("<<vec_layer[iHit]<<","<<vec_wire[iHit]<<")"<<" flt_truth: "<<vec_flt_truth[iHit]<<" z_truth: "<<vec_ztruth[iHit]<<" pos_flag: "<<vec_posflag_truth[iHit]<<" digi_truth: "<<vec_digitruth[iHit]<<endl;
689 }
690 if( m_drawTuple ){
691 m_e_delta=if_exit_delta_e;
692 m_nHitDigi=nhit_digi;
693 }
694 }
695
696 // calcu the last layer
697 vector<int>::iterator laymax=max_element( vec_layer.begin(),vec_layer.end() );
698 if(m_drawTuple) m_layer_max=*laymax;
699
700 //------------------------- finish hit -------------------------------
701 int nhit;
702 int nhitaxial;
703 if(m_data==0 && m_useMcInfo && m_fillTruth) {
704 nhit=m_nHit_Mc;
705 nhitaxial=m_nHitAxial_Mc;
706 }
707 else{
708 nhit=digiId;
709 nhitaxial=nHitAxial;
710 }
711
712 binX=m_binx;
713 binY=m_biny;
714 TH2D *h1 = new TH2D("h1","h1",binX,0,M_PI,binY,-m_maxrho,m_maxrho);
715 TH2D *h2 = new TH2D("h2","h2",binX,0,M_PI,binY,-m_maxrho,m_maxrho);
716
717 //method 0
718 /*
719 if(m_method==0){
720 vector<double> vec_rho;
721 vector<double> vec_theta;
722 vector< vector<int> > vec_hitNum(2,vector<int>()); //store 2 hits in a cross point
723 vector<int> xybin;
724 //RhoTheta(nhit,vec_u,vec_v,vec_rho,vec_theta,vec_hitNum,vec_digitruth); //calculate cross point
725 RhoThetaAxial(vec_slant,nhit,vec_u,vec_v,vec_rho,vec_theta,vec_hitNum,vec_digitruth); //calculate cross point
726 FillRhoTheta(h1,vec_theta,vec_rho); //fill histgram method by rhotheta
727 if(m_nCross>125000) return StatusCode::SUCCESS;
728
729 //sum in x
730 int xsum_bin;
731 int xsum=0;
732 TF1 *f =new TF1("f","gaus",0,3.1415926);
733 TH1D *hx=new TH1D("hx","hx",binX,0,3.1415926);
734 TH1D *hx_new=new TH1D("hx_new","hx_new",m_binx,0,3.1415926);
735 for(int ibinx =0;ibinx<binX;ibinx++){
736 int t_xsum=0;
737 for(int ibiny =0;ibiny<binY;ibiny++){
738 t_xsum+=h1->GetBinContent(ibinx+1,ibiny+1);
739 }
740 hx->SetBinContent(ibinx+1,t_xsum);
741 if( xsum <= t_xsum ) {
742 xsum=t_xsum;
743 xsum_bin=ibinx+1;
744 }
745 }
746 for(int ibinx =0;ibinx<binX;ibinx++){
747 int hx_content = hx->GetBinContent(ibinx+1);
748 int binx_new=(ibinx+1)-(xsum_bin-(binX/2+1));
749 if(binx_new<1) { binx_new+=binX; }
750 if(binx_new>binX) { binx_new-=binX; }
751 hx_new->SetBinContent( binx_new , hx_content);
752 }
753 hx_new->Fit("f","QN");
754 double xsigma=f->GetParameter(2);
755 delete hx;
756 delete hx_new;
757
758 //sum in y axial
759 TH1D *hy=new TH1D("hy","hy",binY,-0.3,0.3);
760 or(int ibiny =0;ibiny<binY;ibiny++){
761 int t_ysum=0;
762 for(int ibinx =0;ibinx<binX;ibinx++){
763 t_ysum+=h1->GetBinContent(ibinx+1,ibiny+1);
764 }
765 hy->SetBinContent(ibiny+1,t_ysum);
766 }
767 hy->Fit("f","QN");
768 double ysigma=f->GetParameter(2);
769 delete hy;
770 delete f;
771
772 for(int ibinx=0;ibinx<binX;ibinx++){
773 for(int ibiny=0;ibiny<binY;ibiny++){
774 int binNum=h1->GetBinContent(ibinx+1,ibiny+1);
775 xybin.push_back(binNum);
776 m_xybin[binY*ibinx+ibiny]=binNum;
777 }
778 }
779 m_xybinNum=binX*binY;
780 m_xsigma=xsigma;
781 m_ysigma=ysigma;
782 }
783 */
784
785 //method 1
786 vector< vector< vector<int> > > vec_selectNum(binX,vector< vector<int> >(binY,vector<int>() ) );
787 M2 peak_hit_num;
788 M1 peak_hit_list;
789 int peak_track;
790 if(m_method==1){
791 FillHist(h1,vec_u,vec_v,nhit,vec_selectNum);
792 int max_count=0;
793 int max_binx=0;
794 int max_biny=0;
795 for(int i=0;i<binX;i++){
796 for(int j=0;j<binY;j++){
797 int count=h1->GetBinContent(i+1,j+1);
798 if(max_count<count) {
799 max_count=count;
800 max_binx=i+1;
801 max_biny=j+1;
802 }
803 }
804 }
805
806 if(vec_selectNum[max_binx-1][max_biny-1].size() != max_count ) cout<< "ERROR IN VEC!!! "<<endl;
807 if(m_debug>4) {
808 cout<<"maxBin: ["<<max_binx-1<<","<<max_biny-1<<"]: "<<vec_selectNum[max_binx-1][max_biny-1].size()<<endl;
809 for(int i =0;i<vec_selectNum[max_binx-1][max_biny-1].size();i++) cout<<" maxBin select hit: "<<vec_selectNum[max_binx-1][max_biny-1][i]<<endl;
810 }
811
812 if(m_debug>4) {
813 for(int ibinx=0;ibinx<binX;ibinx++){
814 for(int ibiny=0;ibiny<binY;ibiny++){
815 int bincount=h1->GetBinContent(ibinx+1,ibiny+1);
816 if(vec_selectNum[ibinx][ibiny].size() != bincount ) cout<< "ERROR IN VECTORSELECT filling "<<endl;
817 if(vec_selectNum[ibinx][ibiny].size() == 0 ) continue;
818 cout<<"bin:"<<"["<<ibinx<<","<<ibiny<<"]"<<" select:"<<vec_selectNum[ibinx][ibiny].size()<<endl;
819 for(int i=0;i<vec_selectNum[ibinx][ibiny].size();i++){
820 int ihit=vec_selectNum[ibinx][ibiny][i];
821 cout<<" hit: "<<ihit<<" ("<<vec_layer[ihit]<<","<<vec_wire[ihit]<<")"<<endl;
822 }
823 }
824 }
825 }
826
827 //find peak
828 // set threshold
829 int count_use=0;
830 double sum=0;
831 double sum2=0;
832 for(int i=0;i<binX;i++){
833 for(int j=0;j<binY;j++){
834 int count=h1->GetBinContent(i+1,j+1);
835 if(count!=0){
836 count_use++;
837 sum+=count;
838 sum2+=count*count;
839 }
840 }
841 }
842 double f_n=m_ndev1;
843 double aver=sum/count_use;
844 double dev=sqrt(sum2/count_use-aver*aver);
845 int min_counts=(int)(aver+f_n*dev);
846 if (min_counts<m_ndev2) min_counts=m_ndev2;
847 if(m_debug>2) cout<<"aver: "<<aver<<" "<<"dev: "<<dev<<" min: "<<min_counts<<endl;
848
849 vector< vector <int> > hough_trans_CS(binX,vector <int> (binY,0) );
850 vector< vector <int> > hough_trans_CS_peak(binX,vector <int> (binY,0) );
851 for(int i=0;i<binX;i++){
852 for(int j=0;j<binY;j++){
853 hough_trans_CS[i][j]=h1->GetBinContent(i+1,j+1);
854 }
855 }
856
857 int npeak_1=0;
858 for (int r=0; r<binY; r++) {
859 double binHigh=2*m_maxrho/binY;
860 double rho_peak=r*binHigh-m_maxrho;
861 for (int a=0; a<binX; a++) {
862 long max_around=0;
863 for (int ar=a-1; ar<=a+1; ar++) {
864 for (int rx=r-1; rx<=r+1; rx++) {
865 int ax;
866 if (ar<0) { ax=ar+binX; }
867 else if (ar>=binX) { ax=ar-binX; }
868 else { ax=ar; }
869 if ( (ax!=a || rx!=r) && rx>=0 && rx<binY) {
870 if (hough_trans_CS[ax][rx]>max_around) { max_around=hough_trans_CS[ax][rx]; }
871 }
872 }
873 }
874
875 if (hough_trans_CS[a][r]<=min_counts||fabs(rho_peak)<m_rhocut) { hough_trans_CS_peak[a][r]=0; }
876 else if (hough_trans_CS[a][r]<max_around) { hough_trans_CS_peak[a][r]=0; }
877 else if (hough_trans_CS[a][r]==max_around) { hough_trans_CS_peak[a][r]=1; }
878 else if (hough_trans_CS[a][r]>max_around) { hough_trans_CS_peak[a][r]=2; }
879 if(hough_trans_CS_peak[a][r]!=0) {
880 if(m_debug>2) cout<<" possible peak1:"<<"["<<a<<","<<r<<"]"<<": "<<hough_trans_CS_peak[a][r]<<" "<<hough_trans_CS[a][r]<<endl;
881 npeak_1++;
882 }
883 }
884 }
885
886
887 //find double-point-peaks in parameter space
888 int npeak_2=0;
889 for (int r=0; r<binY; r++) {
890 for (int a=0; a<binX; a++) {
891 if (hough_trans_CS_peak[a][r]==1) {
892 hough_trans_CS_peak[a][r]=2;
893 for (int ar=a-1; ar<=a+1; ar++) {
894 for (int rx=r-1; rx<=r+1; rx++) {
895 int ax;
896 if (ar<0) { ax=ar+binX; }
897 else if (ar>=binX) { ax=ar-binX; }
898 else { ax=ar; }
899 if ( (ax!=a || rx!=r) && rx>=0 && rx<binY) {
900 if (hough_trans_CS_peak[ax][rx]==1) { hough_trans_CS_peak[ax][rx]=0; }
901 }
902 }
903 }
904 }
905 if(hough_trans_CS_peak[a][r]==2) {
906 double binHigh=2*m_maxrho/binY;
907 double rho_peak=r*binHigh-m_maxrho;
908 npeak_2++;
909 if(m_debug>2){
910 cout<<" possible peak2: "<<"["<<a<<","<<r<<"]"<<": "<<hough_trans_CS_peak[a][r]<<" "<<hough_trans_CS[a][r]<<" rhopeak: "<<rho_peak<<endl;
911 for(int i=0;i<hough_trans_CS[a][r];i++){
912 int hitNum=vec_selectNum[a][r][i];
913 if(m_debug>2) cout<<" select hit: "<<hitNum<<"("<<vec_layer[hitNum]<<","<<vec_wire[hitNum]<<")"<<endl;
914 }
915 }
916 }
917 }
918 }
919
920 //fill the histogram again
921 for(int i=0;i<binX;i++){
922 for(int j=0;j<binY;j++){
923 if(hough_trans_CS_peak[i][j]==2){
924 h2->SetBinContent(i+1,j+1,hough_trans_CS[i][j]);
925 }
926 }
927 }
928
929 vector<int> peakList[3];
930 for(int n=0;n<npeak_2;n++){
931 for (int r=0; r<binY; r++) {
932 for (int a=0; a<binX; a++) {
933 if (hough_trans_CS_peak[a][r]==2) {
934 peakList[0].push_back(a);
935 peakList[1].push_back(r);
936 peakList[2].push_back(hough_trans_CS[a][r]);
937 }
938 }
939 }
940 }
941
942 if(m_drawTuple){
943 m_npeak_1=npeak_1;
944 m_npeak_2=npeak_2;
945 }
946 if(m_debug>0) {
947 cout<<"npeak_1: "<<npeak_1<<endl;
948 cout<<"npeak_2: "<<npeak_2<<endl;
949 }
950
951 //sort peaks by height
952 int n_max;
953 for (int na=0; na<npeak_2-1; na++) {
954 n_max=na;
955 for (int nb=na+1; nb<npeak_2; nb++) {
956 if (peakList[2][n_max]<peakList[2][nb]) { n_max=nb; }
957 }
958 if (n_max!=na) { // swap
959 float swap[3];
960 for (int i=0; i<3; i++) {
961 swap[i]=peakList[i][na];
962 peakList[i][na]=peakList[i][n_max];
963 peakList[i][n_max]=swap[i];
964 }
965 }
966 }
967
968 if(npeak_2==0||npeak_2>100){
969 if(m_debug>0) cout<<" FAILURE IN NPEAK2 !!!!!! "<<endl;
970 delete h1;
971 delete h2;
972 if(m_drawTuple){
973 m_trackFailure=2;
974 m_npeak_2=-999;
975 }
976 if(m_drawTuple) ntuplehit->write();
977 return StatusCode::SUCCESS;
978 }
979
980 if(m_debug>2){
981 for(int n=0;n<npeak_2;n++){
982 int bintheta=peakList[0][n];
983 int binrho=peakList[1][n];
984 int count=peakList[2][n];
985 cout<<"possible peakList after SORT: "<<n<<": "<<"["<<bintheta<<","<<binrho<<"]: "<<count<<endl;
986 for(int i=0;i<count;i++){
987 int hitNum=vec_selectNum[bintheta][binrho][i];
988 if(m_debug>2) cout<<" "<<" select hit:"<<":"<<hitNum<<" ------ "<<"("<<vec_layer[hitNum]<<","<<vec_wire[hitNum]<<")"<<endl;
989 }
990 }
991 }
992
993 // combine peak to track
994 peak_track=npeak_2;
995 Combine(h1,m_peakCellNumX,m_peakCellNumY,vec_selectNum,peak_hit_list,peak_hit_num,peak_track,peakList);
996 if(m_drawTuple) m_npeak_after_tracking=peak_track;
997
998 if(m_debug>0) cout<<"event"<<t_eventNum<<": "<<"has found: "<<peak_track<<" track."<<endl;
999 for( int i=0;i<peak_track;i++){
1000 if(m_debug>0) cout<<"peak["<<i<<"] collect "<<peak_hit_num[i]<<" hits "<<endl;
1001 int nhitaxialselect=0; // temp for each peak_track
1002 for(int j =0;j<peak_hit_num[i];j++){
1003 int hit_number=peak_hit_list[i][j];
1004 if(vec_slant[hit_number]==0) nhitaxialselect++ ;
1005 if(m_debug>0) cout<<" "<<" collect hits: ("<<vec_layer[hit_number]<<","<<vec_wire[hit_number]<<")"<<endl;
1006 }
1007 if(m_drawTuple){
1008 m_nHitSelect[i]=peak_hit_num[i];
1009 m_nHitAxialSelect[i]=nhitaxialselect;
1010 m_nHitStereoSelect[i]=peak_hit_num[i]-nhitaxialselect;
1011 m_axiallose[i]=m_nHitAxial-m_nHitAxialSelect[i];
1012 m_stereolose[i]=m_nHit-m_nHitSelect[i]-m_axiallose[i];
1013 }
1014 }
1015 }
1016
1017 vector<int> vec_hitbeforefit;
1018 vector<bool> vec_truthHit(nhit,false);
1019 int peak_fit=peak_track;
1020 if(m_setpeak_fit!=-1) {
1021 peak_fit=m_setpeak_fit;
1022 }
1023 if(m_drawTuple) m_trackNum=peak_fit;
1024
1025 vector<int> vec_hit_use;
1026 // vector<int> vec_hit_use_in2d;
1027 // vector<int> vec_hit_use_intrack1;
1028 // vector<int> vec_Qchoise;
1029 vector<TrkRecoTrk*> vec_newTrack;
1030 vector<TrkRecoTrk*> vec_track_in_tds;
1031 vector<int> vec_hitOnTrack;
1032 vector<MdcHit*> vec_for_clean;
1033 vector<TrkRecoTrk*> vec_trk_for_clean;
1034 vector<Mytrack> vec_myTrack;
1035 int track_fit_success=0;
1036
1037 for(track_fit=0;track_fit<peak_fit;track_fit++){
1038 double d0_before_least=-999.;
1039 double phi0_before_least=-999.;
1040 double omega_before_least=-999.;
1041 map<int,double> hit_to_circle1;
1042 map<int,double> hit_to_circle2;
1043 map<int,double> hit_to_circle3;
1044 if(m_debug>0) cout<<"Do fitting track: "<<track_fit<<endl;
1045
1046 for(int i=0;i<nhit;i++) vec_truthHit[i]=false;
1047
1048 // confirm every track has which hits
1049 if(m_debug>1) cout<<"candi track["<<track_fit<<"] collect "<<peak_hit_num[track_fit]<<" hits "<<endl;
1050
1051 for(int i=0;i<peak_hit_num[track_fit];i++){
1052 int hitNum=peak_hit_list[track_fit][i];
1053 int hittemp=vec_layer[hitNum]*1000+vec_wire[hitNum];
1054 vector<int>::iterator iter_ihit = find( vec_hit_use.begin(),vec_hit_use.end(),hittemp );
1055 if( iter_ihit==vec_hit_use.end() ) vec_truthHit[hitNum]=true;
1056 if( m_debug >1 && vec_truthHit[hitNum]==true) cout<<" "<<"collect hit :"<<"("<<vec_layer[hitNum]<<","<<vec_wire[hitNum]<<")"<<" "<<endl;;
1057 }
1058 if( m_debug >1){
1059 for(int i=0;i<nhit;i++){
1060 if(vec_truthHit[i]==false){
1061 cout<<"candi track "<<"uncollect hit: "<<endl;
1062 cout<<" "<<"uncollect hit :"<<"("<<vec_layer[i]<<","<<vec_wire[i]<<")"<<" " <<endl;
1063 }
1064 }
1065 }
1066 // begin chisq fit
1067 int t_nHitAxialSelect=0;
1068 for(int i=0;i<nhit;i++){
1069 if(vec_truthHit[i]==true){
1070 if(vec_slant[i]==0) t_nHitAxialSelect++;
1071 }
1072 }
1073 if(m_debug>1) cout<<"track "<<track_fit<<" with axial select: "<<t_nHitAxialSelect<<endl;
1074 if(t_nHitAxialSelect<3 && m_drawTuple){
1075 m_x_circle[track_fit]=-999.;
1076 m_y_circle[track_fit]=-999.;
1077 m_r[track_fit]=-999.;
1078 m_chis_pt[track_fit] = -999.;
1079 m_pt[track_fit]=-999.;
1080 continue;
1081 }
1082 double circle_x=0;
1083 double circle_y=0;
1084 double circle_r=0;
1085 int leastSquare=LeastSquare(nhit,vec_x,vec_y,vec_slant,vec_layer,vec_truthHit,d0_before_least,phi0_before_least,omega_before_least,circle_x,circle_y,circle_r);
1086 //hitdis to circle1
1087 for(int i=0;i<nhit;i++){
1088 int hittemp=vec_layer[i]*1000+vec_wire[i];
1089 double dist_temp=sqrt(pow( (vec_y[i]-circle_y),2)+pow( (vec_x[i]-circle_x),2))-circle_r;
1090 if(dist_temp>10.) vec_truthHit[i]=false;
1091 if(m_debug>1&&vec_truthHit[i]==true) cout<<"IN LEAST1: "<<"("<<vec_layer[i]<<","<<vec_wire[i]<<"):"<<dist_temp<<endl;
1092 }
1093
1094 t_nHitAxialSelect=0;
1095 for(int i=0;i<nhit;i++){
1096 if(vec_truthHit[i]==true){
1097 if(vec_slant[i]==0) t_nHitAxialSelect++;
1098 }
1099 }
1100 if(m_debug>1) cout<<"track "<<track_fit<<"with axial2 select: "<<t_nHitAxialSelect<<endl;
1101 if(t_nHitAxialSelect<3) continue;
1102
1103 int leastSquare2=LeastSquare(nhit,vec_x,vec_y,vec_slant,vec_layer,vec_truthHit,d0_before_least,phi0_before_least,omega_before_least,circle_x,circle_y,circle_r);
1104 for(int i=0;i<nhit;i++){
1105 int hittemp=vec_layer[i]*1000+vec_wire[i];
1106 double dist_temp=sqrt(pow( (vec_y[i]-circle_y),2)+pow( (vec_x[i]-circle_x),2))-circle_r;
1107 hit_to_circle1[hittemp]=dist_temp;
1108 if(m_debug>1&&vec_truthHit[i]==true) cout<<" IN LEAST2: "<<"("<<vec_layer[i]<<","<<vec_wire[i]<<"):"<<dist_temp<<endl;
1109 }
1110
1111 if(m_drawTuple){
1112 m_x_circle[track_fit]=circle_x;
1113 m_y_circle[track_fit]=circle_y;
1114 m_r[track_fit]=circle_r;
1115 m_chis_pt[track_fit]=circle_r/333.567;
1116 }
1117
1118 // q select
1119 TrkHitList* qhits[2];
1120 const TrkFit* newFit[2];
1121 TrkHitList* qhits2[2];
1122 const TrkFit* newFit2[2];
1123 TrkRecoTrk* newTrack2[2];
1124
1125 int Q_iq[]={-1,1};
1126 double Q_Chis_2d[]={9999,9999};
1127 double Q_Chis_3d[]={9999,9999};
1128 double Q_z[]={999,999};
1129 double Q_pt2[]={999,999};
1130 double Q_tanl[]={999,999};
1131 int Q_ok[2][2]={0};
1132
1133 int q_start=0;
1134 int q_end=1;
1135 if(m_q==-1) {q_start=0;q_end=0;}
1136 if(m_q==1) {q_start=1;q_end=1;}
1137 for(int i_q=q_start;i_q<=q_end;i_q++){
1138 double d0=d0_before_least;
1139 double phi0=phi0_before_least;
1140 double omega=omega_before_least;
1141 double z0=0;
1142 double tanl=0;
1143 if(m_debug>0 ){
1144 cout<<"BY LEASTSQUARE: "<<endl;
1145 cout<<" d0: "<<d0<<" d0_mc "<<d0_mc<<endl;
1146 cout<<" phi0: "<<phi0<<" phi0_mc "<<phi0_mc<<endl;
1147 cout<<" omega0: "<<omega<<" omega_mc "<<omega_mc<<endl;
1148 }
1149
1150 vector<double> vec_flt_least; //use circle info
1151 for(int i=0;i<nhit;i++){
1152 double theta_temp;
1153 if(circle_x==0||vec_x[i]-circle_x==0){
1154 theta_temp=0;
1155 }
1156 else{
1157 theta_temp=M_PI-atan2(vec_y[i]-circle_y,vec_x[i]-circle_x)+atan2(circle_y,circle_x);
1158 if(theta_temp>2*M_PI){
1159 theta_temp=theta_temp-2*M_PI;
1160 }
1161 if(theta_temp<0){
1162 theta_temp=theta_temp+2*M_PI;
1163 }
1164 }
1165 if(Q_iq[i_q]==-1) {
1166 double l_temp=circle_r*theta_temp;
1167 vec_flt_least.push_back(l_temp);
1168 }
1169 if(Q_iq[i_q]==1) {
1170 theta_temp=2*M_PI-theta_temp;
1171 double l_temp=circle_r*(theta_temp);
1172 vec_flt_least.push_back(l_temp);
1173 }
1174 }
1175 if(m_debug>2) {
1176 for(int i=0;i<nhit;i++){
1177 cout<<"By least 2d: "<< "("<<vec_layer[i]<<","<<vec_wire[i]<<"):"<<vec_flt_least[i]<<endl;
1178 }
1179 }
1180
1181 //exchange par to 2d
1182 if(Q_iq[i_q]==-1){
1183 d0=-d0;
1184 omega=-1./circle_r;
1185 }
1186 if(Q_iq[i_q]==1){
1187 d0=d0;
1188 omega=1./circle_r;
1189 phi0=phi0-M_PI;
1190 }
1191
1192 // 2d global fit
1193 double x_cirtemp;
1194 double y_cirtemp;
1195 double r_temp;
1196 TrkExchangePar tt(d0,phi0,omega,z0,tanl);
1197 TrkCircleMaker circlefactory;
1198 float chisum =0.;
1199 TrkRecoTrk* newTrack = circlefactory.makeTrack(tt, chisum, *m_context, m_bunchT0);
1200 //vec_trk_for_clean.push_back(newTrack);
1201 bool permitFlips = true;
1202 bool lPickHits = m_pickHits;
1203 circlefactory.setFlipAndDrop(*newTrack, permitFlips, lPickHits);
1204 int nDigi = digiToHots(mdcDigiVec,newTrack,vec_truthHit,getDigiFlag,vec_flt_least,hit_to_circle1,vec_for_clean);
1205 if(m_debug>2){
1206 newTrack->printAll(std::cout);
1207 }
1208 //fit
1209 // TrkHitList* qhits = newTrack->hits();
1210 qhits[i_q] = newTrack->hits();
1211 TrkErrCode err=qhits[i_q]->fit();
1212 newFit[i_q] = newTrack->fitResult();
1213
1214 //test fit result
1215 if (!newFit[i_q] || (newFit[i_q]->nActive()<3) || err.failure()!=0) {
1216 if(m_debug>0){
1217 cout << "evt "<<t_eventNum<<" global 2d fit failed ";
1218 if(newFit[i_q]) cout <<" nAct "<<newFit[i_q]->nActive();
1219 cout<<"ERR1 failure ="<<err.failure()<<endl;
1220 }
1221 // m_2dFit[track_fit]=0;
1222 Q_ok[i_q][0]=0;
1223 Q_ok[i_q][1]=0;
1224 Q_Chis_2d[i_q]=-999.;
1225 delete newTrack;
1226 continue;
1227 }else{
1228 Q_ok[i_q][0]=1;
1229 Q_ok[i_q][1]=0;
1230 if(m_debug>0) cout <<"evt "<<t_eventNum<< " global 2d fit success"<<endl;
1231 if(m_debug>2) {
1232 newTrack->print(std::cout);
1233 }
1234 Q_Chis_2d[i_q]=newFit[i_q]->chisq();
1235
1236 TrkExchangePar par = newFit[i_q]->helix(0.);
1237 d0=par.d0();
1238 phi0=par.phi0();
1239 omega=par.omega();
1240 r_temp=Q_iq[i_q]/par.omega();
1241 x_cirtemp = sin(par.phi0()) *(par.d0()+1/par.omega()) * -1.; //z axis verse,x_babar = -x_belle
1242 y_cirtemp = -1.*cos(par.phi0()) *(par.d0()+1/par.omega()) * -1;//???
1243
1244 if(m_debug>1) cout<<" circle after globle 2d: "<<"("<<x_cirtemp<<","<<y_cirtemp<<","<<r_temp<<")"<<endl;
1245 if(m_debug>0) cout<<"pt_rec: "<<-1*Q_iq[i_q]/omega/333.567<<endl;
1246
1247 if(m_drawTuple){
1248 m_x_circle[track_fit]=x_cirtemp;
1249 m_y_circle[track_fit]=y_cirtemp;
1250 m_r[track_fit]=r_temp;
1251 m_chis_pt[track_fit] = newFit[i_q]->chisq();
1252 m_pt[track_fit]=r_temp/333.567;
1253 }
1254 }
1255
1256 int nfit2d=0;
1257 if(m_debug>1) cout<<" hot list:"<<endl;
1258 TrkHotList::hot_iterator hotIter= qhits[i_q]->hotList().begin();
1259 int lay=((MdcHit*)(hotIter->hit()))->layernumber();
1260 int wir=((MdcHit*)(hotIter->hit()))->wirenumber();
1261 int hittemp=lay*1000+wir;
1262 while (hotIter!=qhits[i_q]->hotList().end()) {
1263 if(m_debug>1){ cout <<"(" <<((MdcHit*)(hotIter->hit()))->layernumber()
1264 <<","<<((MdcHit*)(hotIter->hit()))->wirenumber()
1265 <<":"<<hotIter->isActive()<<") ";
1266 }
1267 if (hotIter->isActive()==1) nfit2d++;
1268 hotIter++;
1269 }
1270 if(m_debug>0) cout<<"charge "<<i_q<<" In 2Dfit Num Of Active Hits: "<<nfit2d<<endl;
1271
1272 //caculate z position
1273 if(m_debug>0 && m_drawTuple){
1274 cout<<" By global 2d charge "<<i_q<<endl;
1275 cout<<" d0: " <<d0<<" "<<m_drTruth[0]<<endl;
1276 cout<<" phi0: " <<phi0<<" "<<m_phi0Truth[0]<<endl;
1277 cout<<" omega: "<<omega<<" "<<m_omegaTruth[0]<<endl;
1278 cout<<" z0: " <<z0<<" "<<m_dzTruth[0]<<endl;
1279 cout<<" tanl: "<<tanl<<" "<<m_tanl_mc[0]<<endl;
1280 }
1281
1282 if(m_debug>2) {
1283 cout<<"least:( "<<circle_x<<","<<circle_y<<","<<circle_r<<endl;
1284 cout<<"2d:( "<<x_cirtemp<<","<<y_cirtemp<<","<<r_temp<<endl;
1285 }
1286 delete newTrack;
1287
1288 //calcu every hit after 2dfit
1289 vector<double> vec_flt;
1290 vector<double> vec_theta_ontrack;
1291 for(int i=0;i<nhit;i++){
1292 double theta_temp;
1293 if(x_cirtemp==0||vec_x[i]-x_cirtemp==0){
1294 theta_temp=0;
1295 }
1296 else{
1297 theta_temp=M_PI-atan2(vec_y[i]-y_cirtemp,vec_x[i]-x_cirtemp)+atan2(y_cirtemp,x_cirtemp);
1298 if(theta_temp>2*M_PI){
1299 theta_temp=theta_temp-2*M_PI;
1300 }
1301 if(theta_temp<0){
1302 theta_temp=theta_temp+2*M_PI;
1303 }
1304 if(Q_iq[i_q]==-1) {
1305 double l_temp=r_temp*theta_temp;
1306 vec_flt.push_back(l_temp);
1307 vec_theta_ontrack.push_back(theta_temp);
1308 // if(vec_truthHit[i]==true) cout<<"("<<vec_layer[i]<<","<<vec_wire[i]<<"):l:"<<vec_flt[i]<<" theta:"<<vec_theta_ontrack[i]<<endl;
1309 }
1310 if(Q_iq[i_q]==1) {
1311 theta_temp=2*M_PI-theta_temp;
1312 double l_temp=r_temp*(theta_temp);
1313 vec_flt.push_back(l_temp);
1314 vec_theta_ontrack.push_back(theta_temp);
1315 // if(vec_truthHit[i]==true) cout<<"("<<vec_layer[i]<<","<<vec_wire[i]<<"):l:"<<vec_flt[i]<<" theta:"<<vec_theta_ontrack[i]<<endl;
1316 }
1317 }
1318 }
1319
1320 if(m_debug>3){
1321 for(int i=0;i<nhit;i++){
1322 cout<<"i: "<<"theta: "<<vec_theta_ontrack[i]<<" l: "<<vec_flt[i]<<endl;
1323 }
1324 }
1325
1326 for(int i=0;i<nhit;i++){
1327 int hittemp=vec_layer[i]*1000+vec_wire[i];
1328 double dist_temp=sqrt(pow( (vec_y[i]-y_cirtemp),2)+pow( (vec_x[i]-x_cirtemp),2))-r_temp;
1329 hit_to_circle2[hittemp]=dist_temp;
1330 }
1331
1332 vector<double> vec_l_Calcu_Left;
1333 vector<double> vec_l_Calcu_Right;
1334 vector<double> vec_z_Calcu_Left;
1335 vector<double> vec_z_Calcu_Right;
1336 vector<double> vec_z_Calcu;
1337 vector<double> vec_l_Calcu;
1338 vector<double> vec_delta_z;
1339 vector<double> vec_delta_l;
1340
1341 double l_Calcu_Left=-999.;
1342 double l_Calcu_Right=-999.;
1343 double z_Calcu_Left=-999.;
1344 double z_Calcu_Right=-999.;
1345 double z_Calcu=-999.;
1346 double l_Calcu=-999.;
1347 double delta_z=-999.;
1348 double delta_l=-999.;
1349 // z caculate
1350 MdcDigiVec::iterator iter2 = mdcDigiVec.begin();
1351 int zPos;
1352 int digiId = 0;
1353 for (;iter2 != mdcDigiVec.end(); iter2++, digiId++) {
1354 if(vec_truthHit[digiId]==false) continue;
1355 if(vec_slant[digiId]==0) continue;
1356 if(vec_theta_ontrack[digiId]>M_PI) continue;
1357 const MdcDigi* aDigi = *iter2;
1358 MdcHit *hit = new MdcHit(aDigi, m_gm);
1359 hit->setCalibSvc(m_mdcCalibFunSvc);
1360 hit->setCountPropTime(true);
1361 zPos=zPosition(*hit,m_bunchT0,x_cirtemp,y_cirtemp,r_temp,digiId,delta_z,delta_l,l_Calcu_Left,l_Calcu_Right,z_Calcu_Left,z_Calcu_Right,z_Calcu,l_Calcu,Q_iq[i_q]);
1362 if(m_debug>2) cout<<"in ZS calcu hitPos: "<<"("<<vec_layer[digiId]<<","<<vec_wire[digiId]<<")"<<" l_truth: "<<vec_flt_truth[digiId]<<" z_truth: "<<vec_ztruth[digiId]<<" l_Cal: "<<l_Calcu<<" z_Cal: "<<z_Calcu<<endl;
1363 delete hit;
1364 if(zPos==-1||zPos==-2) continue;
1365 vec_l_Calcu_Left.push_back(l_Calcu_Left);
1366 vec_l_Calcu_Right.push_back(l_Calcu_Right);
1367 vec_z_Calcu_Left.push_back(z_Calcu_Left);
1368 vec_z_Calcu_Right.push_back(z_Calcu_Right);
1369 vec_z_Calcu.push_back(z_Calcu);
1370 vec_l_Calcu.push_back(l_Calcu_Right);
1371 vec_delta_z.push_back(delta_z);
1372 vec_delta_l.push_back(delta_l);
1373 }
1374 int nHitUseInZs=vec_delta_z.size();
1375
1376 if(m_drawTuple){
1377 for(int i=0;i<nHitUseInZs;i++){
1378 m_nHitStereo_use=vec_delta_z.size();
1379 m_lCalcuLeft[i]=vec_l_Calcu_Left[i];
1380 m_lCalcuRight[i]=vec_l_Calcu_Right[i];
1381 m_zCalcuLeft[i]=vec_z_Calcu_Left[i];
1382 m_zCalcuRight[i]=vec_z_Calcu_Right[i];
1383 m_lCalcu[i]=vec_l_Calcu[i];
1384 m_zCalcu[i]=vec_z_Calcu[i];
1385 m_delta_z[i]=vec_delta_z[i];
1386 m_delta_l[i]=vec_delta_l[i];
1387 }
1388 }
1389
1390 //ambig choose
1391 vector< vector< vector <double> > > point(2, vector< vector<double> >(2,vector<double>() ));
1392 for(int iHit=0;iHit<nHitUseInZs;iHit++){
1393 point[0][0].push_back(vec_l_Calcu_Left[iHit]);
1394 point[0][1].push_back(vec_z_Calcu_Left[iHit]);
1395 point[1][0].push_back(vec_l_Calcu_Right[iHit]);
1396 point[1][1].push_back(vec_z_Calcu_Right[iHit]);
1397 }
1398 vector<int> ambigList;
1399 if(nHitUseInZs!=0){
1400 AmbigChoose(point,nHitUseInZs,ambigList,tanl,z0);
1401 if(m_drawTuple){
1402 m_tanl_Cal=tanl;
1403 m_z0_Cal=z0;
1404 }
1405
1406 if(m_useMcInfo && m_drawTuple){
1407 int ambig_no_match_num=0;
1408 for(int iHit=0;iHit<nHitUseInZs;iHit++){
1409 if(ambigList[iHit]==0) ambigList[iHit]=1;
1410 else ambigList[iHit]=0;
1411 if(ambigList[iHit]!=m_postruth[iHit]) { m_ambig_no_match=1; ambig_no_match_num++; }
1412 }
1413 m_ambig_no_match_propotion=(double)(ambig_no_match_num)/m_nHitStereo_use;
1414 for (int iHit=0;iHit<ambigList.size();iHit++){
1415 m_amb[iHit]=ambigList[iHit];
1416 }
1417 }
1418 }
1419
1420 if(m_debug>0 && m_drawTuple){
1421 cout<<"By 3d track zs fit:"<<endl;
1422 cout<<"d0: "<<d0<<" mc : "<<m_drTruth[0]<<endl;
1423 cout<<"phi0: "<<phi0<<" mc : "<<m_phi0Truth[0]<<endl;
1424 cout<<"omega: "<<omega<<" mc : "<<m_omegaTruth[0]<<endl;
1425 cout<<"z0: "<<z0<<" mc : "<<m_dzTruth[0]<<endl;
1426 cout<<"tanl: "<<tanl<<" mc : "<<m_tanl_mc[0]<<endl;
1427 }
1428
1429 if(m_mcpar==1){
1430 d0=m_drTruth[0];
1431 phi0=m_phi0Truth[0];
1432 if(Q_iq[i_q]==-1) {phi0=m_phi0Truth[0]-3./2.*M_PI;omega=m_omegaTruth[0];}
1433 else {phi0=m_phi0Truth[0]-1./2.*M_PI;omega=-1*m_omegaTruth[0];}
1434 z0=m_dzTruth[0];
1435 tanl=m_tanl_mc[0];
1436 }
1437
1438 //-------------------------------------------5 parameter fit-----------------------
1439
1440 if(m_debug>0) cout<< "become 3d fit "<<endl;
1441 TrkExchangePar tt2(d0,phi0,omega,z0,tanl);
1442 TrkHelixMaker helixfactory;
1443 chisum =0.;
1444 newTrack2[i_q] = helixfactory.makeTrack(tt2, chisum, *m_context, m_bunchT0);
1445 vec_trk_for_clean.push_back(newTrack2[i_q]);
1446 permitFlips = true;
1447 lPickHits = m_pickHits;
1448 helixfactory.setFlipAndDrop(*newTrack2[i_q], permitFlips, lPickHits);
1449 int nDigi2 = digiToHots2(mdcDigiVec,newTrack2[i_q],vec_truthHit,vec_flt_truth,getDigiFlag,vec_slant,vec_l_Calcu,vec_flt,vec_theta_ontrack,vec_hitbeforefit,hit_to_circle2,vec_for_clean,tanl);
1450 if(m_debug>2){
1451 cout<<__FILE__<<__LINE__<<"AFTER digiToHots 3d ---------BEFORE 3d fit"<<endl;
1452 newTrack2[i_q]->printAll(std::cout);
1453 }
1454 //fit
1455 qhits2[i_q] = newTrack2[i_q]->hits();
1456 TrkErrCode err2=qhits2[i_q]->fit();
1457 newFit2[i_q] = newTrack2[i_q]->fitResult();
1458 int nActiveHit = newTrack2[i_q]->hots()->nActive();
1459 int fitSuccess_3d = 0;
1460 if (!newFit2[i_q] || (newFit2[i_q]->nActive()<5) || err2.failure()!=0) {
1461 fitSuccess_3d=0;
1462 Q_ok[i_q][1]=0;
1463 Q_Chis_3d[i_q]=-999.;
1464 if(m_debug>0){
1465 cout << "evt "<<t_eventNum<<" global 3d fit failed ";
1466 if(!newFit2[i_q]) cout<<" newfit2 point is NULL!!!" <<endl;
1467 if(newFit2[i_q]) cout <<" nAct "<<newFit2[i_q]->nActive();
1468 cout<<"ERR2 failure ="<<err2.failure()<<endl;
1469 }
1470 }else{
1471 //fit success
1472 TrkExchangePar par2 = newFit2[i_q]->helix(0.);
1473 if( abs( 1/(par2.omega()) )>0.001){
1474 fitSuccess_3d=1;
1475 Q_Chis_3d[i_q]=newFit2[i_q]->chisq();
1476 Q_ok[i_q][1]=1;
1477 if(m_debug>0) cout <<"evt "<<t_eventNum<< " global 3d fit success "<<err2.failure()<<endl;
1478 if(m_debug>2){
1479 cout<<__FILE__<<__LINE__<<"AFTER fit 3d "<<endl;
1480 newTrack2[i_q]->print(std::cout);
1481 }
1482 } else {
1483 fitSuccess_3d=0;
1484 Q_ok[i_q][1]=0;
1485 Q_Chis_3d[i_q]=-999.;
1486 if(m_debug>2) cout<<"3dfit failure of omega "<<endl;
1487 }
1488 bool badQuality = false;
1489 //yzhang add 2016-06-15
1490 //test quality of track
1491 if(fabs(Q_Chis_3d[i_q])>m_qualityFactor*m_dropTrkChi2Cut ){
1492 if(m_debug>0){
1493 std::cout<<__FILE__<<" "<<__LINE__<<" drop track by chi2 "<<Q_Chis_3d[i_q]<<" > "<<m_qualityFactor<<" * "<<m_dropTrkChi2Cut <<std::endl;
1494 }
1495 badQuality=1;
1496 }
1497 if( fabs(par2.d0())>m_qualityFactor*m_dropTrkDrCut) {
1498 if(m_debug>0){
1499 std::cout<<__FILE__<<" "<<__LINE__<<" drop track by d0 "<<par2.d0()<<" > "<<m_qualityFactor<<" * "<<m_dropTrkDrCut <<std::endl;
1500 }
1501 badQuality=1;
1502 }
1503 if( fabs(par2.z0())>m_qualityFactor*m_dropTrkDzCut) {
1504 if(m_debug>0){
1505 std::cout<<__FILE__<<" "<<__LINE__<<" drop track by z0 "<<par2.z0()<<" > "<<m_qualityFactor<<" * "<<m_dropTrkDzCut <<std::endl;
1506 }
1507 badQuality=1;
1508 }
1509 if( (fabs(Q_Chis_3d[i_q])/qhits2[i_q]->nHit()) > m_qualityFactor*m_dropTrkChi2NdfCut) {
1510 if(m_debug>0){
1511 std::cout<<__FILE__<<" "<<__LINE__<<" drop track by chi2/ndf"<<(fabs(Q_Chis_3d[i_q])/qhits2[i_q]->nHit()) <<" > "<<m_qualityFactor<<" * "<<m_dropTrkChi2NdfCut<<std::endl;
1512 }
1513 badQuality=1;
1514 }
1515 if( nActiveHit <= m_dropTrkNhitCut) {
1516 if(m_debug>0){
1517 std::cout<<__FILE__<<" "<<__LINE__<<" drop track by nhit"<<nActiveHit <<" <= "<<m_qualityFactor<<" * "<<m_dropTrkNhitCut<<std::endl;
1518 }
1519 badQuality=1;
1520 }
1521 if(badQuality) {
1522 fitSuccess_3d=0;
1523 Q_ok[i_q][1]=0;
1524 Q_Chis_3d[i_q]=-999.;
1525 if(m_debug>2) cout<<"3dfit failure of bad quality"<<endl;
1526 }
1527 //zhangy
1528 }
1529
1530 // -------------try to out put parameters---
1531 if(fitSuccess_3d==1){
1532 TrkExchangePar par2 = newFit2[i_q]->helix(0.);
1533 if(m_debug>0){
1534 cout<<"BY 3d global fit charge "<<i_q<<endl;
1535 cout<<"d0: "<<par2.d0()<<endl;
1536 cout<<"phi0: "<<par2.phi0()<<endl;
1537 cout<<"w: "<<par2.omega()<<endl;
1538 cout<<"z: "<<par2.z0()<<endl;
1539 cout<<"tanl: "<<par2.tanDip()<<endl;
1540 }
1541 r_temp=Q_iq[i_q]/par2.omega();
1542 x_cirtemp = sin(par2.phi0()) *(par2.d0()+1/par2.omega()) * -1.; //z axis verse,x_babar = -x_belle
1543 y_cirtemp = -1.*cos(par2.phi0()) *(par2.d0()+1/par2.omega()) * -1;//???
1544 if(m_debug>0) cout<<" circle after globle 3d: "<<"("<<x_cirtemp<<","<<y_cirtemp<<","<<r_temp<<")"<<endl;
1545 double pxy=-1*r_temp/333.567;
1546 double pz=pxy*par2.tanDip();
1547 double pxyz=sqrt(pxy*pxy+pz*pz);
1548
1549 Q_pt2[i_q]=fabs(pxy);
1550 Q_z[i_q]=par2.z0();
1551 Q_tanl[i_q]=par2.tanDip();
1552 } else{
1553 continue;
1554 }
1555 //------------------------------------clean-------------------------
1556
1557 for(int i=0;i<nhit;i++){
1558 int hittemp=vec_layer[i]*1000+vec_wire[i];
1559 double dist_temp=sqrt(pow( (vec_y[i]-y_cirtemp),2)+pow( (vec_x[i]-x_cirtemp),2))-r_temp;
1560 hit_to_circle3[hittemp]=dist_temp;
1561 }
1562 }
1563
1564 if(m_debug>3){
1565 cout<<"chis 2d: "<<Q_Chis_2d[0]<<" "<<Q_Chis_2d[1]<<endl;
1566 cout<<"chis 3d: "<<Q_Chis_3d[0]<<" "<<Q_Chis_3d[1]<<endl;
1567 cout<<"Z: "<<Q_z[0]<<" "<<Q_z[1]<<endl;
1568 cout<<"chis ok-: "<<Q_ok[0][0]<<" "<<Q_ok[0][1]<<endl;
1569 cout<<"chis ok+: "<<Q_ok[1][0]<<" "<<Q_ok[1][1]<<endl;
1570 }
1571
1572 int Q;
1573 int Q_judge[2]={0};
1574 if(Q_ok[0][1]==1) Q_judge[0]=1;
1575 if(Q_ok[1][1]==1) Q_judge[1]=1;
1576 if(Q_judge[0]==1&&Q_judge[1]==0) Q=-1;
1577 if(Q_judge[0]==0&&Q_judge[1]==1) Q=1;
1578 if(Q_judge[0]==1&&Q_judge[1]==1) {
1579 if(fabs(Q_z[0])>fabs(Q_z[1])) Q=1;
1580 else Q=-1;
1581 }
1582 if(Q_judge[0]==0&&Q_judge[1]==0) {Q=0; continue;}
1583 int q_choose=0;
1584 if(Q==-1) q_choose=0;
1585 if(Q== 1) q_choose=1;
1586 TrkExchangePar par_final = newFit2[q_choose]->helix(0.);
1587 if(m_debug>0){
1588 cout<<"after q choose By 3d global fit: "<<Q<<endl;
1589 cout<<"d0: "<<par_final.d0()<<endl;
1590 cout<<"phi0: "<<par_final.phi0()<<endl;
1591 cout<<"w: "<<par_final.omega()<<endl;
1592 cout<<"z: "<<par_final.z0()<<endl;
1593 cout<<"tanl: "<<par_final.tanDip()<<endl;
1594 }
1595 double pxy=-1*Q_iq[q_choose]/par_final.omega()/333.567;
1596 double pz=pxy*par_final.tanDip();
1597 double pxyz=sqrt(pxy*pxy+pz*pz);
1598
1599 if(m_debug>0) cout<<"pt2_rec: "<<pxy<<endl;
1600 if(m_drawTuple){
1601 m_pt2_rec_final[track_fit_success]=pxy;
1602 m_p_rec_final[track_fit_success]=pxyz;
1603 m_d0_final[track_fit_success]=par_final.d0();
1604 m_phi0_final[track_fit_success]=par_final.phi0();
1605 m_omega_final[track_fit_success]=par_final.omega();
1606 m_z0_final[track_fit_success]=par_final.z0();
1607 m_tanl_final[track_fit_success]=par_final.tanDip();
1608 m_Q[track_fit_success]=Q;
1609 }
1610
1611 int nfit3d=0;
1612 if(m_debug>1) cout<<" hot list:"<<endl;
1613 TrkHotList::hot_iterator hotIter2= qhits2[q_choose]->hotList().begin();
1614 while (hotIter2!=qhits2[q_choose]->hotList().end()) {
1615 int lay=((MdcHit*)(hotIter2->hit()))->layernumber();
1616 int wir=((MdcHit*)(hotIter2->hit()))->wirenumber();
1617 int hittemp=lay*1000+wir;
1618 if(m_debug>1){ cout <<"(" <<((MdcHit*)(hotIter2->hit()))->layernumber()
1619 <<","<<((MdcHit*)(hotIter2->hit()))->wirenumber()
1620 <<":"<<hotIter2->isActive()<<") ";
1621 }
1622 if( hotIter2->isActive()==1) {
1623 nfit3d++;
1624 if(track_fit_success==0) vec_hitOnTrack.push_back(hittemp); //single track
1625 }
1626 hotIter2++;
1627 }
1628 if(m_debug>0) cout<<"In 3D fit Num Of Active Hits: "<<nfit3d<<endl;
1629
1630 track_fit_success++;
1631 int choise_temp,choise_no;
1632 if(Q==-1) choise_temp=0,choise_no=1;
1633 if(Q==1) choise_temp=1,choise_no=0;
1634 vec_newTrack.push_back( newTrack2[choise_temp] );
1635 } //end loop of all track
1636
1637 for(int iTrack=0;iTrack<vec_newTrack.size();iTrack++){
1638 const TrkFit* newFit_combine = vec_newTrack[iTrack]->fitResult();
1639 TrkExchangePar par_combine= newFit_combine->helix(0.);
1640 double cx_combine=(-333.567/par_combine.omega()+par_combine.d0())*cos(par_combine.phi0());
1641 double cy_combine=(-333.567/par_combine.omega()+par_combine.d0())*sin(par_combine.phi0());
1642 double phi_combine=par_combine.phi0()-M_PI/2;
1643 double pxy_combine=1./par_combine.omega()/333.567;
1644 if(pxy_combine<0) phi_combine+=M_PI;
1645 if(phi_combine<0) phi_combine+=2*M_PI;
1646 if(phi_combine>2*M_PI) phi_combine-=2*M_PI;
1647
1648 Mytrack mytrack;
1649 mytrack.pt=pxy_combine;
1650 mytrack.charge=(int)(fabs(pxy_combine)/pxy_combine);
1651 mytrack.phi=phi_combine;
1652 mytrack.r=-333.567/par_combine.omega();
1653 mytrack.x_cir=cx_combine;
1654 mytrack.y_cir=cy_combine;
1655 mytrack.newTrack=vec_newTrack[iTrack];
1656 mytrack.use_in_tds=true;
1657 mytrack.vec_hit_on_trk=vec_hitOnTrack;
1658 // cout<<"size of hitontrk: "<<mytrack.vec_hit_on_trk.size()<<endl;
1659 vec_myTrack.push_back(mytrack);
1660
1661 }
1662
1663 std::sort(vec_myTrack.begin(),vec_myTrack.end(),compare);
1664
1665 for(int i=0;i<vec_newTrack.size();i++){
1666 Mytrack *mytrack_i=&(vec_myTrack[i]);
1667 if(mytrack_i->use_in_tds==false) continue;
1668 for(int j=i+1;j<vec_newTrack.size();j++){
1669 Mytrack *mytrack_j=&(vec_myTrack[j]);
1670 if(mytrack_j->use_in_tds==false) continue;
1671 if( fabs(mytrack_j->phi-mytrack_i->phi)<0.1 ) mytrack_j->use_in_tds=false;
1672 if(fabs(mytrack_j->r)*(1.-0.25) <= fabs(mytrack_i->r) && fabs(mytrack_i->r) <= fabs(mytrack_j->r)*(1.+0.25)){
1673 if(fabs(mytrack_j->x_cir-mytrack_i->x_cir) <= fabs(mytrack_j->r)*(0.25)&& fabs(mytrack_j->y_cir-mytrack_i->y_cir) <= fabs(mytrack_j->r)*(0.25) ){
1674 if(mytrack_j->charge==mytrack_i->charge)
1675 mytrack_j->use_in_tds=false;
1676 }
1677 }
1678 }
1679 }
1680
1681 int nTrack_tds=0;
1682 vector<Mytrack> vec_mytrack_in_tds;
1683 for(int i=0;i<track_fit_success;i++){
1684 Mytrack mytrack=vec_myTrack[i];
1685 if(mytrack.use_in_tds==false) continue;
1686 vec_mytrack_in_tds.push_back(mytrack);
1687 nTrack_tds++;
1688 }
1689
1690 if(m_drawTuple){
1691 m_trackNum_tds=nTrack_tds;
1692 m_trackNum_fit=track_fit_success;
1693 }
1694
1695 // RecMdcTrackCol::iterator iteritrk = trackList->begin();
1696 // for(;iteritrk!=trackList->end();iteritrk++){
1697 // cout<<"IN PATTSF: "<<endl;
1698 // cout<<"parameter: "<<(*iteritrk)->helix(0)<<" "<<(*iteritrk)->helix(1)<<" "<<(*iteritrk)->helix(2)<<" "<<(*iteritrk)->helix(3)<<" "<<(*iteritrk)->helix(4)<<endl;
1699 // cout<<"chi: "<<(*iteritrk)->chi2()<<" ndof: "<<(*iteritrk)->ndof()<<"nster: "<<(*iteritrk)->nster()<<endl;
1700 // }
1701
1702 //print track in pattsf with bad vertex
1703 // RecMdcTrackCol::iterator iteritrk_pattsf = vec_trackList.begin();
1704 // for(;iteritrk_pattsf!=vec_trackList.end();iteritrk_pattsf++){
1705 // cout<<"in PATTSF LOST: "<<(*iteritrk_pattsf)->helix(0)<<" "<<(*iteritrk_pattsf)->helix(1)<<" "<<(*iteritrk_pattsf)->helix(2)<<" "<<(*iteritrk_pattsf)->helix(3)<<" "<<(*iteritrk_pattsf)->helix(4)<<endl;
1706 // }
1707
1708 int outputTrk=0;
1709 int itrack=0;
1710 for(int i=0;i<nTrack_tds;i++){
1711 Mytrack mytrack=vec_mytrack_in_tds[i];
1712 const TrkFit* newFit_tds= mytrack.newTrack->fitResult();
1713 TrkExchangePar par_tds= newFit_tds->helix(0.);
1714 // cout<<"IN HOUGH: "<<endl;
1715 // cout<<" parameter: "<<par_tds.d0()<<" "<<par_tds.phi0()<<" "<<333.567*par_tds.omega()<<" "<<par_tds.z0()<<" "<<par_tds.tanDip()<<endl;
1716 // cout<<" chis: "<<newFit_tds->chisq()<<" ndof: "<<newFit_tds->nDof()<<" nMdc: "<<newFit_tds->nMdc()<<endl;
1717 double cr= 1./ par_tds.omega();
1718 double cx= sin(par_tds.phi0()) *(par_tds.d0()+1/par_tds.omega()) * -1.; //z axis verse,x_babar = -x_belle
1719 double cy= -1.*cos(par_tds.phi0()) *(par_tds.d0()+1/par_tds.omega()) * -1;//???
1720
1721 unsigned bestIndex = 0;
1722 double bestDiff = 1.0e+20;
1723 double cR, cX, cY;
1724 vector<double> a0,a1,a2,a3,a4;
1725 vector<double> zdelta;
1726 RecMdcTrackCol::iterator iteritrk = trackList->begin();
1727 int itrk=0;
1728 for(;iteritrk!=trackList->end();iteritrk++){
1729 double pt=(*iteritrk)->pxy();
1730 a0.push_back( (*iteritrk)->helix(0) );
1731 a1.push_back( (*iteritrk)->helix(1) );
1732 a2.push_back( (*iteritrk)->helix(2) );
1733 a3.push_back( (*iteritrk)->helix(3) );
1734 a4.push_back( (*iteritrk)->helix(4) );
1735 zdelta.push_back( par_tds.z0()-(*iteritrk)->helix(3) );
1736 // cout<<"IN the NEW List: "<<endl;
1737 // cout<<" parameter: "<<a0[itrk]<<" "<<a1[itrk]<<" "<<a2[itrk]<<" "<<a3[itrk]<<" "<<a4[itrk]<<endl;
1738 // cout<<" chi: "<<(*iteritrk)->chi2()<<" ndof: "<<(*iteritrk)->ndof()<<"nster: "<<(*iteritrk)->nster()<<endl;
1739 cR=(-333.567)/a2[itrk];
1740 cX=(cR+a0[itrk])*cos(a1[itrk]);
1741 cY=(cR+a0[itrk])*sin(a1[itrk]);
1742
1743 if(fabs(cr)*(1.-0.25) <= fabs(cR) && fabs(cR) <= fabs(cr)*(1.+0.25)){
1744 if(fabs(cx-cX) <= fabs(cr)*(0.25)&& fabs(cy-cY) <= fabs(cr)*(0.25) ){
1745 double diff = fabs((fabs(cr)-fabs(cR))*(cx-cX)*(cy-cY));
1746 if(diff < bestDiff){
1747 bestDiff = diff;
1748 bestIndex = itrk;
1749 }
1750 }
1751 }
1752 itrk++;
1753 }
1754
1755 if(bestDiff == 1.0e20) { if(m_debug>0) cout<<" no merge "<<endl; }
1756 else continue;
1757
1758 // TrkHitList* hitlist = mytrack.newTrack->hits();
1759 // TrkHitList::hot_iterator hotIter= hitlist->begin();
1760 // while (hotIter!=hitlist->end()) {
1761 // cout <<"(" <<((MdcHit*)(hotIter->hit()))->layernumber()
1762 // <<","<<((MdcHit*)(hotIter->hit()))->wirenumber()
1763 // <<":"<<hotIter->isActive()<<") ";
1764 //
1765 // if (hotIter->isActive()==0) hitlist->remove() ;
1766 // hotIter++;
1767 // }
1768
1769
1770 MdcTrack* mdcTrack;
1771 mdcTrack= new MdcTrack(mytrack.newTrack);
1772 vec_track_in_tds.push_back(mytrack.newTrack);
1773 int tkStat = 4;//track find by Hough set stat=4
1774 int tkId = nTdsTk + itrack;
1775 mdcTrack->storeTrack(tkId, trackList, hitList, tkStat);
1776 if(m_drawTuple) m_hitOnTrk[outputTrk]=mdcTrack->track().hots()->nActive();
1777 outputTrk++;
1778 delete mdcTrack;
1779
1780 double pxy=1/par_tds.omega()/333.567;
1781 double pz=pxy*par_tds.tanDip();
1782 double pxyz=sqrt(pxy*pxy+pz*pz);
1783 if(m_drawTuple){
1784 m_pt2_rec_tds[itrack]=pxy;
1785 m_p_rec_tds[itrack]=pxyz;
1786 m_d0_tds[itrack]=par_tds.d0();
1787 m_phi0_tds[itrack]=par_tds.phi0();
1788 m_omega_tds[itrack]=par_tds.omega();
1789 m_z0_tds[itrack]=par_tds.z0();
1790 m_tanl_tds[itrack]=par_tds.tanDip();
1791 m_Q_tds[itrack]=pxy>0?1:-1;
1792 }
1793 itrack++;
1794 }
1795 if(m_drawTuple) m_outputTrk=outputTrk;
1796 int nTdsTk_temp=trackList->size();
1797 if(m_debug>0) m_mdcPrintSvc->printRecMdcTrackCol();
1798
1799 /*
1800 m_track_use_intrk1=vec_hit_use_intrack1.size();
1801 int nNoise_intrk1=0;
1802 for(int ihit=0;ihit<vec_hit_use_intrack1.size();ihit++){
1803 int noise_yes = hit_noise.count( vec_hit_use_intrack1[ihit] );
1804 if(noise_yes==1) {nNoise_intrk1++; cout<<"noise hit in track1: "<<vec_hit_use_intrack1[ihit]<<endl;}
1805 }
1806 m_noise_intrk1=nNoise_intrk1;
1807 */
1808
1809
1810 // m_nhitdis1=vec_hit_use.size();
1811 // m_nhitdis2=vec_hitbeforefit.size();
1812 // for(int ihit=0;ihit<vec_hit_use.size();ihit++){
1813 // int hittemp=vec_hit_use[ihit];
1814 // m_hit_dis1[ihit]=hit_to_circle2[hittemp];
1815 // m_hit_dis1_3d[ihit]=hit_to_circle3[hittemp];
1816 // }
1817 // for(int ihit=0;ihit<vec_hitbeforefit.size();ihit++){
1818 // int hittemp=vec_hitbeforefit[ihit];
1819 // m_hit_dis2[ihit]=hit_to_circle2[hittemp];
1820 // m_hit_dis2_3d[ihit]=hit_to_circle3[hittemp];
1821 // }
1822
1823 for(int i=0;i<vec_for_clean.size();i++){
1824 delete vec_for_clean[i];
1825 }
1826 for(int i=0;i<vec_trk_for_clean.size();i++){
1827 //cout<<"size "<<vec_trk_for_clean.size()<<endl;
1828 //cout<<"vec_clean: "<<vec_trk_for_clean[i]<<endl;
1829 vector<TrkRecoTrk*>::iterator iterTrk =find(vec_track_in_tds.begin(),vec_track_in_tds.end(),vec_trk_for_clean[i]);
1830 if(iterTrk ==vec_track_in_tds.end() ) delete vec_trk_for_clean[i];
1831 //for(int j=0;j<vec_newTrack.size();j++) cout<<"vec_newTrack: "<<vec_newTrack[j]<<endl;
1832 }
1833 delete h1;
1834 delete h2;
1835
1836 if(m_drawTuple) ntuplehit->write();
1837 if(m_debug>0) cout<<"end event" <<endl;
1838 return StatusCode::SUCCESS;
1839}
1840
1841// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
1843 MsgStream log(msgSvc(), name());
1844 delete m_bfield ;
1845 delete m_context ;
1846 log << MSG::INFO << "in finalize()" << endreq;
1847 return StatusCode::SUCCESS;
1848}
1849int HoughValidUpdate::digiToHots(MdcDigiVec mdcDigiVec,TrkRecoTrk* newTrack,vector<bool> vec_truthHit,uint32_t getDigiFlag,vector<double> vec_flt_least,map<int,double> hit_to_circle1,vector<MdcHit*>& vec_for_clean){
1850 TrkHitList* trkHitList = newTrack->hits();
1851 int digiId=0;
1852 if(m_debug>0) cout<<"MdcDigiVec(in 2d fit): "<<mdcDigiVec.size()<<endl;
1853 MdcDigiVec::iterator iter = mdcDigiVec.begin();
1854 for (;iter != mdcDigiVec.end(); iter++, digiId++) {
1855 if(vec_truthHit[digiId]==false) continue;
1856 const MdcDigi* aDigi = *iter;
1857 MdcHit *hit = new MdcHit(aDigi, m_gm);
1858 vec_for_clean.push_back(hit);
1859 // MdcHit *hit=MdcHit(aDigi, m_gm);
1860 Identifier id = aDigi->identify();
1861 int layer = MdcID::layer(id);
1862 int wire = MdcID::wire(id);
1863 int hittemp=layer*1000+wire;
1864 if ((layer>=0&&layer<=7)||(layer>=20&&layer<=35)) {continue;}
1865 hit->setCalibSvc(m_mdcCalibFunSvc);
1866 hit->setCountPropTime(true);
1867 // new fltLen and ambig
1868 int newAmbig = 0;
1869 // calc. position of this point
1870 MdcRecoHitOnTrack temp(*hit, newAmbig, m_bunchT0*1.e9);//m_bunchT0 nano second here
1871 MdcHitOnTrack* newhot = &temp;
1872 double fltLen = m_gm->Layer(layer)->rMid();
1873 // newhot->setFltLen(fltLen);
1874 newhot->setFltLen(vec_flt_least[digiId]); //caculate by mc
1875 //newhot->setFltLen(0);
1876 //newhot->setHitRms(0.02);
1877 //if(m_debug>1) std::cout<<name()<<" ("<<layer<<","<<wire<<") fltLen "<<newhot->fltLen()<<std::endl;
1878 double ddCut;
1879 //if(layer<8) ddCut=0.6;
1880 //else ddCut=0.8;
1881 if(layer<8) ddCut=1.0;
1882 else ddCut=1.0;
1883 int use_in2d=1;
1884 if(hit->driftDist(m_bunchT0,0)>ddCut||vec_truthHit[digiId]==false/*||vec_theta_ontrack[digiId]>M_PI*/||fabs(hit_to_circle1[hittemp])>10.) { use_in2d=0; }
1885 if(m_debug>1) std::cout<<" ("<<layer<<","<<wire<<",rt "<<hit->rawTime()<<",dt "<<hit->driftTime(m_bunchT0,0)<<") T0 " << m_mdcCalibFunSvc->getT0(layer,wire) <<" timewalk "<< m_mdcCalibFunSvc->getTimeWalk(layer, aDigi->getChargeChannel())<<" m_bunchT0 "<<m_bunchT0<<" hit to circle: "<<hit_to_circle1[hittemp]<<" dist: "<<hit->driftDist(m_bunchT0,0)<<" use?: "<<use_in2d<<endl;
1886 //if(hit->driftTime(m_bunchT0,0)>800) continue;
1887 if(use_in2d==0) continue;
1888 trkHitList->appendHot(newhot);
1889 //delete hit;
1890 }
1891 if(m_debug>0) std::cout<<std::endl;
1892 return trkHitList->nHit();
1893}
1894
1895double HoughValidUpdate::CFtrans(double x,double y){
1896 return 2*x/(x*x+y*y);
1897}
1898
1899int HoughValidUpdate::SlantId(int layer){
1900 if ((layer>=0&&layer<=7)||(layer>=20&&layer<=35)) {
1901 if ((layer>=0&&layer<=3)||(layer>=20&&layer<=23)||(layer>=28&&layer<=31)){
1902 // m_slant[digiId]=-1;
1903 return -1;
1904 }else{
1905 return 1;
1906 }
1907 } else{
1908 return 0;
1909 }
1910}
1911
1912int HoughValidUpdate::GetMcInfo(){
1913 MsgStream log(msgSvc(), name());
1914 StatusCode sc;
1915 SmartDataPtr<McParticleCol> mcParticleCol(eventSvc(),"/Event/MC/McParticleCol");
1916 if (!mcParticleCol) {
1917 log << MSG::ERROR << "Could not find McParticle" << endreq;
1918 return -999;
1919 }
1920
1921 int itk = 0;
1922 int t_qTruth = 0; //zhangjin
1923 int t_pidTruth = -999;
1924 int t_McTkId = -999;
1925 Helix* mchelix;
1926 vector<int> vec_decay;
1927 McParticleCol::iterator iter_mc = mcParticleCol->begin();
1928 for (;iter_mc != mcParticleCol->end(); iter_mc++ ) {
1929 // if(!(*iter_mc)->primaryParticle()) continue;
1930 t_pidTruth = (*iter_mc)->particleProperty();
1931 bool t_decay= (*iter_mc)->decayInFlight();
1932 if(m_debug>0) cout<<"Decay : "<<t_decay<<endl;
1933 vec_decay.push_back(t_decay);
1934
1935 if(m_debug>2) cout<< "tk "<<itk<< " pid="<< t_pidTruth<< endl;
1936 if((m_pid!=0) && (t_pidTruth != m_pid)){ continue; } //zhangjin
1937 if(itk>=10) break;
1938
1939 int pid = t_pidTruth;
1940 if( pid == 0 ) {
1941 log << MSG::WARNING << "Wrong particle id" <<endreq;
1942 }else{
1943 IPartPropSvc* p_PartPropSvc;
1944 static const bool CREATEIFNOTTHERE(true);
1945 StatusCode PartPropStatus = service("PartPropSvc", p_PartPropSvc, CREATEIFNOTTHERE);
1946 if (!PartPropStatus.isSuccess() || 0 == p_PartPropSvc) {
1947 std::cout<< " Could not initialize Particle Properties Service" << std::endl;
1948 }
1949 HepPDT::ParticleDataTable* p_particleTable = p_PartPropSvc->PDT();
1950 std::string name;
1951 if( p_particleTable->particle(pid) ){
1952 name = p_particleTable->particle(pid)->name();
1953 t_qTruth = p_particleTable->particle(pid)->charge();
1954 }else if( p_particleTable->particle(-pid) ){
1955 name = "anti " + p_particleTable->particle(-pid)->name();
1956 t_qTruth = (-1)*p_particleTable->particle(-pid)->charge();
1957 }
1958 if(m_debug>2) std::cout << " particle "<<name<<" charge= "<<t_qTruth<<std::endl;
1959 }
1960
1961 t_McTkId = (*iter_mc)->trackIndex();
1962 double px = (*iter_mc)->initialFourMomentum().px();//GeV
1963 double py = (*iter_mc)->initialFourMomentum().py();//GeV
1964 double pz = (*iter_mc)->initialFourMomentum().pz();//GeV
1965
1966
1967 mchelix = new Helix((*iter_mc)->initialPosition().v(), (*iter_mc)->initialFourMomentum().v(), t_qTruth);//cm
1968 mchelix->pivot( HepPoint3D(0.,0.,0.) );
1969 //cout<<"pt, p : "<<mchelix->pt()<<" "<<mchelix->momentum()<<endl;
1970
1971 if(m_debug>2){
1972 std::cout<<"Truth tk "<<itk<<" pid "<<pid<<" charge "<<t_qTruth
1973 << " helix "<< mchelix->a()<<" p "<<mchelix->momentum(0.)<<std::endl;
1974 }
1975 if(m_drawTuple){
1976 m_pzTruth[itk]=pz;
1977 m_pidTruth[itk] = t_pidTruth;
1978 m_costaTruth[itk] = mchelix->cosTheta();
1979 m_ptTruth[itk] = mchelix->pt();
1980 m_pTruth[itk] = mchelix->momentum(0.).mag();
1981 m_qTruth[itk] = t_qTruth;
1982 m_drTruth[itk] = mchelix->dr();
1983 m_phi0Truth[itk] = mchelix->phi0();
1984 m_omegaTruth[itk] =1./(mchelix->radius()); //Q
1985 m_dzTruth[itk] = mchelix->dz();
1986 m_tanl_mc[itk] =mchelix->tanl();
1987 }
1988 itk++;
1989 if(m_debug>2){
1990 cout<<" d0: " <<" "<<mchelix->dr()<<endl;
1991 cout<<" phi0: " <<" "<<mchelix->phi0()<<endl;
1992 cout<<" omega: "<<" "<<1./(mchelix->radius())<<endl;
1993 cout<<" z0: " <<" "<<mchelix->dz()<<endl;
1994 cout<<" tanl: "<<" "<<mchelix->tanl()<<endl;
1995 cout<<" pt: " <<" "<<mchelix->pt()<<endl; //Q
1996 cout<<" costaTruth: " <<" "<<mchelix->cosTheta()<<endl; //Q
1997 }
1998 delete mchelix;
1999 }
2000 if(m_drawTuple){
2001 vector<int>::iterator iter_idecay=find(vec_decay.begin(),vec_decay.end(),1 );
2002 if (iter_idecay==vec_decay.end() ) m_decay=0;
2003 else m_decay=1;
2004 m_nTrkMC = itk;
2005 }
2006 if(itk!=1) {
2007 if(m_debug>2) std::cout<<"WARNING run "<<t_runNum<<" evt "<<t_eventNum<<" not single event. nTrkMc="<<itk<<std::endl;
2008 return -1;
2009 }else{
2010 if(m_debug>2) std::cout<<"nTrkMc=1"<<std::endl;
2011 return 1;
2012 }
2013
2014}
2015
2016
2017int HoughValidUpdate::LeastSquare(int n,vector<double> vec_x,vector<double> vec_y, vector<int> vec_slant,vector<int> vec_layer,vector<bool> vec_truthHit,double &d0,double &phi0, double &omega,double& circleX,double& circleY,double& circleR){
2018
2019 double x_sum=0;
2020 double y_sum=0;
2021 double x2_sum=0;
2022 double y2_sum=0;
2023 double x3_sum=0;
2024 double y3_sum=0;
2025 double x2y_sum=0;
2026 double xy2_sum=0;
2027 double xy_sum=0;
2028 double a1=0;
2029 double a2=0;
2030 double b1=0;
2031 double b2=0;
2032 double c1=0;
2033 double c2=0;
2034 double x_aver,y_aver,r2;
2035 int use_in_least=0;
2036 for(int i=0;i<n;i++){
2037 if(vec_truthHit[i]==false) continue;
2038 if(vec_slant[i]!=0) continue;
2039 x_sum=x_sum+vec_x[i];
2040 y_sum=y_sum+vec_y[i];
2041 x2_sum=x2_sum+vec_x[i]*vec_x[i];
2042 y2_sum=y2_sum+vec_y[i]*vec_y[i];
2043 x3_sum=x3_sum+vec_x[i]*vec_x[i]*vec_x[i];
2044 y3_sum=y3_sum+vec_y[i]*vec_y[i]*vec_y[i];
2045 x2y_sum=x2y_sum+vec_x[i]*vec_x[i]*vec_y[i];
2046 xy2_sum=xy2_sum+vec_x[i]*vec_y[i]*vec_y[i];
2047 xy_sum=xy_sum+vec_x[i]*vec_y[i];
2048 use_in_least++;
2049 }
2050 a1=x2_sum-x_sum*x_sum/n;
2051 a2=xy_sum-x_sum*y_sum/n;
2052 b1=a2;
2053 b2=y2_sum-y_sum*y_sum/n;
2054 c1=(x3_sum+xy2_sum-x_sum*(x2_sum+y2_sum)/n)/2.;
2055 c2=(y3_sum+x2y_sum-y_sum*(x2_sum+y2_sum)/n)/2.;
2056
2057 x_aver=x_sum/n;
2058 y_aver=y_sum/n;
2059
2060 double x_cirtemp =(b1*c2-b2*c1)/(b1*b1-a1*b2);
2061 double y_cirtemp =(b1*c1-a1*c2)/(b1*b1-a1*b2);
2062 r2=(x2_sum+y2_sum-2*x_cirtemp*x_sum-2*y_cirtemp*y_sum)/n+x_cirtemp*x_cirtemp+y_cirtemp*y_cirtemp;
2063 double r_temp=sqrt(r2);
2064
2065 circleX = x_cirtemp;
2066 circleY = y_cirtemp;
2067 circleR = r_temp;
2068
2069 double d0_temp=sqrt(x_cirtemp*x_cirtemp+y_cirtemp*y_cirtemp)-r_temp;
2070 double pt_temp=r_temp/333.567;
2071 if(m_debug>0){
2072 cout<<" in least fit : "<<endl;
2073 cout<<"rtemp: "<<r_temp<<" "<<endl;
2074 cout<<"xtemp: "<<x_cirtemp<<" "<<endl;
2075 cout<<"ytemp: "<<y_cirtemp<<" "<<endl;
2076 cout<<"d0temp: "<<d0_temp<<" "<<endl;
2077 cout<<"pt_temp: "<<pt_temp<<endl;
2078 }
2079
2080 d0=d0_temp; //opposite with Q
2081 //phi0=atan2(y_cirtemp,x_cirtemp)-(m_q)*M_PI/2.; //charge- + ; charge+ -;
2082 phi0=atan2(y_cirtemp,x_cirtemp)+M_PI/2.; //charge- + ; charge+ -;
2083 omega=1/r_temp; //according with Q
2084 double z0=0.;
2085 double tanl=0.;
2086 if(m_debug>0) std::cout<<" before global fit Helix PatPar :"<<d0<<","<<phi0<<","<<omega<<","<<z0<<","<<tanl<< std::endl;
2087 return 0;
2088
2089}
2090
2091int HoughValidUpdate::Zposition(int n,vector<int> vec_slant,double x_cirtemp,double y_cirtemp,double r_temp,vector<double> vec_x_east,vector<double> vec_x_west,vector<double> vec_y_east,vector<double> vec_y_west,vector<double> vec_z_east,vector<double> vec_z_west,vector<int> vec_layer,vector<int> vec_wire,vector<double> vec_z,vector<double>& vec_zStereo,vector<double>& l,vector<bool> vec_truthHit){
2092 double x_stereo;
2093 double y_stereo;
2094 // double theta;
2095 int stereoId=0;
2096 //vector<double> z_stereo;
2097 // vector<double> vec_zStereo;
2098 int nDropDelta=0;
2099 for(int i=0;i<n;i++){
2100 if(vec_truthHit[i]==false) continue;
2101 double k;
2102 double b;
2103 if(vec_slant[i]!=0){
2104 if(vec_x_east[i]!=vec_x_west[i]){
2105 k=(vec_y_east[i]-vec_y_west[i])/(vec_x_east[i]-vec_x_west[i]);
2106 b=-k*vec_x_east[i]+vec_y_east[i];
2107 // cout<<"eventNum: "<<t_eventNum<<" "<<"layerid: "<<" "<<m_layer[i]<<"cellid: "<<m_cell[i]<<" "<<"k: "<<k<<" "<<"b: "<<b<<" "<<m_y_east[i]<<" "<<m_y_west[i]<<" "<<m_x_east[i]<<" "<<m_x_west[i]<<" "<<endl;
2108 double delta=4*(k*b-k*y_cirtemp-x_cirtemp)*(k*b-k*y_cirtemp-x_cirtemp)-4*(k*k+1)*(x_cirtemp*x_cirtemp+b*b+y_cirtemp*y_cirtemp-2*b*y_cirtemp-r_temp*r_temp);
2109 //cout<<"layer: "<<vec_layer[i]<<"wire: "<<vec_wire[i]<<endl;
2110 //cout<<"x_east: "<<vec_x_east[i]<<" "<<"x_west: "<<vec_x_west[i]<<endl;
2111 //cout<<"k: "<<k<<" "<<"b: "<<b<<endl;
2112 //cout<<"x_cirtemp: "<<x_cirtemp<<" "<<"y_cirtemp: "<<y_cirtemp<<endl;
2113 if(delta<0) {
2114 nDropDelta++;
2115 continue;
2116 }
2117 //cout<<"delta: "<<delta<<endl;
2118 double x1=(2*x_cirtemp+2*k*y_cirtemp-2*k*b+sqrt(delta))/(2*(k*k+1));
2119 double x2=(2*x_cirtemp+2*k*y_cirtemp-2*k*b-sqrt(delta))/(2*(k*k+1));
2120 double x_middle=(vec_x_east[i]+vec_x_west[i])/2.;
2121 // if(abs(x1-vec_x[i])>abs(x2-vec_x[i])) {x_stereo=x2;}
2122 if(abs(x1-x_middle)>abs(x2-x_middle)) {x_stereo=x2;}
2123 else {x_stereo=x1;}
2124 y_stereo=k*x_stereo+b;
2125 }
2126 else{
2127 //cout<<"layer: "<<vec_layer[i]<<"wire: "<<vec_wire[i]<<endl;
2128 //cout<<"x_east: "<<vec_x_east[i]<<" "<<"x_west: "<<vec_x_west[i]<<endl;
2129 x_stereo=vec_x_east[i];
2130 double y1=sqrt(r_temp*r_temp-(x_cirtemp-x_stereo)*(x_cirtemp-x_stereo))+y_cirtemp;
2131 double y2=y_cirtemp-sqrt(r_temp*r_temp-(x_cirtemp-x_stereo)*(x_cirtemp-x_stereo));
2132 double y_middle=(vec_y_east[i]+vec_y_west[i])/2.;
2133 if(abs(y1-y_middle)>abs(y2-y_middle)) {y_stereo=y2;}
2134 else{y_stereo=y1;}
2135 }
2136 // cout<<"layer: "<<vec_layer[i]<<"wire: "<<vec_wire[i]<<"x_stereo: "<<x_stereo<<" "<<"y_stereo: "<<y_stereo<<endl;
2137
2138 double theta_temp;
2139 if(x_cirtemp==0||x_stereo-x_cirtemp==0){
2140 theta_temp=0;
2141 }
2142 else{
2143 theta_temp=M_PI-atan2(y_stereo-y_cirtemp,x_stereo-x_cirtemp)+atan2(y_cirtemp,x_cirtemp);
2144 if(theta_temp>2*M_PI){
2145 theta_temp=theta_temp-2*M_PI;
2146 }
2147 if(theta_temp<0){
2148 theta_temp=theta_temp+2*M_PI;
2149 }
2150 }
2151 //cout<<"thetatemp: "<<theta_temp<<endl;
2152 //if(theta_temp>2*M_PI) {
2153 // theta_temp=theta_temp-2*M_PI;
2154 //}
2155 //if(theta_temp<0.) {
2156 // theta_temp=theta_temp+2*M_PI;
2157 //}
2158 //cout<<"debug: if thetatemp is cout over"<<endl;
2159 l.push_back(r_temp*theta_temp);
2160 //cout<<"debug: if l is pushback"<<endl;
2161 //l.push_back(M_PI-atan2(y_stereo,x_stereo)-atan2(y_cirtemp,x_cirtemp));
2162 //l.push_back(r_temp*(M_PI-atan2(y_stereo,x_stereo)-atan2(y_cirtemp,x_cirtemp)));
2163
2164 double d1=sqrt((x_stereo-vec_x_west[i])*(x_stereo-vec_x_west[i])+(y_stereo-vec_y_west[i])*(y_stereo-vec_y_west[i]));
2165 double d2=sqrt((vec_x_east[i]-vec_x_west[i])*(vec_x_east[i]-vec_x_west[i])+(vec_y_east[i]-vec_y_west[i])*(vec_y_east[i]-vec_y_west[i]));
2166 vec_zStereo.push_back(vec_z_west[i]-(vec_z_west[i]-vec_z_east[i])*d1/d2);
2167 //cout<<"debug: if vec_zStereo is pushback"<<endl;
2168 //if(stereoId==0) {vec_zStereo.push_back(vec_z_west[i]-(vec_z_west[i]-vec_z_east[i])*d1/d2);}
2169 //if(stereoId>0){
2170 // if(vec_layer[i]==vec_layer.at(i-1)) {
2171 // vec_zStereo[(stereoId-1)]=((z_stereo.at(stereoId)+z_stereo.at(stereoId-1))/2.);
2172 // vec_zStereo.push_back((z_stereo.at(stereoId)+z_stereo.at(stereoId-1))/2.);
2173 // }
2174 // else{
2175 // vec_zStereo.push_back(vec_z_west[i]-(vec_z_west[i]-vec_z_east[i])*d1/d2);
2176 // }
2177 //}
2178
2179 // double delphi=atan2(y_stereo,x_stereo)-par.phi0();
2180 //cout<<"eventNum: "<<t_eventNum<<" "<<"layerid: "<<" "<<m_layer[i]<<"cellid: "<<" "<<m_cell[i]<<" "<<"z_stereo: "<<z_stereo<<" "<<d1<<" "<<d2<<" "<<endl;
2181 //cout<<"eventNum: "<<t_eventNum<<" "<<"layerid: "<<" "<<m_layer[i]<<"cellid: "<<m_cell[i]<<" "<<"k: "<<k<<" "<<"b: "<<b<<" "<<m_y_east[i]<<" "<<m_y_west[i]<<" "<<m_x_east[i]<<" "<<m_x_west[i]<<" "<<"x_stereo: "<<" "<<x_stereo<<" "<<"x: "<<vec_x[i]<<"y_stereo: "<<" "<<y_stereo<<" "<<"y: "<<vec_y[i]<<" "<<"zstereo: "<<z_stereo<<endl;
2182 // cout<<"layerid: "<<" "<<vec_layer[i]<<" "<<"cellid: "<<vec_wire[i]<<" "<<"x_stereo: "<<" "<<x_stereo<<" "<<"y_stereo: "<<" "<<y_stereo<<" "<<"zstereo: "<<z_stereo.at(stereoId)<<" "<<vec_zStereo.at(stereoId)<<" "<<"ztruth: "<<vec_z[i]<<endl;
2183
2184 // cout<<"layer: "<<vec_layer[i]<<"wire: "<<vec_wire[i]<<"z: "<<vec_zStereo[stereoId]<<" "<<"l: "<<l[stereoId]<<" "<<vec_truthHit[i]<<endl;
2185 stereoId=stereoId+1;
2186 //cout<<"theta: "<<theta<<endl;
2187 }
2188 else {k=b=0;}
2189 }
2190 cout<<"nDropDelta: "<<nDropDelta<<endl;
2191 // cout<<"if for is finished : "<<endl;
2192 //int digiId=0;
2193 //for(int i=0;i<n;i++){
2194 // if(vec_slant[i]!=0){
2195 // cout<<"layerid: "<<" "<<vec_layer[i]<<" "<<"cellid: "<<vec_wire[i]<<" "<<"zstereo: "<<z_stereo.at(digiId)<<" "<<vec_zStereo.at(digiId)<<" "<<"ztruth: "<<vec_z[i]<<endl;
2196 // digiId++;
2197 // }
2198 //}
2199 //double sum_zstereo=0;
2200 //for(int i=0;i<stereoId;i++){
2201 // sum_zstereo=sum_zstereo+z_stereo[i];
2202 //}
2203 //cout<<"sum_zstereo: "<<sum_zstereo<<endl;
2204 return stereoId;
2205}
2206
2207
2208void HoughValidUpdate::Linefit(int z_stereoNum,vector<double> vec_zStereo,vector<double> l,double& tanl,double& z0){
2209
2210 double l_sum=0;
2211 double z_sum=0;
2212 double l2_sum=0;
2213 double z2_sum=0;
2214 double lz_sum=0;
2215 for(int i=0;i<z_stereoNum;i++){
2216 // cout<<"event: "<<t_eventNum<<" "<<"z: "<<vec_zStereo[i]<<" "<<"l: "<<l[i]<<endl;
2217 l_sum=l_sum+l[i];
2218 z_sum=z_sum+vec_zStereo[i];
2219 l2_sum=l2_sum+l[i]*l[i];
2220 // cout<<l2_sum<<endl;
2221 z2_sum=z2_sum+vec_zStereo[i]*vec_zStereo[i];
2222 lz_sum=lz_sum+l[i]*vec_zStereo[i];
2223 }
2224 double b=(l2_sum*z_sum-lz_sum*l_sum)/(z_stereoNum*l2_sum-l_sum);
2225 double k=(z_stereoNum*lz_sum-l_sum*z_sum)/(z_stereoNum*l2_sum-l_sum*l_sum);
2226 z0=b;
2227 tanl=k;
2228 cout<<k<<" "<<b<<endl;
2229}
2230
2231
2232
2233int HoughValidUpdate::digiToHots2(MdcDigiVec mdcDigiVec,TrkRecoTrk* newTrack,vector<bool> vec_truthHit,vector<double> vec_flt_truth,uint32_t getDigiFlag, vector<int> vec_slant, vector<double> vec_l_Calcu,vector<double> vec_flt,vector<double> vec_theta_ontrack,vector<int>& vec_hitbeforefit,map<int,double> hit_to_circle2,vector<MdcHit*>& vec_for_clean,double tanl){
2234 TrkHitList* trkHitList = newTrack->hits();
2235 //MdcDigiVec mdcDigiVec = m_rawDataProviderSvc->getMdcDigiVec(getDigiFlag);
2236 if(m_debug>0) cout<<"MdcDigiVec(in 3d fit): "<<mdcDigiVec.size()<<endl;
2237 MdcDigiVec::iterator iter1 = mdcDigiVec.begin();
2238 int digiId=0;
2239 int nhit_beforefit_temp=0;
2240 for (;iter1 != mdcDigiVec.end(); iter1++, digiId++) {
2241 //cout<<"fltcut: "<<m_fltcut<<endl;
2242 if(vec_theta_ontrack[digiId]>M_PI) continue;
2243 // if(digiId>20&&vec_slant[digiId]!=0) continue;
2244 const MdcDigi* aDigi = *iter1;
2245 MdcHit *hit = new MdcHit(aDigi, m_gm);
2246 vec_for_clean.push_back(hit);
2247 //MdcHit *hit=MdcHit(aDigi, m_gm);
2248 Identifier id = aDigi->identify();
2249 int layer = MdcID::layer(id);
2250 int wire = MdcID::wire(id);
2251 int hittemp=layer*1000+wire;
2252 hit->setCalibSvc(m_mdcCalibFunSvc);
2253 hit->setCountPropTime(true); //hit->setCountPropTime(m_countPropTime);
2254
2255 // new fltLen and ambig
2256 int newAmbig = 0;
2257 // calc. position of this point
2258 MdcRecoHitOnTrack temp(*hit, newAmbig, m_bunchT0*1.e9);//m_bunchT0 nano second here
2259 MdcHitOnTrack* newhot = &temp;
2260 double fltLen = m_gm->Layer(layer)->rMid();
2261 // newhot->setFltLen(fltLen);
2262 newhot->setFltLen(vec_flt[digiId]);
2263 //if (vec_slant[digiId]==0) {newhot->setFltLen(vec_flt[digiId]);/* cout<<" ("<<layer<<","<<wire<<") "<<"l_truth: "<<vec_flt_truth[digiId]<<" l_axil: "<<vec_flt[digiId]<<endl;*/} //caculate by mc
2264 // else {newhot->setFltLen(vec_l_Calcu[digiId]);/* cout<<" ("<<layer<<","<<wire<<") "<<" l_truth: "<<vec_flt_truth[digiId]<<" l_Calcu: "<<vec_l_Calcu[digiId]<<endl;*/} //caculate by mc
2265 // newhot->setActivity(1);
2266 //bool active = newhot->isActive();
2267 //newhot->setHitRms(0.02);
2268 //if(m_debug>1) std::cout<<name()<<" ("<<layer<<","<<wire<<") fltLen "<<newhot->fltLen()<<std::endl;
2269 double ddCut;
2270 double distcirCut;
2271 //if(layer<8) {ddCut=0.8; distcirCut=5;}
2272 //else {ddCut=0.6; distcirCut=2;}
2273 if(layer<8) {ddCut=1.0; distcirCut=5;}
2274 else {ddCut=1.0; distcirCut=2;}
2275 int use_in3d=1;
2276 if(hit->driftDist(m_bunchT0,0)>ddCut||vec_truthHit[digiId]==false||/*vec_theta_ontrack[digiId]>M_PI||*/fabs(hit_to_circle2[hittemp])>distcirCut) { use_in3d=0; }
2277 if(m_debug>2) std::cout<<" ("<<layer<<","<<wire<<",rt "<<hit->rawTime()<<",dt "<<hit->driftTime(m_bunchT0,0)<<") T0 " << m_mdcCalibFunSvc->getT0(layer,wire) <<" timewalk "<< m_mdcCalibFunSvc->getTimeWalk(layer, aDigi->getChargeChannel())<<" vec_truth: "<<vec_truthHit[digiId]<<" theta: "<<vec_theta_ontrack[digiId]<<" ddcut?: "<<hit->driftDist(m_bunchT0,0)<<" distocir: "<<hit_to_circle2[hittemp]<<" use:? "<<use_in3d<<endl;
2278 if(use_in3d==0) continue;
2279
2280 vec_hitbeforefit.push_back(hittemp);
2281 trkHitList->appendHot(newhot);
2282 }
2283 if(m_debug>0) std::cout<<std::endl;
2284 return trkHitList->nHit();
2285}
2286void HoughValidUpdate:: Multi_array(int binX,int binY){
2287 int **count=new int*[binX];
2288 for(int i=0;i<binX;i++){
2289 count[i]=new int [binY];
2290 }
2291 //vector<int> vec_selectNum[binX][binY];
2292 int ***selectNum=new int**[binX];
2293 for(int i=0;i<binX;i++){
2294 selectNum[i]=new int* [binY];
2295 for(int j=0;j<binY;j++){
2296 //selectNum[i][j]=new int[];
2297 selectNum[i][j]=new int [count[i][j]];
2298 }
2299 }
2300}
2301
2302void HoughValidUpdate::RhoTheta(int nhit,vector<double> vec_u,vector<double> vec_v,vector<double>& vec_rho,vector<double>& vec_theta,vector< vector<int> >& vec_hitNum,vector<int> vec_digitruth){
2303 for(int i=0;i<nhit;i++){
2304 if(vec_digitruth[i]==0) continue;
2305 for(int j=i+1;j<nhit;j++){
2306 if(vec_digitruth[j]==0) continue;
2307 double k,b,x_cross,y_cross;
2308 double rho_temp,theta_temp;
2309 k=(vec_v[i]-vec_v[j])/(vec_u[i]-vec_u[j]);
2310 b=vec_v[i]-k*vec_u[i];
2311 x_cross=-b/(k+1/k);
2312 y_cross=b/(k*k+1);
2313 rho_temp=sqrt(x_cross*x_cross+y_cross*y_cross);
2314 theta_temp=atan2(y_cross,x_cross);
2315 if(theta_temp<0) {
2316 theta_temp=theta_temp+M_PI;
2317 rho_temp=-rho_temp;
2318 }
2319 vec_rho.push_back(rho_temp);
2320 vec_theta.push_back(theta_temp);
2321 vec_hitNum[0].push_back(i);
2322 vec_hitNum[1].push_back(j);
2323 //cout<<"u:"<<vec_u[i]<<" "<<"v:"<<vec_v[i]<<" "<<"("<<vec_theta[i]<<","<<vec_rho[i]<<")"<<endl;
2324 }
2325 }
2326 int nCross=vec_rho.size();
2327 m_nCross=vec_rho.size();
2328 if(m_nCross>125000) return ;
2329 // cout<<"numCross: "<<nCross<<endl;
2330
2331 for(int i=0;i<nCross;i++){
2332 m_rho[i]=vec_rho[i];
2333 m_theta[i]=vec_theta[i];
2334 //cout<<" "<<vec_theta[i]<<" "<<vec_rho[i]<<endl;
2335 }
2336}
2337void HoughValidUpdate::RhoThetaAxial(vector<int> vec_slant,int nhit,vector<double> vec_u,vector<double> vec_v,vector<double>& vec_rho,vector<double>& vec_theta,vector< vector<int> >& vec_hitNum,vector<int> vec_digitruth){
2338 for(int i=0;i<nhit;i++){
2339 if(vec_digitruth[i]==0) continue;
2340 if(vec_slant[i]!=0) continue;
2341 for(int j=i+1;j<nhit;j++){
2342 if(vec_slant[j]!=0) continue;
2343 if(vec_digitruth[j]==0) continue;
2344 double k,b,x_cross,y_cross;
2345 double rho_temp,theta_temp;
2346 k=(vec_v[i]-vec_v[j])/(vec_u[i]-vec_u[j]);
2347 b=vec_v[i]-k*vec_u[i];
2348 x_cross=-b/(k+1/k);
2349 y_cross=b/(k*k+1);
2350 rho_temp=sqrt(x_cross*x_cross+y_cross*y_cross);
2351 theta_temp=atan2(y_cross,x_cross);
2352 if(theta_temp<0) {
2353 theta_temp=theta_temp+M_PI;
2354 rho_temp=-rho_temp;
2355 }
2356 vec_rho.push_back(rho_temp);
2357 vec_theta.push_back(theta_temp);
2358 vec_hitNum[0].push_back(i);
2359 vec_hitNum[1].push_back(j);
2360 //cout<<"u:"<<vec_u[i]<<" "<<"v:"<<vec_v[i]<<" "<<"("<<vec_theta[i]<<","<<vec_rho[i]<<")"<<endl;
2361 }
2362 }
2363 int nCross=vec_rho.size();
2364 if( m_drawTuple ) m_nCross=vec_rho.size();
2365
2366 if(m_drawTuple){
2367 for(int i=0;i<nCross;i++){
2368 m_rho[i]=vec_rho[i];
2369 m_theta[i]=vec_theta[i];
2370 }
2371 }
2372
2373}
2374void HoughValidUpdate::FillHist(TH2D *h1,vector<double> vec_u,vector<double> vec_v,int nhit,vector< vector < vector< int > > > &vec_selectNum){
2375 for(int n=0;n<nhit;n++){
2376 // if(vec_digitruth[n]==0) continue;
2377 for(int i=0;i<binX;i++){
2378 double binwidth=M_PI/binX;
2379 double binhigh=2*m_maxrho/binY;
2380 double theta=(i+0.5)*binwidth;
2381 double rho=vec_u[n]*cos(theta)+vec_v[n]*sin(theta);
2382 int j=(int)((rho+m_maxrho)/binhigh);
2383 int count=h1->GetBinContent(i+1,j+1);
2384 count+=1;
2385 h1->SetBinContent(i+1,j+1,count);
2386 // h1->Fill(theta,rho);
2387 vec_selectNum[i][j].push_back(n);
2388 }
2389 }
2390}
2391void HoughValidUpdate::FillRhoTheta(TH2D *h1 ,vector<double> vec_theta,vector<double> vec_rho){
2392 for(int i=0;i<vec_theta.size();i++){
2393 double binwidth=M_PI/binX;
2394 double binhigh=2*m_maxrho/binY;
2395 int binxNum=vec_theta[i]/binwidth+1;
2396 int binyNum=(vec_rho[i]+m_maxrho)/binhigh+1;
2397 int count =h1->GetBinContent(binxNum,binyNum);
2398 count++;
2399 h1->SetBinContent(binxNum,binyNum,count);
2400 }
2401}
2402//int HoughValidUpdate::zPosition(MdcHitUse & hitUse, const TrkExchangePar &par, MdcLine* span,int hitFit, double t0, double Bz) const
2403
2404int HoughValidUpdate::zPosition(MdcHit& h, double t0, double x_cirtemp,double y_cirtemp, double r_temp,int digiId,double& delta_z,double& delta_l,double& l_Calcu_Left,double& l_Calcu_Right,double& z_Calcu_Left,double& z_Calcu_Right,double& z_Calcu,double& l_Calcu,int& i_q) {
2405 //const MdcHit & h = *(hitUse.mdcHit());
2406
2407 //ambig not sure
2408 //int ambig = hitUse.ambig();
2409 int return_d[2];
2410 int return_ok[2];
2411 return_d[0]=1;
2412 return_d[1]=1;
2413 return_ok[0]=1;
2414 return_ok[1]=1;
2415 for(int ambig=-1;ambig<=1;ambig=ambig+2){
2416 //int ambig =m_postruth[digiId];
2417 //if(ambig==0) ambig =-1;
2418 HepPoint3D fp ( h.wire()->getWestPoint()->x(),h.wire()->getWestPoint()->y(),h.wire()->getWestPoint()->z());
2419 HepPoint3D rp ( h.wire()->getEastPoint()->x(),h.wire()->getEastPoint()->y(),h.wire()->getEastPoint()->z());
2420
2421
2422 HepVector3D X = 0.5 * (fp + rp);
2423 if(m_debug>0){
2424 std::cout<<"---------- "<<std::endl;//yzhang debug
2425 h.print(std::cout);
2426 //h.wire()->print(std::cout);
2427 std::cout<<"fp rp "<<fp<<" "<<rp<<std::endl;//yzhang debug
2428 std::cout<<"Xmid "<<X<<std::endl;//yzhang debug
2429 }
2430 HepVector3D xx = HepVector3D(X.x(), X.y(), 0.);
2431 //center of helix/circle
2432 //change to belle param
2433
2434 /*
2435 double d0 = -par.d0();
2436 double phi0 = (par.phi0()-pi/2);
2437 double _charge = -1;
2438 if((-1)*par.omega()/Bz > 0) _charge = 1;
2439 */
2440
2441 //d0temp,phi0,omega,x_cirtemp,y_cirtemp, r_temp,m_q
2442 //double r;
2443 //if (fabs(par.omega())< Constants::epsilon) r = 9999.;
2444 //else r = 1/par.omega(); //+ -
2445
2446 //double xc = sin(par.phi0()) *(-d0+r) * -1.; //z axis verse,x_babar = -x_belle
2447 //double yc = -1.*cos(par.phi0()) *(-d0+r) * -1;//???
2448 //HepPoint3D center (xc, yc, 0. );
2449 HepPoint3D center (x_cirtemp, y_cirtemp, 0. );
2450
2451 HepVector3D yy = center - xx;
2452 HepVector3D ww = HepVector3D(yy.x(), yy.y(), 0.);
2453 double wwmag2 = ww.mag2();
2454 double wwmag = sqrt(wwmag2);
2455
2456
2457 //dirftDist
2458 double driftdist = fabs( h.driftDist(t0,ambig) );
2459 HepVector3D lr(driftdist/wwmag * ww.x(),
2460 driftdist/wwmag * ww.y(), 0.);
2461 if (m_debug >4){
2462 std::cout<<"xc "<<x_cirtemp << " yc "<<y_cirtemp<< " drfitdist "<<driftdist<<std::endl;
2463 //std::cout<<"r1 "<<r <<" d0 "<<d0 <<" phi0 "<<phi0<<std::endl;//
2464 //std::cout<<"lr "<<lr<<" hit ambig "<<hitUse.ambig()<<" ambig "<<ambig
2465 std::cout<<"lr "<<lr<<" "<<" ambig "<<ambig
2466 <<" left "<<h.driftDist(0,1)
2467 <<" right "<<h.driftDist(0,-1)<<std::endl;
2468 }
2469
2470 //...Check left or right...
2471 HepPoint3D ORIGIN = HepPoint3D(0., 0., 0.);
2472 //ambig
2473 //-1 right -phi direction driftDist-=lr
2474 //, +1 left +phi directon driftDist+=lr
2475 if (ambig == 0) lr = ORIGIN;
2476 // cout<<"m_q: "<<m_q<<" i_q: "<<i_q<<endl;
2477 if (i_q> 0){ //yzhang
2478 if (ambig == -1){
2479 lr = - lr;//right
2480 }
2481 }else{
2482 if (ambig == 1){
2483 lr = - lr;//left
2484 }
2485 }
2486 X += lr;
2487
2488 //...Prepare vectors...
2489 // HepPoint3D center = _helix->center();
2490 HepPoint3D tmp(-9999., -9999., 0.);
2491 HepVector3D x = HepVector3D(X.x(), X.y(), 0.);
2492 HepVector3D w = x - center;
2493 // //modified the next sentence because the direction are different from belle.
2494 HepVector3D V = h.wire()->zAxis();
2495 HepVector3D v = HepVector3D(V.x(), V.y(), 0.);
2496 double vmag2 = v.mag2();
2497 //double vmag = sqrt(vmag2);
2498
2499 //double r = _helix->curv();
2500 double wv = w.dot(v);
2501 // wv = abs(wv);
2502 double d2 = wv * wv - vmag2 * (w.mag2() - r_temp * r_temp);
2503 if (m_debug >4){
2504 std::cout<<"X_fix "<<X<<" center "<<center<<std::endl;
2505 std::cout<<"V "<<V<<std::endl;//yzhang debug
2506 std::cout<<"w "<<w<<" v "<<v<<std::endl;
2507 std::cout<<"wv "<<wv<<endl;
2508 cout<<"d2: "<<d2<<endl;
2509 }
2510 //...No crossing in R/Phi plane... This is too tight...
2511
2512 if (d2 < 0.) {
2513 //hitUse.position(tmp);
2514 if (m_debug>4){
2515 cout<<"d2: "<<d2<<endl;
2516 std::cout << "in MdcSegInfoSterO !!! stereo: 0. > d2 = " << d2 << " "
2517 << ambig << std::endl;
2518 }
2519 //return -1;
2520 if (ambig==-1) return_d[ambig+1]=0;
2521 else return_d[ambig]=0;
2522 continue;
2523 }
2524 double d = sqrt(d2);
2525 // cout<<"d: "<<d<<endl;
2526
2527
2528 double l[2];
2529 l[0] =-1*( (- wv + d) / vmag2 ); //multiply -1 ......?
2530 l[1] =-1*( (- wv - d) / vmag2 );
2531
2532 double z[2];
2533 //z[0] = X.z() + l[0] * V.z();
2534 z[0] = X.z() - l[0] * V.z(); //l *-1
2535 z[1] = X.z() - l[1] * V.z();
2536
2537
2538 //...Cal. length to crossing points...
2539 // double l[2];
2540 //if (debug >0){
2541 //std::cout<<"wv "<<wv<<" d "<<d<<" vmag2 "<<vmag2<<std::endl;//yzhang debug
2542 //}
2543 // l[0] = (- wv + d) / vmag2;
2544
2545 // l[1] = (- wv - d) / vmag2;
2546
2547 //...Cal. z of crossing points...
2548 bool ok[2];
2549 ok[0] = true;
2550 ok[1] = true;
2551 //double z[2];
2552 // z[0] = X.z() + l[0] * V.z();
2553 //z[1] = X.z() + l[1] * V.z();
2554 if (m_debug >4){//yzhang m_debug
2555 std::cout << "X.z "<<X.z()<<" V.z "<<V.z()<<std::endl;
2556 std::cout << "l0, l1 = " << l[0] << ", " << l[1] << std::endl;
2557 std::cout << "rpz " << rp.z() << " fpz " << fp.z() << std::endl;
2558 std::cout << "z0 " << z[0] << " z1 " << z[1] << std::endl;
2559 }
2560 //...Check z position...
2561 if (ambig == 0) {
2562 if(m_debug>4)std::cout<< " ambig = 0 " <<std::endl;
2563 if (z[0] > rp.z()+20. || z[0] < fp.z()-20.) {
2564 ok[0] = false;
2565 }
2566 if (z[1] > rp.z()+20. || z[1] < fp.z()-20.) {
2567 ok[1] = false;
2568 }
2569 } else {
2570 if(m_debug>4)std::cout<< " ambig != 0 " <<std::endl;
2571 // if (z[0] > rp.z() || z[0] < fp.z() )
2572 if (fabs(z[0]/rp.z())>1.05 ) { ok[0] = false; }
2573 //if (z[1] > rp.z() || z[1] < fp.z() )
2574 if (fabs(z[1]/rp.z())>1.05 ) { ok[1] = false; }
2575 }
2576 if ((! ok[0]) && (! ok[1])) {
2577 if(m_debug>4&&(ambig!=0)&&fabs(z[1]/rp.z())>1.05)std::cout<< " z[1] bad " << std::endl;
2578 if(m_debug>4&&(ambig!=0)&&fabs(z[0]/rp.z())>1.05)std::cout<< " z[0] bad " << std::endl;
2579 //hitUse.position(tmp);
2580 if(m_debug>4) {
2581 std::cout<< " z[1] bad " << "rpz " << rp.z() << " fpz " << fp.z()
2582 << "z0 " << z[0] << " z1 " << z[1] << std::endl;
2583 std::cout<< " !ok[0] && !ok[1] return -2" <<std::endl;
2584 }
2585 //return -2;
2586 if (ambig==-1) return_ok[ambig+1]=0;
2587 else return_ok[ambig]=0;
2588 continue;
2589 }
2590 if (( ok[0]) && ( ok[1])) {
2591 // cout<<" two z is ok ..........?" <<endl;
2592 }
2593 unsigned best = 0;
2594 if (ok[1]) best = 1;
2595 HepVector3D p[2];
2596 p[0] = x + l[0] * v;
2597 p[1] = x + l[1] * v;
2598 double cosdPhi = - center.dot((p[best] - center).unit()) / center.mag();
2599 double dPhi;
2600 if(fabs(cosdPhi)<=1.0) {
2601 dPhi = acos(cosdPhi);
2602 } else if (cosdPhi>1.0) {
2603 dPhi = 0.0;
2604 } else {
2605 dPhi = pi;
2606 }
2607 double stemp=r_temp*dPhi;
2608 if( m_debug >1) cout<<" ambig : "<<ambig<<" z: "<<z[best]<<" stemp: "<<stemp<<endl;
2609 double delta_temp=0;
2610 if(m_drawTuple && m_useMcInfo) delta_temp =z[best]-m_ztruth[digiId];
2611 if( m_debug >3) cout<<"deltatemp: "<<delta_temp<<endl;
2612 // vec_delta_z.push_back(delta_temp); wrong cant push_back in circulate
2613 //vec_delta_z[digiId]=delta_temp;
2614 // delta_z=delta_temp;
2615 // cout<<" vec_delta in position: "<<delta_z<<endl;
2616
2617 // fill z_Cacu
2618 if(ambig==1){
2619 z_Calcu_Left=z[best];
2620 l_Calcu_Left=stemp;
2621 //cout<<"ambig: "<<ambig<<" z0 : "<<z[0]<<" z1: "<<z[1]<<" ztruth: "<<m_ztruth[digiId]<<endl;
2622 if( m_debug >1 && m_drawTuple &&m_useMcInfo) cout<<"ambig: "<<ambig<<" zbest : "<<z[best]<<" ztruth: "<<m_ztruth[digiId]<<endl;
2623 if( m_debug >1 && m_drawTuple &&m_useMcInfo) cout<<"ambig: "<<ambig<<" lbest : "<<stemp<<" ltruth: "<<m_ltruth[digiId]<<endl;
2624 if( m_useMcInfo && m_drawTuple && m_postruth[digiId]==1) { z_Calcu=z[best]; l_Calcu=stemp; delta_z= delta_temp; delta_l =l_Calcu-m_ltruth[digiId];}
2625 }
2626 if(ambig==-1){
2627 z_Calcu_Right=z[best];
2628 l_Calcu_Right=stemp;
2629 //cout<<"ambig: "<<ambig<<" z0 : "<<z[0]<<" z1: "<<z[1]<<" ztruth: "<<m_ztruth[digiId]<<endl;
2630 if( m_debug >1 && m_drawTuple &&m_useMcInfo) cout<<"ambig: "<<ambig<<" zbest : "<<z[best]<<" ztruth: "<<m_ztruth[digiId]<<endl;
2631 if( m_debug >1 && m_drawTuple &&m_useMcInfo) cout<<"ambig: "<<ambig<<" lbest : "<<stemp<<" ltruth: "<<m_ltruth[digiId]<<endl;
2632 if( m_useMcInfo && m_drawTuple && m_postruth[digiId]==0) { z_Calcu=z[best]; l_Calcu=stemp; delta_z= delta_temp; delta_l =l_Calcu-m_ltruth[digiId];}
2633 }
2634
2635
2636 ////...Cal. xy position of crossing points...
2637
2638 //if(m_debug>0){
2639 // std::cout<<__FILE__<<" "<<__LINE__<< " p0 "<<p[0].x()<<" "<<p[0].y()<< std::endl;
2640 // std::cout<<__FILE__<<" "<<__LINE__<< " p1 "<<p[1].x()<<" "<<p[1].y()<< std::endl;
2641 // std::cout<<__FILE__<<" "<<__LINE__<< " c "<<center.x()<<" "<<center.y()<<" "<< std::endl;
2642 // std::cout<<__FILE__<<" "<<__LINE__<< " p0 centerx*y "<<center.x() * p[0].y()<<" centery*x"<<center.y() * p[0].x()<< std::endl;
2643 // std::cout<<__FILE__<<" "<<__LINE__<< " p1 centerx*y "<<center.x() * p[1].y()<<" centery*x"<<center.y() * p[1].x()<< std::endl;
2644 //}
2645
2646
2647 //...Which one is the best?... Study needed...
2648 //unsigned best = 0;
2649 //if (ok[1]) best = 1;
2650 //std::cout<<"in MdcSegInfoSterO Zbelle "<<z[best]<<std::endl;//yzhang m_debug
2651
2652 /*
2653 //...Cal. arc length...
2654 double cosdPhi = - center.dot((p[best] - center).unit()) / center.mag();
2655 double dPhi;
2656 if(fabs(cosdPhi)<=1.0) {
2657 dPhi = acos(cosdPhi);
2658 } else if (cosdPhi>1.0) {
2659 dPhi = 0.0;
2660 } else {
2661 dPhi = pi;
2662 }
2663
2664 //...Finish...
2665 tmp.setX(abs(r * dPhi));
2666 tmp.setY(z[best]);
2667 //hitUse.position(tmp);
2668
2669 //z
2670 span->y[hitFit] = z[best];
2671 //if (ok[0]) span->y[hitFit] = p[0].mag();//r
2672 //else if(ok[1]) span->y[hitFit] = p[1].mag();
2673 //else span->y[hitFit] = tmp.mag();
2674 span->x[hitFit] = abs(r * dPhi);
2675 if (hitUse.ambig()<0) driftdist *= -1.;
2676 span->sigma[hitFit] = h.sigma(driftdist,hitUse.ambig());
2677
2678 if (m_debug >0){
2679 std::cout<<"("<<h.layernumber()<<","<<h.wirenumber()<<") s "<<span->x[hitFit]<<" z "<<span->y[hitFit]<<std::endl;//yzhang m_debug
2680 }
2681 return 1;
2682 */
2683 }
2684 // cout<<"ooooooooooooooooooooooooooooooooooooooooo"<<endl;
2685 if(return_d[0]==0&&return_d[1]==0) return -1;
2686 if(return_ok[0]==0&&return_ok[1]==0) return -2;
2687 return 1;
2688}
2689void HoughValidUpdate::AmbigChoose(vector< vector< vector<double> > > point,int n_use,vector<int>& ambigList,double& tanl, double& z0){
2690 //connect two points
2691 int n =n_use;
2692 int ambig_list[4];
2693 // TF1 *fpol1 =new TF1("fpol1","pol1",7,17);
2694 double Chis_Least;
2695 //TGraph *tgr[4];
2696 double Chis[4];
2697 int ambig_combine=0;
2698 // three points chis
2699 double chi_k[4];
2700 double chi_b[4];
2701 double first_x[3];
2702 double first_y[3];
2703 first_x[0]=0;
2704 first_y[0]=0;
2705 vector< vector <int> > select(4,vector<int>() );
2706 for(int icombine1=0;icombine1<2;icombine1++){
2707 for(int icombine2=0;icombine2<2;icombine2++){
2708 int combine=icombine1*2+icombine2;
2709 first_x[1]=point[icombine1][0][n-1];
2710 first_x[2]=point[icombine2][0][n-2];
2711 first_y[1]=point[icombine1][1][n-1];
2712 first_y[2]=point[icombine2][1][n-2];
2713 double k,b;
2714 double chi2=0;
2715 LeastLine(3,first_x,first_y,k,b,chi2);
2716 // TGraph *gr_first=new TGraph(3,first_x,first_y);
2717 //gr_first->Fit("fpol1","QN");
2718 //delete gr_first;
2719 //double k=fpol1->GetParameter(1);
2720 //double b=fpol1->GetParameter(0);
2721 //cout<<"K: "<<k<<" B: "<<b<<endl;
2722
2723 // sprintf(hname,"h%d",combine);
2724 // h[combine]=new TH2D("hname","hname",100,7,17,100,-5,35);
2725 // double k=(point[icombine1][1][n-1]-point[icombine2][1][n-2])/(point[icombine1][0][n-1]-point[icombine2][0][n-2]); //left-left
2726 // double b=point[icombine1][1][n-1]-k*point[icombine1][0][n-1];
2727 // cout<<"k: "<<k<<"b: "<<b<<endl;
2728
2729 for(int iHit=0;iHit<n-2;iHit++){
2730 double dist_to_line[2];
2731 for(int j=0;j<2;j++){
2732 double x=point[j][0][iHit];
2733 double y=point[j][1][iHit];
2734 dist_to_line[j]=( abs(k*x-y+b) ) / sqrt( k*k +1);
2735 }
2736 if(dist_to_line[0]>dist_to_line[1]) select[combine].push_back(1);
2737 else select[combine].push_back(0);
2738 // cout<<select[combine][iHit]<<endl;
2739 }
2740 select[combine].push_back(icombine2); //left -left
2741 select[combine].push_back(icombine1);
2742
2743 //least 2* fit
2744 //add (0,0)
2745 // vector<double> l;
2746 // vector<double> z;
2747 double *l_new=new double[n+1];
2748 double *z_new=new double[n+1];
2749 for(int i=0;i<n;i++){
2750 int ambig=select[combine][i];
2751 l_new[i]=point[ambig][0][i];
2752 z_new[i]=point[ambig][1][i];
2753 }
2754 l_new[n]=0;
2755 z_new[n]=0;
2756 //for(int i=0;i<n;i++){
2757 // int ambig=select[combine][i];
2758 // cout<<"ambig : "<<ambig<<endl;
2759 // l.push_back(point[ambig][0][i]);
2760 // z.push_back(point[ambig][1][i]);
2761 // cout<<"hit: "<<i<<" :("<<l[i]<<","<<z[i]<<")"<<endl;
2762 //}
2763 //l.push_back(0);
2764 //z.push_back(0);
2765
2766 double k_least,b_least;
2767 double chi2_least=0;
2768 LeastLine(n+1,l_new,z_new,k_least,b_least,chi2_least);
2769 Chis[combine]=chi2_least;
2770 chi_k[combine]=k_least;
2771 chi_b[combine]=b_least;
2772
2773 // tgr[combine]=new TGraph(n+1,l_new,z_new);
2774 // tgr[combine]->Fit("fpol1","QN");
2775 delete []l_new;
2776 delete []z_new;
2777
2778
2779 // TH2D *h1=new TH2D("h1","h1",100,7,17,100,-5,35);
2780 //for(int i =0 ;i<n+1; i++){
2781 // h[combine]->Fill(l[i],z[i]);
2782 // //h->Draw("SAME");
2783 //}
2784 // h[combine]->Fit("fpol1");
2785 //Chis[combine]=fpol1->GetChisquare();
2786 //chi_k[combine]=fpol1->GetParameter(1);
2787 //chi_b[combine]=fpol1->GetParameter(0);
2788 // cout<<" chis: "<<Chis[combine]<<endl;
2789 //delete tgr[combine];
2790 // delete h[combine];
2791 } //icombine1
2792 } //icombine2
2793 // for(int i =0;i<4;i++){
2794 // delete h[i];
2795 // }
2796 // delete fpol1;
2797 Chis_Least=Chis[0];
2798 for(int i=0;i<4;i++){
2799 if(Chis_Least>=Chis[i]) {Chis_Least=Chis[i]; ambig_combine=i;/* cout<<"least i: "<< i<<endl;*/}
2800 }
2801 for(int iHit=0;iHit<n;iHit++){
2802 ambigList.push_back(select[ambig_combine][iHit]);
2803 // cout<<"ambig_combine: "<<ambig_combine<<endl;
2804 // cout<<" iHit: "<<select[ambig_combine][iHit]<<endl;
2805 // cout<<" iHit: "<<ambigList[iHit]<<endl;
2806 }
2807
2808 tanl=chi_k[ambig_combine];
2809 z0=chi_b[ambig_combine];
2810 // cout<<"Chi least: "<<Chis_Least<<" ambig: "<<ambig_combine<<endl;
2811 // cout<<"ambig_combine: "<<ambig_list[ambig_combine]<<endl;
2812}
2813void HoughValidUpdate::Combine(TH2D *h1,int widthX,int widthY,vector< vector < vector<int> > > vec_selectNum,M1 &peak_hit_list,M2 &peak_hit_num,int &peak_track,vector<int> peakList[]){
2814 int m=0;
2815 int n=0;
2816 int addnum=999;
2817 int npeak1=peak_track;
2818 vector<bool> peaktrack(npeak1,true);
2819 while(addnum!=0)
2820 {
2821 // cout<<"Ntrack: "<<n<<endl;
2822 addnum=0;
2823 // int peakXtemp=peakList[0][0]-1;
2824 // int peakYtemp=peakList[1][0]-1;
2825 // cout<<"vec_selectpeak: ("<<peakXtemp<<","<<peakYtemp<<"): "<<vec_selectNum[peakXtemp][peakYtemp].size()<<endl;
2826 // for(int i =0;i<vec_selectNum[peakXtemp][peakYtemp].size();i++){cout<<" iN:vec: "<<vec_selectNum[peakXtemp][peakYtemp][i]<<endl;}
2827
2828 int peakX=peakList[0][n];
2829 int peakY=peakList[1][n];
2830 for (int px=peakX-widthX; px<=peakX+widthX; px++) {
2831 for (int py=peakY-widthY; py<=peakY+widthY; py++) {
2832 int ax;
2833 if (px<0) { ax=px+binX; }
2834 else if (px>=binX) { ax=px-binX; }
2835 else { ax=px; }
2836 if ( (ax!=peakX || py!=peakY) && py>=0 && py<binY) Combine_two_cell(h1,peakX,peakY,ax,py,vec_selectNum,m,peak_hit_list,peak_hit_num);
2837 }
2838 }
2839 // for(int i=0;i<peak_hit_list[m].size();i++){
2840 // cout<<"COMBINE: "<<peak_hit_list[m][i]<<endl;
2841 // }
2842
2843 for(int i=n+1;i<npeak1;i++){
2844 if(peaktrack[i]==false) continue;
2845 int peaktheta=peakList[0][i];
2846 int peakrho=peakList[1][i];
2847 int peakhitNum=peakList[2][i];
2848 int count_same_hit=0;
2849 for(int j=0;j<peakhitNum;j++){
2850 for(int k=0;k<peak_hit_num[m];k++){
2851 if(vec_selectNum[peaktheta][peakrho][j]==peak_hit_list[m][k]){
2852 count_same_hit++;
2853 }
2854 }
2855 }
2856 if(m_debug>1) cout<<" pro: "<<"peak: "<<i<<" "<<(double)count_same_hit/peakhitNum<<endl;
2857 double f_hit=m_hit_pro;
2858 if(count_same_hit>f_hit*peakhitNum){
2859 peaktrack[i]=false;
2860 // cout<<" track_candi: "<<i<<" is false"<<endl;
2861 }
2862 }
2863 for(int i=n+1;i<npeak1;i++){
2864 if(peaktrack[i]==true){
2865 addnum=i-n;
2866 break;
2867 }
2868 }
2869 if(addnum!=0) m++;
2870 n=n+addnum;
2871 }
2872 int ntrack=0;
2873 for(int i=0;i<npeak1;i++){
2874 //cout<<"track"<<i<<": "<<"("<<peakList[0][i]<<","<<peakList[1][i]<<","<<peakList[2][i]<<")"<<" "<<"truth: "<<peaktrack[i]<<endl;
2875 if(peaktrack[i]==true){
2876 ntrack++;
2877 }
2878 }
2879 peak_track=ntrack;
2880}
2881void HoughValidUpdate::Combine_two_cell(TH2D *h1,int xPeakCell,int yPeakCell,int xAround,int yAround,vector< vector < vector<int> > > vec_selectNum,int m,M1 &peak_hit_list,M2 &peak_hit_num){
2882 int size_of_around=vec_selectNum[xAround][yAround].size();
2883 if(size_of_around==0) return;
2884 // cout<<"size of around : "<<size_of_around<<endl;
2885 //for(int iaround=0;iaround<size_of_around;iaround++){
2886 // cout<<"around: "<<vec_selectNum[xAround][yAround][iaround]<<endl;
2887 //}
2888 int size_of_peak=vec_selectNum[xPeakCell][yPeakCell].size();
2889 // cout<<"size of peak: "<<size_of_peak<<endl;
2890 // for(int ipeak=0;ipeak<size_of_peak;ipeak++){
2891 // cout<<"peak: "<<vec_selectNum[xPeakCell][yPeakCell][ipeak]<<endl;
2892 // }
2893 if(peak_hit_list[m].size()==0){
2894 for(int inum=0;inum<size_of_peak;inum++){
2895 peak_hit_list[m][inum]=vec_selectNum[xPeakCell][yPeakCell][inum];
2896 }
2897 }
2898 vector<int> vec_more_select;
2899 for (int i =0;i<size_of_around;i++){
2900 int same_vec_with_hitcombin=0;
2901 for(int j= 0;j<peak_hit_list[m].size();j++){
2902 if(vec_selectNum[xAround][yAround][i]==peak_hit_list[m][j]){
2903 same_vec_with_hitcombin=1;
2904 break;
2905 }
2906 }
2907 if(same_vec_with_hitcombin==0) {
2908 // cout<<" peak_hit_list push : "<<vec_selectNum[xAround][yAround][i]<<endl;
2909 vec_more_select.push_back(vec_selectNum[xAround][yAround][i]);
2910 }
2911 }
2912 int peak_hit_list_size=peak_hit_list[m].size();
2913 for(int i=0;i<vec_more_select.size();i++ ){
2914 peak_hit_list[m][i+peak_hit_list_size]=vec_more_select[i];
2915 }
2916 int peak_hit_list_size_new=peak_hit_list[m].size();
2917 peak_hit_num[m]=peak_hit_list_size_new;
2918 // cout<<"peak_combine: "<<peak_hit_num[m]<<endl;
2919 for(int i= 0;i<peak_hit_list_size_new;i++){
2920 // cout<<" peak_hit_list: "<<peak_hit_list[m][i]<<endl;
2921 }
2922
2923
2924
2925 // for(int i =0;i<size_of_around;i++){
2926 // int count_same_with_peak=0;
2927 // for(int j =0;j<vec_selectNum[xPeakCell][yPeakCell].size();j++){
2928 // if(vec_selectNum[xAround][yAround][i]==vec_selectNum[xPeakCell][yPeakCell][j]) {
2929 // count_same_with_peak=1;
2930 // break;
2931 // }
2932 // }
2933 // if(count_same_with_peak==0) {
2934 // // cout<<" push : "<<vec_selectNum[xAround][yAround][i]<<endl;
2935 // vec_selectNum[xPeakCell][yPeakCell].push_back(vec_selectNum[xAround][yAround][i]);
2936 // }
2937 // }
2938 //
2939 // int size_of_peak_update=vec_selectNum[xPeakCell][yPeakCell].size();
2940}
2941//bool HoughValidUpdate::operator<(const HoughValidUpdate::Mytrack& a,const HoughValidUpdate::Mytrack& b){
2942// return a.pt>b.pt;
2943//}
2945 return abs(a.pt)>abs(b.pt);
2946}
2947int HoughValidUpdate::LeastLine(int nhit,double x[],double y[],double &k,double &b,double& chi2){
2948 double x_sum=0;
2949 double y_sum=0;
2950 double x2_sum=0;
2951 double y2_sum=0;
2952 double xy_sum=0;
2953 for(int i=0;i<nhit;i++){
2954 x_sum=x_sum+x[i];
2955 y_sum=y_sum+y[i];
2956 x2_sum=x2_sum+x[i]*x[i];
2957 y2_sum=y2_sum+y[i]*y[i];
2958 xy_sum=xy_sum+x[i]*y[i];
2959 }
2960 b=(x2_sum*y_sum-xy_sum*x_sum)/(nhit*x2_sum-x_sum*x_sum);
2961 k=(nhit*xy_sum-x_sum*y_sum)/(nhit*x2_sum-x_sum*x_sum);
2962
2963 double yE[3];
2964 for(int i=0;i<nhit;i++){
2965 yE[i]=k*x[i]+b;
2966 double chi2_temp;
2967 if(yE[i]!=0) chi2_temp=(y[i]-yE[i])*(y[i]-yE[i])/yE[i]*yE[i];
2968 else chi2_temp=0;
2969 chi2+=chi2_temp;
2970 }
2971
2972 return 1;
2973}
HepGeom::Vector3D< double > HepVector3D
const Int_t n
Double_t x[10]
EvtStreamInputIterator< typename Generator::result_type > iter(Generator gen, int N=0)
HepGeom::Point3D< double > HepPoint3D
Definition: Gam4pikp.cxx:37
bool compare(const HoughValidUpdate::Mytrack &a, const HoughValidUpdate::Mytrack &b)
double sin(const BesAngle a)
double cos(const BesAngle a)
ObjectVector< RecMdcHit > RecMdcHitCol
ObjectVector< RecMdcTrack > RecMdcTrackCol
SmartRefVector< RecMdcHit > HitRefVec
const HepPoint3D ORIGIN
Constants.
Definition: TMDCUtil.cxx:47
**********Class see also m_nmax DOUBLE PRECISION m_amel DOUBLE PRECISION m_x2 DOUBLE PRECISION m_alfinv DOUBLE PRECISION m_Xenph INTEGER m_KeyWtm INTEGER m_idyfs DOUBLE PRECISION m_zini DOUBLE PRECISION m_q2 DOUBLE PRECISION m_Wt_KF DOUBLE PRECISION m_WtCut INTEGER m_KFfin *COMMON c_KarLud $ !Input CMS energy[GeV] $ !CMS energy after beam spread beam strahlung[GeV] $ !Beam energy spread[GeV] $ !z boost due to beam spread $ !electron beam mass *ff pair spectrum $ !minimum v
Definition: KarLud.h:35
bool compare(const HoughValidUpdate::Mytrack &a, const HoughValidUpdate::Mytrack &b)
#define M_PI
Definition: TConstant.h:4
Hep3Vector momentum(double dPhi=0.) const
returns momentum vector after rotating angle dPhi in phi direction.
double radius(void) const
returns radious of helix.
double dr(void) const
returns an element of parameters.
const HepVector & a(void) const
returns helix parameters.
const HepPoint3D & pivot(void) const
returns pivot position.
StatusCode beginRun()
HoughValidUpdate(const std::string &name, ISvcLocator *pSvcLocator)
StatusCode initialize()
double getT0(int layid, int cellid) const
double getTimeWalk(int layid, double Q) const
const MdcLayer * Layer(unsigned id) const
static MdcDetector * instance()
Definition: MdcDetector.cxx:21
const MdcGeoWire *const Wire(unsigned id)
Definition: MdcGeomSvc.cxx:768
void setCalibSvc(const MdcCalibFunSvc *calibSvc)
Definition: MdcHit.cxx:136
void print(std::ostream &o) const
double driftTime(double tof, double z) const
Definition: MdcHit.cxx:142
void setCountPropTime(const bool count)
double driftDist(double, int, double, double, double) const
Definition: MdcHit.cxx:156
const MdcSWire * wire() const
static int layer(const Identifier &id)
Values of different levels (failure returns 0)
Definition: MdcID.cxx:49
static int wire(const Identifier &id)
Definition: MdcID.cxx:54
void printRecMdcTrackCol() const
const Hep3Vector & zAxis(void) const
const HepPoint3D * getWestPoint(void) const
const HepPoint3D * getEastPoint(void) const
void storeTrack(int trackId, RecMdcTrackCol *trackList, RecMdcHitCol *hitList, int tkStat)
Definition: MdcTrack.cxx:143
static void readMCppTable(std::string filenm)
virtual Identifier identify() const
Definition: RawData.cxx:15
unsigned int getChargeChannel() const
Definition: RawData.cxx:45
virtual double chisq() const =0
virtual int nActive() const =0
virtual TrkExchangePar helix(double fltL) const =0
TrkErrCode fit()
Definition: TrkHitList.cxx:59
TrkHitOnTrk * appendHot(const TrkHitOnTrk *theHot)
Definition: TrkHitList.cxx:91
const TrkHotList & hotList() const
Definition: TrkHitList.cxx:51
virtual int nActive(TrkEnums::TrkViewInfo view=TrkEnums::bothView) const =0
virtual void print(std::ostream &) const
const TrkFit * fitResult() const
Definition: TrkRecoTrk.cxx:387
virtual void printAll(std::ostream &) const
bool setFlipAndDrop(TrkRecoTrk &, bool allowFlips, bool allowDrops) const
TrkRecoTrk * makeTrack(const TrkExchangePar &helix, const double chi2, const TrkContext &, double trackT0) const
Pair combine(Index i, Index j)
Definition: EvtCyclic3.cc:158
uint32_t count(const node_t &list)
Definition: node.cxx:42