BRAT 2.4.5
Class index
Full class index
brahmlib
BRAHMS
ROOT page
//  $Id: BrDCTrackingModule.cxx,v 1.34 2002/06/10 16:40:26 ufstasze Exp $
//
#include "BrDCTrackingModule.h"

#include "BrTableNames.h"
#include "BrGeometryDbManager.h"
#include "BrMath.h"
#ifndef BRAT_BrTofDig
#include "BrTofDig.h"
#endif
#ifndef BRAT_BrEvent
#include "BrEvent.h"
#endif
#ifndef BRAT_BrCalibrationManager
#include "BrCalibrationManager.h"
#endif
#ifndef BRAT_BrRunInfo
#include "BrRunInfo.h"
#endif
//Needed for event display
#include "BrDetectorDC.h"
#include "TROOT.h"
#include "TCanvas.h"
#include "TView.h"
#include "TGeometry.h"
#ifndef ROOT_TDirectory
#include "TDirectory.h"
#endif
#ifndef ROOT_TString
#include "TString.h"
#endif

#define USEGETLAST
#define USESHRINK

#define COMPARE(n1,n2,cname,pname)  if (n1 != n2) cout << cname << "::" << pname << ":" << __LINE__ << " " << n1 << " " << n2 << endl;

ClassImp(BrDCTrackingModule);

//////////////////////////////////////////////////////////////////////////////////
//
// The BrDCTrackingModule class provides an interface to the BRAHMS DC Local Tracking
//
//////////////////////////////////////////////////////////////////////////////////

 BrDCTrackingModule::BrDCTrackingModule()
  :BrLocalTrackingModule()
{
  fClusterFinder = 0;
  fAssociatedHits = 0;
  fAssoHits = kFALSE;
  fCalibration = 0;
  fMaxTdc = 0;
  fMinTdc = 0;
  fPromptDelay = 0;
  fGlobalTdcOffset = -1111;
  fUseMySql = kFALSE;
  fUseOwnCalib = kFALSE;
}

//_____________________________________________________________
 BrDCTrackingModule::BrDCTrackingModule(Char_t *Name,Char_t *Title,Bool_t EventByEvent)
  :BrLocalTrackingModule(Name,Title)
{
  // In this constructor we set default parameters that define tracking conditions.
  // You can alse define this parameters using SetClusterFinderParams,
  // Set2DTrackFinderParams, SetViewCombinerParams method. This settings should be
  // done before Init method is called. 

  fCalibration = 0;
  fMaxTdc = 0;
  fMinTdc = 0;
  fPromptDelay = 0;
  fGlobalTdcOffset = -1111;
  fUseMySql = kFALSE;
  fUseOwnCalib = kFALSE;
  fAssoHits = kFALSE;

  //fParams_p       = NULL;
  for(Int_t i=0;i<4;i++) fVolumeParams_p[i] = NULL;
  fNumVol = 0;
  fEventByEvent = EventByEvent;


  //Default parameters for cluster finder: 
  fPlaneCombineMode = kPairsWhenTogether;
  //Float_t posRes = 0.015; 
  //Float_t MaxAngle = 3.0; // Maximum angle not supressed when combining hits [deg].
  //Double_t rad = .017453292519943;
  //fClusDx   = 6.0*posRes + 1.5*MaxAngle*rad;
  //fClusDy   = 6.0*posRes + 1.5*MaxAngle*rad;
  
  //Float_t N=3.0; // three degrees of freedom
  //fTrioChisqWin = N + 3.0*TMath::Sqrt(2*N); //Mean plus 3 sigma of chi2 distribution

  //Default parameters for view list:
  fMainView =   0;
  fSubView  = -90;

  //Default parameters for 2D track finder; 
  //fDCCutMatchDx = (Float_t)0.015; // should be the same as posRes
  //fDCCutMatchDy = (Float_t)0.015; //
  //fAsigma       = (Float_t)3.0;

  //Default parameters for  view combiner; 
  //fDCCutMaxMissMain  = 1;
  //fDCCutMaxMissSub   = 1;
  //fDCCutMaxMissOther = 2;

  //Extra parameters for view combiner; then create view combiner. 
  fUVDx   = (Float_t)0.0;
  fUVDy   = (Float_t)0.0;

  fLowLimit=0;
  fUpLimit=2000;

  
  cout<<"BrDCTrackingModule:  Default tracking setup for "<<GetName()<<endl;
  if(!strcmp("T3",GetName())){
    SetClusterFinderParams(4.0, 0.015, 3);
    Set2DTrackFinderParams(0.0, 0.0, 3.0);  
    SetViewCombinerParams(1,1,2,0,0);
  }
  if(!strcmp("T4",GetName())){
    SetClusterFinderParams(3.0, 0.018, 3);
    Set2DTrackFinderParams(0.0, 0.0, 3.0); 
    SetViewCombinerParams(1,1,2,0,0);
  }
  if(!strcmp("T5",GetName())){
    SetClusterFinderParams(3.0, 0.019, 3);
    Set2DTrackFinderParams(0.0, 0.0, 3.0);
    SetViewCombinerParams(1,1,4,0,0);
  }
  
  // Setting parameters that describe general TDC spectra characteristics
  if(!strcmp("T3",GetName())){
    fMinTdcPP=3970;
    fPromptDelayPP=0;
    fMaxTdcPP=4420;
    fMinWidth=16;
    fDriftTime=0; //290
  }
  if(!strcmp("T4",GetName())){
    fMinTdcPP=2250;
    fPromptDelayPP=3520;
    fMaxTdcPP=4470;
    fDriftTime=0; //460
  }
  if(!strcmp("T5",GetName())){
    fMinTdcPP=2250;
    fPromptDelayPP=3320;
    fMaxTdcPP=4370;
    fDriftTime=0; //460
  }

  fUseShrink = kTRUE;
  
  fTrackHits2D = new TObjArray();
  fTrackHits   = new TObjArray();
  fAssociatedHits = 0;

  // define some default parameters:
  SetBFSMatchingMode();
  SetFSMatchingMode();
  SetAssociatedHitsMode();

  fDCDriver = 0;

}

