// ------------------------------------------------------------------------- // ----- CbmSts source file ----- // ----- Created 26/07/04 by V. Friese ----- // ------------------------------------------------------------------------- #include "CbmSts.h" #include "CbmGeoSts.h" #include "CbmStsPoint.h" #include "CbmGeoStsPar.h" #include "CbmDetectorList.h" #include "CbmStsDetectorId.h" #include "FairGeoInterface.h" #include "FairGeoLoader.h" #include "FairGeoNode.h" #include "FairGeoRootBuilder.h" #include "FairRootManager.h" #include "CbmStack.h" #include "FairRuntimeDb.h" #include "FairRun.h" #include "FairVolume.h" #include "TClonesArray.h" #include "TGeoMCGeometry.h" #include "TParticle.h" #include "TVirtualMC.h" #include "TObjArray.h" #include "TGeoManager.h" #include using std::cout; using std::cerr; using std::endl; // ----- Default constructor ------------------------------------------- CbmSts::CbmSts() : FairDetector("STS", kTRUE, kSTS), fTrackID(0), fVolumeID(0), fDetectorId(0), fPosIn(0.,0.,0.,0.), fPosOut(0.,0.,0.,0.), fMomIn(0.,0.,0.,0.), fMomOut(0.,0.,0.,0.), fTime(0.), fLength(0.), fELoss(0.), fPosIndex(0), fStsCollection(NULL), kGeoSaved(kFALSE), flGeoPar(new TList), fVolumeMap(), fVolumeMapIter() { ResetParameters(); fStsCollection = new TClonesArray("CbmStsPoint"); fPosIndex = 0; kGeoSaved = kFALSE; flGeoPar = new TList(); flGeoPar->SetName( GetName()); fVerboseLevel = 1; } // ------------------------------------------------------------------------- // ----- Standard constructor ------------------------------------------ CbmSts::CbmSts(const char* name, Bool_t active) : FairDetector(name, active, kSTS), fTrackID(0), fVolumeID(0), fDetectorId(0), fPosIn(0.,0.,0.,0.), fPosOut(0.,0.,0.,0.), fMomIn(0.,0.,0.,0.), fMomOut(0.,0.,0.,0.), fTime(0.), fLength(0.), fELoss(0.), fPosIndex(0), fStsCollection(NULL), kGeoSaved(kFALSE), flGeoPar(new TList), fVolumeMap(), fVolumeMapIter() { ResetParameters(); fStsCollection = new TClonesArray("CbmStsPoint"); fPosIndex = 0; kGeoSaved = kFALSE; flGeoPar = new TList(); flGeoPar->SetName( GetName()); fVerboseLevel = 1; } // ------------------------------------------------------------------------- // ----- Destructor ---------------------------------------------------- CbmSts::~CbmSts() { if ( flGeoPar ) delete flGeoPar; if (fStsCollection) { fStsCollection->Delete(); delete fStsCollection; } } // ------------------------------------------------------------------------- // ----- Public method ProcessHits -------------------------------------- Bool_t CbmSts::ProcessHits(FairVolume* vol) { // cout << " -I process hit called for:" << vol->GetName() << endl; // Set parameters at entrance of volume. Reset ELoss. // if ( vol ) { // cout << " Name Id:copy " // << vol->getName() << " : " << vol->getMCid() << " : " << vol->getCopyNo() << endl; // Int_t copyNo=0; // cout << " Geant: " << gMC->CurrentVolID(copyNo) << ":" << copyNo << endl; // } if ( gMC->IsTrackEntering() ) { fELoss = 0.; fTime = gMC->TrackTime() * 1.0e09; fLength = gMC->TrackLength(); gMC->TrackPosition(fPosIn); gMC->TrackMomentum(fMomIn); } // Sum energy loss for all steps in the active volume fELoss += gMC->Edep(); // Set additional parameters at exit of active volume. Create CbmStsPoint. if ( gMC->IsTrackExiting() || gMC->IsTrackStop() || gMC->IsTrackDisappeared() ) { fTrackID = gMC->GetStack()->GetCurrentTrackNumber(); fDetectorId = vol->getMCid(); gMC->TrackPosition(fPosOut); gMC->TrackMomentum(fMomOut); if (fELoss == 0. ) return kFALSE; if (gMC->IsTrackExiting()) { const Double_t* oldpos; const Double_t* olddirection; Double_t newpos[3]; Double_t newdirection[3]; Double_t safety; gGeoManager->FindNode(fPosOut.X(),fPosOut.Y(),fPosOut.Z()); oldpos = gGeoManager->GetCurrentPoint(); olddirection = gGeoManager->GetCurrentDirection(); // cout << "1st direction: " << olddirection[0] << "," << olddirection[1] << "," << olddirection[2] << endl; for (Int_t i=0; i<3; i++){ newdirection[i] = -1*olddirection[i]; } gGeoManager->SetCurrentDirection(newdirection); TGeoNode *bla = gGeoManager->FindNextBoundary(2); safety = gGeoManager->GetSafeDistance(); gGeoManager->SetCurrentDirection(-newdirection[0],-newdirection[1],-newdirection[2]); for (Int_t i=0; i<3; i++){ newpos[i] = oldpos[i] - (3*safety*olddirection[i]); } /*if ( fPosIn.Z() < 30. && newpos[2] > 30.02 ) { cerr << "2nd direction: " << olddirection[0] << "," << olddirection[1] << "," << olddirection[2] << " with safety = " << safety << endl; cerr << "oldpos = " << oldpos[0] << "," << oldpos[1] << "," << oldpos[2] << endl; cerr << "newpos = " << newpos[0] << "," << newpos[1] << "," << newpos[2] << endl; }*/ fPosOut.SetX(newpos[0]); fPosOut.SetY(newpos[1]); fPosOut.SetZ(newpos[2]); } AddHit(fTrackID, fDetectorId, TVector3(fPosIn.X(), fPosIn.Y(), fPosIn.Z()), TVector3(fPosOut.X(), fPosOut.Y(), fPosOut.Z()), TVector3(fMomIn.Px(), fMomIn.Py(), fMomIn.Pz()), TVector3(fMomOut.Px(), fMomOut.Py(), fMomOut.Pz()), fTime, fLength, fELoss); // Increment number of StsPoints for this track CbmStack* stack = (CbmStack*) gMC->GetStack(); stack->AddPoint(kSTS); ResetParameters(); } return kTRUE; } // ---------------------------------------------------------------------------- //void CbmSts::SaveGeoParams(){ // // cout << " -I Save STS geo params " << endl; // // TFolder *mf = (TFolder*) gDirectory->FindObjectAny("cbmroot"); // cout << " mf: " << mf << endl; // TFolder *stsf = NULL; // if (mf ) stsf = (TFolder*) mf->FindObjectAny(GetName()); // cout << " stsf: " << stsf << endl; // if (stsf) stsf->Add( flGeoPar0 ) ; // FairRootManager::Instance()->WriteFolder(); // mf->Write("cbmroot",TObject::kWriteDelete); //} // ----- Public method EndOfEvent ----------------------------------------- void CbmSts::BeginEvent() { // if (! kGeoSaved ) { // SaveGeoParams(); // cout << "-I STS geometry parameters saved " << endl; // kGeoSaved = kTRUE; // } } // ----- Public method EndOfEvent ----------------------------------------- void CbmSts::EndOfEvent() { if (fVerboseLevel) Print(); // fStsCollection->Clear(); fStsCollection->Delete(); ResetParameters(); } // ---------------------------------------------------------------------------- // ----- Public method Register ------------------------------------------- void CbmSts::Register() { FairRootManager::Instance()->Register("StsPoint", GetName(), fStsCollection, kTRUE); } // ---------------------------------------------------------------------------- // ----- Public method GetCollection -------------------------------------- TClonesArray* CbmSts::GetCollection(Int_t iColl) const { if (iColl == 0) return fStsCollection; else return NULL; } // ---------------------------------------------------------------------------- // ----- Public method Print ---------------------------------------------- void CbmSts::Print() const { Int_t nHits = fStsCollection->GetEntriesFast(); cout << "-I- CbmSts: " << nHits << " points registered in this event." << endl; } // ---------------------------------------------------------------------------- // ----- Public method Reset ---------------------------------------------- void CbmSts::Reset() { // fStsCollection->Clear(); fStsCollection->Delete(); ResetParameters(); } // ---------------------------------------------------------------------------- // ----- Public method CopyClones ----------------------------------------- void CbmSts::CopyClones(TClonesArray* cl1, TClonesArray* cl2, Int_t offset) { Int_t nEntries = cl1->GetEntriesFast(); cout << "-I- CbmSts: " << nEntries << " entries to add." << endl; TClonesArray& clref = *cl2; CbmStsPoint* oldpoint = NULL; for (Int_t i=0; iAt(i); Int_t index = oldpoint->GetTrackID() + offset; oldpoint->SetTrackID(index); new (clref[fPosIndex]) CbmStsPoint(*oldpoint); fPosIndex++; } cout << " -I- CbmSts: " << cl2->GetEntriesFast() << " merged entries." << endl; } // ---------------------------------------------------------------------------- // ----- ConstructGeometry -------------------------------------------------- void CbmSts::ConstructGeometry() { TString fileName = GetGeometryFileName(); if ( fileName.EndsWith(".root") ) { gLogger->Info(MESSAGE_ORIGIN, "Constructing STS geometry from ROOT file %s", fileName.Data()); ConstructRootGeometry(); } else if ( fileName.EndsWith(".geo") ) { gLogger->Info(MESSAGE_ORIGIN, "Constructing STS geometry from ASCII file %s", fileName.Data()); ConstructAsciiGeometry(); } else gLogger->Fatal(MESSAGE_ORIGIN, "Geometry format of STS file %S not supported.", fileName.Data()); } // ---------------------------------------------------------------------------- // ----- ConstructAsciiGeometry ------------------------------------------- void CbmSts::ConstructAsciiGeometry() { FairGeoLoader* geoLoad = FairGeoLoader::Instance(); FairGeoInterface* geoFace = geoLoad->getGeoInterface(); CbmGeoSts* stsGeo = new CbmGeoSts(); stsGeo->setGeomFile(GetGeometryFileName()); geoFace->addGeoModule(stsGeo); Bool_t rc = geoFace->readSet(stsGeo); if (rc) stsGeo->create(geoLoad->getGeoBuilder()); TList* volList = stsGeo->getListOfVolumes(); // store geo parameter FairRun *fRun = FairRun::Instance(); FairRuntimeDb *rtdb= FairRun::Instance()->GetRuntimeDb(); CbmGeoStsPar* par=(CbmGeoStsPar*)(rtdb->getContainer("CbmGeoStsPar")); TObjArray *fSensNodes = par->GetGeoSensitiveNodes(); TObjArray *fPassNodes = par->GetGeoPassiveNodes(); TListIter iter(volList); FairGeoNode* node = NULL; FairGeoVolume *aVol=NULL; while( (node = (FairGeoNode*)iter.Next()) ) { aVol = dynamic_cast ( node ); if ( node->isSensitive() ) { fSensNodes->AddLast( aVol ); }else{ fPassNodes->AddLast( aVol ); } } par->setChanged(); par->setInputVersion(fRun->GetRunId(),1); ProcessNodes( volList ); } // ---------------------------------------------------------------------------- // ----- CheckIfSensitive ------------------------------------------------- Bool_t CbmSts::CheckIfSensitive(std::string name) { TString volName = name; if ( volName.Contains("Sensor") ) return kTRUE; return kFALSE; } // ---------------------------------------------------------------------------- // ----- Private method AddHit -------------------------------------------- CbmStsPoint* CbmSts::AddHit(Int_t trackID, Int_t detID, TVector3 posIn, TVector3 posOut, TVector3 momIn, TVector3 momOut, Double_t time, Double_t length, Double_t eLoss) { TClonesArray& clref = *fStsCollection; Int_t size = clref.GetEntriesFast(); if (fVerboseLevel>1) cout << "-I- CbmSts: Adding Point at (" << posIn.X() << ", " << posIn.Y() << ", " << posIn.Z() << ") cm, detector " << detID << ", track " << trackID << ", energy loss " << eLoss*1e06 << " keV" << endl; return new(clref[size]) CbmStsPoint(trackID, detID, posIn, posOut, momIn, momOut, time, length, eLoss); } // ---------------------------------------------------------------------------- ClassImp(CbmSts)