CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_6_2_5/src/SimG4CMS/ShowerLibraryProducer/src/HcalForwardAnalysis.cc

Go to the documentation of this file.
00001 // system include files
00002 #include <cmath>
00003 #include <iostream>
00004 #include <iomanip>
00005 
00006 // user include files
00007 #include "SimG4CMS/ShowerLibraryProducer/interface/HcalForwardAnalysis.h"
00008 
00009 #include "SimG4Core/Notification/interface/BeginOfRun.h"
00010 #include "SimG4Core/Notification/interface/BeginOfEvent.h"
00011 #include "SimG4Core/Notification/interface/EndOfEvent.h"
00012 
00013 #include "DataFormats/Math/interface/Point3D.h"
00014 
00015 #include "FWCore/Framework/interface/Event.h"
00016 #include "FWCore/Framework/interface/EventSetup.h"
00017 #include "FWCore/Framework/interface/ESHandle.h"
00018 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00019 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00020 
00021 #include "G4SDManager.hh"
00022 #include "G4VProcess.hh"
00023 #include "G4HCofThisEvent.hh"
00024 #include "CLHEP/Units/GlobalSystemOfUnits.h"
00025 #include "CLHEP/Units/GlobalPhysicalConstants.h"
00026 
00027 HcalForwardAnalysis::HcalForwardAnalysis(const edm::ParameterSet &p) {
00028 
00029   edm::ParameterSet m_SLP = p.getParameter<edm::ParameterSet> ("HFShowerLibraryProducer");
00030   theNames = m_SLP.getParameter<std::vector<std::string> > ("Names");
00031   //LibVer = m_HS.getParameter<std::string> ("LibVer");
00032   //produces<HFShowerPhotonCollection> ();
00033   init();
00034   theEventCounter = 0;
00035   nphot = 0;
00036   for (int i = 0; i < 10000; ++i) {
00037     x[i] = 0.;
00038     y[i] = 0.;
00039     z[i] = 0.;
00040     t[i] = 0.;
00041     lambda[i] = 0.;
00042     fiberId[i] = 0;
00043   }
00044   primX = primY = primZ = primT = 0.;
00045   primMomX = primMomY = primMomZ = 0.;
00046 
00047 }
00048 
00049 HcalForwardAnalysis::~HcalForwardAnalysis() {
00050 }
00051 
00052 //
00053 // member functions
00054 //
00055 
00056 void HcalForwardAnalysis::produce(edm::Event& iEvent, const edm::EventSetup&) {
00057 
00058         //std::auto_ptr<HFShowerPhotonCollection> product(new HFShowerPhotonCollection);
00059         //edm::LogInfo("HcalForwardLib") << "HcalForwardAnalysis: =====> Filling  event";
00060         //fillEvent(*product);
00061         //iEvent.put(product);
00062         //std::auto_ptr<PHcalForwardLibInfo> product(new PHcalForwardLibInfo);
00063         //fillEvent(*product);
00064         fillEvent();
00065         //iEvent.put(product);
00066 }
00067 
00068 void HcalForwardAnalysis::init() {
00069 
00070         theTree = theFile->make<TTree> ("CherenkovPhotons", "Cherenkov Photons");
00071         theTree->Branch("nphot", &nphot, "nphot/I");
00072         theTree->Branch("x", &x, "x[nphot]/F");
00073         theTree->Branch("y", &y, "y[nphot]/F");
00074         theTree->Branch("z", &z, "z[nphot]/F");
00075         theTree->Branch("t", &t, "t[nphot]/F");
00076         theTree->Branch("lambda", &lambda, "lambda[nphot]/F");
00077         theTree->Branch("fiberId", &fiberId, "fiberId[nphot]/I");
00078         theTree->Branch("primX", &primX, "primX/F");
00079         theTree->Branch("primY", &primY, "primY/F");
00080         theTree->Branch("primZ", &primZ, "primZ/F");
00081         theTree->Branch("primMomX", &primMomX, "primMomX/F");
00082         theTree->Branch("primMomY", &primMomY, "primMomY/F");
00083         theTree->Branch("primMomZ", &primMomZ, "primMomZ/F");
00084         theTree->Branch("primT", &primT, "primT/F");
00085 
00086         // counter
00087         count = 0;
00088         evNum = 0;
00089         clear();
00090 }
00091 
00092 void HcalForwardAnalysis::update(const BeginOfRun * run) {
00093 
00094         int irun = (*run)()->GetRunID();
00095         edm::LogInfo("HcalForwardLib") << " =====> Begin of Run = " << irun;
00096 
00097 }
00098 
00099 void HcalForwardAnalysis::update(const BeginOfEvent * evt) {
00100 
00101         evNum = (*evt)()->GetEventID();
00102         clear();
00103         edm::LogInfo("HcalForwardLib") << "HcalForwardAnalysis: =====> Begin of event = " << evNum;
00104 }
00105 
00106 void HcalForwardAnalysis::update(const G4Step * aStep) {
00107 }
00108 
00109 void HcalForwardAnalysis::update(const EndOfEvent * evt) {
00110 
00111         count++;
00112 
00113         //fill the buffer
00114         LogDebug("HcalForwardLib")<< "HcalForwardAnalysis::Fill event "
00115         << (*evt)()->GetEventID();
00116         setPhotons(evt);
00117 
00118         int iEvt = (*evt)()->GetEventID();
00119         if (iEvt < 10)
00120         edm::LogInfo("HcalForwardLib") << "HcalForwardAnalysis:: Event " << iEvt;
00121         else if ((iEvt < 100) && (iEvt%10 == 0))
00122         edm::LogInfo("HcalForwardLib") << "HcalForwardAnalysis:: Event " << iEvt;
00123         else if ((iEvt < 1000) && (iEvt%100 == 0))
00124         edm::LogInfo("HcalForwardLib") << "HcalForwardAnalysis:: Event " << iEvt;
00125         else if ((iEvt < 10000) && (iEvt%1000 == 0))
00126         edm::LogInfo("HcalForwardLib") << "HcalForwardAnalysis:: Event " << iEvt;
00127 }
00128 
00129 void HcalForwardAnalysis::setPhotons(const EndOfEvent * evt) {
00130 
00131         int idHC, j;
00132         FiberG4HitsCollection* theHC;
00133         // Look for the Hit Collection of HCal
00134         G4HCofThisEvent* allHC = (*evt)()->GetHCofThisEvent();
00135         std::string sdName = theNames[0];//name for fiber hits
00136         idHC = G4SDManager::GetSDMpointer()->GetCollectionID(sdName);
00137         theHC = (FiberG4HitsCollection*) allHC->GetHC(idHC);
00138         LogDebug("HcalForwardLib")<< "HcalForwardAnalysis::setPhotons() Hit Collection for " << sdName
00139         << " of ID " << idHC << " is obtained at " << theHC;
00140         std::vector<HFShowerPhoton> ShortFiberPhotons;
00141         std::vector<HFShowerPhoton> LongFiberPhotons;
00142         LongFiberPhotons.clear();
00143         ShortFiberPhotons.clear();
00144         if (idHC >= 0 && theHC> 0) {
00145                 std::cout << "FiberhitSize " << theHC->entries() << std::endl;
00146                 for (j = 0; j < theHC->entries(); j++) {
00147                         FiberG4Hit* aHit = (*theHC)[j];
00148                         std::vector<HFShowerPhoton> thePhotonsFromHit = aHit->photon();
00149                         std::cout << "Fiberhit " << j << " has " << thePhotonsFromHit.size() << " photons." << std::endl;
00150                         int fTowerId = -1;
00151                         int fCellId = -1;
00152                         int fFiberId = -1;
00153                         parseDetId(aHit->towerId(), fTowerId, fCellId, fFiberId);
00154                         for(unsigned int iph = 0; iph < thePhotonsFromHit.size(); ++iph){
00155                             if(aHit->depth() == 1)LongFiberPhotons.push_back(thePhotonsFromHit[iph]);
00156                             if(aHit->depth() == 2)ShortFiberPhotons.push_back(thePhotonsFromHit[iph]);
00157                         }
00158                         LogDebug("HcalForwardLib") << "HcalForwardAnalysis::setPhotons() NbPhotons " << thePhotonsFromHit.size()
00159                         << " towerId " << fTowerId << " cellId " << fCellId << " fiberId " << fFiberId << " depth " << aHit->depth();
00160                 }
00161         }else{
00162                 LogDebug("HcalForwardLib") << "HcalForwardAnalysis::setPhotons(): No Photons!";
00163                 return;
00164         }
00165         LogDebug("HcalForwardLib") << "HcalForwardAnalysis::setPhotons() LongFibPhotons: "
00166            << LongFiberPhotons.size() << " ShortFibPhotons: " << ShortFiberPhotons.size();
00167         std::cout << "HcalForwardAnalysis::setPhotons() LongFibPhotons: "
00168                    << LongFiberPhotons.size() << " ShortFibPhotons: " << ShortFiberPhotons.size() << std::endl;
00169 
00170         //Chamber hits to find information about primary particle on surface
00171         HFShowerG4HitsCollection* theChamberHC;
00172         G4HCofThisEvent* allChamberHC = (*evt)()->GetHCofThisEvent();
00173         sdName= theNames[1];
00174         idHC = G4SDManager::GetSDMpointer()->GetCollectionID(sdName);
00175         theChamberHC = (HFShowerG4HitsCollection*) allChamberHC->GetHC(idHC);
00176         math::XYZPoint primPosOnSurf(0,0,0);
00177         math::XYZPoint primMomDirOnSurf(0,0,0);
00178         float primTimeOnSurf = 0;
00179         //      the chamber hit is for primary particle, but step size can be small
00180         //      (in newer Geant4 versions) and as a result primary particle may have
00181         //      multiple hits. We want to take last one which is close the HF absorber
00182         if(idHC >= 0 && theChamberHC> 0) {
00183                 LogDebug("HcalForwardLib") << "HcalForwardAnalysis::setPhotons() Chamber Hits size: " << theChamberHC->entries();
00184                 for(j = 0; j < theChamberHC->entries();++j) {
00185                         HFShowerG4Hit* aHit = (*theChamberHC)[j];
00186                         LogDebug("HcalForwardLib") << "HcalForwardAnalysis::setPhotons() Chamber Hit id " << aHit->hitId()
00187                         << " track id " << aHit->trackId() << " prim. pos. " << aHit->globalPosition() << " prom mom. dir. "
00188                         << aHit->primaryMomDir() << " time " << aHit->time();
00189                         primPosOnSurf.SetXYZ(aHit->globalPosition().x(), aHit->globalPosition().y(), aHit->globalPosition().z());
00190                         primMomDirOnSurf.SetXYZ(aHit->primaryMomDir().x(), aHit->primaryMomDir().y(), aHit->primaryMomDir().z());
00191                         primTimeOnSurf = aHit->time();
00192                 }
00193         }else{
00194                 LogDebug("HcalForwardLib") << "HcalForwardAnalysis::setPhotons(): No Chamber hits. Something is wrong!";
00195                 return;
00196         }
00197         primX = primPosOnSurf.x();
00198         primY = primPosOnSurf.y();
00199         primZ = primPosOnSurf.z();
00200         primT = primTimeOnSurf;
00201         primMomX = primMomDirOnSurf.x();
00202         primMomY = primMomDirOnSurf.y();
00203         primMomZ = primMomDirOnSurf.z();
00204         //production point of Cherenkov photons in long fibers w.r.t local coordinates
00205         float photonProdX = 0.;
00206         float photonProdY = 0.;
00207         float photonProdZ = 0.;
00208         float photonProdTime = 0.;
00209   //angles for rotation matrices
00210   double theta = primMomDirOnSurf.theta();
00211   double phi = primMomDirOnSurf.phi();
00212 
00213         for(unsigned int k = 0; k < LongFiberPhotons.size(); ++k){
00214                 HFShowerPhoton aPhoton =  LongFiberPhotons[k];
00215                 photonProdX =  aPhoton.x()*cos(theta)*cos(phi) + aPhoton.y()*cos(theta)*sin(phi) - aPhoton.z()*sin(theta) - primPosOnSurf.x();
00216                 photonProdY =  -aPhoton.x()*sin(phi) + aPhoton.y()*cos(phi) - primPosOnSurf.y();
00217                 photonProdZ = aPhoton.x()*sin(theta)*cos(phi) + aPhoton.y()*sin(theta)*sin(phi) + aPhoton.z()*cos(theta) - primPosOnSurf.z();
00218                 photonProdTime = aPhoton.t()- primTimeOnSurf;
00219                 thePhotons.push_back(Photon(1, photonProdX, photonProdY, photonProdZ, photonProdTime, aPhoton.lambda()));
00220         }
00221         for(unsigned int k = 0; k < ShortFiberPhotons.size(); ++k){
00222                         HFShowerPhoton aPhoton =  ShortFiberPhotons[k];
00223                         photonProdX =  aPhoton.x()*cos(theta)*cos(phi) + aPhoton.y()*cos(theta)*sin(phi) - aPhoton.z()*sin(theta) - primPosOnSurf.x();
00224                         photonProdY =  -aPhoton.x()*sin(phi) + aPhoton.y()*cos(phi) - primPosOnSurf.y();
00225                         photonProdZ = aPhoton.x()*sin(theta)*cos(phi) + aPhoton.y()*sin(theta)*sin(phi) + aPhoton.z()*cos(theta) - primPosOnSurf.z();
00226                         photonProdTime = aPhoton.t()- primTimeOnSurf;
00227                         thePhotons.push_back(Photon(2, photonProdX, photonProdY, photonProdZ, photonProdTime, aPhoton.lambda()));
00228         }
00229 }
00230 void HcalForwardAnalysis::fillEvent() {
00231         /*
00232          edm::LogInfo("HcalForwardLib") << "HcalForwardAnalysis: =====> filledEvent";
00233          */
00234         nphot = int(thePhotons.size());
00235         for (int i = 0; i < nphot; ++i) {
00236                 x[i] = thePhotons[i].x;
00237                 y[i] = thePhotons[i].y;
00238                 z[i] = thePhotons[i].z;
00239                 t[i] = thePhotons[i].t;
00240                 lambda[i] = thePhotons[i].lambda;
00241                 fiberId[i] = thePhotons[i].fiberId;
00242         }
00243         theTree->Fill();
00244 }
00245 void HcalForwardAnalysis::parseDetId(int id, int& tower, int& cell, int& fiber) {
00246         tower = id / 10000;
00247         cell = id / 10 - tower * 10;
00248         fiber = id - tower * 10000 - cell * 10;
00249 }
00250 
00251 void HcalForwardAnalysis::clear() {
00252         nphot = 0;
00253         for (int i = 0; i < 10000; ++i) {
00254                 x[i] = 0.;
00255                 y[i] = 0.;
00256                 z[i] = 0.;
00257                 t[i] = 0.;
00258                 lambda[i] = 0.;
00259                 fiberId[i] = 0;
00260         }
00261         primX = primY = primZ = primT = 0.;
00262         primMomX = primMomY = primMomZ = 0.;
00263 
00264         thePhotons.clear();
00265 }