//_____________________________________________________________
 void BrDCTrackingModule::Init()
{
  // The Init member function/method is used to define tracking modules
  // It also executes InitVol.
  
  SetState(kInit);

  // first pick up global run info
   BrRunInfoManager* RunMan = BrRunInfoManager::Instance();
  
  if (!RunMan) {
    Error("Init", "Could not get an instance of BrRunInfoManager");
    return;
  }
  
  const BrRunInfo* run = RunMan->GetCurrentRun();
  if (run->GetRunNo() == -1) {
    Error("Init", "Run number is -1, check it out");
    return;
  }  

  if (fUseMySql) {
    BrCalibrationManager* calMan = BrCalibrationManager::Instance();
    fCalibration = (BrDcCalibration*)calMan->Register("BrDcCalibration", GetName());
    if (!fCalibration) {
      Abort("Init", "Couldn't get a pointer to the DC Calibration parameter");
      return;
    }

    fCalibration->Use("tdcOffset", BrCalibration::kRead, 1);
    fCalibration->Use("driftTime", BrCalibration::kRead, 1);
  }

   
  //------------------------------------------------------

  fClusterFinder = new BrDCClusterFinder((Char_t*)GetName(),
					 (Char_t*)GetTitle(),
					 fPlaneCombineMode,
					 fClusDx,fClusDy,fTrioChisqWin,
					 fEventByEvent,GetMode(),1,1,
					 fPosRes,fLowLimit,fUpLimit);
  
  // Set additional parameter for BrDCClusterFinder. In future this
  // parameter will be probably read from the data base;
  
  fClusterFinder -> SetMinWidth(fMinWidth);
  fClusterFinder -> SetMinTdc(fMinTdc);     
  fClusterFinder -> SetPromptDelay(fPromptDelay);
  fClusterFinder -> SetMaxTdc(fMaxTdc);     
  fClusterFinder -> SetDriftTime(fDriftTime);
  fClusterFinder -> SetGlobalTdcOffset(fGlobalTdcOffset);
  fClusterFinder -> SetOffsetFile(fOffsetFile);
  fClusterFinder -> SetCalibFile(fCalibFile);
  fClusterFinder -> SetRunNo(run->GetRunNo());
  if(fUseMySql)fClusterFinder -> UseMySql();

  fViewList = new BrDCViewList(GetName(),GetTitle(), 
			       fPlaneCombineMode,fMainView,fSubView);
  
  
  f2DTrackFinder = new BrDC2DTrackFinder(GetName(),GetTitle(),
					 fClusterFinder,
					 fDCCutMatchDx,
					 fDCCutMatchDy,fAsigma,
					 fDCCutMaxMissMain,
					 fDCCutMaxMissSub,
					 fDCCutMaxMissOther,fEventByEvent);

  f2DTrackFinder->SetBfsAngle((Int_t)run->GetBFSAngle());
  f2DTrackFinder->SetRunNo(run->GetRunNo());
  
  fViewCombiner = new BrDCViewCombiner(GetName(),GetTitle(),
				       fClusterFinder,fAsigma,
				       fDCCutMaxMissMain,fDCCutMaxMissSub,
				       fDCCutMaxMissOther,fUVDx,fUVDy,
				       fEventByEvent);

  fViewCombiner->SetBfsAngle((Int_t)run->GetBFSAngle());
  fViewCombiner->SetRunNo(run->GetRunNo());


  fClusterFinder -> Init();
  f2DTrackFinder -> Init();
  fViewCombiner  -> Init();

  fDetectorHits = new BrClonesArray("BrDCHit", 2000); // Temp. fixe!!
  // We hidden fDetectorHits from BrLocalTracingModule and the code
  // works OK, but now we left BrLocalTracingModule::fDetectorHits empty. 

  // define pointer to BrDriverDC
  if(!fDCDriver) {
    fDCDriver = new  BrDriverDC(GetName(),GetName());   //Pawel
  }
  
  InitVol();
}
//_____________________________________________________________
 void BrDCTrackingModule::DefineHistograms() {

  if (GetState() != kInit) {
    Stop("DefineHistograms", "Must be called after Init"); 
    return;  
  }
  
  TDirectory* saveDir = gDirectory;
  TDirectory* histDir = gDirectory->mkdir(Form("%sTrack", GetName()));
  histDir->cd();

  Char_t HistName[32];
  sprintf(HistName,"hNumTra2DInit%s",GetName());
  h_NumTra2DInit = new TH1F(HistName,"NumTra2DInit",51,0,50);
  sprintf(HistName,"hNumTra2DInter%s",GetName());
  h_NumTra2DInter = new TH1F(HistName,"NumTra2DInter",51,0,50);
  sprintf(HistName,"hNumLoctraInit%s",GetName());
  h_NumLoctraInit = new TH1F(HistName,"NumLoctraInit",250,0,500);

  sprintf(HistName,"hDDu%s",GetName());
  h_Ddu = new TH1F(HistName,"Dist of ddu",200,-1,3);
  sprintf(HistName,"hDuc%s",GetName());
  h_Duc = new TH1F(HistName,"Dist of duc",200,-.5,.5);
  sprintf(HistName,"hChisq%s",GetName());
  h_Chisq = new TH1F(HistName,"Dist of chisq for Rec. Tracks",150,0,15);

  sprintf(HistName,"assoHitsVsPlane%s",GetName());
  fhAssoHitsPerPlane = new TH1F(HistName,"Primary detector associated hits per plane",30,0.5,30.5);

  fClusterFinder->Book();
  f2DTrackFinder->Book();
  fViewCombiner->Book();
  
  gDirectory = saveDir;
}

