#ifdef DO_TRACKING_BBE
#include "THaBBe.h"
#include "THaBBTotalShower.h"
#include "THaBBShower.h"
#elif defined DO_TRACKING_BIGBITE
#include "THaBigBite.h"
#endif
#include "THaMWDCPlane.h"
#include "THaMWDCGroup.h"
#include "THaMWDCHit.h"
#include "THaScintillator.h"
#include "THaEvData.h"
#include "THaDetMap.h"
#include "THaTrack.h"
#include "THaTrackProj.h"
#include "THaBenchmark.h"
#include "VarDef.h"
#include "TMath.h"
#include "TList.h"
#include "TH1F.h"
#include "TError.h"
#include "TClonesArray.h"
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <vector>
#ifndef _WIN32
#include <sys/time.h>
#endif
#include "THaMWDC.h"
using namespace std;
THaMWDC::THaMWDC( const char* name, const char* description,
THaApparatus* apparatus ) :
THaTrackingDetector(name,description,apparatus), fNtracks(0),
fDoBench(kTRUE)
{
fPlanes = new TClonesArray("THaMWDCPlane",0);
SetBit( kIgnoreNegDrift );
fNmatrices = 0;
fShowerStatus = 0;
fTotalConsideredEvents = 0;
fTotalTrackingEvents = 0;
fRejectedTrackingEvents = 0;
fHardMaxTrackingEvents = 0;
fSoftMaxTrackingEvents = 0;
fNtracks = 0;
fBench = new THaBenchmark;
}
THaMWDC::~THaMWDC()
{
if (fIsSetup)
RemoveVariables();
delete fBench; fBench = 0;
delete fPlanes; fPlanes = 0;
}
THaAnalysisObject::EStatus THaMWDC::Init( const TDatime& date )
{
Int_t useshowercuts=0;
fReconstructBits = 0;
if( IsZombie() ) {
return fStatus = kInitError;
}
EStatus status;
status = (THaTrackingDetector::Init( date )) ;
if (status!=0) return fStatus = status;
for (UInt_t i=0; i<GetNPlanes(); i++) {
status=GetPlane(i)->Init( date );
if (status!=0)return fStatus = status;
fReconstructBits |= (BIT(i)*GetPlane(i)->IsInReconstruction() );
}
#ifdef DO_TRACKING_BBE
THaBBe* bbe = dynamic_cast<THaBBe*>(GetApparatus());
if( bbe ) {
fMaxGroups = (UInt_t) bbe->GetMaxGroups();
fHardMaxGroups = (UInt_t) bbe->GetHardMaxGroups();
fMinimumPlanes = (UInt_t) bbe->GetMinimumPlanesInRecon();
fShowerXExt = bbe->GetShowerXExtension();
fShowerYExt = bbe->GetShowerYExtension();
fTargetXExt = bbe->GetTargetXExtension();
fTargetYExt = bbe->GetTargetYExtension();
fTargetWindowXOffset = bbe->GetTargetWindowXOffset();
useshowercuts = bbe->UseShowerCuts();
fMaxCallThreshold = bbe->GetMaxCallThreshold();
}
else {
fMaxGroups = 1000000;
fHardMaxGroups = 1000000;
fMinimumPlanes =4;
fShowerXExt = 0.0;
fShowerYExt = 0.0;
fTargetXExt = 0.0;
fTargetYExt = 0.0;
fTargetWindowXOffset = 0.0;
useshowercuts = false;
fMaxCallThreshold = 1000000;
}
#elif defined DO_TRACKING_BIGBITE
THaBigBite* bb = dynamic_cast<THaBigBite*>(GetApparatus());
if( bb ) {
fShowerXExt = bb->GetShowerXExtension();
fShowerYExt = bb->GetShowerYExtension();
fTargetXExt = bb->GetTargetXExtension();
fTargetYExt = bb->GetTargetYExtension();
fTargetWindowXOffset = bb->GetTargetWindowXOffset();
}
else {
fShowerXExt = 0.0;
fShowerYExt = 0.0;
fTargetXExt = 0.0;
fTargetYExt = 0.0;
fTargetWindowXOffset = 0.0;
}
#else
fShowerXExt = 0.0;
fShowerYExt = 0.0;
fTargetXExt = 0.0;
fTargetYExt = 0.0;
fTargetWindowXOffset = 0.0;
#endif
SetUseShowerCuts(useshowercuts);
#ifdef DO_TRACKING_BBE
GenerateConstructMatrices();
clog << "Requiring at least " << fMinimumPlanes << " in reconstruction" << endl;
clog << "Only using planes ";
#endif //#ifdef DO_TRACKING_BBE
for( UInt_t i = 0; i < GetNPlanes(); i++ )
{
if( GetPlane(i)->IsInReconstruction() )
clog << GetPlane(i)->GetName() << " ";
}
#ifdef DO_TRACKING_BBE
clog << "in reconstruction" << endl;
clog << "Skipping tracking on events that will have more than " << fMaxCallThreshold
<< " estimated calls to consider."<< endl;
#endif //#ifdef DO_TRACKING_BBE
if( UseShowerCuts() )
{
clog << endl << "Shower cut specifications: " << endl
<< "-------------------------------------------" << endl;
clog << "Shower X Range (+/- m):\t\t" << fShowerXExt << endl;
clog << "Shower Y Range (+/- m):\t\t" << fShowerYExt << endl;
clog << "Target X Extension (+/- m):\t" << fTargetXExt << endl;
clog << "Target Y Extension (+/- m):\t" << fTargetYExt << endl;
clog << "Target X Offset (m):\t\t"<< fTargetWindowXOffset << endl;
clog << "-------------------------------------------" << endl;
}
else
{
clog << endl << "*Not using cuts on shower*" << endl;
}
LoadTargetData();
return fStatus = kOK;
}
Int_t THaMWDC::Decode( const THaEvData& evdata )
{
Clear();
for (UInt_t i=0; i<GetNPlanes() ;i++) {
GetPlane(i)->Decode(evdata);
}
return 0;
}
Int_t THaMWDC::CoarseTrack( TClonesArray& tracks )
{
#ifndef _WIN32
timeval start, stop;
gettimeofday(&start, NULL);
#endif
SetValidShowerData(false);
tracks.Clear("C");
fNtracks = 0;
fNtracks = ConstructTracks( &tracks, kCoarseTracking );
#ifndef _WIN32
gettimeofday(&stop, NULL);
fCoarseProcTime = stop.tv_sec+stop.tv_usec/1000000.0-start.tv_sec-start.tv_usec/1000000.0;
#else
fCoarseProcTime=0;
#endif
return fNtracks;
}
Int_t THaMWDC::FineTrack( TClonesArray& tracks )
{
#ifndef _WIN32
timeval start, stop;
gettimeofday(&start, NULL);
#endif //#ifndef _WIN32
if( TestBit(kCoarseOnly) )
return 0;
tracks.Clear("C");
fNtracks = 0;
fNtracks = ConstructTracks( &tracks, kFineTracking );
#ifndef _WIN32
gettimeofday(&stop, NULL);
fFineProcTime = stop.tv_sec+stop.tv_usec/1000000.0-start.tv_sec-start.tv_usec/1000000.0;
#else
fFineProcTime=0;
#endif //#ifndef _WIN32
return fNtracks;
}
Int_t THaMWDC::DefineVariables( EMode mode )
{
if( mode == kDefine && fIsSetup ) return kOK;
fIsSetup = ( mode == kDefine );
RVarDef vars[] = {
{ "trackskipped", "Tracking skipped or truncated", "fTooBusy" },
{ "cproctime", "Coarse Processing Time", "fCoarseProcTime"},
{ "fproctime", "Fine Processing Time", "fFineProcTime" },
{ "estngrp", "Estimated Number of Groups", "fEstNGroups" },
{ "estncall", "Estimated Number of Calls", "fEstNCalls" },
{ "ngrp", "Number of Groups", "fNGroups" },
{ "ncall", "Number of recursive calls", "fNCalls" },
{ 0 }
};
DefineVarsFromList( vars, mode );
return 0;
}
void THaMWDC::Print(const Option_t* opt) const {
THaTrackingDetector::Print(opt);
clog<<"Number of Planes "<<GetNPlanes()<<endl;
fBench->Print();
return;
}
void THaMWDC::SetDebug( Int_t level )
{
THaTrackingDetector::SetDebug( level );
for( UInt_t i = 0; i<GetNPlanes(); i++ ) {
THaMWDCPlane* thePlane = static_cast<THaMWDCPlane*>( fPlanes->At(i) );
thePlane->SetDebug( level );
}
}
void THaMWDC::EnableBenchmarks( Bool_t b )
{
fDoBench = b;
}
Int_t THaMWDC::ReadDatabase( const TDatime& date )
{
FILE* file = OpenFile( date );
if( !file ) return kFileError;
static const char* const here = "ReadDatabase";
const int LEN = 200;
char buff[LEN];
TString tag1(fPrefix);
TString tag2(fPrefix);
Ssiz_t pos = tag1.Index(".");
if( pos != kNPOS )
tag1 = tag1(0,pos+1);
else
tag1.Append(".");
tag1.Prepend("[");
tag1.Append("global]");
pos = tag2.Index(".");
if( pos != kNPOS )
tag2 = tag2(0,pos+1);
else
tag2.Append(".");
tag2.Prepend("[");
tag2.Append("global.done]");
TString line, tag3(tag1);
tag1.ToLower();
tag2.ToLower();
bool found = false;
while (!found && fgets (buff, LEN, file) != NULL) {
char* buf = ::Compress(buff);
line = buf;
delete [] buf;
if( line.EndsWith("\n") ) line.Chop();
line.ToLower();
if ( tag1 == line )
found = true;
}
if( !found ) {
Error(Here(here), "Database entry %s not found!", tag3.Data() );
fclose(file);
return kInitError;
}
if( GetNPlanes()>0 || fIsInit ) {
Warning(Here(here), "Database has already be read in. Using the planes we already have." );
Warning(Here(here), "Hope you have the same configureation in the databases." );
}
else
{
TString planename = " ";
TString planedescr = " ";
Int_t nPlanes = 0;
while ((planename!=tag2)&&(fgets(buff, LEN, file)!=NULL)) {
char* buf = ::Compress(buff);
planename = buf;
delete [] buf;
if( planename.EndsWith("\n") ) planename.Chop();
planename.ToLower();
if (planename!=tag2) {
fgets(buff, LEN, file);
planedescr = buff;
if( planedescr.EndsWith("\n") ) planedescr.Chop();
planedescr.ToLower();
THaMWDCPlane* thePlane =
new((*fPlanes)[nPlanes]) THaMWDCPlane(planename,planedescr,this);
thePlane->SetDebug(fDebug);
nPlanes++;
}
}
}
fclose(file);
if (MAX_PLANES<GetNPlanes()) {
Error(Here(here),"More planes in database than permitted by MAX_PLANES!");
return kInitError;
}
fIsInit = true;
return kOK;
}
void THaMWDC::Clear( Option_t* opt )
{
THaTrackingDetector::Clear(opt);
fTooBusy = 0;
for( UInt_t counter = 0; counter < GetNGroups(); counter++ )
{
delete GetGroup(counter);
}
fGroups.clear();
fNGroups = 0;
fCoarseProcTime = -1e9;
fFineProcTime = -1e9;
fEstNGroups = 0;
fEstNCalls = 0;
fNCalls = 0;
}
Int_t THaMWDC::End(THaRunBase *run)
{
PrintCutInfo();
fBench->Print();
return THaTrackingDetector::End(run);
}
Int_t THaMWDC::ConstructTracks( TClonesArray* tracks, ETrackingMode mode )
{
#if !DO_THAMWDC_TRACKING
{
}
return 0;
#elif defined DO_TRACKING_OTHER
#elif defined DO_TRACKING_BBE
{
const char *lbl="THaMWDC::Tracking ENABLED";
static void *use_lbl = (void*)&lbl;
}
vector<THaMWDCGroup*> groups_with_track;
vector<THaMWDCGroup*> unique_groups;
UInt_t i, j;
Int_t nTracks = 0;
if (fDoBench) fBench->Begin("THaMWDC::ConstructTracks");
if( mode == kCoarseTracking )
{
if (fDoBench) fBench->Begin("THaMWDC::FindGroups");
FindGroups();
if (fDoBench) fBench->Stop("THaMWDC::FindGroups");
}
if( mode == kFineTracking )
{
SetScintCorrection();
}
if (fDoBench) fBench->Begin("THaMWDC::FindTrack");
for( i = 0; i < GetNGroups(); i++ )
{
if( GetGroup(i) )
{
if( mode == kCoarseTracking )
{
GetGroup(i)->DoCoarseTracking();
if( GetGroup(i)->IsValidTrack() )
groups_with_track.push_back(GetGroup(i));
}
if( mode == kFineTracking && GetGroup(i)->IsL1Group() )
{
GetGroup(i)->DoFineTracking();
if( GetGroup(i)->IsValidTrack() )
groups_with_track.push_back(GetGroup(i));
}
}
}
if (fDoBench) fBench->Stop("THaMWDC::FindTrack");
if (fDoBench) fBench->Begin("THaMWDC::SortTracksGroups");
HeapSortGroupsByChi2(groups_with_track);
Bool_t isUnique;
if( mode == kCoarseTracking )
{
for( i = 0; i < TOPNCOARSETRACKS && i < groups_with_track.size(); i++ )
{
groups_with_track[i]->SetL1Group(true);
unique_groups.push_back( groups_with_track[i] );
}
}
if( mode == kFineTracking )
{
i = 0;
while( unique_groups.size() < TOPNTRACKS && i < groups_with_track.size() )
{
isUnique = true;
for( j = 0; j < unique_groups.size(); j++ )
{
if( groups_with_track[i]->IsSimilarTrack( unique_groups[j]->GetTrack() ) )
{
isUnique = false;
}
}
if( isUnique )
{
groups_with_track[i]->SetL2Group(true);
unique_groups.push_back( groups_with_track[i] );
}
i++;
}
}
if (fDoBench) fBench->Stop("THaMWDC::SortTracksGroups");
if (fDoBench) fBench->Begin("THaMWDC::CheckTracks");
for( i = 0; i < unique_groups.size(); i++ )
{
THaTrack *tr = new((*tracks)[nTracks++]) THaTrack( unique_groups[i]->GetX(), unique_groups[i]->GetY(),
unique_groups[i]->GetTheta(), unique_groups[i]->GetPhi() );
tr->SetChi2( unique_groups[i]->GetChi2(), unique_groups[i]->GetNDoF() );
if( mode == kCoarseTracking )
{
tr->SetFlag(1);
}
if( mode == kFineTracking )
{
unique_groups[i]->SetTrackNumber(nTracks-1);
unique_groups[i]->SetChi2Cont();
tr->SetFlag(2);
}
}
if (fDoBench) fBench->Stop("THaMWDC::CheckTracks");
if (fDoBench) fBench->Begin("THaMWDC::CleanTracks");
if (fDoBench) fBench->Stop("THaMWDC::CleanTracks");
if (fDoBench) fBench->Stop("THaMWDC::ConstructTracks");
groups_with_track.clear();
unique_groups.clear();
return nTracks;
#endif //#ifndef DO_TRACKING_BBE
}
#ifdef DO_TRACKING_BBE
void THaMWDC::FindGroups()
{
UInt_t nplanes = GetNPlanes();
if( nplanes < fMinimumPlanes ) return;
UInt_t planebits;
UInt_t sortcounter;
UInt_t plane;
UInt_t counter;
UInt_t trunc = 0;
UInt_t estimatedgroups = 0;
UInt_t estimatedcalls = 0;
Int_t validwires = 0;
Int_t hitplanes = 0;
Int_t sum;
Int_t product;
vector<THaMWDCGroup*> unsortedgroups;
vector<THaMWDCHit*> currenthits;
UInt_t usablebits;
Int_t usablewires[MAX_PLANES];
UInt_t lastbits = (1 << nplanes)-1;
if( UseShowerCuts() )
{
GetShowerData();
}
usablebits = 0;
product = 1;
for( UInt_t planecounter = 0; planecounter < nplanes; planecounter++ )
{
if( GetPlane(planecounter)->IsInReconstruction() )
{
validwires = 0;
for( counter = 0; counter < GetPlane(planecounter)->GetNHits(); counter++ )
{
HitValidByShower( GetPlane(planecounter)->GetHit(counter) );
}
for( counter = 0; counter < GetPlane(planecounter)->GetNHits(); counter++ )
{
if( GetPlane(planecounter)->GetHit(counter)->IsValidByShower() )
validwires++;
}
if( validwires > 0 )
{
product *= validwires;
estimatedgroups += product;
hitplanes++;
usablebits |= BIT(planecounter);
}
usablewires[planecounter] = validwires;
}
}
fEstNGroups = estimatedgroups;
Int_t ncombos;
for( planebits = lastbits; planebits > 0; planebits-- )
{
sum = 0;
ncombos = 1;
if( fValidBits[planebits] && ((usablebits & planebits)==planebits) )
{
for( plane = 0; plane < nplanes; plane++ )
{
if( BIT(plane) & planebits )
{
ncombos *= usablewires[plane];
sum += ncombos;
}
}
estimatedcalls += sum;
}
}
fEstNCalls = estimatedcalls;
fTotalTrackingEvents++;
if( estimatedcalls > fMaxCallThreshold )
{
fTooBusy |= kRejected;
fRejectedTrackingEvents++;
return;
}
fNCalls = 0;
if( hitplanes >= (Int_t) fMinimumPlanes )
{
for( planebits = lastbits; planebits > 0; planebits-- )
{
if( fValidBits[planebits] && ((usablebits & planebits)==planebits) )
{
AllCombos( planebits, unsortedgroups, currenthits, 1 );
}
}
}
if(fHardMaxGroups == (UInt_t) unsortedgroups.size() )
{
fHardMaxTrackingEvents++;
fTooBusy |= kHardMax;
}
for( sortcounter = nplanes; sortcounter >= fMinimumPlanes; sortcounter-- )
{
if( GetNGroups() < fMaxGroups )
{
for( counter = 0; counter < unsortedgroups.size(); counter++ )
{
if( unsortedgroups[counter] != NULL )
{
if( unsortedgroups[counter]->GetNHits() == sortcounter )
fGroups.push_back( unsortedgroups[counter] );
}
}
}
else
{
for( counter = 0; counter < unsortedgroups.size(); counter++ )
{
if( unsortedgroups[counter] != NULL )
{
if( !trunc )
{
trunc = 1;
fSoftMaxTrackingEvents++;
fTooBusy |= kSoftMax;
}
if( unsortedgroups[counter]->GetNHits() == sortcounter )
{
delete unsortedgroups[counter];
unsortedgroups[counter] = NULL;
}
}
}
}
}
unsortedgroups.clear();
currenthits.clear();
fNGroups = GetNGroups();
return;
}
#endif //#ifdef DO_TRACKING_BBE
#ifdef DO_TRACKING_BBE
void THaMWDC::AllCombos( Int_t planebits, vector<THaMWDCGroup*> &groups, vector<THaMWDCHit *> hits, Int_t depth )
{
fNCalls++;
Int_t counter;
THaMWDCPlane *plane;
vector<THaMWDCHit *> tempvector;
if( groups.size() >= fHardMaxGroups )
{
return;
}
if( fPlaneNumbersForBits[planebits][depth-1] != -1 )
{
plane = GetPlane(fPlaneNumbersForBits[planebits][depth-1]);
for( counter = 0; counter < (Int_t) plane->GetNHits(); counter++ )
{
if( HitValidByShower( plane->GetHit(counter) ) )
{
tempvector = hits;
tempvector.push_back( plane->GetHit(counter) );
AllCombos( planebits, groups, tempvector, depth+1 );
tempvector.clear();
}
}
}
else
{
groups.push_back( new THaMWDCGroup( hits, planebits, fConstructMatrix[planebits] ) );
}
return;
}
#endif //#ifdef DO_TRACKING_BBE
#ifdef DO_TRACKING_BBE
Bool_t THaMWDC::HitValidByShower( THaMWDCHit *hit )
{
Bool_t inside = false;
Double_t wirepos, wirex1, wirex2, Angle, maxlimitx, maxlimity, minlimitx, minlimity,
targetxhigh, targetyhigh, targetxlow, targetylow, targetz;
wirepos = wirex1 = wirex2 = Angle = maxlimitx = maxlimity = minlimitx = minlimity =
targetxhigh = targetyhigh = targetxlow = targetylow = targetz = 0.0;
if( hit->VerifiedByShower() )
{
return hit->IsValidByShower();
}
if( !HaveValidShowerData() )
{
hit->SetVerifiedByShower(true);
hit->SetIsValidByShower(true);
return 1;
}
Angle = hit->GetPlane()->GetWAngle();
wirepos = hit->GetWire()->GetPos() + hit->GetPlane()->GetXYCorr();
targetxhigh = fTargetXhigh;
targetyhigh = fTargetYhigh;
targetxlow = fTargetXlow;
targetylow = fTargetYlow;
targetz = fTargetZ;
maxlimitx = ( (fShowerOrigin.X() + fShowerXExt - targetxhigh) / (fShowerOrigin.Z() - targetz ) )
* (hit->GetPlane()->GetZ() - targetz) + targetxhigh;
minlimitx = ( (fShowerOrigin.X() - fShowerXExt - targetxlow) / (fShowerOrigin.Z() - targetz ) )
* (hit->GetPlane()->GetZ() - targetz) + targetxlow;
maxlimity = ( (fShowerOrigin.Y() + fShowerYExt - targetyhigh) / (fShowerOrigin.Z() - targetz ) )
* (hit->GetPlane()->GetZ() - targetz) + targetyhigh;
minlimity = ( (fShowerOrigin.Y() - fShowerYExt - targetylow) / (fShowerOrigin.Z() - targetz ) )
* (hit->GetPlane()->GetZ() - targetz) + targetylow;
wirex1 = (wirepos / TMath::Sin(TMath::Abs(Angle))) + (minlimity * TMath::Cos(Angle)/TMath::Sin(Angle));
wirex2 = (wirepos / TMath::Sin(TMath::Abs(Angle))) + (maxlimity * TMath::Cos(Angle)/TMath::Sin(Angle));
if( !( (wirex1 > maxlimitx && wirex2 > maxlimitx) || (wirex1 < minlimitx && wirex2 < minlimitx )) )
{
inside = true;
}
else
{
inside = false;
}
hit->SetVerifiedByShower(true);
hit->SetIsValidByShower(inside);
return inside;
}
#endif //#ifdef DO_TRACKING_BBE
#ifdef DO_TRACKING_BBE
void THaMWDC::GetShowerData()
{
SetValidShowerData(false);
SetValidClusterData(false);
THaBBTotalShower *totalshower = dynamic_cast<THaBBTotalShower *>( GetApparatus()->GetDetector("ts"));
if( totalshower == 0 )
return;
THaBBShower *shower = totalshower->GetShower();
if( shower == 0 )
return;
if( shower->GetNclust() == 0 ){
return;
}
else
{
SetValidClusterData(true);
}
fShowerOrigin = TVector3( shower->GetX(0), shower->GetY(0), shower->GetOrigin().Z() );
SetValidShowerData(true);
return;
}
#endif //#ifdef DO_TRACKING_BBE
void THaMWDC::LoadTargetData()
{
TVector3 targetstart;
TVector3 targetend;
TVector3 targetcenter;
#ifdef DO_TRACKING_BBE
THaBBe* bb = dynamic_cast<THaBBe*>(GetApparatus());
if( bb ) {
targetstart = bb->GetTargetStartInDet();
targetend = bb->GetTargetEndInDet();
targetcenter = bb->GetTargetCenterInDet();
fTargetZ = (bb->GetTargetCenterInDet()).Z();
if( targetstart. X() > targetend.X() ) {
fTargetXhigh = targetstart.X() + fTargetXExt + fTargetWindowXOffset;
fTargetXlow = targetend.X() - fTargetXExt + fTargetWindowXOffset;
}
else {
fTargetXhigh = targetend.X() + fTargetXExt + fTargetWindowXOffset;
fTargetXlow = targetstart.X() - fTargetXExt + fTargetWindowXOffset;
}
if( targetstart.Y() > targetend.Y() ) {
fTargetYhigh = targetstart.Y() + fTargetYExt;
fTargetYlow = targetend.Y() - fTargetYExt;
}
else {
fTargetYhigh = targetend.Y() + fTargetYExt;
fTargetYlow = targetstart.Y() - fTargetYExt;
}
}
else {
fTargetZ = 0.0;
fTargetXhigh = 0.0;
fTargetYhigh = 0.0;
fTargetXlow = 0.0;
fTargetYlow = 0.0;
}
#elif defined DO_TRACKING_BIGBITE
THaBigBite* bb = dynamic_cast<THaBigBite*>(GetApparatus());
if( bb ) {
targetstart = bb->GetTargetStartInDet();
targetend = bb->GetTargetEndInDet();
targetcenter = bb->GetTargetCenterInDet();
fTargetZ = (bb->GetTargetCenterInDet()).Z();
if( targetstart. X() > targetend.X() ) {
fTargetXhigh = targetstart.X() + fTargetXExt + fTargetWindowXOffset;
fTargetXlow = targetend.X() - fTargetXExt + fTargetWindowXOffset;
}
else {
fTargetXhigh = targetend.X() + fTargetXExt + fTargetWindowXOffset;
fTargetXlow = targetstart.X() - fTargetXExt + fTargetWindowXOffset;
}
if( targetstart.Y() > targetend.Y() ) {
fTargetYhigh = targetstart.Y() + fTargetYExt;
fTargetYlow = targetend.Y() - fTargetYExt;
}
else {
fTargetYhigh = targetend.Y() + fTargetYExt;
fTargetYlow = targetstart.Y() - fTargetYExt;
}
}
else {
fTargetZ = 0.0;
fTargetXhigh = 0.0;
fTargetYhigh = 0.0;
fTargetXlow = 0.0;
fTargetYlow = 0.0;
}
#else
fTargetZ = 0.0;
fTargetXhigh = 0.0;
fTargetYhigh = 0.0;
fTargetXlow = 0.0;
fTargetYlow = 0.0;
#endif //#ifdef DO_TRACKING_BBE
clog << "Beam position in detector coords: (" << targetcenter.X()
<< ", " << targetcenter.Y()
<< ", " << targetcenter.Z() << ")"
<< endl << endl;
clog << "Beam direction in detector coords: ("
<< (targetstart - targetend).Unit().X() << ", "
<< (targetstart - targetend).Unit().Y() << ", "
<< (targetstart - targetend).Unit().Z() << ")"
<< endl << endl;
return;
}
void THaMWDC::PrintCutInfo()
{
UInt_t plane;
if( TestBit( kIgnoreNegDrift ) )
{
clog <<endl << endl << "Drift Time Cuts" << endl;
clog << "---------------------------------------------------------------------------" << endl;
clog << "Plane Rejected Total Min Drift MaxDrift" << endl;
for( plane = 0; plane < GetNPlanes(); plane++ )
{
GetPlane(plane)->PrintDriftCuts();
}
clog << "---------------------------------------------------------------------------" << endl;
clog << endl;
}
if( fTotalTrackingEvents > 0 )
{
clog <<endl << endl << "Group Cuts" << endl;
clog << "---------------------------------------------------------------------------" << endl;
clog << fRejectedTrackingEvents << "\trejected tracking events" << endl;
clog << fHardMaxTrackingEvents << "\t events trucated by hard maximum" << endl;
clog << fSoftMaxTrackingEvents << "\t events trucated by soft maximum" << endl;
clog << fTotalTrackingEvents << "\tTOTAL tracking events" << endl;
clog << "---------------------------------------------------------------------------" << endl;
clog << endl;
}
return;
}
#ifdef DO_TRACKING_BBE
void THaMWDC::GenerateConstructMatrices()
{
if( GetNPlanes() < fMinimumPlanes ) return;
memset(fValidBits,0,MAX_MATRIX*sizeof(*fValidBits));
UInt_t lastbits = (1 << GetNPlanes())-1;
clog << "Generating Matrices" << endl;
if( lastbits > MAX_MATRIX )
{
clog << "You don't have enough space to store all the matrices." << endl
<< "\t" << MAX_MATRIX << " maximum array size" << "\t" << lastbits << " potential matrices"
<< endl << endl
<< "Please increase the size of the MAX_MATRIX definition in THaMWDC.h to at least " << lastbits << endl;
exit(1);
}
for( UInt_t trialbits = 0; trialbits <= lastbits; trialbits++ )
{
if( (~fReconstructBits & trialbits) == 0 )
GenerateConstructMatrixForBits( trialbits );
else
fValidBits[trialbits] = false;
}
clog << "Completed Matrix Generation - " << fNmatrices << " matrices generated" << endl;
return;
}
void THaMWDC::GenerateConstructMatrixForBits( Int_t trialbits )
{
Int_t planenumber[MAX_PLANES];
UInt_t nplanes;
UInt_t counter;
Double_t mvalues[MAX_PLANES*4];
TMatrixD *M;
TMatrixD *Mt;
TMatrixD *Minv;
TMatrixD *Mfinal;
for( counter = 0; counter < GetNPlanes(); counter++ )
{
mvalues[counter*4+0] = 0.0;
mvalues[counter*4+1] = 0.0;
mvalues[counter*4+2] = 0.0;
mvalues[counter*4+3] = 0.0;
}
nplanes = 0;
counter = 0;
for( counter = 0; counter < GetNPlanes(); counter++ )
{
if( trialbits & BIT(counter) )
{
nplanes++;
planenumber[nplanes-1] = counter;
fPlaneNumbersForBits[trialbits][nplanes-1] = counter;
}
}
fPlaneNumbersForBits[trialbits][nplanes] = -1;
if( nplanes >= fMinimumPlanes )
{
for( counter = 0; counter < nplanes; counter++ )
{
mvalues[planenumber[counter]*4+0] =
TMath::Sin( TMath::Abs(GetPlane(planenumber[counter])->GetWAngle()) )
/ GetPlane(planenumber[counter])->GetPosRes();
mvalues[planenumber[counter]*4+1] = GetPlane(planenumber[counter])->GetZ() *
TMath::Sin( TMath::Abs(GetPlane(planenumber[counter])->GetWAngle()) )
/ GetPlane(planenumber[counter])->GetPosRes();
mvalues[planenumber[counter]*4+2] = -1.0 * (GetPlane(planenumber[counter])->GetWAngle()
/ TMath::Abs(GetPlane(planenumber[counter])->GetWAngle()) )
* TMath::Cos(GetPlane(planenumber[counter])->GetWAngle() )
/ GetPlane(planenumber[counter])->GetPosRes();
mvalues[planenumber[counter]*4+3] = GetPlane(planenumber[counter])->GetZ() *
-1.0 * (GetPlane(planenumber[counter])->GetWAngle()
/ TMath::Abs(GetPlane(planenumber[counter])->GetWAngle()) )
* TMath::Cos(GetPlane(planenumber[counter])->GetWAngle() )
/ GetPlane(planenumber[counter])->GetPosRes();
}
M = new TMatrixD( GetNPlanes(), 4, mvalues );
Mt = new TMatrixD(4,GetNPlanes());
Mt->Transpose(*M);
Minv = new TMatrixD( 4, 4 );
Minv->Mult( *Mt, *M );
if( TMath::Abs(Minv->Determinant()) > MIN_DETERMINANT )
{
Minv->Invert();
Mfinal = new TMatrixD( 4, GetNPlanes() );
Mfinal->Mult(*Minv, *Mt);
fConstructMatrix[trialbits] = Mfinal;
fValidBits[trialbits] = true;
fNmatrices++;
}
else
{
fValidBits[trialbits] = false;
}
delete M;
delete Minv;
delete Mt;
}
return;
}
void THaMWDC::HeapSortGroupsByChi2( vector <THaMWDCGroup *> &v )
{
vector <THaMWDCGroup *> sorted;
HeapSortBuildHeap(v);
while( !v.empty() )
{
sorted.push_back( v.front() );
v[0] = v.back();
v.pop_back();
HeapSortSiftDown( v, 0);
}
v.clear();
v = sorted;
sorted.clear();
return;
}
void THaMWDC::HeapSortSiftDown( vector <THaMWDCGroup *> &v, UInt_t n )
{
UInt_t node1, node2;
THaMWDCGroup *temp;
node1 = 2*n+1;
node2 = 2*n+2;
if( n >= v.size() || node1 >= v.size() ) return;
if( v[node1]->GetChi2NDoF() < v[n]->GetChi2NDoF() )
{
if( node2 < v.size() )
{
if( v[node1]->GetChi2NDoF() > v[node2]->GetChi2NDoF() )
{
temp = v[n];
v[n] = v[node2];
v[node2] = temp;
HeapSortSiftDown( v, node2 );
}
else
{
temp = v[n];
v[n] = v[node1];
v[node1] = temp;
HeapSortSiftDown( v, node1 );
}
}
else
{
temp = v[n];
v[n] = v[node1];
v[node1] = temp;
}
}
return;
}
void THaMWDC::HeapSortBuildHeap( vector <THaMWDCGroup *> &v )
{
Int_t i;
for( i = v.size()/2+1; i >= 0; i-- )
{
HeapSortSiftDown(v,(UInt_t) i);
}
}
Bool_t THaMWDC::ValidPlaneBits( UInt_t bits )
{
if( bits >= MAX_MATRIX || bits < 0 )
{
clog << "Tried to see if " << bits << " was valid (they're not)" << endl;
return 0;
}
return fValidBits[bits];
}
Double_t THaMWDC::GetScintillatorTime()
{
Double_t scinttime;
THaScintillator *scint = dynamic_cast<THaScintillator *>( GetApparatus()->GetDetector("s"));
if( scint == 0 )
{
return 0.0;
}
const TClonesArray *TrackHits;
const Double_t *times;
if( scint->GetTrackHits()->GetEntries() == 0 ) return 0.0;
TrackHits = scint->GetTrackHits();
THaTrackProj* primary_track = (THaTrackProj *) TrackHits->At(0);
times = scint->GetTimes();
scinttime = times[primary_track->GetChannel()] - SCINT_OFFSET;
if( TMath::Abs(scinttime)>SCINT_TIME_WIDTH ) return 0.0;
return scinttime;
}
Double_t THaMWDC::GetScintillatorZ()
{
THaScintillator *scint = dynamic_cast<THaScintillator *>( GetApparatus()->GetDetector("s"));
if( scint == 0 )
{
return 0.0;
}
return scint->GetOrigin().Z();
}
Double_t THaMWDC::GetScintTimeCorrection(THaMWDCPlane *pl)
{
return (GetScintillatorTime() - SPEED_OF_LIGHT*( GetScintillatorZ() - pl->GetZ()));
}
#endif //#ifdef DO_TRACKING_BBE
void THaMWDC::SetScintCorrection()
{
UInt_t plane, hit;
for( plane = 0; plane < GetNPlanes(); plane++ )
{
for( hit = 0; hit < GetPlane(plane)->GetNHits(); hit++ )
{
#ifdef DO_TRACKING_BBE
GetPlane(plane)->GetHit(hit)->SetScintOffset(GetScintTimeCorrection(GetPlane(plane)));
#elif defined DO_TRACKING_OTHER
#else
Warning(Here("SetScintCorrection()"),"No SetScintCorrection algorithm yet, set to 0 by default");
GetPlane(plane)->GetHit(hit)->SetScintOffset(0);
#endif //#ifdef DO_TRACKING_BBE
}
}
}
ClassImp(THaMWDC)
Last update: Tue Jul 7 19:26:17 2009
This page has been automatically generated. If you have any comments or suggestions about the page layout send a mail to ROOT support, or contact the developers with any questions or problems regarding ROOT.