CMS 3D CMS Logo

/data/git/CMSSW_5_3_11_patch5/src/SimG4CMS/Forward/src/CastorTestAnalysis.cc

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 //
00003 // Package:     Forward
00004 // Class  :     CastorTestAnalysis
00005 //
00006 // Implementation:
00007 //     <Notes on implementation>
00008 //
00009 // Original Author: P. Katsas
00010 //         Created: 02/2007 
00011 //
00012 #include "SimG4CMS/Calo/interface/CaloG4Hit.h"
00013 #include "SimG4CMS/Calo/interface/CaloG4HitCollection.h"
00014 #include "DataFormats/Math/interface/Point3D.h"
00015 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00016 
00017 #include "SimG4CMS/Forward/interface/CastorTestAnalysis.h"
00018 //#include "SimG4CMS/Forward/interface/CastorNumberingScheme.h"
00019 
00020 #include "TFile.h"
00021 #include <cmath>
00022 #include <iostream>
00023 #include <iomanip>
00024 
00025 #define debugLog
00026 
00027 enum ntcastors_elements {
00028   ntcastors_evt, ntcastors_trackid, ntcastors_charge, ntcastors_pdgcode, ntcastors_x, ntcastors_y, ntcastors_z, ntcastors_stepl, ntcastors_stepe, ntcastors_eta, ntcastors_phi, ntcastors_vpx, ntcastors_vpy, ntcastors_vpz
00029 };
00030 
00031 enum ntcastore_elements {
00032   ntcastore_evt, ntcastore_ihit, ntcastore_detector, ntcastore_sector, ntcastore_module, ntcastore_enem, ntcastore_enhad, ntcastore_hitenergy, ntcastore_x, ntcastore_y, ntcastore_z
00033 };
00034 
00035 CastorTestAnalysis::CastorTestAnalysis(const edm::ParameterSet &p) {
00036 
00037   edm::ParameterSet m_Anal = p.getParameter<edm::ParameterSet>("CastorTestAnalysis");
00038   verbosity                = m_Anal.getParameter<int>("Verbosity");
00039   doNTcastorstep           = m_Anal.getParameter<int>("StepNtupleFlag");
00040   doNTcastorevent          = m_Anal.getParameter<int>("EventNtupleFlag");
00041   stepNtFileName           = m_Anal.getParameter<std::string>("StepNtupleFileName");
00042   eventNtFileName          = m_Anal.getParameter<std::string>("EventNtupleFileName");
00043 
00044   if (verbosity > 0)
00045    std::cout<<std::endl;
00046    std::cout<<"============================================================================"<<std::endl;
00047    std::cout << "CastorTestAnalysis:: Initialized as observer"<< std::endl;
00048    if (doNTcastorstep  > 0){
00049      std::cout <<" Step Ntuple will be created"<< std::endl;
00050      std::cout <<" Step Ntuple file: "<<stepNtFileName<<std::endl;
00051    }
00052    if (doNTcastorevent > 0){
00053      std::cout <<" Event Ntuple will be created"<< std::endl;
00054      std::cout <<" Step Ntuple file: "<<stepNtFileName<<std::endl;
00055    }
00056    std::cout<<"============================================================================"<<std::endl;
00057    std::cout<<std::endl;
00058   
00059   if (doNTcastorstep  > 0)  
00060   castorstepntuple = new TNtuple("NTcastorstep","NTcastorstep","evt:trackid:charge:pdgcode:x:y:z:stepl:stepe:eta:phi:vpx:vpy:vpz");
00061   
00062   if (doNTcastorevent  > 0)
00063   castoreventntuple = new TNtuple("NTcastorevent","NTcastorevent","evt:ihit:detector:sector:module:enem:totalenergy:hitenergy:x:y:z");
00064 }
00065 
00066 CastorTestAnalysis::~CastorTestAnalysis() {
00067   //destructor of CastorTestAnalysis
00068     
00069     Finish();
00070   if (verbosity > 0) {
00071     std::cout << std::endl << "End of CastorTestAnalysis"
00072               << std::endl; 
00073   }
00074   
00075   std::cout<<"CastorTestAnalysis: End of process"<<std::endl;
00076   
00077 }
00078   
00079 //=================================================================== per EVENT
00080 void CastorTestAnalysis::update(const BeginOfJob * job) {
00081 
00082   std::cout << " Starting new job " << std::endl;
00083 }
00084 
00085 //==================================================================== per RUN
00086 void CastorTestAnalysis::update(const BeginOfRun * run) {
00087 
00088  std::cout << std::endl << "CastorTestAnalysis: Starting Run"<< std::endl; 
00089   if (doNTcastorstep) { 
00090     std::cout << "CastorTestAnalysis: output step root file created"<< std::endl;
00091     TString stepfilename = stepNtFileName;
00092     castorOutputStepFile = new TFile(stepfilename,"RECREATE");
00093 
00094   }
00095   
00096   if (doNTcastorevent) {
00097     std::cout << "CastorTestAnalysis: output event root file created"<< std::endl;
00098     TString stepfilename = eventNtFileName;
00099     castorOutputEventFile = new TFile(stepfilename,"RECREATE");
00100   }
00101 
00102   eventIndex = 0;
00103 }
00104 
00105 void CastorTestAnalysis::update(const BeginOfEvent * evt) {
00106   std::cout << "CastorTestAnalysis: Processing Event Number: "<<eventIndex<< std::endl;
00107   eventIndex++;
00108   stepIndex = 0;
00109 }
00110 
00111 
00112 
00113 
00114 void CastorTestAnalysis::update(const G4Step * aStep) {
00115   stepIndex++;
00116   
00117   
00118   if (doNTcastorstep) {
00119   
00120   G4StepPoint * preStepPoint = aStep->GetPreStepPoint();
00121 //  G4StepPoint * postStepPoint= aStep->GetPostStepPoint();
00122   G4double stepL = aStep->GetStepLength();
00123   G4double stepE = aStep->GetTotalEnergyDeposit();
00124   
00125   if (verbosity >= 2) 
00126     std::cout << "Step " << stepL << ", " << stepE << std::endl;
00127     
00128   G4Track * theTrack    = aStep->GetTrack();
00129   G4int theTrackID      = theTrack->GetTrackID();
00130   G4double theCharge    = theTrack->GetDynamicParticle()->GetCharge();
00131   //  G4String particleType = theTrack->GetDefinition()->GetParticleName();
00132   G4int pdgcode         = theTrack->GetDefinition()->GetPDGEncoding();
00133 
00134   G4ThreeVector vert_mom = theTrack->GetVertexMomentumDirection();
00135   G4double vpx = vert_mom.x();
00136   G4double vpy = vert_mom.y();
00137   G4double vpz = vert_mom.z();
00138   double eta = 0.5 * log( (1.+vpz) / (1.-vpz) );
00139   double phi = atan2(vpy,vpx);
00140 
00141   G4ThreeVector hitPoint = preStepPoint->GetPosition();
00142 
00143    // Fill-in ntuple
00144   //  castorsteparray[ntcastors_evt] = (*evt)()->GetEventID();
00145   castorsteparray[ntcastors_evt] = (float)eventIndex;
00146   castorsteparray[ntcastors_trackid] = (float)theTrackID;
00147   castorsteparray[ntcastors_charge] = theCharge;
00148   castorsteparray[ntcastors_pdgcode] = pdgcode;
00149   castorsteparray[ntcastors_x] = hitPoint.x();
00150   castorsteparray[ntcastors_y] = hitPoint.y();
00151   castorsteparray[ntcastors_z] = hitPoint.z();
00152   castorsteparray[ntcastors_stepl] = stepL;
00153   castorsteparray[ntcastors_stepe] = stepE;
00154   castorsteparray[ntcastors_eta] = eta;
00155   castorsteparray[ntcastors_phi] = phi;
00156   castorsteparray[ntcastors_vpx] = vpx;
00157   castorsteparray[ntcastors_vpy] = vpy;
00158   castorsteparray[ntcastors_vpz] = vpz;
00159 
00160   /*
00161   std::cout<<"TrackID: "<< theTrackID<<std::endl;
00162   std::cout<<"   StepN: "<< theTrack->GetCurrentStepNumber() <<std::endl;
00163   std::cout<<"      ParentID: "<< aStep->GetTrack()->GetParentID() <<std::endl;
00164   std::cout<<"      PDG: "<< pdgcode <<std::endl;
00165   std::cout<<"      X,Y,Z (mm): "<< theTrack->GetPosition().x() <<","<< theTrack->GetPosition().y() <<","<< theTrack->GetPosition().z() <<std::endl;
00166   std::cout<<"      KE (MeV): "<< theTrack->GetKineticEnergy() <<std::endl;
00167   std::cout<<"      Total EDep (MeV): "<< aStep->GetTotalEnergyDeposit() <<std::endl;
00168   std::cout<<"      StepLength (mm): "<< aStep->GetStepLength() <<std::endl;
00169   std::cout<<"      TrackLength (mm): "<< theTrack->GetTrackLength() <<std::endl;
00170 
00171   if ( theTrack->GetNextVolume() != 0 )
00172       std::cout<<"      NextVolume: "<< theTrack->GetNextVolume()->GetName() <<std::endl;
00173   else 
00174       std::cout<<"      NextVolume: OutOfWorld"<<std::endl;
00175   
00176   if(aStep->GetPostStepPoint()->GetProcessDefinedStep() != NULL)
00177       std::cout<<"      ProcessName: "<< aStep->GetPostStepPoint()->GetProcessDefinedStep()->GetProcessName() <<std::endl;
00178   else
00179       std::cout<<"      ProcessName: UserLimit"<<std::endl;
00180   
00181 
00182    std::cout<<std::endl;
00183   */
00184 
00185 #ifdef DebugLog
00186   if ( theTrack->GetNextVolume() != 0 )
00187     LogDebug("ForwardSim") << " NextVolume: " << theTrack->GetNextVolume()->GetName() ;
00188   else 
00189     LogDebug("ForwardSim") << " NextVolume: OutOfWorld" ;
00190 #endif
00191 
00192  
00193 //fill ntuple with step level information
00194   castorstepntuple->Fill(castorsteparray);
00195   }
00196 }
00197 
00198 //================= End of EVENT ===============
00199 void CastorTestAnalysis::update(const EndOfEvent * evt) {
00200 
00201   // Look for the Hit Collection 
00202   std::cout << std::endl << "CastorTest::update(EndOfEvent * evt) - event #" << (*evt)()->GetEventID() << std::endl;
00203 
00204   // access to the G4 hit collections 
00205   G4HCofThisEvent* allHC = (*evt)()->GetHCofThisEvent();
00206   std::cout << "update(*evt) --> accessed all HC";
00207   
00208   int CAFIid = G4SDManager::GetSDMpointer()->GetCollectionID("CastorFI");
00209     
00210   CaloG4HitCollection* theCAFI = (CaloG4HitCollection*) allHC->GetHC(CAFIid);
00211 
00212   theCastorNumScheme = new CastorNumberingScheme();
00213   // CastorNumberingScheme *theCastorNumScheme = new CastorNumberingScheme();
00214 
00215 /*
00216   unsigned int volumeID=0;
00217   int det, zside, sector, zmodule;
00218   std::map<int,float,std::less<int> > themap;
00219   double totalEnergy = 0;
00220   double hitEnergy = 0;
00221   double en_in_fi = 0.;
00222   double en_in_pl = 0.;
00223 */
00224 //  double en_in_bu = 0.;
00225 //  double en_in_tu = 0.;
00226 
00227   if (doNTcastorevent) {
00228     
00229     eventGlobalHit = 0 ;
00230     // int eventGlobalHit = 0 ;
00231     
00232     //  Check FI TBranch for Hits
00233     if (theCAFI->entries() > 0) getCastorBranchData(theCAFI) ;
00234     
00235     // Find Primary info:
00236       int trackID = 0;
00237       int particleType = 0;
00238       G4PrimaryParticle* thePrim=0;
00239       G4int nvertex = (*evt)()->GetNumberOfPrimaryVertex();
00240       std::cout << "Event has " << nvertex << " vertex" << std::endl; 
00241       if (nvertex==0) std::cout << "CASTORTest End Of Event  ERROR: no vertex" << std::endl;
00242 
00243       for (int i = 0 ; i<nvertex; i++) {
00244         G4PrimaryVertex* avertex = (*evt)()->GetPrimaryVertex(i);
00245         if (avertex == 0) 
00246           std::cout << "CASTORTest End Of Event ERR: pointer to vertex = 0" << std::endl;
00247         std::cout << "Vertex number :" <<i << std::endl;
00248         int npart = avertex->GetNumberOfParticle();
00249         if (npart ==0)
00250           std::cout << "CASTORTest End Of Event ERR: no primary!" << std::endl;
00251         if (thePrim==0) thePrim=avertex->GetPrimary(trackID);
00252       }
00253     
00254       double px=0.,py=0.,pz=0., pInit=0;
00255       double eta = 0., phi = 0.;
00256     
00257       if (thePrim != 0) {
00258         px = thePrim->GetPx();
00259         py = thePrim->GetPy();
00260         pz = thePrim->GetPz();
00261         pInit = sqrt(pow(px,2.)+pow(py,2.)+pow(pz,2.));
00262         if (pInit==0) {
00263           std::cout << "CASTORTest End Of Event  ERR: primary has p=0 " << std::endl;
00264         } else {   
00265           float costheta = pz/pInit;
00266           float theta = acos(std::min(std::max(costheta,float(-1.)),float(1.)));
00267           eta = -log(tan(theta/2));
00268 
00269           if (px != 0) phi = atan(py/px);  
00270         }
00271         particleType    = thePrim->GetPDGcode();
00272       } else {
00273         std::cout << "CASTORTest End Of Event ERR: could not find primary "
00274                   << std::endl;
00275       }
00276       LogDebug("ForwardSim") << "CastorTestAnalysis: Particle Type " 
00277                              << particleType << " p/eta/phi " << pInit << ", "
00278                              << eta << ", " << phi;
00279   }
00280 
00281   int iEvt = (*evt)()->GetEventID();
00282   if (iEvt < 10) 
00283     std::cout << " CastorTest Event " << iEvt << std::endl;
00284   else if ((iEvt < 100) && (iEvt%10 == 0)) 
00285     std::cout << " CastorTest Event " << iEvt << std::endl;
00286   else if ((iEvt < 1000) && (iEvt%100 == 0)) 
00287     std::cout << " CastorTest Event " << iEvt << std::endl;
00288   else if ((iEvt < 10000) && (iEvt%1000 == 0)) 
00289     std::cout << " CastorTest Event " << iEvt << std::endl;
00290                                                  
00291   std::cout << std::endl << "===>>> Done writing user histograms " << std::endl;
00292 }
00293 
00294 void CastorTestAnalysis::update(const EndOfRun * run) {;}
00295   
00296 //=================================================================== 
00297 void CastorTestAnalysis::getCastorBranchData(const CaloG4HitCollection * hc) {
00298 
00299     int nentries = hc->entries();
00300     
00301     if (nentries > 0) {
00302       
00303       unsigned int volumeID=0;
00304       int det=0, zside, sector, zmodule;
00305       std::map<int,float,std::less<int> > themap;
00306       double totalEnergy = 0;
00307       double hitEnergy = 0;
00308       double en_in_sd = 0.;
00309 
00310       for (int ihit = 0; ihit < nentries; ihit++) {
00311         CaloG4Hit* aHit = (*hc)[ihit];
00312         totalEnergy += aHit->getEnergyDeposit();
00313       }
00314     
00315       for (int ihit = 0; ihit < nentries; ihit++) {
00316         CaloG4Hit* aHit = (*hc)[ihit];
00317         volumeID = aHit->getUnitID();
00318         hitEnergy = aHit->getEnergyDeposit();
00319         en_in_sd += aHit->getEnergyDeposit();
00320 //      double enEm = aHit->getEM();
00321 //      double enHad = aHit->getHadr();
00322         
00323         themap[volumeID] += aHit->getEnergyDeposit();
00324         // int det, zside, sector, zmodule;
00325         theCastorNumScheme->unpackIndex(volumeID, zside, sector,zmodule);
00326 
00327         // det = 2 ;  //  det=2/3 for CAFI/CAPL
00328         
00329         castoreventarray[ntcastore_evt]       = (float)eventIndex;
00330 //      castoreventarray[ntcastore_ihit]      = (float)ihit;
00331         castoreventarray[ntcastore_ihit]      = (float)eventGlobalHit;
00332         castoreventarray[ntcastore_detector]  = (float)det;
00333         castoreventarray[ntcastore_sector]    = (float)sector;
00334         castoreventarray[ntcastore_module]    = (float)zmodule;
00335         castoreventarray[ntcastore_enem]      = en_in_sd;
00336         castoreventarray[ntcastore_enhad]     = totalEnergy;
00337         castoreventarray[ntcastore_hitenergy] = hitEnergy;
00338         castoreventarray[ntcastore_x]         = aHit->getPosition().x();
00339         castoreventarray[ntcastore_y]         = aHit->getPosition().y();
00340         castoreventarray[ntcastore_z]         = aHit->getPosition().z();
00341 //      castoreventarray[ntcastore_x]         = aHit->getEntry().x();
00342 //      castoreventarray[ntcastore_y]         = aHit->getEntry().y();
00343 //      castoreventarray[ntcastore_z]         = aHit->getEntry().z();
00344         
00345         castoreventntuple->Fill(castoreventarray);
00346         
00347         eventGlobalHit++ ;
00348       }
00349     } // nentries > 0
00350 }
00351 
00352 //=================================================================== 
00353 
00354 void CastorTestAnalysis::Finish() {
00355   if (doNTcastorstep) {
00356     castorOutputStepFile->cd();
00357     castorstepntuple->Write();
00358     std::cout << "CastorTestAnalysis: Ntuple step  written" <<std::endl;
00359     castorOutputStepFile->Close();
00360     std::cout << "CastorTestAnalysis: Step file closed" << std::endl;
00361   }
00362   
00363    if (doNTcastorevent) {
00364    castorOutputEventFile->cd();
00365    castoreventntuple->Write("",TObject::kOverwrite);
00366    std::cout << "CastorTestAnalysis: Ntuple event written" << std::endl;   
00367    castorOutputEventFile->Close();
00368    std::cout << "CastorTestAnalysis: Event file closed" << std::endl;
00369  }
00370 
00371 }