//_____________________________________________________________
 BrDCTrackingModule::~BrDCTrackingModule(){
  //
  // Is there a problem with the fact the the fParams_p can point to
  // different kinds of datastructures i.e. potential memory
  // leak. probably yes since fParams_p is defined as BrDetectorParams
  // nor BrDetectorParamsTPC. By second thought it is probably ok. As
  // long as all parameters are defined/set Using the
  // SetDetectorParamsTPC / DC /BB etc.. 
  //
  //if( fParams_p != 0) delete fParams_p;
  for(Int_t i=0;i<4;i++) {
    if( fVolumeParams_p[i] != 0) 
      delete fVolumeParams_p[i];
  }
  if(fAssociatedHits) delete fAssociatedHits;
}

//_____________________________________________________________
 void BrDCTrackingModule::SetDetectorVolume(BrDetectorVolume *vol) 
{
  Int_t i;
  for(i=0;i<fNumVol;i++) {
    if(!strcmp(fVolumeParams_p[i]->GetName(),vol->GetName())) {
      fVolumeParams_p[i] = vol;
      return;
    }
  }
  if(fNumVol<4) {
    fVolumeParams_p[fNumVol] = vol;
    fNumVol++;
  }
  else 
    cout<<"Too many volume parameters, this one not added"<<endl;
}

//_____________________________________________________________
 BrDetectorVolume *BrDCTrackingModule::GetDetectorVolume(const Char_t *name) {
Int_t i;
 for(i=0;i<fNumVol;i++) {
   if(!strcmp(fVolumeParams_p[i]->GetName(),name)) {
     return fVolumeParams_p[i];
   }
 }
 cout<<"Detector Volume "<<name<<" does not exist"<<endl;
 return NULL;
}

//_____________________________________________________________
 void BrDCTrackingModule::ListDetectorVolume() 
{
  cout<<"There are "<<fNumVol<<" volumes defined"<<endl;
  for(Int_t i=0;i<fNumVol;i++) {
    cout<<"Volume "<<i<<" has name "<<fVolumeParams_p[i]->GetName()<<endl;
  }
}

//_____________________________________________________________
 void BrDCTrackingModule::InitVol(){
  // The init member function/method is used to define histograms
  // The histograms is added to the list of hist objects in this
  // module.
  Int_t i;
  Char_t volbuf[64];
  BrGeometryDbManager *gGeomDb;
  BrDetectorVolume    *vol;
  
  //First Set Volumes
  //Create instance of data base manager
  gGeomDb = BrGeometryDbManager::Instance();
  
  //Get Master volume for DC's
  strcpy(volbuf,GetName());
  vol = 
    (BrDetectorVolume*)gGeomDb->GetDetectorVolume("BrDetectorVolume",volbuf); 
  if(vol) {
    vol->Print();
    SetDetectorVolume(vol);
    // Get DC Sub Modules
    for(i=0;i<3;i++) {
      sprintf(volbuf,"%sA%d",GetName(),i+1);
      vol =
	(BrDetectorVolume*)gGeomDb->GetDetectorVolume("BrDetectorVolume",
						      volbuf);
      vol->Print();
	  SetDetectorVolume(vol);
    }
  }
  
}

//_____________________________________________________________
 void BrDCTrackingModule::Clear() {
  //Clear all DC tables.
  BrLocalTrackingModule::Clear();
  fTrackHits->Delete();
  fTrackHits2D->Delete();
  //fTrackHits->Clear();
  //fTrackHits2D->Clear();
}

//_____________________________________________________________
 void BrDCTrackingModule::Begin()
{
  SetState(kBegin);
  if (!fUseMySql)
    return;

  if (!fCalibration->RevisionExists("*")) {
    Abort("Begin", "A calibration revision is missing for %s,"
	  " check out the calib tool on the DAQ home page", GetName());
    return;
  }

  if (!fCalibration->ValidCalibration()) {
    Abort("Begin", "One of the calibration parameters is badly calibrated in %s", GetName());
    return;
  }
  
  fClusterFinder -> SetGlobalTdcOffset(fCalibration->GetTdcOffset());
  if(fUseOwnCalib)fClusterFinder -> SetGlobalTdcOffset(0);

  Int_t minTdc      = fMinTdcPP      + fCalibration->GetTdcOffset();
  Int_t promptDelay = fPromptDelayPP + fCalibration->GetTdcOffset();
  Int_t maxTdc      = fMaxTdcPP      + fCalibration->GetTdcOffset();
  Int_t driftTime   = fCalibration->GetDriftTime();

  fClusterFinder -> SetMinTdc(minTdc);
  fClusterFinder -> SetPromptDelay(promptDelay);
  fClusterFinder -> SetMaxTdc(maxTdc);
  fClusterFinder -> SetDriftTime(driftTime);
  fClusterFinder->Begin();
}  

