Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-08 07:53:11

0001 //
0002 // ********************************************************************
0003 // * License and Disclaimer                                           *
0004 // *                                                                  *
0005 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
0006 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
0007 // * conditions of the Geant4 Software License,  included in the file *
0008 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
0009 // * include a list of copyright holders.                             *
0010 // *                                                                  *
0011 // * Neither the authors of this software system, nor their employing *
0012 // * institutes,nor the agencies providing financial support for this *
0013 // * work  make  any representation or  warranty, express or implied, *
0014 // * regarding  this  software system or assume any liability for its *
0015 // * use.  Please see the license in the file  LICENSE  and URL above *
0016 // * for the full disclaimer and the limitation of liability.         *
0017 // *                                                                  *
0018 // * This  code  implementation is the result of  the  scientific and *
0019 // * technical work of the GEANT4 collaboration.                      *
0020 // * By using,  copying,  modifying or  distributing the software (or *
0021 // * any work based  on the software)  you  agree  to acknowledge its *
0022 // * use  in  resulting  scientific  publications,  and indicate your *
0023 // * acceptance of all terms of the Geant4 Software license.          *
0024 // ********************************************************************
0025 //
0026 /// \file XAluminumElectrodeSensitivity.cc
0027 /// \brief Implementation of the XAluminumElectrodeSensitivity class
0028 
0029 #include "XAluminumElectrodeSensitivity.hh"
0030 
0031 #include "XAluminumElectrodeHit.hh"
0032 
0033 #include "G4AutoLock.hh"
0034 #include "G4HCofThisEvent.hh"
0035 #include "G4Navigator.hh"
0036 #include "G4SDManager.hh"
0037 #include "G4Step.hh"
0038 #include "G4SystemOfUnits.hh"
0039 #include "G4Threading.hh"
0040 #include "G4TouchableHistory.hh"
0041 #include "G4Track.hh"
0042 
0043 #include <fstream>
0044 
0045 std::fstream* XAluminumElectrodeSensitivity::fWriter = 0;
0046 std::fstream* XAluminumElectrodeSensitivity::fWriter2 = 0;
0047 
0048 G4Mutex theMutex = G4MUTEX_INITIALIZER;  // Just need one
0049 
0050 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
0051 
0052 XAluminumElectrodeSensitivity::XAluminumElectrodeSensitivity(const G4String& name)
0053   : G4VSensitiveDetector(name)
0054 {
0055   collectionName.insert("XAluminumElectrodeHit");
0056   fHCID = -1;
0057 
0058   G4AutoLock lockIt(&theMutex);  // Only one thread opens files!
0059   fWriter = new std::fstream("caustic.ssv", std::fstream::out | std::fstream::ate);
0060   if (!fWriter->is_open()) {
0061     G4cerr << "XAluminumElectrodeSensitivity::Constructor:"
0062            << "\n\tFailed to open caustic.ssv for appending data."
0063            << "\n\tCreating caustic.ssv" << G4endl;
0064     fWriter->open("caustic.ssv");
0065   }
0066 
0067   fWriter2 = new std::fstream("timing.ssv", std::fstream::out | std::fstream::ate);
0068   if (!fWriter2->is_open()) {
0069     G4cerr << "XAluminumElectrodeSensitivity::Constructor: "
0070            << "\n\tFailed to open timing.ssv for appending data."
0071            << "\n\tCreating timing.ssv." << G4endl;
0072     fWriter2->open("timing.ssv");
0073   }
0074 
0075   if (!(fWriter->is_open() && fWriter2->is_open())) {
0076     G4cerr << "XAluminumElectrodeSensitivity::Constructor: "
0077            << "\nERROR: COULD NOT CREATE OUTPUT FILES FOR WRITING" << G4endl;
0078   }
0079 }
0080 
0081 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
0082 
0083 XAluminumElectrodeSensitivity::~XAluminumElectrodeSensitivity()
0084 {
0085   G4AutoLock lockIt(&theMutex);  // Only one thread deletes!
0086 
0087   if (fWriter) {
0088     fWriter->close();
0089     delete fWriter;
0090     fWriter = 0;
0091   }
0092 
0093   if (fWriter2) {
0094     fWriter2->close();
0095     delete fWriter2;
0096     fWriter2 = 0;
0097   }
0098 }
0099 
0100 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
0101 
0102 XAluminumElectrodeHitsCollection* XAluminumElectrodeSensitivity::GetHitsCollection()
0103 {
0104   return fHitsCollection;
0105 }
0106 
0107 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
0108 
0109 void XAluminumElectrodeSensitivity::Initialize(G4HCofThisEvent* HCE)
0110 {
0111   fHitsCollection = new XAluminumElectrodeHitsCollection(SensitiveDetectorName, collectionName[0]);
0112   if (fHCID < 0) {
0113     fHCID = G4SDManager::GetSDMpointer()->GetCollectionID(fHitsCollection);
0114   }
0115   HCE->AddHitsCollection(fHCID, fHitsCollection);
0116 }
0117 
0118 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
0119 
0120 G4bool XAluminumElectrodeSensitivity::ProcessHits(G4Step* aStep, G4TouchableHistory* /*ROhist*/)
0121 {
0122   // if(aStep->GetTrack()->GetDefinition()!=Phonon::PhononDefinition()) return true;
0123   G4double edp = aStep->GetNonIonizingEnergyDeposit();
0124   if (edp == 0.) return true;
0125 
0126   G4StepPoint* preStepPoint = aStep->GetPreStepPoint();
0127   G4StepPoint* postStepPoint = aStep->GetPostStepPoint();
0128   G4TouchableHistory* theTouchable = (G4TouchableHistory*)(preStepPoint->GetTouchable());
0129   G4ThreeVector fWorldPos = postStepPoint->GetPosition();
0130   G4ThreeVector fLocalPos = theTouchable->GetHistory()->GetTopTransform().TransformPoint(fWorldPos);
0131 
0132   XAluminumElectrodeHit* aHit = new XAluminumElectrodeHit();
0133   aHit->fTime = postStepPoint->GetGlobalTime();
0134   aHit->fEdep = edp;
0135   aHit->fWorldPos = fWorldPos;
0136   aHit->fLocalPos = fLocalPos;
0137 
0138   fHitsCollection->insert(aHit);
0139 
0140   return true;
0141 }
0142 
0143 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
0144 
0145 void XAluminumElectrodeSensitivity::EndOfEvent(G4HCofThisEvent* /*HCE*/)
0146 {
0147   if (!fHitsCollection || fHitsCollection->GetSize() == 0) return;
0148 
0149   for (size_t i = 0; i < fHitsCollection->GetSize(); i++) {
0150     WriteHitInfo(dynamic_cast<XAluminumElectrodeHit*>(fHitsCollection->GetHit(i)));
0151   }
0152 }
0153 
0154 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
0155 
0156 void XAluminumElectrodeSensitivity::WriteHitInfo(const XAluminumElectrodeHit* aHit)
0157 {
0158   if (!aHit) return;
0159 
0160   G4AutoLock lockIt(&theMutex);  // Only one event can write at a time
0161 
0162   *fWriter << aHit->fWorldPos.getX() / mm << "," << aHit->fWorldPos.getY() / mm << ","
0163            << aHit->fWorldPos.getZ() / mm << "\n";
0164 
0165   *fWriter2 << aHit->fTime / ns << " " << aHit->fEdep / eV << "\n";
0166 }