00001 #include "NueAna/ParticlePID/ParticleFinder/Display/HoughView.h"
00002 #include "NueAna/ParticlePID/ParticleFinder/Managed/ClusterManager.h"
00003 #include "NueAna/ParticlePID/ParticleFinder/Managed/ManagedCluster.h"
00004 #include "TPolyLine.h"
00005 #include "TMarker.h"
00006 #include "TArrow.h"
00007 #include "TMath.h"
00008
00009 #include "Conventions/SimFlag.h"
00010 #include <vector>
00011 #include "MCNtuple/NtpMCStdHep.h"
00012 #include "MCNtuple/NtpMCRecord.h"
00013 #include "MCNtuple/NtpMCTruth.h"
00014 #include <math.h>
00015 #include "TruthHelperNtuple/NtpTHEvent.h"
00016
00017 #include <map>
00018
00019 #include "NueAna/ParticlePID/ParticleFinder/HoughLine.h"
00020
00021
00022 using namespace std;
00023
00024 HoughView::HoughView():Page()
00025 {
00026 viewU=0;
00027 viewV=0;
00028 padU=0;
00029 padV=0;
00030 houghviewU=0;
00031 houghviewV=0;
00032
00033 }
00034
00035
00036 HoughView::~HoughView()
00037 {}
00038
00039
00040 void HoughView::BuildDisplay(TCanvas *c)
00041 {
00042 myCanvas=c;
00043 myCanvas->cd();
00044 padU=new TPad("houghview_padu","houghview_padu",0,0.5,0.7,1.0);
00045 padV=new TPad("houghview_padv","houghview_padv",0,0,0.7,0.5);
00046
00047
00048 padU->Draw();
00049 padV->Draw();
00050
00051
00052
00053 }
00054
00055
00056 void HoughView::DrawEvent(ParticleObjectHolder * poh, NtpStRecord * ,int )
00057 {
00058 myCanvas->cd();
00059
00060 padU->Clear();
00061 padV->Clear();
00062
00063 padU->Divide(2,1);
00064 padV->Divide(2,1);
00065
00066 mypoh=poh;
00067
00068 int isMC=0;
00069 if(poh->GetHeader().GetVldContext().GetSimFlag() == SimFlag::kMC)isMC=1;
00070
00071 double minz= mypoh->event.minz;
00072 double minu= mypoh->event.minu;
00073 double minv= mypoh->event.minv;
00074 double maxz= mypoh->event.maxz;
00075 double maxu= mypoh->event.maxu;
00076 double maxv= mypoh->event.maxv;
00077
00078
00079
00080 if(isMC)
00081 {
00082 if(mypoh->mctrue.vtx_z>=0)minz=minz<mypoh->mctrue.vtx_z ? minz : mypoh->mctrue.vtx_z;
00083 maxz=maxz>mypoh->mctrue.vtx_z ? maxz : mypoh->mctrue.vtx_z;
00084 minu=minu<mypoh->mctrue.vtx_u ? minu : mypoh->mctrue.vtx_u;
00085 maxu=maxu>mypoh->mctrue.vtx_u ? maxu : mypoh->mctrue.vtx_u;
00086 minv=minv<mypoh->mctrue.vtx_v ? minv : mypoh->mctrue.vtx_v;
00087 maxv=maxv>mypoh->mctrue.vtx_v ? maxv : mypoh->mctrue.vtx_v;
00088 }
00089
00090
00091
00092
00093 minz -= 0.1;
00094 minu -= 0.1;
00095 minv -= 0.1;
00096 maxz += 0.1;
00097 maxu += 0.1;
00098 maxv += 0.1;
00099
00100 int nu=(int)((maxu-minu)/0.0412);
00101 int nv=(int)((maxv-minv)/0.0412);
00102 int nz=(int)((maxz-minz)/0.0354);
00103
00104
00105 if(viewU)delete viewU;
00106 if(viewV)delete viewV;
00107
00108 if(houghviewU)delete houghviewU;
00109 if(houghviewV)delete houghviewV;
00110
00111 viewU = new TH2D("hu","Clusters U", nz, minz,maxz,nu,minu, maxu);
00112 viewV = new TH2D("hv","Clusters V", nz, minz,maxz,nv,minv, maxv);
00113 houghviewU = new TH2D("hhu","Hough U", 80, 0,3.141592,50,-1,1);
00114 houghviewV = new TH2D("hhv","Hough V", 80, 0,3.141592,50,-1,1);
00115 viewU->SetStats(0);
00116 viewV->SetStats(0);
00117 viewU->SetDirectory(0);
00118 viewV->SetDirectory(0);
00119
00120 houghviewU->SetStats(0);
00121 houghviewV->SetStats(0);
00122 houghviewU->SetDirectory(0);
00123 houghviewV->SetDirectory(0);
00124
00125 if(mypoh)
00126 {
00127
00128
00129
00130
00131
00132 padU->cd(1);
00133
00134 viewU->Draw();
00135 DrawClusters(2,viewU);
00136 DrawVertex(2);
00137 if(isMC)DrawTrueVertex(2);
00138
00139
00140 padU->cd(2);
00141 DrawHough(2,houghviewU);
00142
00143
00144
00145 padV->cd(1);
00146 viewV->Draw();
00147 DrawClusters(3,viewV);
00148 DrawVertex(3);
00149 if(isMC)DrawTrueVertex(3);
00150
00151 padV->cd(2);
00152 DrawHough(3,houghviewV);
00153
00154
00155
00156
00157 }
00158
00159
00160 double maxz_u = viewU->GetMaximum();
00161 double maxz_v = viewV->GetMaximum();
00162 double maxz_both = maxz_u>maxz_v?maxz_u:maxz_v;
00163 viewU->GetZaxis()->SetRangeUser(0, maxz_both);
00164 viewV->GetZaxis()->SetRangeUser(0, maxz_both);
00165
00166 padU->Update();
00167 padV->Update();
00168
00169 myCanvas->Update();
00170
00171 }
00172
00173
00174 void HoughView::DrawChains(int view)
00175 {
00176
00177 ChainHelper * ch = 0;
00178 if(view==2)
00179 ch=&mypoh->chu;
00180 else if(view==3)
00181 ch=&mypoh->chv;
00182 else return;
00183
00184
00185 std::vector<int> path =ch->FindMaxPath();
00186
00187 for(unsigned int i=0;i<ch->finished.size();i++)
00188 {
00189 if(ch->finished[i].parentChain>-1 || ch->finished[i].entries < 1)continue;
00190 int parentcount=2;
00191 Chain *todraw=ch->GetChain(ch->finished[i].myId);
00192
00193
00194 int dsize = todraw->t.size();
00195 int needconnect=0;
00196
00197 if (dsize==1)
00198
00199 {
00200
00201 if(todraw->parentChain>-1)
00202 {
00203 dsize=2;
00204 needconnect=1;
00205 }
00206 }
00207
00208 double * tt = new double[todraw->t.size()];
00209 double * tz = new double[todraw->t.size()];
00210 if(needconnect)
00211 {
00212 tt[0]=ch->GetChain(todraw->parentChain)->end_t;
00213 tz[0]=ch->GetChain(todraw->parentChain)->end_z;
00214 tz[1]=todraw->z[0];
00215 tt[1]=todraw->t[0];
00216 }
00217 else
00218 {
00219 std::copy(todraw->t.begin(),todraw->t.end(),tt);
00220 std::copy(todraw->z.begin(),todraw->z.end(),tz);
00221
00222 if(dsize==1)
00223 {
00224 tt[1]=tt[0];
00225 tz[1]=tz[0]-0.03;
00226 dsize=2;
00227
00228 }
00229
00230 }
00231
00232 TPolyLine *pl = new TPolyLine(dsize,tz,tt );
00233
00234
00235
00236
00237
00238 pl->SetLineColor(2);
00239
00240 int width=1;
00241
00242
00243 for(unsigned int j=0;j<path.size();j++)
00244 {
00245 if(path[j]==todraw->myId)width+=1;
00246 }
00247
00248 pl->SetLineWidth(width);
00249
00250 pl->Draw("same");
00251
00252 for (unsigned int k=0;k<ch->finished[i].children.size();k++)
00253 {
00254 parentcount++;
00255
00256 std::vector<int> c = ch->GetAllChildren(ch->GetChain(ch->finished[i].children[k]));
00257
00258 printf("parent %d children ", ch->finished[i].myId);
00259
00260 for(unsigned int j=0;j<c.size();j++)
00261 {
00262
00263 Chain *todraw=ch->GetChain(c[j]);
00264
00265 printf("%d ",todraw->myId);
00266
00267
00268 int dsize = todraw->t.size();
00269 int needconnect=0;
00270
00271 if (dsize==1)
00272
00273 {
00274
00275
00276 if(todraw->parentChain>-1)
00277 {
00278 dsize=2;
00279 needconnect=1;
00280 }
00281 }
00282
00283 double * tt = new double[todraw->t.size()];
00284 double * tz = new double[todraw->t.size()];
00285 if(needconnect)
00286 {
00287 tt[0]=ch->GetChain(todraw->parentChain)->end_t;
00288 tz[0]=ch->GetChain(todraw->parentChain)->end_z;
00289 tz[1]=todraw->z[0];
00290 tt[1]=todraw->t[0];
00291 }
00292 else
00293 {
00294 std::copy(todraw->t.begin(),todraw->t.end(),tt);
00295 std::copy(todraw->z.begin(),todraw->z.end(),tz);
00296
00297 if(dsize==1)
00298 {
00299 tt[1]=tt[0];
00300 tz[1]=tz[0]-0.03;
00301 dsize=2;
00302
00303 }
00304
00305 }
00306
00307 TPolyLine *pl = new TPolyLine(dsize,tz,tt );
00308
00309
00310
00311
00312 pl->SetLineColor(parentcount);
00313
00314
00315 int width=1;
00316
00317
00318 for(unsigned int j=0;j<path.size();j++)
00319 {
00320 if(path[j]==todraw->myId)width+=1;
00321 }
00322
00323 pl->SetLineWidth(width);
00324
00325 pl->Draw("same");
00326
00327 }printf("\n");
00328 }}
00329
00330 }
00331
00332
00333
00334
00335
00336 void HoughView::DrawClusters(int view, TH2 * h)
00337 {
00338
00339 std::map<double, std::map<double, int> >::iterator p_iterr;
00340 std::map<double, int>::iterator s_iterr;
00341
00342
00343 Managed::ClusterManager *cluh = &mypoh->clusterer;
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360 for(p_iterr=cluh->GetClusterMap(view)->begin();p_iterr!=cluh->GetClusterMap(view)->end(); p_iterr++)
00361 {
00362
00363 for(s_iterr=p_iterr->second.begin();s_iterr!=p_iterr->second.end(); s_iterr++)
00364 {
00365
00366 h->Fill( p_iterr->first,s_iterr->first, cluh->GetCluster(s_iterr->second)->e);
00367
00368 }
00369 }
00370
00371
00372
00373 Managed::ClusterSaver * cluhs = &mypoh->cluster_saver;
00374 for(p_iterr=cluhs->GetClusterMap(view)->begin();p_iterr!=cluhs->GetClusterMap(view)->end(); p_iterr++)
00375 {
00376
00377 for(s_iterr=p_iterr->second.begin();s_iterr!=p_iterr->second.end(); s_iterr++)
00378 {
00379 Managed::ManagedCluster * clus = cluhs->GetCluster(s_iterr->second);
00380
00381
00382 if(clus->GetStatus()==1)continue;
00383
00384 h->Fill( p_iterr->first,s_iterr->first, cluhs->GetCluster(s_iterr->second)->e);
00385
00386 }
00387 }
00388 h->SetStats(0);
00389 h->Draw("colz");
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423
00424
00425
00426
00427
00428 double maxvale=0;
00429 for(p_iterr=cluh->GetClusterMap(view)->begin();p_iterr!=cluh->GetClusterMap(view)->end(); p_iterr++)
00430 {
00431 for(s_iterr=p_iterr->second.begin();s_iterr!=p_iterr->second.end(); s_iterr++)
00432 {
00433 Managed::ManagedCluster * clus = cluh->GetCluster(s_iterr->second);
00434 if(clus->e>maxvale)
00435 {
00436 maxvale=clus->e;
00437 }
00438 }
00439 }
00440
00441
00442 for(p_iterr=cluh->GetClusterMap(view)->begin();p_iterr!=cluh->GetClusterMap(view)->end(); p_iterr++)
00443 {
00444 for(s_iterr=p_iterr->second.begin();s_iterr!=p_iterr->second.end(); s_iterr++)
00445 {
00446 Managed::ManagedCluster * clus = cluh->GetCluster(s_iterr->second);
00447
00448
00449
00450 if(clus->GetStatus()==1)continue;
00451 TMarker *m=new TMarker(clus->z,clus->t, 25);
00452 m->Draw("same");
00453
00454
00455 }
00456 }
00457
00458
00459
00460
00461
00462
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472
00473
00474
00475
00476
00477 }
00478
00479 bool operator < (const HoughLine & left, const HoughLine & right)
00480 {
00481 if(left.ncluster < right.ncluster)return true;
00482 return false;
00483 }
00484
00485
00486
00487 void HoughView::DrawVertex(int view)
00488 {
00489
00490 double t=0;
00491 if(view==2)
00492 t=mypoh->event.vtx_u;
00493 else if(view==3)
00494 t=mypoh->event.vtx_v;
00495 else return;
00496
00497 TMarker *m = new TMarker(mypoh->event.vtx_z, t, 3);
00498 m->Draw("same");
00499
00500 }
00501
00502 void HoughView::DrawTrueVertex(int view)
00503 {
00504
00505 double t=0;
00506 if(view==2)
00507 t=mypoh->mctrue.vtx_u;
00508 else if(view==3)
00509 t=mypoh->mctrue.vtx_v;
00510 else return;
00511
00512 TMarker *m = new TMarker(mypoh->mctrue.vtx_z, t, 2);
00513 m->Draw("same");
00514
00515 }
00516
00517
00518 void HoughView::DrawHough(int view, TH2 * )
00519 {
00520
00521 if(view==2)padU->cd(2);
00522 if(view==3)padV->cd(2);
00523
00524 double t=0;
00525 double z=0;
00526
00527 if(!mypoh->psf.ran)return;
00528 int cp = mypoh->psf.FindFirstIntersection(view,t,z);
00529 if(cp)
00530 {
00531 TMarker *m=new TMarker(z,t, 29);
00532 m->Draw("same");
00533 printf("closest intersection z %f t %f\n",z,t);
00534 }
00535
00536
00537
00538 TH2D * inth = view==2? mypoh->psf.intU:mypoh->psf.intV;
00539 if(inth)
00540 {
00541 inth->SetStats(0);
00542 inth->Draw("colz");
00543 }
00544
00545
00546
00547
00548
00549
00550
00551
00552 if(view==2)padU->cd(1);
00553 if(view==3)padV->cd(1);
00554
00555
00556
00557
00558
00559
00560
00561
00562
00563
00564
00565
00566 std::vector<HoughLine>*houghlines = mypoh->psf.GetHoughLines(view);
00567
00568 printf("# hough lines %d\n",(int)houghlines->size());
00569
00570 int max =-1;
00571 if((*houghlines).size()>50)max = (*houghlines).size()-50;
00572 for(int i=(*houghlines).size()-1;i>max;i--)
00573 {
00574 if((*houghlines)[i].ncluster<1)continue;
00575
00576
00577
00578
00579
00580
00581
00582
00583
00584
00585 TLine *l=new TLine((*houghlines)[i].start_z,(*houghlines)[i].start_t,(*houghlines)[i].end_z,(*houghlines)[i].end_t);
00586
00587 if((*houghlines)[i].primary)l->SetLineWidth(2);
00588 l->Draw();
00589
00590
00591
00592 }
00593
00594
00595
00596
00597
00598
00599
00600
00601
00602
00603
00604 return;
00605
00607
00608
00609
00610
00611
00612
00613
00614
00615
00616
00617
00618
00619
00620
00621
00622
00623
00624
00625
00626
00627
00628
00629
00630
00631
00632
00633
00634
00635
00636
00637
00638
00639
00640
00641
00642
00643
00644
00645
00646
00647
00648
00649
00650
00651
00652
00653
00654
00655
00656
00657
00658
00659
00660
00661
00662
00663
00664
00665
00666
00667
00668
00669
00670
00671
00672
00673
00674
00675
00676
00677
00678
00679
00680
00681
00682
00683
00684
00685
00686
00687
00688
00689
00690
00691
00692
00693
00694
00695
00696
00697
00698
00699
00700
00701
00702
00703
00704
00705
00706
00707
00708
00709
00710
00711
00712
00713
00714
00715
00716
00717
00718
00719
00720
00721
00722
00723
00724
00725
00726
00727
00728
00729
00730
00731
00732
00733
00734
00735
00736
00737
00738
00739
00740
00741
00742
00743
00744
00745
00746
00747
00748
00749
00750
00751
00752
00753
00754
00755
00756
00757
00758
00759
00760
00761
00762
00763
00764
00765
00766
00767
00768
00769
00770
00771
00772
00773
00774
00775
00776
00777
00778
00779
00780
00781
00782
00783
00784
00785
00786
00787
00788
00789
00790
00791
00792
00793
00794
00795
00796
00797
00798
00799
00800
00801
00802
00803
00804
00805
00806
00807
00808
00809
00810
00811
00812
00813
00814
00815
00816
00817
00818
00819
00820
00821
00822
00823
00824
00825
00826
00827
00828
00829
00830
00831
00832
00833
00834
00835
00836
00837
00838
00839
00840
00841
00842
00843
00844
00845
00846
00847
00848
00849
00850
00851
00852
00853
00854
00855
00856
00857
00858
00859
00860
00861
00862
00863
00864
00865
00866
00867
00868
00869
00870
00871
00872
00873
00874
00875
00876
00877
00878
00879
00880
00881
00882
00883
00884
00885
00886
00887
00888
00889
00890
00891
00892
00893
00894
00895
00896
00897
00898
00899
00900
00901
00902
00903
00904
00905
00906
00907
00908
00909
00910
00911
00912
00913
00914
00915
00916
00917
00918
00919
00920
00921
00922
00923
00924
00925
00926
00927
00928
00929 }
00930
00931
00932 void HoughView::ClearHit(TH2D * his, double below_val, int curx, int cury)
00933 {
00934 if(curx<1 || cury < 1 || curx> his->GetNbinsX() || cury >his->GetNbinsY())return;
00935
00936
00937
00938 double thisval = his->GetBinContent(curx,cury);
00939 if(thisval>=below_val)return;
00940
00941
00942
00943 ClearHit(his,below_val,curx,cury+1);
00944 ClearHit(his,below_val,curx+1,cury);
00945 ClearHit(his,below_val,curx,cury-1);
00946 ClearHit(his,below_val,curx-1,cury);
00947 ClearHit(his,below_val,curx+1,cury+1);
00948 ClearHit(his,below_val,curx-1,cury-1);
00949 ClearHit(his,below_val,curx+1,cury-1);
00950 ClearHit(his,below_val,curx-1,cury+1);
00951
00952
00953
00954 his->SetBinContent(curx,cury,0);
00955
00956 }
00957
00958 void HoughView::SaveHitGroup(TH2D * his, TH2D * saver, double save_val, double with_val, int curx, int cury)
00959 {
00960 if(curx<1 || cury < 1 || curx> his->GetNbinsX() || cury >his->GetNbinsY())return;
00961
00962
00963
00964 double thisval = his->GetBinContent(curx,cury);
00965
00966
00967 if(thisval==0)return;
00968 if(fabs(thisval-save_val)<0.001)
00969 {
00970 saver->SetBinContent(curx, cury,thisval);
00971
00972 }
00973 else if(thisval>=with_val)return;
00974
00975
00976 his->SetBinContent(curx,cury,0);
00977
00978 SaveHitGroup(his,saver,save_val,thisval,curx+1,cury);
00979 SaveHitGroup(his,saver,save_val,thisval,curx,cury+1);
00980 SaveHitGroup(his,saver,save_val,thisval,curx-1,cury);
00981 SaveHitGroup(his,saver,save_val,thisval,curx,cury-1);
00982 SaveHitGroup(his,saver,save_val,thisval,curx+1,cury+1);
00983 SaveHitGroup(his,saver,save_val,thisval,curx-1,cury-1);
00984 SaveHitGroup(his,saver,save_val,thisval,curx+1,cury-1);
00985 SaveHitGroup(his,saver,save_val,thisval,curx-1,cury+1);
00986
00987
00988
00989
00990
00991 }
00992
00993
00994 void HoughView::GetPeakAreaAverage(double &x, double &y,double &val, int & cnt, int curx, int cury, TH2D * hist)
00995 {
00996
00997 double thisval = hist->GetBinContent(curx,cury);
00998
00999 if(thisval==0)return;
01000
01001 val+=thisval;
01002 x+=hist->GetXaxis()->GetBinCenter(curx);
01003 y+=hist->GetYaxis()->GetBinCenter(cury);
01004 cnt++;
01005 hist->SetBinContent(curx,cury,0);
01006
01007
01008 GetPeakAreaAverage(x, y, val,cnt, curx+1, cury, hist);
01009 GetPeakAreaAverage(x, y, val,cnt, curx, cury+1, hist);
01010 GetPeakAreaAverage(x, y, val,cnt, curx, cury-1, hist);
01011 GetPeakAreaAverage(x, y, val,cnt, curx-1, cury, hist);
01012 GetPeakAreaAverage(x, y, val,cnt, curx+1, cury+1, hist);
01013 GetPeakAreaAverage(x, y, val,cnt, curx-1, cury-1, hist);
01014 GetPeakAreaAverage(x, y, val,cnt, curx+1, cury-1, hist);
01015 GetPeakAreaAverage(x, y, val,cnt, curx-1, cury+1, hist);
01016
01017
01018 }