//_____________________________________________________________
 void BrDCTrackingModule::Event(BrEventNode* InputTable,
			       BrEventNode* OutputTable)
{
  Int_t status;

  if(DebugLevel()>0) cout<<"Entering local tracking for "<<GetName()<<endl;

  Clear();
  
  BrEvent* Event = (BrEvent*)InputTable;
  Int_t latch = (Event->GetEventHeader())->TriggerWord(1);

  if (latch & 0x0101) fTrigger=1;
  if (latch & 0x0202) fTrigger=2;
  if (latch & 0x0404) fTrigger=3;
  if (latch & 0x0808) fTrigger=4;
  if (latch & 0x1010) fTrigger=5;
  if (latch & 0x2020) fTrigger=6;
  if (latch & 0x4040) fTrigger=7;
  if (latch & 0x8080) fTrigger=8;


  //Get stuff from the input table
  Char_t TableName[32];
  sprintf(TableName,"%s %s", GetTableName(), GetName());
  BrDataTable *digitizedHits = InputTable->GetDataTable(TableName);

  if (digitizedHits == 0) {
    if(DebugLevel() > 0 ) 
      cout<<"No DigitizedDC in this event for "<<GetName()<<endl;
    return;
  }

  
  if (fBFSMatching) {
    if (!strcmp("T3", GetName()) || !strcmp("T5", GetName())) {
      sprintf(TableName,"DetectorTrack T4");
      BrDataTable *DetectorTracks = OutputTable->GetDataTable(TableName);

      if (DetectorTracks) {
	Int_t numtracks = DetectorTracks->GetEntries();
	if (!numtracks)
	  return; // can not match because there is no tracks in T4
      }
      else {
	cout << "No table: " << TableName << endl;
	return;
      }
    }
  }
  
  if (fFSMatching) {
    sprintf(TableName,"DetectorTrack T2");
    BrDataTable *DetectorTracks = InputTable->GetDataTable(TableName);
    if (DetectorTracks) {
      Int_t numtracks = DetectorTracks->GetEntries();
      if (!numtracks) 
	return; // can not match because there is no tracks in T2
    }
    else {
      cout<<"No table: "<<TableName<<endl;
      return;
    }  
  }
  

  // tof stuff
  BrDataTable *tof1Table = InputTable->GetDataTable("DigTof TOF1");
  if (tof1Table) {
    Int_t Tof1Hits = tof1Table->GetEntries();
    Int_t ii=0;
    for (Int_t ih = 0; ih < Tof1Hits; ih++){
      BrTofDig* hitTof = (BrTofDig*)tof1Table->At(ih);
      if ((hitTof->GetTdcUp() < 4090) && (hitTof->GetTdcDown() < 4090)) {
	fTof1Time[ii] = (hitTof->GetTdcUp() + hitTof->GetTdcDown())/40.0;
	ii++;
      }
    }
    
    fTof1GoodHits = ii;
  }
  else {
    if (DebugLevel() > 1) 
      cout << "No 'DigTof TOF1' table" << endl;
  }
  
  Int_t Tof1TimeRef = 0;
  //if(tof1Table && fTrigger==1) Tof1TimeRef=GetTof1TimeRef();
  
  fDetectorHits->Clear();
  fClusterFinder->FindClusters(digitizedHits, fDetectorHits, fNDetectorHits, Tof1TimeRef);
  
  status = TrackLocalElement();

  if (!status) FillDetectorTracks(OutputTable);
  if (!status) FillDetectorTracks(InputTable);

  
  if (fAssoHits) FillDCHits(OutputTable);

  if (fEventByEvent) {    
    // what follows is just for debugging purpose:
    OutputTable->ListObjects();
    BrDataTable *DetectorTracks;
    sprintf(TableName,"DetectorTrack %s", GetName());
    DetectorTracks = OutputTable->GetDataTable(TableName);  
    Int_t numtracks = DetectorTracks->GetEntries();
    cout<<"Event: final number of tracks for "<<GetName()<<" is "<<numtracks<< endl;
    //cout<<GetName()<<" "<<numtracks<< endl;
    //Int_t ii;
    //if(numtracks)cin>>ii;
    }  
  if(DebugLevel()>5) {
    BrDataTable *DetectorTracks;
    sprintf(TableName,"DetectorTrack %s",GetName());
    DetectorTracks = OutputTable->GetDataTable(TableName);  
    Int_t numtracks = DetectorTracks->GetEntries();
    cout<<"Event: final number of tracks for "<<GetName()<<" is "<<numtracks<< endl;
  }  
}

//_____________________________________________________________
 Int_t BrDCTrackingModule::TrackLocalElement()
{
//Do the local DC tracking
  Int_t i;
  
  Float_t loctra_pos[3];
  Float_t loctra_vec[3];
  
  BrLocalTrack* loctra_p;
  
  Float_t sol[4];
  Float_t covar[4][4];
  Float_t chisq;
  Int_t ifail;
  
  Int_t nloctra;
  
  
  //BrDetectorParamsDC *par;
  
  if( DebugLevel() > 4 ) {
    // See what kind of hitcmb's we have.
    Int_t numDetectorHits = fDetectorHits->GetEntries();
    for(i=0;i<numDetectorHits;i++) {
      BrDCHit *hit_p = (BrDCHit*)fDetectorHits->At(i);
      cout<<hit_p;
    }
  }
  
  //par = GetDetectorParamsDC();
  
  //Setup which hits are in which views.
  fViewList->FindHitsInViews(fDetectorHits);

  //Main and submain view 2d tracking
  //=================================
  
  //Define local 2d tracks by taking hitcmb for the first and last
  //planes in main and subview

 
  f2DTrackFinder->Find2DTracks(fViewList,fTrackHits,fTrackHits2D);
  

  Int_t num_ftracks = fTrackHits->GetEntries();
  Int_t num_ftrack2ds = fTrackHits2D->GetEntries();
  //printf("num_ftracks = %d, num_ftrack2ds = %dn",num_ftracks,num_ftrack2ds);
  if(fEventByEvent)
    cout<<"Total Number of 2D Tracks: "<<num_ftrack2ds<<endl;
  

  //Combine hits from other views with the 2D tracks we found above.
  
  fNLocalTracks=0;
  fViewCombiner->CombineViews(fViewList,fTrackHits2D,fTrackHits,fLocalTracks,fNLocalTracks);

  Int_t numLoctra = fLocalTracks->GetLast() + 1;
  h_NumLoctraInit->Fill(numLoctra);
  if(fEventByEvent)cout<<"Leaving CombineViews: track number is "<<numLoctra<<endl;
  
  if(fLocalTracks->GetLast()+1 < 2000) {
    FillDetectorHits();
    FillTrackGroups(fViewList->GetNCutoff());
    
  
    // Fit the local tracks
    nloctra = fLocalTracks->GetLast()+1;
    for(i=0;i<nloctra;i++) {
      loctra_p = (BrLocalTrack*)fLocalTracks->At(i);

      //      cout<<"befor: "<<loctra_p->GetVec()[0]<<" "<<loctra_p->GetVec()[1]<<" "
      //     <<loctra_p->GetPos()[0]-loctra_p->GetVec()[0]*loctra_p->GetPos()[2]<<" "
      //    <<loctra_p->GetPos()[1]-loctra_p->GetVec()[1]*loctra_p->GetPos()[2]<<endl;

      FitLocalDCTrack(loctra_p,sol,covar[0],chisq,ifail);
      chisq=GetChisq(loctra_p,sol); 
      if(DebugLevel() > 0) {
	printf("DC fit   %f %f %f %f %fn",
	       sol[0],sol[2],sol[1],sol[3],chisq);
      }
      loctra_pos[0] = sol[1];
      loctra_pos[1] = sol[3];
      loctra_pos[2] = (Float_t)0.0;
      loctra_p->SetPos(loctra_pos);
      loctra_vec[0] = sol[0];
      loctra_vec[1] = sol[2];
      loctra_vec[2] = (Float_t)1.0;
      loctra_p->SetVec(loctra_vec);
      loctra_p->SetChisq(chisq);
      h_Chisq->Fill(chisq);
      //cout<<"fit: "<<loctra_p->GetVec()[0]<<" "<<loctra_p->GetVec()[1]<<" "
      //	  <<loctra_p->GetPos()[0]<<" "<<loctra_p->GetPos()[1]<<" "<<chisq<<endl;
    }
   }
  else
    Warning("TrackLocalElement",
	    "Aborting event before FillVTracks because " 
	    "NumLoctra too large %d",fLocalTracks->GetLast()+1);
return(0);
}

