CMS 3D CMS Logo

GflashG4Watcher.cc

Go to the documentation of this file.
00001 #include "G4Step.hh"
00002 #include "G4Electron.hh"
00003 #include "G4Positron.hh"
00004 #include "G4VProcess.hh"
00005 #include "G4SDManager.hh"
00006 #include "G4PrimaryParticle.hh"
00007 #include "G4PrimaryVertex.hh"
00008 
00009 #include "SimG4CMS/Calo/interface/CaloG4HitCollection.h"
00010 #include "SimG4Core/Notification/interface/BeginOfEvent.h"
00011 #include "SimG4Core/Notification/interface/EndOfEvent.h"
00012 
00013 #include "SimG4Core/GFlash/interface/GflashG4Watcher.h"
00014 #include "SimG4Core/GFlash/interface/GflashObjects.h"
00015 
00016 #include <TVector2.h>
00017 
00018 const double radLength = 8.9; // mm
00019 const double rMoliere = 21.9; // mm
00020 // constructors and destructor
00021 //
00022 GflashG4Watcher::GflashG4Watcher(const edm::ParameterSet& p) {
00023 
00024   edm::ParameterSet myP = p.getParameter<edm::ParameterSet>("GflashG4Watcher");
00025   histFileName_ = myP.getParameter<std::string>("histFileName");
00026   histFile_ = new TFile(histFileName_.c_str(),"RECREATE");
00027 
00028   TH1::AddDirectory(kTRUE);
00029 
00030   gflashObject_ = new GflashObject;
00031   watcherTree_ = new TTree("watcherTree","Watcher Tree Variable");
00032   watcherTree_->Branch("gflashObject","GflashObject",&gflashObject_,6400,99);
00033   watcherTree_->SetAutoSave();
00034 
00035   longitudinal_ = new TH1F("longitudinal","Logitudinal profile;X_{0};Energy",100,0.0,50.0);
00036   lateral_r_ = new TH1F("lateral_r","Lateral profile;r_{M};Number of hits",300,0.0,3.0);
00037   showerStartingPosition_ = new TH1F("showerStartingPosition","Shower starting position;r(cm);Number of hits",100,120.0,170.0);
00038   nHits_ = new TH1F("nHits","Number of hits;N_{hit};Events",30,4000.0,7000.0);
00039   hitEnergy_ = new TH1F("hitEnergy","Energy of hits;Energy (MeV);Number of hits",100,0.0,10.0);
00040   rzHits_ = new TH2F("rzHits","r vs. z of hits;z (X_{0});r_{M}",100,0.0,50.0,300,0.0,3.0);
00041   incEnergy_ = new TH1F("incEnergy","Incoming energy;energy (GeV);Events",100,0.0,100.0);
00042   outEnergy_ = new TH1F("outEnergy","Outgoing energy;energy (GeV);Events",100,0.0,100.0);
00043 
00044 }
00045 
00046 
00047 GflashG4Watcher::~GflashG4Watcher() {
00048   histFile_->cd();
00049   histFile_->Write();
00050   histFile_->Close();
00051 }
00052 
00053 
00054 void GflashG4Watcher::update(const BeginOfEvent* g4Event){
00055   inc_flag = false;
00056   inc_energy = 0;
00057   inc_direction *= 0;
00058   inc_position *= 0;
00059   gflashObject_->Init();
00060 }
00061 
00062 void GflashG4Watcher::update(const EndOfEvent* g4Event){
00063 
00064   if(!inc_flag) return;
00065 
00066   const G4Event* evt = (*g4Event)();
00067   double primP = evt->GetPrimaryVertex(0)->GetPrimary(0)->GetMomentum().mag();
00068   double primM = evt->GetPrimaryVertex(0)->GetPrimary(0)->GetMass();
00069   double primE = std::sqrt(primP*primP + primM+primM);
00070 
00071   incEnergy_->Fill(inc_energy/GeV);
00072 
00073   if(inc_energy < 0.95*primE) return;
00074 
00075 
00076   // Now fill GflashObject
00077 
00078   gflashObject_->energy = inc_energy;
00079   gflashObject_->direction.SetXYZ(inc_direction.x(),inc_direction.y(),inc_direction.z());
00080   gflashObject_->position.SetXYZ(inc_position.x(),inc_position.y(),inc_position.z());
00081   showerStartingPosition_->Fill(inc_position.rho()/cm);
00082 
00083 
00084   double outEnergy = 0.0;
00085 
00086   for(std::vector<GflashHit>::iterator it = gflashObject_->hits.begin();
00087       it != gflashObject_->hits.end(); it++){
00088     TVector3 diff = it->position - gflashObject_->position;
00089     double angle = diff.Angle(gflashObject_->direction);
00090     double diff_z = std::abs(diff.Mag()*std::cos(angle));
00091     double diff_r = std::abs(diff.Mag()*std::sin(angle));
00092 
00093     lateral_r_->Fill(diff_r/rMoliere,it->energy);
00094     rzHits_->Fill(diff_z/radLength,diff_r/rMoliere,it->energy);
00095     hitEnergy_->Fill(it->energy);
00096     longitudinal_->Fill(diff_z/radLength,it->energy);
00097 
00098     outEnergy += it->energy;
00099   }
00100 
00101   nHits_->Fill(gflashObject_->hits.size());
00102   outEnergy_->Fill(outEnergy/GeV);
00103 
00104   watcherTree_->Fill();
00105 
00106 }
00107 
00108 void GflashG4Watcher::update(const G4Step* aStep){
00109 
00110   if(aStep == NULL) return;
00111 
00112   if(inc_flag){
00113     if(aStep->GetTotalEnergyDeposit() > 1.0e-6){
00114       G4ThreeVector hitPosition = aStep->GetPreStepPoint()->GetPosition();
00115       GflashHit gHit;
00116       gHit.energy = aStep->GetTotalEnergyDeposit();
00117       gHit.position.SetXYZ(hitPosition.x(),hitPosition.y(),hitPosition.z());
00118       gflashObject_->hits.push_back(gHit);
00119     }
00120   }
00121   else {
00122     G4bool trigger = aStep->GetPreStepPoint()->GetKineticEnergy() > 1.0*GeV;
00123     trigger = trigger && (aStep->GetTrack()->GetDefinition() == G4Electron::ElectronDefinition() || 
00124                           aStep->GetTrack()->GetDefinition() == G4Positron::PositronDefinition());
00125 
00126     G4LogicalVolume* lv = aStep->GetPreStepPoint()->GetPhysicalVolume()->GetLogicalVolume();
00127     trigger = trigger && (lv->GetRegion()->GetName() == "GflashRegion");
00128 
00129     std::size_t pos1 = lv->GetName().find("EBRY");
00130     std::size_t pos2 = lv->GetName().find("EFRY");
00131     trigger = trigger && (pos1 != std::string::npos || pos2 != std::string::npos);
00132 
00133     if(trigger){
00134       inc_energy = aStep->GetPreStepPoint()->GetKineticEnergy();
00135       inc_direction = aStep->GetPreStepPoint()->GetMomentumDirection();
00136       inc_position = aStep->GetPreStepPoint()->GetPosition();
00137       inc_flag = true;
00138     }
00139   }
00140 
00141 }
00142 
00143 
00144 //define this as a plug-in
00145 #include "SimG4Core/Watcher/interface/SimWatcherFactory.h"
00146 #include "FWCore/PluginManager/interface/ModuleDef.h"
00147 
00148 DEFINE_SEAL_MODULE ();
00149 DEFINE_SIMWATCHER(GflashG4Watcher);
00150 

Generated on Tue Jun 9 17:47:04 2009 for CMSSW by  doxygen 1.5.4