MollerAnalysis.cc

Go to the documentation of this file.
00001 
00002 #include "MollerAnalysis.hh"
00003 #include "MollerDetectorHit.hh"
00004 #include "RootAnalysis.hh"
00005 
00006 #include "G4SystemOfUnits.hh"
00007 #include "G4ios.hh"
00008 
00009 #include "G4RunManager.hh"
00010 #include "G4VPhysicalVolume.hh"
00011 #include "G4Event.hh"
00012 #include "G4Run.hh"
00013 #include "G4Track.hh"
00014 #include "G4Material.hh"
00015 #include "G4ClassificationOfNewTrack.hh"
00016 #include "G4TrackStatus.hh"
00017 #include "G4Step.hh"
00018 #include "G4Types.hh"
00019 
00020 #include "G4HCofThisEvent.hh"
00021 #include "G4VHitsCollection.hh"
00022 #include "G4TrajectoryContainer.hh"
00023 #include "G4Trajectory.hh"
00024 #include "G4VVisManager.hh"
00025 #include "G4SDManager.hh"
00026 #include "G4UImanager.hh"
00027 #include "G4UIcommand.hh"
00028 
00029 #include "MollerAnalysisMessenger.hh"
00030 #include "MollerDetectorConstruction.hh"
00031 
00032 #include <time.h>
00033 #include <iostream>
00034 
00035 #include "TROOT.h"
00036 #include "TNtuple.h"
00037 #include "TApplication.h"
00038 #include "TSystem.h"
00039 
00040 using namespace std;
00041 
00042 MollerAnalysis::MollerAnalysis()
00043 {
00044   if (gSystem) gSystem->ProcessEvents();
00045   
00046   ev_num=0;
00047   partType=-1;
00048   splitProcess=-1;
00049   creatorProcess=-1;
00050   index=-1;
00051 
00052   //  for (int i=0; i<NUM_DETS; i++) {
00053   //    hitsCollID[i] = -1;
00054   //  }
00055 
00056   verboseLevel=0;
00057 
00058   kineE0=0;  kineE1=0;  kineE2=0;
00059 
00060   px0=0;  py0=0;  pz0=0;
00061   px1=0;  py1=0;  pz1=0;
00062   px2=0;  py2=0;  pz2=0;
00063 
00064   theta0=0;  theta1=0;  theta2=0;
00065 
00066   x0=0;  y0=0;  z0=0;
00067 
00068   analysisMessenger = new MollerAnalysisMessenger(this);
00069   //G4cout << "Root file name is "<<rootfileName << G4endl;
00070 }
00071 MollerAnalysis::MollerAnalysis(MollerDetectorConstruction* detector)
00072 :myDetector(detector)
00073 {
00074   if (gSystem) gSystem->ProcessEvents();
00075 
00076   G4int NUM_COLS = myDetector->getNumColl();
00077 
00078   NUM_DETS = NUM_COLS;
00079   G4cout<<G4endl<<G4endl<<"NUM_COLS = "<<NUM_COLS<<G4endl<<G4endl;
00080 
00081   ev_num=0;
00082   partType=-1;
00083   splitProcess=-1;
00084   creatorProcess=-1;
00085   index=-1;
00086 
00087   //  for (int i=0; i<NUM_DETS; i++) {
00088   //    hitsCollID[i] = -1;
00089   //  }
00090 
00091   verboseLevel=0;
00092 
00093   kineE0=0;  kineE1=0;  kineE2=0;
00094 
00095   px0=0;  py0=0;  pz0=0;
00096   px1=0;  py1=0;  pz1=0;
00097   px2=0;  py2=0;  pz2=0;
00098 
00099   theta0=0;  theta1=0;  theta2=0;
00100 
00101   x0=0;  y0=0;  z0=0;
00102 
00103   analysisMessenger = new MollerAnalysisMessenger(this);
00104   //G4cout << "Root file name is "<<rootfileName << G4endl;
00105 }
00106 
00107 MollerAnalysis::MollerAnalysis(const MollerAnalysis &right)
00108   : RootAnalysis()
00109 {
00110   if (gSystem) gSystem->ProcessEvents();
00111   
00112   ev_num=right.ev_num;
00113   partType=right.partType;
00114   splitProcess=right.splitProcess;
00115   creatorProcess=right.creatorProcess;
00116   index=right.index;
00117   
00118 
00119   //  for (int i=0; i<NUM_DETS; i++) {
00120   //    hitsCollID[i]=right.hitsCollID[i];
00121   //  }
00122   
00123   verboseLevel=right.verboseLevel;
00124 
00125   kineE0=right.kineE0;  
00126   kineE1=right.kineE1;  
00127   kineE2=right.kineE2;
00128  
00129   px0=right.px0;  
00130   py0=right.py0;  
00131   pz0=right.pz0;
00132   
00133   px1=right.px1;  
00134   py1=right.py1;  
00135   pz1=right.pz1;
00136 
00137   px2=right.px2;  
00138   py2=right.py2;  
00139   pz2=right.pz2;
00140 
00141   theta0=right.theta0;  
00142   theta1=right.theta1;  
00143   theta2=right.theta2;
00144 
00145   x0=right.x0;  
00146   y0=right.y0;  
00147   z0=right.z0;
00148 
00149 }
00150 
00151 const MollerAnalysis& MollerAnalysis::operator=(const MollerAnalysis &right)
00152 {
00153   if (gSystem) gSystem->ProcessEvents();
00154   
00155   ev_num=right.ev_num;
00156   partType=right.partType;
00157   splitProcess=right.splitProcess;
00158   creatorProcess=right.creatorProcess;
00159   index=right.index;
00160   
00161   //  for (int i=0; i<NUM_DETS; i++) {
00162   //    hitsCollID[i]=right.hitsCollID[i];
00163   //  }
00164   
00165   verboseLevel=right.verboseLevel;
00166 
00167   kineE0=right.kineE0;  
00168   kineE1=right.kineE1;  
00169   kineE2=right.kineE2;
00170   
00171   px0=right.px0;  
00172   py0=right.py0;  
00173   pz0=right.pz0;
00174   
00175   px1=right.px1;  
00176   py1=right.py1;  
00177   pz1=right.pz1;
00178 
00179   px2=right.px2;  
00180   py2=right.py2;  
00181   pz2=right.pz2;
00182 
00183   theta0=right.theta0;  
00184   theta1=right.theta1;  
00185   theta2=right.theta2;
00186 
00187   x0=right.x0;  
00188   y0=right.y0;  
00189   z0=right.z0;
00190 
00191   return *this;
00192 
00193 }
00194 
00195 MollerAnalysis::~MollerAnalysis()
00196 {
00197   delete analysisMessenger;
00198 }
00199 void MollerAnalysis::SetRootFileName(const G4String& nam) 
00200 {
00201   rootfileName = nam;
00202 }
00203 
00204 
00205 void MollerAnalysis::BeginOfRunAction(const G4Run* /*aRun*/)
00206 {
00207   if (gSystem) gSystem->ProcessEvents();
00208 
00209   G4cout << "Run Action has been entered\n" << G4endl;
00210 
00211   G4cout << "Root file name is "<<rootfileName << G4endl;
00212 
00213 
00214   // This works:
00215   //G4String filenamechar=("~/scratch/ROOTfiles/"+rootfileName+".root");
00216   G4String filenamechar=(rootfileName+".root");
00217 
00218   // This works:
00219   /*
00220   G4String filenamechar;
00221   filenamechar="~/scratch/ROOTfiles/";
00222   filenamechar += rootfileName;
00223   filenamechar += ".root";
00224   G4cout<<filenamechar<<G4endl;
00225   */
00226 
00227   //char filenamechar[200];
00228   /*
00229   time_t rawtime;
00230   tm * ptm;
00231   time ( &rawtime );
00232   ptm = gmtime ( &rawtime );
00233   snprintf(filenamechar,200,"~/scratch/ROOTfiles/coll_test0_%02i%02i%02i_%02i%02i%02i.root",
00234        ptm->tm_year-100,ptm->tm_mon+1,ptm->tm_mday,(ptm->tm_hour+19)%24,ptm->tm_min,ptm->tm_sec);
00235   //  snprintf(filenamechar,200,"~/scratch/ROOTfiles/moller_%02i%02i%02i_%02i%02i%02i.root",
00236   //     ptm->tm_year-100,ptm->tm_mon+1,ptm->tm_mday,(ptm->tm_hour+19)%24,ptm->tm_min,ptm->tm_sec);
00237   printf(filenamechar);
00238   */
00239   //snprintf(filenamechar,200,"~/scratch/ROOTfiles/%s.root",rootfileName);
00240 
00241   hfile = new TFile(filenamechar,"RECREATE","Root/G4 analysis");
00242   //hfile = new TFile(rootfileName,"RECREATE","Root/G4 analysis");
00243 
00244 
00245   string myntuple_string;
00246   myntuple_string="ion:x:y:z:x0:y0:z0:kineE:px:py:pz:kineE0:px0:py0:pz0:kineE1:px1:py1:pz1:kineE2:px2:py2:pz2:type:volume:theta0:theta1:theta2:ev_num:process:event:creator:hit:kineE_org:theta_org:track:diffXS:totXS:rate";
00247 
00248   char *string_ntup = new char[myntuple_string.length()+1];
00249   strncpy (string_ntup,myntuple_string.c_str(),myntuple_string.length()+1);
00250 
00251   ntup = new TNtuple("geant","G4 Results",(char *)string_ntup);
00252 
00253   G4cout << "Run Action has been completed\n" << G4endl;
00254 }
00255 
00256 void MollerAnalysis::BeginOfEventAction(const G4Event */*anEvent*/)
00257 {
00258   ev_num++;
00259   if (gSystem) gSystem->ProcessEvents();
00260 
00261   G4SDManager * SDman = G4SDManager::GetSDMpointer();
00262 
00263   /*  if(hitsCollID[0]<0 || hitsCollID[1]<0 || 
00264      hitsCollID[2]<0 || hitsCollID[3]<0 || 
00265      hitsCollID[4]<0 || hitsCollID[5]<0 || 
00266      hitsCollID[6]<0 || hitsCollID[7]<0 || 
00267      hitsCollID[8]<0 || hitsCollID[9]<0 || 
00268      hitsCollID[10]<0 || hitsCollID[11]<0 || 
00269      hitsCollID[12]<0 || hitsCollID[13]<0 || 
00270      hitsCollID[14]<0 || hitsCollID[15]<0 || hitsCollID[16]<0
00271      )
00272   */ 
00273 
00274   if((int) hitsCollID.size()==0)   
00275     {
00276       G4cout<<G4endl<<G4endl<<"Setting hitsCollID"<<G4endl<<G4endl;
00277      
00278       for (int i=0; i<NUM_DETS; i++) {
00279   G4String colNam = "detector";
00280   colNam+=G4UIcommand::ConvertToString(i+1);
00281   colNam+="/hitsColl";
00282   
00283   G4cout << colNam << G4endl;
00284   
00285   hitsCollID.push_back (SDman->GetCollectionID(colNam));
00286       }
00287 
00288       G4cout<<G4endl<<"Finished setting hitsCollID"<<G4endl<<G4endl;
00289       
00290     }
00291 
00292 
00293   gRootAnalysis->SetProcess(1);
00294   /* These are you lines Dustin! Only these three lines. */
00295   //gRootAnalysis->SetMomentum0();
00296   //gRootAnalysis->SetMomentum1();
00297   //gRootAnalysis->SetMomentum2();
00298   /*                 ****************                    */
00299 }
00300 
00301 
00302 void MollerAnalysis::UserSteppingAction(const G4Step *aStep)
00303 {
00304   /** Get data that will go into ROOT tree **/
00305 
00306   G4Track* fTrack = aStep->GetTrack();
00307 
00308 
00309   if (fTrack->GetTrackStatus()!=fAlive) { 
00310  //   printf("track is not 'alive.'\n");
00311     return; 
00312   }
00313 
00314   G4int tracking = 1;
00315 
00316   G4StepPoint* thePrePoint = aStep->GetPreStepPoint();
00317   G4StepPoint* thePostPoint = aStep->GetPostStepPoint();
00318   G4VPhysicalVolume* thePrePV = thePrePoint->GetPhysicalVolume();
00319   G4Material* material = fTrack->GetMaterial();
00320 
00321   const G4VProcess* originProcess = fTrack->GetCreatorProcess();
00322   const G4VProcess* stepProcess = thePostPoint->GetProcessDefinedStep();
00323 
00324   G4String process;
00325   if (originProcess!=0) process = originProcess->GetProcessName();
00326   G4String process1;
00327   if (stepProcess!=0) process1 = stepProcess->GetProcessName();
00328 
00329 
00330   G4String volume = fTrack->GetVolume()->GetName();
00331   G4String particle = fTrack->GetDefinition()->GetParticleName();
00332 
00333 
00334   if (process=="eIoni") creatorProcess=0;
00335   else if (process=="conv") creatorProcess=1;
00336   else if (process=="Decay") creatorProcess=2;
00337   else if (process=="annihil") creatorProcess=3;
00338   else if (process=="eBrem") creatorProcess=4;
00339   else if (process=="phot") creatorProcess=5;
00340   else if (process=="compt") creatorProcess=6;
00341   else if (process=="Transportation") creatorProcess=7;
00342   else if (process=="msc") creatorProcess=8;
00343   else creatorProcess=9;
00344 
00345   //G4bool ion = process=="eIoni";
00346   
00347   if ((particle == "e-") 
00348       && (fTrack->GetVertexKineticEnergy()==11*GeV) 
00349       && ((thePrePoint->GetKineticEnergy()-
00350      thePostPoint->GetKineticEnergy())>1.00*GeV) 
00351       && (process1=="eIoni"))
00352     { 
00353       if (kineE0==0 || kineE1==0) {
00354   kineE0 = thePrePoint->GetKineticEnergy();
00355   kineE1 = thePostPoint->GetKineticEnergy();
00356   px0 = thePrePoint->GetMomentumDirection().x();
00357   py0 = thePrePoint->GetMomentumDirection().y();
00358   pz0 = thePrePoint->GetMomentumDirection().z();
00359   px1 = thePostPoint->GetMomentumDirection().x();
00360   py1 = thePostPoint->GetMomentumDirection().y();
00361   pz1 = thePostPoint->GetMomentumDirection().z();
00362   theta0 = sqrt(px0*px0 + py0*py0);
00363   theta1 = sqrt(px1*px1 + py1*py1);
00364   x0 = thePostPoint->GetPosition().x();
00365   y0 = thePostPoint->GetPosition().y();
00366   z0 = thePostPoint->GetPosition().z();
00367       }
00368       
00369       if (process1=="eIoni") splitProcess=0;
00370       else if (process1=="conv") splitProcess=1;
00371       else if (process1=="Decay") splitProcess=2;
00372       else if (process1=="annihil") splitProcess=3;
00373       else if (process1=="eBrem") splitProcess=4;
00374       else if (process1=="photo") splitProcess=5;
00375       else if (process1=="compton") splitProcess=6;
00376       else if (process1=="Transportation") splitProcess=7;
00377       else splitProcess=8;
00378     }
00379   
00380   if (tracking) {
00381     
00382     G4ThreeVector worldPos = fTrack->GetPosition();
00383     G4ThreeVector worldMomentum = fTrack->GetMomentumDirection();
00384     G4ThreeVector vertexMomentum = fTrack->GetVertexMomentumDirection();
00385     
00386     G4double scat_ang = sqrt(vertexMomentum.x()*vertexMomentum.x() + 
00387            vertexMomentum.y()*vertexMomentum.y());
00388     
00389     /*    if (scat_ang<0.002 || scat_ang>0.020) { 
00390       return; 
00391       }*/
00392     
00393     G4double kineE = fTrack->GetKineticEnergy();
00394     G4double kineE0 = fTrack->GetVertexKineticEnergy();
00395     
00396     G4bool ion = (process=="eIoni");
00397     
00398     if (particle=="e-") partType=0;
00399     else if (particle=="e+") partType=1;
00400     else if (particle=="proton") partType=2;
00401     else if (particle=="antiproton") partType=3;
00402     else if (particle=="gamma") partType=4;
00403     else partType=5;
00404     /*   
00405     if (partType!=4) {
00406       
00407       fntup[0] = (Float_t)ion;
00408       fntup[1] = (Float_t)worldPos.x();
00409       fntup[2] = (Float_t)worldPos.y();
00410       fntup[3] = (Float_t)worldPos.z();
00411       fntup[4] = 0;
00412       fntup[5] = 0;
00413       fntup[6] = 0;
00414       fntup[7] = (Float_t)kineE;
00415       fntup[8] = (Float_t)worldMomentum.x();
00416       fntup[9] = (Float_t)worldMomentum.y();
00417       fntup[10] = (Float_t)worldMomentum.z();
00418       
00419       fntup[11] = (Float_t)kineE0;
00420       fntup[12] = (Float_t)px0;
00421       fntup[13] = (Float_t)py0;
00422       fntup[14] = (Float_t)pz0;
00423       fntup[15] = (Float_t)kineE1;
00424       fntup[16] = (Float_t)px1;
00425       fntup[17] = (Float_t)py1;
00426       fntup[18] = (Float_t)pz1;
00427       
00428       fntup[19] = (Float_t)fTrack->GetVertexKineticEnergy();
00429       fntup[20] = (Float_t)fTrack->GetVertexMomentumDirection().x();
00430       fntup[21] = (Float_t)fTrack->GetVertexMomentumDirection().y();
00431       fntup[22] = (Float_t)fTrack->GetVertexMomentumDirection().z();
00432       
00433       fntup[23] = (Float_t)partType;
00434       fntup[24] = (Float_t)atof(volume);
00435       fntup[25] = (Float_t)theta0;
00436       fntup[26] = (Float_t)theta1;
00437       fntup[27] = (Float_t)theta2;
00438       fntup[28] = (Float_t)ev_num;
00439       fntup[29] = (Float_t)ion;
00440       fntup[30] = (Float_t)ev_num;
00441       
00442       
00443       ntup->Fill(fntup);
00444     }
00445     */
00446   }
00447     
00448   /* KILL particle if it hits a detector */
00449   /* made of tungsten! */
00450   /*
00451   //  if(thePrePV->GetName()=="1"||thePrePV->GetName()=="2"||
00452      thePrePV->GetName()=="3"||thePrePV->GetName()=="4"||
00453      thePrePV->GetName()=="5"||thePrePV->GetName()=="6"|| 
00454      thePrePV->GetName()=="7"||thePrePV->GetName()=="8"|| 
00455      thePrePV->GetName()=="9"||thePrePV->GetName()=="10"|| 
00456      thePrePV->GetName()=="11"||thePrePV->GetName()=="12"|| 
00457      thePrePV->GetName()=="13"||thePrePV->GetName()=="14"|| 
00458      thePrePV->GetName()=="15"||thePrePV->GetName()=="16"||thePrePV->GetName()=="17"
00459      ){
00460   */
00461   //    G4cout<<"Material = "<<material->GetName();
00462   if((material->GetName()=="Tungsten")||(material->GetName()=="Pb")||(material->GetName()=="Copper")){ 
00463     if (kineE2 == 0 && process=="eIoni") {
00464       kineE2 = fTrack->GetVertexKineticEnergy();
00465       px2 = fTrack->GetVertexMomentumDirection().x();
00466       py2 = fTrack->GetVertexMomentumDirection().y();
00467       pz2 = fTrack->GetVertexMomentumDirection().z();
00468       theta2 = sqrt(px2*px2 + py2*py2);
00469       x0 = fTrack->GetVertexPosition().x();
00470       y0 = fTrack->GetVertexPosition().y();
00471       z0 = fTrack->GetVertexPosition().z();
00472       
00473     }
00474     //    G4cout<<" Stopping the track..."<<G4endl;
00475     //    fTrack->SetTrackStatus(fStopAndKill);
00476     fTrack->SetTrackStatus(fKillTrackAndSecondaries);
00477     return;
00478   }
00479   //  G4cout<<endl;
00480 }
00481 
00482 
00483 void MollerAnalysis::EndOfEventAction(const G4Event *anEvent)
00484 {
00485   
00486   if (gSystem) gSystem->ProcessEvents();
00487 
00488   G4HCofThisEvent * HCE = anEvent->GetHCofThisEvent();
00489   
00490   event = anEvent->GetEventID();
00491 
00492   std::vector<MollerDetectorHitsCollection*> THC;
00493 
00494  
00495   if(HCE) {
00496     for (int i=0; i<NUM_DETS; i++) {
00497       THC.push_back ((MollerDetectorHitsCollection*)(HCE->GetHC(hitsCollID[i])));
00498     }
00499   }
00500   G4int checkTrack1=0;
00501   G4int checkTrack2=0;
00502   //  G4cout<<"Checking for hits..."<<G4endl;
00503   for (int i=0; i<NUM_DETS; i++) {
00504     // somehow use: aHit->GetTrackID()==1 and aHit->GetTrackID()==2 with an indicator of first entry for that event/track?
00505     if(THC[i]){
00506       int n_hit = THC[i]->entries();
00507       /* // Just do first hit; rate ~65GHz - missing second electron?
00508   if (n_hit>0) {
00509   MollerDetectorHit* aHit = (*THC[i])[0];
00510   //Decide here what kind of data we want to keep
00511   if (aHit->GetType()==0) AddData(aHit);
00512   if (aHit->GetType()==4) AddData(aHit);
00513   }
00514   }
00515   }*/
00516       //  G4cout<<"Done checking for hits..."<<G4endl;
00517       checkTrack1=0; //need to reset for each detector...
00518       checkTrack2=0;
00519       if (n_hit>0) {
00520   
00521   for(int i1=0;i1<n_hit;i1++) {
00522     MollerDetectorHit* aHit = (*THC[i])[i1];
00523     //aHit->Print();
00524     //aHit->Draw();
00525     //    G4cout<<"Ev "<<event<<", det: "<<i<<", hit: "<<i1<<", track: "<<aHit->GetTrackID()<<", check: "<<checkTrack1<<", "<<checkTrack2<<", "<<G4endl;
00526     //Try to get only the first hit, but for both primary electrons...
00527     //Really need to define an ntuple variable for the n_hit somehow...
00528     //and/or do this checking in the analysis scripts...
00529 
00530     if ((aHit->GetTrackID()==1)&&(checkTrack1==0)) {
00531       if (aHit->GetType()==0) AddData(aHit);
00532       if (aHit->GetType()==4) AddData(aHit);
00533       checkTrack1++;
00534     } else if ((aHit->GetTrackID()==2)&&(checkTrack2==0)){
00535       if (aHit->GetType()==0) AddData(aHit);
00536       if (aHit->GetType()==4) AddData(aHit);      
00537       checkTrack2++;
00538     }
00539 
00540   }
00541 
00542       }
00543     }
00544   }
00545       
00546   gRootAnalysis->SetMomentum0(0,0,0,0);
00547   gRootAnalysis->SetMomentum1(0,0,0,0);
00548   gRootAnalysis->SetMomentum2(0,0,0,0);
00549   gRootAnalysis->SetProcess(0);
00550   
00551 }
00552 
00553 
00554 
00555 void MollerAnalysis::EndOfRunAction(const G4Run */*aRun*/)
00556 {
00557   if (gSystem) gSystem->ProcessEvents();
00558   
00559   hfile->Write();
00560   hfile->Close();
00561   
00562 }
00563 
00564 

Generated on 14 Apr 2013 for mollersim by  doxygen 1.6.1