//_________________________________________________________________
 void BrDCTrackingModule::FillDCHits(BrEventNode* OutputTable)
{
  // Here we add AssociatedHits table to the OutputTable  
  
  Char_t TableName[32];
  sprintf(TableName,"AssociatedHits %s",GetName());
  fAssociatedHits = new BrDataTable(TableName);
  
  sprintf(TableName,"DetectorTrack %s",GetName());
  BrDataTable* DetectorTracks = OutputTable->GetDataTable(TableName);
  Int_t ndettra = DetectorTracks->GetEntries();
  Int_t nloctra = fLocalTracks->GetLast()+1;
  for(Int_t i=0;i<nloctra;i++) {
    BrLocalTrack* loctra = (BrLocalTrack*)fLocalTracks->At(i);
    Int_t loctraID = loctra->GetID();
    Bool_t LocTraInTable=kFALSE;
    for(Int_t j=0;j<ndettra;j++){
      BrDetectorTrack* dettra = (BrDetectorTrack*)DetectorTracks->At(j);
      if(loctraID == dettra->GetID())LocTraInTable=kTRUE;
    }
    if(!LocTraInTable)continue;
    TObjArray *detectorHits = loctra->GetHitList();
    Int_t numHits = detectorHits->GetLast() + 1;
    for(Int_t ic=0;ic<numHits;ic++) {
      BrDCHit* hit_p = (BrDCHit*)detectorHits->At(ic);
      
      BrDCHit* dchit = new BrDCHit();
      fAssociatedHits->Add(dchit);
      dchit->SetImod(hit_p->GetImod());
      dchit->SetNumCombined(hit_p->GetNumCombined());
      dchit->SetU(hit_p->GetU());
      dchit->SetZ(hit_p->GetZ());
      dchit->SetPlane(hit_p->GetPlane());
      dchit->SetWire(hit_p->GetWire());
      dchit->SetTdc(hit_p->GetTdc());
      dchit->SetID(loctraID);
      Int_t im=hit_p->GetImod()/100 -1;
      for(Int_t ica=0;ica<hit_p->GetNumCombined();ica++){
	Int_t ip=hit_p->GetPlane()[ica];
	Int_t GlobalPlaneNum = (im*fDCDriver->GetMaxPlaneNum())+(ip+1);
	fhAssoHitsPerPlane->Fill(GlobalPlaneNum);
      }
    }
  }
  if(fAssociatedHits->GetEntries())OutputTable->AddObject(fAssociatedHits);
  
}  

//_____________________________________________________________________________
 void BrDCTrackingModule::FillDetectorHits()
{
  //Fill the detector hits using information from the track hits as well as the
  //2d tracks.
  Int_t ic,itrack;
  
  //Find the hitcmb which are related through the loctra part.
  //i.e. the non-mainview planes.
  Int_t numTrackHits;
  BrLocalTrack *loctra_p;
  BrTrackHit *trahit_p;
  BrDCHit *hit_p;
  
  numTrackHits = fTrackHits->GetEntries();
  
  for(itrack=0;itrack<fLocalTracks->GetEntries();itrack++) {
    loctra_p = (BrLocalTrack*)fLocalTracks->At(itrack);
    
    for(ic=0;ic<numTrackHits;ic++) {
      trahit_p = (BrTrackHit*)fTrackHits->At(ic);
      if(loctra_p == trahit_p->GetLocalTrack()) {
	hit_p = (BrDCHit*)trahit_p->GetDetectorHit();
	loctra_p->AddDetectorHit(hit_p);
      }
      if(loctra_p->GetTr1() == trahit_p->GetTra2d()) {
	hit_p = (BrDCHit*)trahit_p->GetDetectorHit();
	loctra_p->AddDetectorHit(hit_p);
      }
      if(loctra_p->GetTr2() == trahit_p->GetTra2d()) {
	hit_p = (BrDCHit*)trahit_p->GetDetectorHit();
	loctra_p->AddDetectorHit(hit_p);
      }
    }
  }
}

