bool KVSpiderIdentificator::SearchPeack(TH1F* h1_, double theta_, int create_, double sigma_, double peakmin_, int , int smooth_, TString opt_) { if (!TestHistogram(h1_)) { Warning("SearchPeack", "Bad histo..."); return false; } _dtemp = (KVDroite*)_dlist.FindObject(Form("CUT_%06.3lf", theta_)); TF1* ff = _dtemp->GetFunction(); // h1_->Rebin(rebin_*_bfactor); h1_->Smooth(smooth_); if (_nAngleUp == 0 && _nAngleDown == 0) { opt_ = ""; } int nfound = _ss.Search(h1_, sigma_, opt_.Data(), (peakmin_ / h1_->GetMaximum())); if (_debug && _nAngleUp == 0 && _nAngleDown == 0) { h1_->SaveAs("/home/dgruyer/e613/thesis/doc/proj.root"); } // return 0; // if(_nAngleUp==0&&_nAngleDown==0) // { // TCanvas* cc = new TCanvas("can","can",800,800); // cc->cd(); // h1_->DrawClone(); // TGraph* gg = new TGraph(nfound,_ss.GetPositionX(),_ss.GetPositionY()); // gg->DrawClone("P"); // return 0; // } // return 0; double theta = _dtemp->GetTheta(); double costh = TMath::Cos(TMath::DegToRad() * theta); list<double> lx; #if ROOT_VERSION_CODE > ROOT_VERSION(5,99,01) Double_t* xpeaks = _ss.GetPositionX(); #else Float_t* xpeaks = _ss.GetPositionX(); #endif for (int p = 0; p < nfound; p++) { double xp = xpeaks[p]; lx.push_back(xp); } lx.sort(); double ox = 0.; double oy = 0.; double dist = 0.; int TrueZ = 0; int OTZ = 0; int p = 1; int ok = 0; int npp = 1; list<double>::iterator it; for (it = lx.begin(); it != lx.end(); ++it) { if (p != 1) { Float_t xp = *it; double xx = xp * costh; double yy = ff->Eval(xx); // double yyp = _y0 + TMath::Sin(TMath::DegToRad()*(_otheta))*TMath::Cos(TMath::DegToRad()*(theta-_otheta))*TMath::Sqrt((xx-_x0)*(xx-_x0)+(yy-_y0)*(yy-_y0)); bool valid = true; // bool assoc = false; bool TrueAss = false; _invalid->SetPoint(_invalid->GetN(), xx, yy); double d = TMath::Sqrt((xx - ox) * (xx - ox) + (yy - oy) * (yy - oy)); if ((!(_spline = (KVSpiderLine*)_llist.FindObject(Form("Z=%d", p))))) { if (create_ == 0) { if (valid) { _spline = new KVSpiderLine(p, GetY0()); _llist.AddLast(_spline); } } else valid = false; } double caca = 0.4; if (create_ == 0) caca = 0.6; if ((p >= 4) && (d <= caca * dist) && (valid)) { valid = false; _spline = (KVSpiderLine*)_llist.FindObject(Form("Z=%d", TrueZ)); _spline->SetStatus(false); double xmoy = (_spline->GetX() * npp + xx) / (npp + 1); double ymoy = (_spline->GetY() * npp + yy) / (npp + 1); if (_spline->TestPoint(xmoy, ymoy, dist * 0.3, _useFit)) { _spline->ReplaceLastPoint(xmoy, ymoy); _spline->SetStatus(true); TrueZ = _spline->GetZ(); TrueAss = true; ok++; } p--; } if ((p >= 4) && (valid)) { if ((yy >= 4000.) || (xx >= 4000.)) valid = false; } if ((create_ == 0) && (valid)) { if ((p >= 10) && (d >= 1.7 * dist)) { valid = false; return true; } else { _spline->AddPoint(xx, yy); _spline->SetStatus(true); TrueZ = _spline->GetZ(); TrueAss = true; ok++; } } else { _invalid->SetPoint(_invalid->GetN(), xx, yy); } if ((create_ != 0) && (valid)) { bool assoc = false; // bool empty = false; bool test = false; int ii = -5; double d1 = 20000.; double d2 = 0.; KVSpiderLine* izp = 0; while ((!assoc) && (valid)) { if ((izp = (KVSpiderLine*)_llist.FindObject(Form("Z=%d", p + ii)))) { if ((izp->GetInterpolateN() != 0)) { d2 = izp->GetDistance(xx, yy); if ((d2 > d1) || test) { _spline = (KVSpiderLine*)_llist.FindObject(Form("Z=%d", p + ii - 1)); if (_spline->TestPoint(xx, yy, -1., _useFit)) { _spline->AddPoint(xx, yy); _spline->SetStatus(true); TrueZ = _spline->GetZ(); assoc = true; valid = false; TrueAss = true; } else { test = true; } } d1 = d2; } // else empty = true; } if (ii >= 10) { valid = true; assoc = true; } ii++; } if (!valid) p += ii - 2; } if (TrueZ == OTZ) npp++; else npp = 1; if ((_spline = (KVSpiderLine*)_llist.FindObject(Form("Z=%d", TrueZ))) && (TrueZ != OTZ)) { xx = _spline->GetInterpolateX(); yy = _spline->GetInterpolateY(); if ((_spline = (KVSpiderLine*)_llist.FindObject(Form("Z=%d", OTZ)))) { ox = _spline->GetInterpolateX(); oy = _spline->GetInterpolateY(); } else { ox = 0.; oy = 0.; } dist = TMath::Sqrt((xx - ox) * (xx - ox) + (yy - oy) * (yy - oy)) / (TrueZ - OTZ); ox = xx; oy = yy; } if (TrueAss) OTZ = TrueZ; } p++; _otheta = theta; } return true; }
void KVZALineFinder::MakeGrid() { fGeneratedGrid = new KVIDZAGrid; fGeneratedGrid->SetName(Form("%s_Up",fGrid->GetName())); fGeneratedGrid->SetVarX(fGrid->GetVarX()); fGeneratedGrid->SetVarY(fGrid->GetVarY()); fGeneratedGrid->AddIDTelescopes(fGrid->GetIDTelescopes()); KVIDZALine* TheLine = 0; KVIDZALine* TheZLine = 0; KVSpiderLine* spline = 0; TIter next_line(fLines); while((spline = (KVSpiderLine*)next_line())) // generate KVLines from KVSpiderLines { spline->Sort(); if((spline->GetN()>5)) { TheLine = (KVIDZALine*)((KVIDZAGrid*)fGeneratedGrid)->NewLine("ID"); TheLine->SetZ(spline->GetZ()); TheLine->SetA(spline->GetA()); for(int i=0; i<spline->GetN(); i++) { Int_t ibin = fConvertHisto->FindBin(spline->GetX(i),spline->GetY(i)); Double_t y = fConvertHisto->GetBinContent(ibin); Int_t di = 1; while(!y) { y = fConvertHisto->GetBinContent(ibin+di); di++; } if((i>spline->GetN()-3)&&(abs(y-TheLine->GetY()[TheLine->GetN()-1])>20)) continue; TheLine->SetPoint(i, spline->GetX(i), y);//spline->GetY(i)); } TheZLine = (KVIDZALine*)fGrid->GetIdentifier(TheLine->GetZ(),200); if(!TheZLine) continue; Double_t x0 = TheLine->GetX()[0]; Double_t x0ref = TheZLine->GetX()[0]; Double_t x1ref = TheZLine->GetX()[TheZLine->GetN()-1]; if((x0>2.*x0ref)&&(x0<0.15*x1ref)) { Double_t dy = (TheLine->GetY()[0]) - (TheZLine->Eval(x0)); for(int i=0; i<=TheZLine->GetN();i++) { Double_t x = TheZLine->GetX()[i]; Double_t y = dy + TheZLine->GetY()[i]; if(x>=x0) break; TheLine->SetPoint(TheLine->GetN(),x,y); } TheLine->Sort(&TGraph::CompareX, kTRUE); } Double_t x1 = TheLine->GetX()[TheLine->GetN()-1]; if((x1<x1ref)) { Double_t dy = (TheLine->GetY()[TheLine->GetN()-1]) - (TheZLine->Eval(x1)); for(int i=TheZLine->GetN()-1; i>0;i--) { Double_t x = TheZLine->GetX()[i]; Double_t y = dy + TheZLine->GetY()[i]; if(x<=x1) break; TheLine->SetPoint(TheLine->GetN(),x,y); } TheLine->Sort(&TGraph::CompareX, kTRUE); } fGeneratedGrid->Add("ID",TheLine); fLines->Remove(spline); } } // return; // TheLine = 0; // spline = 0; // TIter next(fLines); // while((spline = (KVSpiderLine*)next())) // scan rejected lines // { // Int_t zl = spline->GetZ(); // if(zl<4) continue; // Int_t aMostProb = 0; // if(zl<=fAList.size()) aMostProb = fAList.at(zl-1); // else aMostProb = 2*zl+1; // int index = 0; // KVIDZALine* oldLine = 0; // oldLine=(fGeneratedGrid->GetZALine(spline->GetZ(),aMostProb,index)); // if(!oldLine) // { // continue; // } // TheLine = (KVIDZALine*)((KVIDZAGrid*)fGeneratedGrid)->NewLine("ID"); // TheLine->SetZ(spline->GetZ()); // TheLine->SetA(spline->GetA()); // Int_t ibin = fConvertHisto->FindBin(spline->GetX(),spline->GetY()); // Double_t y = fConvertHisto->GetBinContent(ibin); // Int_t di = 1; // while(!y) // { // y = fConvertHisto->GetBinContent(ibin+di); // di++; // } // Double_t dy = y - oldLine->Eval(spline->GetX()); // for(int i=0; i<oldLine->GetN(); i++) // { // TheLine->SetPoint(i, (oldLine->GetX()[i]), (oldLine->GetY()[i])+dy); //// TheLine->SetPoint(i, spline->GetX(i), fConvertHisto->Interpolate(spline->GetX(i),spline->GetY(i))); // } //// fGeneratedGrid->Add("ID",TheLine); // } }
void KVSpIdGUI::SpiderIdentification() { if ((!fHisto) || (!fGrid)) return; TVirtualPad* pad = fGrid->GetPad(); fGrid->UnDraw(); fZp = fZpEntry->GetIntNumber(); if (!fUserParameter) fSpFactor = GetFactor(); else fSpFactor = fSpiderFactorEntry->GetNumber(); fAnglesUp = fAngleUpEntry->GetIntNumber(); fAnglesDown = fAngleDownEntry->GetIntNumber(); fAlpha = fApertureUpEntry->GetNumber(); fPiedType = fPiedChoice->GetSelected(); fMatrixType = fTypeChoice->GetSelected(); Int_t type = fMatrixType; TH2* tmpHisto = fHisto; TList* tmpCut = 0; if (fUseCut) { tmpHisto = (TH2*)fHisto->Clone(Form("%s_cut", fHisto->GetName())); tmpHisto->Reset(); for (int i = 1; i <= fHisto->GetNbinsX(); i++) { for (int j = 1; j <= fHisto->GetNbinsY(); j++) { Stat_t ww = fHisto->GetBinContent(i, j); Axis_t x0 = fHisto->GetXaxis()->GetBinCenter(i); Axis_t y0 = fHisto->GetYaxis()->GetBinCenter(j); if (fGrid->IsIdentifiable(x0, y0)) tmpHisto->Fill(x0, y0, ww); } } tmpCut = (TList*)fGrid->GetCuts()->Clone("tmpCuts"); } fGrid->Clear(); if (fScaledHisto) delete fScaledHisto; KVHistoManipulator hm; TF1 RtLt("RtLt", Form("x*%lf", fSfx), 0, tmpHisto->GetXaxis()->GetXmax()); TF1 RtLty("RtLty", Form("x*%lf", fSfy), 0, tmpHisto->GetXaxis()->GetXmax()); fScaledHisto = (TH2F*)hm.ScaleHisto(tmpHisto, &RtLt, &RtLty); if (fIdentificator) delete fIdentificator; fIdentificator = new KVSpiderIdentificator(fScaledHisto, fXm * fSfx, fYm * fSfy); switch (fPiedType) { case kUser: fIdentificator->SetX0(fPdx * fSfx); fIdentificator->SetY0(fPdy * fSfy); break; case kAuto: break; case kNone: fIdentificator->SetX0(0.); fIdentificator->SetY0(0.); } fIdentificator->SetParameters(fSpFactor); fIdentificator->SetNangles(fAnglesUp, fAnglesDown); fIdentificator->SetAlpha(fAlpha); fProgressBar->SetRange(0, fAnglesUp + fAnglesDown + 1); fProgressBar->Reset(); fIdentificator->Connect("Increment(Float_t)", "TGHProgressBar", fProgressBar, "SetPosition(Float_t)"); fTestButton->SetEnabled(kFALSE); fCloseButton->SetEnabled(kFALSE); fIdentificator->ProcessIdentification(); fTestButton->SetEnabled(kTRUE); fCloseButton->SetEnabled(kTRUE); fIdentificator->Disconnect("Increment(Float_t)", fProgressBar, "SetPosition(Float_t)"); fProgressBar->Reset(); if (fDebug) fIdentificator->Draw(fOption.Data()); TList* ll = (TList*)fIdentificator->GetListOfLines(); KVIDZALine* TheLine = 0; int zmax = 0; KVSpiderLine* spline = 0; TIter next_line(ll); while ((spline = (KVSpiderLine*)next_line())) { if ((spline->GetN() > 10)) { //&&(spline->GetX(0)<=fIdentificator->GetX0()+200.)) TF1* ff1 = 0; if (type == kSiCsI) ff1 = spline->GetFunction(fPdx * fSfx, TMath::Max(fScaledHisto->GetXaxis()->GetXmax() * 0.9, spline->GetX(spline->GetN() - 1))); else if (type == kSiSi) ff1 = spline->GetFunction(fPdx * fSfx, TMath::Min(fScaledHisto->GetXaxis()->GetXmax() * 0.9, spline->GetX(spline->GetN() - 1) * 1.5)); else if (type == kChIoSi) ff1 = spline->GetFunction(fPdx * fSfx, TMath::Min(fScaledHisto->GetXaxis()->GetXmax() * 0.9, spline->GetX(spline->GetN() - 1) * 1.5)); else ff1 = spline->GetFunction(); if ((type == kSiCsI) && (ff1->GetParameter(1) >= 3000. || (ff1->GetParameter(2) <= 0.35) || (ff1->GetParameter(2) >= 1.))) { Info("SpiderIdentification", "Z = %d has been rejected (fit parameters)", spline->GetZ()); continue; } TheLine = (KVIDZALine*)((KVIDZAGrid*)fGrid)->NewLine("ID"); TheLine->SetZ(spline->GetZ()); double min, max; ff1->GetRange(min, max); double step = TMath::Min((max - min) * 0.05, 20.); //20.; double stepmax = (max - min) * 0.2; //800.; double x = 0.; for (x = min + 1; x < max + step; x += step) { if (step <= stepmax) step *= 1.3; if (ff1->Eval(x) < 4000) TheLine->SetPoint(TheLine->GetN(), x, ff1->Eval(x)); } if (max > x) TheLine->SetPoint(TheLine->GetN(), max, ff1->Eval(max)); fGrid->Add("ID", TheLine); if (spline->GetZ() >= zmax) zmax = spline->GetZ(); } else { Info("SpiderIdentification", "Z = %d has been rejected (too few points)", spline->GetZ()); } } TF1 fx("fx12", Form("x/%lf", fSfx), 0., fScaledHisto->GetNbinsX() * 1.); TF1 fy("fy12", Form("x/%lf", fSfy), 0., fScaledHisto->GetNbinsY() * 1.); fGrid->Scale(&fx, &fy); if (fUseCut) delete tmpHisto; if (tmpCut) fGrid->GetCuts()->AddAll(tmpCut); pad->cd(); fGrid->Draw(); pad->Modified(); pad->Update(); DoClose(); }
//________________________________________________________________ void KVZALineFinder::FindALine(Int_t zz, Int_t width) { fLinearHisto->SetAxisRange(zz-0.5,zz+0.5,"Y"); KVIDLine* line = (KVIDLine*)fGrid->GetIdentifier(zz,2*zz+1); // A=2*zz+1 : dummy, A is ignored in this case if(!line) { int i=1; while(!(line = (KVIDLine*)fGrid->GetIdentifier(zz+i,2*zz+1))) i++; } if(!line) return; Double_t lX, lY; line->GetStartPoint(lX,lY); Int_t xbmin = 0;//fLinearHisto->GetYaxis()->FindBin(lX); line->GetEndPoint(lX,lY); Int_t xbmax = fLinearHisto->GetXaxis()->FindBin(lX); // create lines TList Lines; KVSpiderLine* tmp = 0; fLinearHisto->SetAxisRange(fLinearHisto->GetXaxis()->GetBinCenter(50),lX,"X");//fLinearHisto->GetXaxis()->GetXmax(),"X"); TH1* tmph = fLinearHisto->ProjectionX(Form("tmph%d",zz)); Int_t startBin = (Int_t)(tmph->GetMaximumBin()*0.95); delete tmph; TH1* projey = 0; if(startBin) { projey = fLinearHisto->ProjectionY("ProjectionAfterLin",startBin-width*3,startBin+width*3); int nfound = fSpectrum.Search(projey,0.05,"goff",0.0001); Info("FindALine","%d peack found...",nfound); #if ROOT_VERSION_CODE > ROOT_VERSION(5,99,01) Double_t* xpeaks = fSpectrum.GetPositionX(); Double_t* ypeaks = fSpectrum.GetPositionY(); #else Float_t* xpeaks = fSpectrum.GetPositionX(); Float_t* ypeaks = fSpectrum.GetPositionY(); #endif for(int p=0;p<nfound;p++) { if(p>8) break; if(ypeaks[p]<10) continue; Double_t xline = fLinearHisto->GetXaxis()->GetBinCenter(startBin); Double_t yline = xpeaks[p]; KVSpiderLine* tmp = 0; TIter next(&Lines); while((tmp=(KVSpiderLine*)next())) { Info("FindALine","line found but I don't know why..."); if(TMath::Abs(tmp->GetY()-yline)<0.05) continue; } tmp = new KVSpiderLine(zz,-1); Lines.AddLast(tmp); tmp->AddPoint(xline,yline); fPoints->SetPoint(fNPoints,xline,yline); fNPoints++; } if(projey) delete projey; } else Error("FindALine","not starting bin indicated..."); SortLines(&Lines); Int_t nLines = Lines.GetSize(); tmp = 0; for(int xx=startBin-width; xx>xbmin; xx-=width) { projey = fLinearHisto->ProjectionY("ProjectionAfterLin",xx-width/2,xx+width/2); int nfound = fSpectrum.Search(projey,0.05,"goff",0.02); #if ROOT_VERSION_CODE > ROOT_VERSION(5,99,01) Double_t* xpeaks = fSpectrum.GetPositionX(); Double_t* ypeaks = fSpectrum.GetPositionY(); #else Float_t* xpeaks = fSpectrum.GetPositionX(); Float_t* ypeaks = fSpectrum.GetPositionY(); #endif for(int p=0;p<nfound;p++) { if(p>=nLines+1) continue; if(ypeaks[p]<5) continue; Double_t xline = fLinearHisto->GetXaxis()->GetBinCenter(xx); Double_t yline = xpeaks[p]; KVSpiderLine* tmp = 0; TIter next(&Lines); while((tmp=(KVSpiderLine*)next())) { if((TMath::Abs(tmp->GetY()-yline)<0.05)) break; } if(tmp) { if((TMath::Abs(tmp->GetX()-xline)<10*width)) tmp->AddPoint(xline,yline); } } if(projey) delete projey; } TIter nextli(&Lines); while((tmp=(KVSpiderLine*)nextli()))tmp->Sort(true); tmp = 0; for(int xx=startBin+width; xx<=xbmax-width/2; xx+=width) { projey = fLinearHisto->ProjectionY("ProjectionAfterLin",xx-width/2,xx+width/2); int nfound = fSpectrum.Search(projey,0.05,"goff",0.02); #if ROOT_VERSION_CODE > ROOT_VERSION(5,99,01) Double_t* xpeaks = fSpectrum.GetPositionX(); Double_t* ypeaks = fSpectrum.GetPositionY(); #else Float_t* xpeaks = fSpectrum.GetPositionX(); Float_t* ypeaks = fSpectrum.GetPositionY(); #endif for(int p=0;p<nfound;p++) { if(p>=nLines+1) continue; if(ypeaks[p]<5) continue; Double_t xline = fLinearHisto->GetXaxis()->GetBinCenter(xx); Double_t yline = xpeaks[p]; KVSpiderLine* tmp = 0; TIter next(&Lines); while((tmp=(KVSpiderLine*)next())) { if(TMath::Abs(tmp->GetY()-yline)<0.05) break; } if(tmp) { if((TMath::Abs(tmp->GetX()-xline)<10*width)) tmp->AddPoint(xline,yline); } } if(projey) delete projey; } fLines->AddAll(&Lines); }