#include "FairTutorialDet4.h" #include "FairDetectorList.h" // for DetectorId::kTutDet #include "FairGeoInterface.h" // for FairGeoInterface #include "FairGeoLoader.h" // for FairGeoLoader #include "FairGeoNode.h" // for FairGeoNode #include "FairGeoVolume.h" // for FairGeoVolume #include "FairLogger.h" // for FairLogger, etc #include "FairRootManager.h" // for FairRootManager #include "FairRun.h" // for FairRun #include "FairRuntimeDb.h" // for FairRuntimeDb #include "FairStack.h" // for FairStack #include "FairTutorialDet4Geo.h" // for FairTutorialDet4Geo #include "FairTutorialDet4GeoHandler.h" // for FairTutorialDet4GeoHandler #include "FairTutorialDet4GeoPar.h" // for FairTutorialDet4GeoPar #include "FairTutorialDet4MisalignPar.h" #include "FairTutorialDet4Point.h" // for FairTutorialDet4Point #include "Riosfwd.h" // for ostream #include "TClonesArray.h" // for TClonesArray #include "TGeoManager.h" // for TGeoManager, gGeoManager #include "TGeoMatrix.h" // for TGeoHMatrix, TGeoCombiTrans, etc #include "TGeoNode.h" // for TGeoNode #include "TGeoPhysicalNode.h" // for TGeoPhysicalNode, etc #include "TList.h" // for TListIter, TList (ptr only) #include "TObjArray.h" // for TObjArray #include "TString.h" // for TString, operator<<, Form #include "TVirtualMC.h" // for TVirtualMC, gMC #include "TVirtualMCStack.h" // for TVirtualMCStack #include // for NULL, printf #include // for operator<<, cout, endl, etc using std::cout; using std::endl; FairTutorialDet4::FairTutorialDet4() : FairDetector("TutorialDet", kTRUE, kTutDet), fTrackID(-1), fVolumeID(-1), fPos(), fMom(), fTime(-1.), fLength(-1.), fELoss(-1), fFairTutorialDet4PointCollection(new TClonesArray("FairTutorialDet4Point")), fGeoHandler(new FairTutorialDet4GeoHandler()), fMisalignPar(NULL), fNrOfDetectors(-1), fShiftX(), fShiftY(), fShiftZ(), fRotX(), fRotY(), fRotZ(), fModifyGeometry(kFALSE) { } FairTutorialDet4::FairTutorialDet4(const char* name, Bool_t active) : FairDetector(name, active, kTutDet), fTrackID(-1), fVolumeID(-1), fPos(), fMom(), fTime(-1.), fLength(-1.), fELoss(-1), fFairTutorialDet4PointCollection(new TClonesArray("FairTutorialDet4Point")), fGeoHandler(new FairTutorialDet4GeoHandler()), fMisalignPar(NULL), fNrOfDetectors(-1), fShiftX(), fShiftY(), fShiftZ(), fRotX(), fRotY(), fRotZ(), fModifyGeometry(kFALSE) { } FairTutorialDet4::~FairTutorialDet4() { LOG(DEBUG4)<<"Entering Destructor of FairTutorialDet4"<Delete(); delete fFairTutorialDet4PointCollection; } LOG(DEBUG4)<<"Leaving Destructor of FairTutorialDet4"<GetRuntimeDb(); LOG_IF(FATAL, !rtdb) << "No runtime database"<getContainer("FairTutorialDet4MissallignPar")); } void FairTutorialDet4::Initialize() { FairDetector::Initialize(); FairRuntimeDb* rtdb= FairRun::Instance()->GetRuntimeDb(); FairTutorialDet4GeoPar* par=(FairTutorialDet4GeoPar*)(rtdb->getContainer("FairTutorialDet4GeoPar")); Bool_t isSimulation = kTRUE; fGeoHandler->Init(isSimulation); } void FairTutorialDet4::InitParContainers() { LOG(INFO)<< "Initialize tutdet missallign parameters"<GetNrOfDetectors(); fShiftX=fMisalignPar->GetShiftX(); fShiftY=fMisalignPar->GetShiftY(); fShiftZ=fMisalignPar->GetShiftZ(); fRotX=fMisalignPar->GetRotX(); fRotY=fMisalignPar->GetRotY(); fRotZ=fMisalignPar->GetRotZ(); } Bool_t FairTutorialDet4::ProcessHits(FairVolume* vol) { /** This method is called from the MC stepping */ //Set parameters at entrance of volume. Reset ELoss. if ( gMC->IsTrackEntering() ) { fELoss = 0.; fTime = gMC->TrackTime() * 1.0e09; fLength = gMC->TrackLength(); gMC->TrackPosition(fPos); gMC->TrackMomentum(fMom); } // Sum energy loss for all steps in the active volume fELoss += gMC->Edep(); // Create FairTutorialDet4Point at exit of active volume if ( gMC->IsTrackExiting() || gMC->IsTrackStop() || gMC->IsTrackDisappeared() ) { fTrackID = gMC->GetStack()->GetCurrentTrackNumber(); // fVolumeID = vol->getMCid(); fVolumeID = fGeoHandler->GetUniqueDetectorId(); if (fELoss == 0. ) { return kFALSE; } // Save positions in local coordinate system, so transform the // global coordinates into local ones. Double_t master[3] = {fPos.X(), fPos.Y(), fPos.Z()}; Double_t local[3]; gMC->Gmtod(master, local, 1); // AddHit(fTrackID, fVolumeID, TVector3(fPos.X(), fPos.Y(), fPos.Z()), AddHit(fTrackID, fVolumeID, TVector3(local[0], local[1], local[2]), TVector3(fMom.Px(), fMom.Py(), fMom.Pz()), fTime, fLength, fELoss); // Increment number of tutorial det points in TParticle FairStack* stack = (FairStack*) gMC->GetStack(); stack->AddPoint(kTutDet); } return kTRUE; } void FairTutorialDet4::EndOfEvent() { fFairTutorialDet4PointCollection->Clear(); } void FairTutorialDet4::Register() { /** This will create a branch in the output tree called FairTutorialDet4Point, setting the last parameter to kFALSE means: this collection will not be written to the file, it will exist only during the simulation. */ FairRootManager::Instance()->Register("TutorialDetPoint", "TutorialDet", fFairTutorialDet4PointCollection, kTRUE); } TClonesArray* FairTutorialDet4::GetCollection(Int_t iColl) const { if (iColl == 0) { return fFairTutorialDet4PointCollection; } else { return NULL; } } void FairTutorialDet4::Reset() { fFairTutorialDet4PointCollection->Clear(); } void FairTutorialDet4::ConstructGeometry() { TString fileName=GetGeometryFileName(); if (fileName.EndsWith(".geo")) { LOG(INFO)<<"Constructing Tutorial4 geometry from ASCII file "<getGeoInterface(); FairTutorialDet4Geo* Geo = new FairTutorialDet4Geo(); LOG(DEBUG)<<"Read Geo file "<setGeomFile(GetGeometryFileName()); geoFace->addGeoModule(Geo); Bool_t rc = geoFace->readSet(Geo); if (rc) { Geo->create(geoLoad->getGeoBuilder()); } TList* volList = Geo->getListOfVolumes(); // store geo parameter FairRun* fRun = FairRun::Instance(); FairRuntimeDb* rtdb= FairRun::Instance()->GetRuntimeDb(); FairTutorialDet4GeoPar* par=(FairTutorialDet4GeoPar*)(rtdb->getContainer("FairTutorialDet4GeoPar")); 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 ); } void FairTutorialDet4::ModifyGeometry() { // When saving the top volume of the geometry to a file the information // about the TGeoPNEntries is not saved. So the misalign procedure // which uses the TGeoPNEntry information can not be used. // The code is here to demonstrate how to use this feature. if (fModifyGeometry) { LOG(INFO)<<"Misalign the geometry for the tutorial detector."<GetAlignableEntry(detStr.Data()); TGeoPhysicalNode* node = entry->GetPhysicalNode(); // LOG(INFO)<<"Nr of alignable objects: "<GetNAlignable()<cd(volPath); TGeoHMatrix* g3 = gGeoManager->GetCurrentMatrix(); // g3->Print(); TGeoNode* n3 = gGeoManager->GetCurrentNode(); TGeoMatrix* l3 = n3->GetMatrix(); // l3->Print(); //we have to express the displacements as regards the old local RS (non misaligned BTOF) Double_t dx = fShiftX[iDet]; Double_t dy = fShiftY[iDet]; Double_t dz = fShiftZ[iDet]; Double_t dphi = fRotX[iDet]; Double_t dtheta = fRotY[iDet]; Double_t dpsi = fRotZ[iDet]; TGeoRotation* rrot = new TGeoRotation("rot",dphi,dtheta,dpsi); TGeoCombiTrans localdelta = *(new TGeoCombiTrans(dx,dy,dz, rrot)); // localdelta.Print(); TGeoHMatrix nlocal = *l3 * localdelta; TGeoHMatrix* nl3 = new TGeoHMatrix(nlocal); // new matrix, representing real position (from new local mis RS to the global one) TGeoPhysicalNode* pn3 = gGeoManager->MakePhysicalNode(volPath); pn3->Align(nl3); TGeoHMatrix* ng3 = pn3->GetMatrix(); //"real" global matrix, what survey sees LOG(DEBUG)<<"************* The Misaligned Matrix in GRS **************"<Print(); } LOG(DEBUG)<<"Align in total "<GetAlignableEntry(detStr); if(entry) { node = gGeoManager->MakeAlignablePN(entry); } TGeoMatrix* l3 = node->GetMatrix(); //we have to express the displacements as regards the old local RS (non misaligned BTOF) Double_t dx = fShiftX[iDet]; Double_t dy = fShiftY[iDet]; Double_t dz = fShiftZ[iDet]; Double_t dphi = fRotX[iDet]; Double_t dtheta = fRotY[iDet]; Double_t dpsi = fRotZ[iDet]; LOG(INFO)<<"Psi: "<Align(nl3); TGeoHMatrix* ng3 = node->GetMatrix(); //"real" global matrix, what survey sees printf("\n\n************* The Misaligned Matrix in GRS **************\n"); ng3->Print(); } //do something LOG(INFO)<<"Total Nr of detectors: "<