//______________________________________________________________________________________________
 void BrDCTrackingModule::FitLocalDCTrack(BrLocalTrack* loctra_p,
					 Float_t *sol, Float_t *covar, Float_t &chisq, Int_t &ifail)
{
  Int_t i,j,ic;
  static const Double_t rad = .017453292519943;
  static const Double_t rad1 = 2.0*TMath::ASin(1.0)/180.0; // TMath::Pi()/180
  
  Double_t a[4];
  Double_t beta[4][1];
  Double_t alpha[4][4];
  Double_t *alphaa[4],*betaa[4];
  Float_t work[4];
  Float_t du,chisqq,dchi;
  Float_t csv[20],siv[20];
  Int_t iv,idet;
  
  BrDCHit* hit_p;
  
  //for(i=0;i<nview;i++) {
  Int_t nview = fViewList->GetNumViews();
  for(i=0;i<nview;i++) {
    //Double_t angl = (Double_t)vlist[i] * rad;
    Double_t angl = (Double_t)fViewList->GetViewAngle(i) * rad;
    csv[i] = (Float_t)TMath::Cos(angl);
    siv[i] = (Float_t)TMath::Sin(angl);
    //cout<<"viewList: "<<i<<" "<<angl<<" "<<siv[i]<<endl;
  }

  for(i=0;i<4;i++) {
    beta[i][0] = (Double_t)0.0;
    for(j=0;j<4;j++) {
      alpha[i][j] = (Double_t)0.0;
    }
  }
  
  //Is there a more elegant way to do this?
  for(i=0;i<4;i++) {
    alphaa[i] = &alpha[i][0];
    betaa [i] = &beta[i][0];
   }

  TObjArray *detectorHits = loctra_p->GetHitList();
  Int_t numHits = detectorHits->GetLast() + 1;
  for(ic=0;ic<numHits;ic++) {
    hit_p = (BrDCHit*)detectorHits->At(ic);
    iv = BrMath::MOD(hit_p->GetImod(),100)-1;
    //cout<<"~~iv prior "<<iv<<endl;
    //Int_t iview=iv+1;
    idet = hit_p->GetImod() / 100-1;
    iv = fViewList->GetViewListID(idet,iv);
    a[0] = csv[iv]*hit_p->GetPos()[2];
    a[1] = csv[iv];
    a[2] = -siv[iv]*hit_p->GetPos()[2];
    a[3] = -siv[iv];
    //cout<<"~~viewList: "<<iv<<" "<<siv[iv]<<endl;
    du = hit_p->GetDpos()[0];
    for(i=0;i<4;i++) {
      for(j=0;j<4;j++) 
	alpha[i][j] += a[i]*a[j]/du/du;
      beta[i][0] += a[i] * hit_p->GetPos()[0]/du/du;
    }
    //if(fWriteOut)
    //Owsk<<hit_p->GetPos()[0]<<" "<<hit_p->GetPos()[2]<<" "<<iview<<" "<<icol+1<<endl;
  }

  //Solve 4x4 matrix for xc,yc and ex,ey vector
  BrMath::DEQINV(4,alphaa,4,work,ifail,1,betaa);
  
  //Calculate chisqr
  chisq = (Float_t)0.0;
  for(ic=0;ic<numHits;ic++) {
    hit_p = (BrDCHit*)detectorHits->At(ic);
    iv = BrMath::MOD(hit_p->GetImod(),100)-1;
    idet = hit_p->GetImod() / 100-1;
    iv = fViewList->GetViewListID(idet,iv);
    a[0] = csv[iv]*hit_p->GetPos()[2];
    a[1] = csv[iv];
    a[2] = -siv[iv]*hit_p->GetPos()[2];
    a[3] = -siv[iv];
    du = hit_p->GetDpos()[0];
    dchi = (Float_t)0.0;
    for(i=0;i<4;i++) dchi += (Float_t)(a[i] * beta[i][0]);
    dchi = (dchi - hit_p->GetPos()[0])/du;
    
    chisqq += dchi*dchi;
  }
  //chisq = chisqq / (Float_t)(numHits-4);
  chisq = chisqq / (Float_t)(numHits);
  //should this really be Float_t???
  for(i=0;i<4;i++) sol[i] = (Float_t)beta[i][0];
}

//________________________________________________________________________________
 Float_t BrDCTrackingModule::GetChisq(BrLocalTrack* loctra_p, Float_t *sol)
{
  // This method calculate chisq for 
  static const Double_t rad = .017453292519943;
  Double_t beta[4],tang[4];
  Double_t ang[4] = {0.0 ,-90.0, -18.0, 18.0};
  for(Int_t i=0;i<4;i++){
    if(i==1){
      beta[i]=1.0e20;
      tang[i]=1.0e20;
    }else{ 
      beta[i]  = TMath::Cos(rad*ang[i]) + TMath::Tan(rad*ang[i])*TMath::Sin(rad*ang[i]);
      tang[i]  = TMath::Tan(rad*ang[i]);
    }
  }

  Double_t ax,ay,x0,y0;
  ax=sol[0]; ay=sol[2]; x0=sol[1]; y0=sol[3];
  
  // Now lets calculate chisq
  BrDCHit* hit_p;
  TObjArray *detectorHits = loctra_p->GetHitList();
  Int_t numHits = detectorHits->GetLast() + 1;
  Float_t chisq = 0.0;
  for(Int_t ih=0;ih<numHits;ih++) {
    hit_p = (BrDCHit*)detectorHits->At(ih);
    Int_t iv = BrMath::MOD(hit_p->GetImod(),100)-1;
    Double_t u  = hit_p->GetPos()[0];
    Double_t z  = hit_p->GetPos()[2];
    Double_t du = hit_p->GetDpos()[0];
    Double_t au,u0;
    if(iv==1){
      au=ay;
      u0=y0;
    }else{
      au=(ax-ay*tang[iv])/beta[iv];
      u0=(x0-y0*tang[iv])/beta[iv];
    }
    Double_t dchi = (u - au*z - u0)/du;
    chisq += dchi*dchi;
  }
  chisq = chisq / (Float_t)(numHits);
  return chisq;
}

//_____________________________________________________________________
 Int_t BrDCTrackingModule::GetTof1TimeRef()
{
  // it is in units of ?
  Int_t Offset=126; 
  
  Double_t MeanTime=0.0;
  Int_t Nitems=0;
  for(Int_t i=0;i<fTof1GoodHits;i++){
    if((fTof1Time[i]>110&&fTof1Time[i]<126.6) || (fTof1Time[i]>.2&&fTof1Time[i]<48)){
      MeanTime += fTof1Time[i];
      Nitems++;
    }
  }
  if(Nitems)MeanTime=MeanTime/(Double_t)Nitems;
  else MeanTime=126.0;
  if(fTrigger==1 || MeanTime<48.0){
    if(!strcmp("T3",GetName()))MeanTime-=27.6;
    if(!strcmp("T4",GetName()))MeanTime-=75.6;
    if(!strcmp("T5",GetName()))MeanTime-=133.6;
  }
  Double_t TdcReferece = MeanTime*1.0; // TdcReferece is now in units of ? 
  return TMath::Nint(TdcReferece) - Offset;
}

//_______________________________________________________________________________
 void BrDCTrackingModule::DrawLocalTracks() {
  Int_t i;
  BrDetectorVolume *vol[4];
  Char_t volbuf[64];
  Char_t canvasname[64];
  Char_t nodename[64];
  TCanvas *canvas;

  sprintf(canvasname,"%sCanvas",GetName());

  sprintf(nodename,"%sDetectorNode",GetName());
  // not used
  // BrDetectorDC *alternate = (BrDetectorDC*)gROOT->FindObject(nodename);

  fDetectorNode = (BrDetectorDC*)gGeometry->GetNode(nodename);

  //Create new canvases and detector node only if 
  if(!fDetectorNode) {
    canvas = (TCanvas*)gROOT->FindObject(canvasname);
    if(!canvas) 
      canvas = new TCanvas(canvasname,canvasname,50,50,400,400);
    sprintf(volbuf,"DC %s",GetName());
    vol[3] = GetDetectorVolume(GetName());
    for(i=0;i<3;i++) {
      sprintf(volbuf,"%sA%d",GetName(),i+1);
      vol[i] = GetDetectorVolume(volbuf);
    }
    fDetectorNode = new BrDetectorDC(nodename,nodename,GetName()); 
    //,vol[3],vol[0],vol[1],vol[2]);

    fDetectorNode->Draw();
    TView *view = canvas->GetView();

    // Set the view to dimensions we know for DC's; this could be made
    // more general but following looks pretty good.
    view->SetRange(-35,-35,-35,35,35,35);
    Int_t irep;
    view->SetView(-90,90,0,irep);
  }
  
  canvas = (TCanvas*)gROOT->FindObject(canvasname);
  
  //Check if canvas still exists; if not, don't draw the tracks.
  if(canvas) {
    canvas->cd();
    fDetectorNode->DrawLocalTracks(fDetectorTracks);
  }
}

 const Char_t* BrDCTrackingModule::GetTableName()
{
  if(GetMode()==kRawData)return BRTABLENAMES kDigDC;
  if(GetMode()==kRedData)return BRTABLENAMES kDigDC;
  if(GetMode()==kDigitized)return BRTABLENAMES kDigitizedDC;
  cout<<"BrDCTrackingModule::GetTableName: You haven't specified table name"<<endl;
  cout<<"BrDCTrackingModule::GetTableName: Do it using SetMode method"<<endl;
  return 0;
}

//_____________________________________________________________
 void BrDCTrackingModule::SetClusterFinderParams(Float_t ClustDx, Float_t ClustDy, Float_t TrioChisq)
{
  // Setting parameter for BrDCClusterFinder module.
  
  fClusDx=ClustDx;
  fClusDy=ClustDy;
  fTrioChisqWin=TrioChisq;
}

//_____________________________________________________________
 void BrDCTrackingModule::SetClusterFinderParams(Float_t MaxAngle, Float_t posRes, Int_t Asigma)
{
  // Another way of setting parameter for BrDCClusterFinder module.
  Double_t rad = .017453292519943;
  fClusDx   = Asigma*posRes*TMath::Sqrt(2.0) + 1.5*MaxAngle*rad;
  fClusDy   = Asigma*posRes*TMath::Sqrt(2.0) + 1.5*rad;
  fPosRes=posRes;
  Float_t N=3.0; // three degrees of freedom
  fTrioChisqWin = N + 3.0*TMath::Sqrt(2*N); //Mean plus 3 sigma of chi2 distribution
}

//____________________________________________________________
 void BrDCTrackingModule::Set2DTrackFinderParams(Float_t CutMatchDx,
						Float_t CutMatchDy, 
						Float_t Asigma)
{
  // Setting parameter for BrDC2DTrackFinder module.

  fDCCutMatchDx = CutMatchDx;
  fDCCutMatchDy = CutMatchDy;
  fAsigma       = Asigma;
}

//____________________________________________________________
 void BrDCTrackingModule::SetViewCombinerParams(Int_t MissMain,Int_t MissSub,Int_t MissOther,
					       Float_t UVDx, Float_t UVDy)
{
  // Setting parameter for BrDCViewCombiner module. 
  fDCCutMaxMissMain  = MissMain;
  fDCCutMaxMissSub   = MissSub;
  fDCCutMaxMissOther = MissOther;

  //Extra parameters for view combiner. 
  fUVDx   = UVDy;
  fUVDy   = UVDx;
}


//  $Log: BrDCTrackingModule.cxx,v $
//  Revision 1.34  2002/06/10 16:40:26  ufstasze
//  Added default setup for fLowLimit and fUpLimit
//
//  Revision 1.33  2002/05/29 12:23:03  ufstasze
//  Moved BrRunInfoManager back to Init(), default value for fGlobalTdcOffset set to -1111
//
//  Revision 1.32  2002/05/27 13:02:17  ufstasze
//  Optimal default setup moved to constructor
//
//  Revision 1.31  2002/05/11 13:58:40  ufstasze
//  Set fDriftTime to 0 (in Constructor)
//
//  Revision 1.30  2002/05/10 12:30:22  ufstasze
//  Added: if(fUseMySql)fClusterFinder -> UseMySql(); in Init()
//
//  Revision 1.29  2002/05/07 17:34:42  ouerdane
//  added driftTime db parameter
//
//  Revision 1.28  2002/05/07 17:32:20  ouerdane
//  added driftTime db parameter
//
//  Revision 1.27  2002/05/02 15:11:53  ufstasze
//   Added member fUseOwnCalib which allows to support case
//   when the own calibration is used (and not from pp run 6618).
//
//  Revision 1.26  2002/04/16 14:55:43  ufstasze
//  Added casting when use SetBfsAngle to avoid compilator warnings
//
//  Revision 1.25  2002/04/11 08:55:30  ufstasze
//  In constructor added: fAssoHits=kFALSE
//
//  Revision 1.24  2002/04/11 08:49:34  ufstasze
//  In constructor added: fDCDriver=0
//
//  Revision 1.23  2002/04/03 12:57:59  ouerdane
//  removed resetting of the track IDs
//
//  Revision 1.22  2002/04/02 14:31:22  ouerdane
//  added a fixe for the track IDs. I saw that they were not set by checking the data recently
//
//  Revision 1.21  2002/03/12 11:02:04  ufstasze
//  Replaced BrDetectorVolume::ListParapeters() by BrDetectorVolume::Print()
//
//  Revision 1.20  2002/03/08 16:27:46  ufstasze
//  Changes that have to do with introducing the readout of global Tdc offset form the data base
//
//  Revision 1.19  2002/03/07 17:50:49  ouerdane
//  added possibility to retrieve the DC global tdc offset from the DB if the user uses UseMySql() method
//
//  Revision 1.18  2002/02/20 13:51:36  ufstasze
//  Added BrRunInfoManager and BrRunInfo in the Init() method and
//  sent BfsAngle and RunNo info. into 2DTrackFinder and ViewCombiner.
//  Commented useless line:
//  //if(tof1Table && fTrigger==1) Tof1TimeRef=GetTof1TimeRef();
//
//  Revision 1.17  2002/02/15 15:36:24  ufstasze
//  Changed definition of fClusDy
//
//  Revision 1.16  2002/01/03 19:52:55  cholm
//  Prepared to use BrTableNames class (or perhaps BrDetectorList) for table names
//
//  Revision 1.15  2001/11/12 10:31:35  ufstasze
//  Added new diagnostic histogram fhAssoHitsPerPlane and pointer to BrDriverDC (fDCDriver).
//
//  Revision 1.14  2001/11/08 13:59:17  staszel
//  Added  SetBFSMatchingMode(),SetFSMatchingMode() and SetAssociatedHitsMode()
//  method in constructor to define a default values.
//
//  Revision 1.13  2001/11/01 14:23:41  staszel
//  Added dchit->SetImod(hit_p->GetImod()) to define module number
//  for hits written to disk.
//
//  Revision 1.12  2001/10/31 15:48:35  staszel
//  Added new method: FillDCHits(BrEventNode*) that creates new table
//  AssociatedHits (DCHits associated with reconstructed tracks).
//
//  Revision 1.10  2001/09/07 14:00:48  staszel
//  minor changes.
//
//  Revision 1.9  2001/08/31 11:43:03  staszel
//  New Tof1 offsetts for exclusive trig. 1.
//
//  Revision 1.8  2001/08/30 13:31:30  staszel
//  Added trigger and Tof1 stuff.
//
//  Revision 1.7  2001/08/23 10:59:49  staszel
//  Changes that have to do with introduction of CheckSingleHits option,
//  plus two options FSMatching and BFSMatching.
//
//  Revision 1.6  2001/08/07 13:48:40  ouerdane
//  Cleaned up a lot the DC tracking modules and utilities:
//   Made all utilities deriving from BrModule
//   Set the class version number to 0 (NOT 1 like before)
//   Removed + in the LinkDef file for all modules
//   Added ClassDef in the View Combiner class (didn't exist)
//   Structurized the histogramming section in TDirectories
//
//  Revision 1.5  2001/08/07 10:56:02  staszel
//  Inline method SetMatchingMode(Bool_t) is added.
//
//  Revision 1.4  2001/08/03 13:24:50  staszel
//  up and low limits on hits number has been added.
//
//  Revision 1.3  2001/08/03 12:50:15  staszel
//  Fill input-node with detector tracks has been added.
//
//  Revision 1.2  2001/07/30 11:57:47  staszel
//  New method SetPromptDelay that allows fPromptDelay to be
//  sent to BrDCClusterFinder.
//
//  Revision 1.1.1.1  2001/06/21 14:55:12  hagel
//  Initial revision of brat2
//
//  Revision 1.8  2001/06/20 12:10:26  staszel
//  BrDetectorHit replaced by BrDCHit.
//
//  Revision 1.7  2001/06/15 15:41:22  staszel
//  Changes having to do with development of dc local tracking package
//
//  Revision 1.6  2001/06/13 13:14:11  staszel
//  father development of dc tracking
//
//  Revision 1.5  2001/01/19 17:54:34  staszel
//  Changes having to do with development of BrDCTrackingModule
//
//  Revision 1.4  2000/10/25 17:58:06  hagel
//  Further development of BrDCTrackingModule
//
//  Revision 1.3  2000/10/05 02:21:31  hagel
//  Further development of BrDCTrackingModule
//
//  Revision 1.2  2000/09/29 02:15:59  hagel
//  Changes having to do with development of BrDCTrackingModule
//
//  Revision 1.1  2000/09/19 21:10:54  hagel
//  Initial placeholder revision
//

This page automatically generated by script docBrat by Christian Holm

Copyright ; 2002 BRAHMS Collaboration <brahmlib@rcf.rhic.bnl.gov>
Last Update on by

Validate HTML
Validate CSS