CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_6_1_2_SLHC4_patch1/src/Validation/Geometry/src/MaterialBudgetAction.cc

Go to the documentation of this file.
00001 
00002 #include "Validation/Geometry/interface/MaterialBudgetAction.h"
00003 #include "Validation/Geometry/interface/MaterialBudgetData.h"
00004 
00005 #include "FWCore/Framework/interface/Event.h"
00006 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00007 
00008 #include "SimG4Core/Notification/interface/BeginOfRun.h"
00009 #include "SimG4Core/Notification/interface/BeginOfTrack.h"
00010 #include "SimG4Core/Notification/interface/EndOfTrack.h"
00011 #include "SimG4Core/Notification/interface/EndOfEvent.h"
00012 
00013 #include "G4Step.hh"
00014 #include "G4Track.hh"
00015 #include "G4UImanager.hh"
00016 #include "G4UIterminal.hh"
00017 #include "G4UItcsh.hh"
00018 #include "G4LogicalVolumeStore.hh"
00019 #include "G4TouchableHistory.hh"
00020 #include "G4VPhysicalVolume.hh"
00021 #include "G4VProcess.hh"
00022 #include "G4ParticleTable.hh"
00023 #include "G4ParticleDefinition.hh"
00024 #include "G4ProcessManager.hh"
00025 #include "G4TransportationManager.hh"
00026 
00027 #include <iostream>
00028 
00029 
00030 //-------------------------------------------------------------------------
00031 MaterialBudgetAction::MaterialBudgetAction(const edm::ParameterSet& iPSet) :
00032   theHistoMgr(0)
00033 {
00034   theData = new MaterialBudgetData;
00035   
00036   edm::ParameterSet m_Anal = iPSet.getParameter<edm::ParameterSet>("MaterialBudgetAction");
00037   
00038   //---- Accumulate material budget only inside selected volumes
00039   std::string theHistoList = m_Anal.getParameter<std::string>("HistogramList");
00040   std::vector<std::string> volList = m_Anal.getParameter< std::vector<std::string> >("SelectedVolumes");
00041   std::vector<std::string>::const_iterator ite;
00042   std::cout << "TestGeometry: List of the selected volumes: " << std::endl;
00043   for( ite = volList.begin(); ite != volList.end(); ite++ ){
00044     if( (*ite) != "None" ) {
00045       theVolumeList.push_back( *ite );
00046       std::cout << (*ite) << std::endl;
00047     }
00048   }
00049   // log
00050   if(theHistoList == "Tracker" ) {
00051     std::cout << "TestGeometry: MaterialBudgetAction running in Tracker Mode" << std::endl;
00052   } 
00053   else if(theHistoList == "ECAL" ) {
00054     std::cout << "TestGeometry: MaterialBudgetAction running in Ecal Mode" << std::endl;
00055   } 
00056   else {
00057     std::cout << "TestGeometry: MaterialBudgetAction running in General Mode" << std::endl;
00058   }
00059   //
00060     
00061   //---- Stop track when a process occurs
00062   theProcessToStop = m_Anal.getParameter<std::string>("StopAfterProcess");
00063   std::cout << "TestGeometry: stop at process " << theProcessToStop << std::endl;
00064 
00065   //---- Save histos to ROOT file 
00066   std::string saveToHistosFile = m_Anal.getParameter<std::string>("HistosFile");
00067   if( saveToHistosFile != "None" ) {
00068     saveToHistos = true;
00069     std::cout << "TestGeometry: saving histograms to " << saveToHistosFile << std::endl;
00070     theHistoMgr = new TestHistoMgr();
00071 
00072     // rr
00073     if(theHistoList == "Tracker" ) {
00074       theHistos = new MaterialBudgetTrackerHistos( theData, theHistoMgr, saveToHistosFile );
00075     } 
00076     else if (theHistoList == "ECAL") {
00077       theHistos = new MaterialBudgetEcalHistos( theData, theHistoMgr, saveToHistosFile );
00078     }
00079     else {
00080       theHistos = new MaterialBudgetHistos( theData, theHistoMgr, saveToHistosFile );
00081     }
00082       // rr
00083   } else {
00084     saveToHistos = false;
00085   }
00086   
00087   //---- Save material budget info to TEXT file
00088   std::string saveToTxtFile = m_Anal.getParameter<std::string>("TextFile");
00089   if( saveToTxtFile != "None" ) {
00090     saveToTxt = true;
00091     std::cout << "TestGeometry: saving text info to " << saveToTxtFile << std::endl;
00092     theTxt = new MaterialBudgetTxt( theData, saveToTxtFile );
00093   } else {
00094     saveToTxt = false;
00095   }
00096   
00097   //---- Compute all the steps even if not stored on file
00098   bool allSteps = m_Anal.getParameter<bool>("AllStepsToTree");  
00099   std::cout << "TestGeometry: all steps are computed " << allSteps << std::endl;
00100   if( allSteps ) theData->SetAllStepsToTree();
00101   
00102   //---- Save tree to ROOT file
00103   std::string saveToTreeFile = m_Anal.getParameter<std::string>("TreeFile");
00104   //  std::string saveToTreeFile = ""; 
00105   if( saveToTreeFile != "None" ) {
00106     saveToTree = true;
00107     theTree = new MaterialBudgetTree( theData, saveToTreeFile );
00108   } else {
00109     saveToTree = false;
00110   }
00111   std::cout << "TestGeometry: saving ROOT TREE to " << saveToTreeFile << std::endl;
00112   
00113   //---- Track the first decay products of the main particle
00114   // if their kinetic energy is greater than  Ekin
00115   storeDecay = m_Anal.getUntrackedParameter<bool>("storeDecay",false);  
00116   Ekin       = m_Anal.getUntrackedParameter<double>("EminDecayProd",1000.0); // MeV
00117   std::cout << "TestGeometry: decay products steps are stored " << storeDecay;
00118   if(storeDecay) std::cout << " if their kinetic energy is greater than " << Ekin << " MeV";
00119   std::cout << std::endl;
00120   firstParticle = false;
00121   }
00122 
00123 
00124 //-------------------------------------------------------------------------
00125 MaterialBudgetAction::~MaterialBudgetAction()
00126 {
00127   if (saveToTxt) delete theTxt;
00128   if (saveToTree) delete theTree;
00129   if (saveToHistos) delete theHistos;
00130   if (theHistoMgr) delete theHistoMgr;
00131   delete theData;
00132 }
00133 
00134 
00135 //-------------------------------------------------------------------------
00136 void MaterialBudgetAction::produce(edm::Event& e, const edm::EventSetup&)
00137 {
00138 }
00139 
00140 
00141 //-------------------------------------------------------------------------
00142 void MaterialBudgetAction::update(const BeginOfRun* trk)
00143 {
00144   //----- Check that selected volumes are indeed part of the geometry
00145   const G4LogicalVolumeStore* lvs = G4LogicalVolumeStore::GetInstance();
00146   std::vector<G4LogicalVolume*>::const_iterator lvcite;
00147   std::vector<G4String>::const_iterator volcite;
00148 
00149   for( volcite = theVolumeList.begin(); volcite != theVolumeList.end(); volcite++ ){
00150   //-  std::cout << " MaterialBudgetAction checking volume " << *volcite << std::endl;
00151     bool volFound = false;
00152     for( lvcite = lvs->begin(); lvcite != lvs->end(); lvcite++ ) {
00153       if( (*lvcite)->GetName() == *volcite )  {
00154         volFound = true;
00155         break;
00156       }
00157     }
00158     if( !volFound ) {
00159       std::cerr << " @@@@@@@ WARNING at MaterialBudgetAction: selected volume not found in geometry " << *volcite << std::endl;
00160     }
00161   }
00162 
00163 
00164   //----- Check process selected is one of the available ones
00165   bool procFound = false;
00166   if( theProcessToStop == "None" ) { 
00167     procFound = true;
00168   } else {
00169     G4ParticleTable * partTable = G4ParticleTable::GetParticleTable();
00170     int siz = partTable->size();
00171     for (int ii= 0; ii < siz; ii++) {
00172       G4ParticleDefinition * particle = partTable->GetParticle(ii);
00173       std::string particleName = particle->GetParticleName();
00174       
00175       //--- All processes of this particle 
00176       G4ProcessManager * pmanager = particle->GetProcessManager();
00177       G4ProcessVector * pvect = pmanager->GetProcessList();
00178       int sizproc = pvect->size();
00179       for (int jj = 0; jj < sizproc; jj++) {
00180         if( (*pvect)[jj]->GetProcessName() == theProcessToStop ) {
00181           procFound = true;
00182           break;
00183         }
00184       }
00185     }
00186   }
00187 
00188   if( !procFound ) {
00189       std::cerr << " @@@@@@@ WARNING at MaterialBudgetAction: selected process to stop tracking not found " << theProcessToStop << std::endl;
00190     }
00191 
00192 }
00193 
00194 
00195 //-------------------------------------------------------------------------
00196 void MaterialBudgetAction::update(const BeginOfTrack* trk)
00197 {
00198   const G4Track * aTrack = (*trk)(); // recover G4 pointer if wanted
00199   
00200   // that was a temporary action while we're sorting out
00201   // about # of secondaries (produced if CutsPerRegion=true)
00202   //
00203   std::cout << "Track ID " << aTrack->GetTrackID() << " Track parent ID " << aTrack->GetParentID() 
00204             << " PDG Id. = " << aTrack->GetDefinition()->GetPDGEncoding()
00205             << " Ekin = " << aTrack->GetKineticEnergy() << " MeV" << std::endl;
00206   if( aTrack->GetCreatorProcess() ) std::cout << " produced through " << aTrack->GetCreatorProcess()->GetProcessType() << std::endl;
00207   
00208   if(aTrack->GetTrackID() == 1) {
00209     firstParticle = true;
00210   } else {
00211     firstParticle = false;
00212   }
00213   
00214   if( storeDecay ) { // if record of the decay is requested
00215     if( aTrack->GetCreatorProcess() ) {
00216       if (
00217           aTrack->GetParentID() == 1
00218           &&
00219           //      aTrack->GetCreatorProcess()->GetProcessType() == 6
00220           //      &&
00221           aTrack->GetKineticEnergy() > Ekin
00222           ) {
00223         // continue
00224       } else {
00225         G4Track * aTracknc = const_cast<G4Track*>(aTrack);
00226         aTracknc->SetTrackStatus(fStopAndKill);
00227         return;
00228       }
00229     } // particles produced from a decay (type=6) of the main particle (ID=1) with Kinetic Energy [MeV] > Ekin
00230   } else { // kill all the other particles (take only the main one until it disappears) if decay not stored
00231     if( aTrack->GetParentID() != 0) {
00232       G4Track * aTracknc = const_cast<G4Track*>(aTrack);
00233       aTracknc->SetTrackStatus(fStopAndKill);
00234       return;  
00235     }
00236   }
00237   
00238   
00239   if(firstParticle) {
00240     //--------- start of track
00241     //-    std::cout << " Data Start Track " << std::endl;
00242     theData->dataStartTrack( aTrack );
00243     if (saveToTree) theTree->fillStartTrack();
00244     if (saveToHistos) theHistos->fillStartTrack();
00245     if (saveToTxt) theTxt->fillStartTrack();
00246   }
00247 }
00248  
00249 
00250 //-------------------------------------------------------------------------
00251 void MaterialBudgetAction::update(const G4Step* aStep)
00252 {
00253 
00254   //----- Check it is inside one of the volumes selected
00255   if( theVolumeList.size() != 0 ) {
00256     if( !CheckTouchableInSelectedVolumes( aStep->GetTrack()->GetTouchable() ) ) return;
00257   } 
00258 
00259   //---------- each step
00260   theData->dataPerStep( aStep );
00261   //-  std::cout << " aStep->GetPostStepPoint()->GetTouchable() " << aStep->GetPostStepPoint()->GetTouchable()->GetVolume() << " " << aStep->GetPreStepPoint()->GetTouchable()->GetVolume() << std::endl;
00262   if (saveToTree) theTree->fillPerStep();
00263   if (saveToHistos) theHistos->fillPerStep();
00264   if (saveToTxt) theTxt->fillPerStep();
00265 
00266 
00267   //----- Stop tracking after selected process
00268   if( StopAfterProcess( aStep ) ) {
00269     G4Track* track = aStep->GetTrack();
00270     track->SetTrackStatus( fStopAndKill );
00271   }
00272 
00273   return;
00274 
00275 }
00276 
00277 
00278 //-------------------------------------------------------------------------
00279 std::string MaterialBudgetAction::getSubDetectorName( G4StepPoint* aStepPoint )
00280 {
00281   G4TouchableHistory* theTouchable
00282     = (G4TouchableHistory*)(aStepPoint->GetTouchable());
00283   G4int num_levels = theTouchable->GetHistoryDepth();
00284   
00285   if( theTouchable->GetVolume() ) {
00286     return theTouchable->GetVolume(num_levels-1)->GetName();
00287   } else { 
00288     return "OutOfWorld";
00289   }
00290 }
00291 
00292 
00293 //-------------------------------------------------------------------------
00294 std::string MaterialBudgetAction::getPartName( G4StepPoint* aStepPoint )
00295 {
00296   G4TouchableHistory* theTouchable
00297     = (G4TouchableHistory*)(aStepPoint->GetTouchable());
00298   G4int num_levels = theTouchable->GetHistoryDepth();
00299   //  theTouchable->MoveUpHistory(num_levels-3);
00300   
00301   if( theTouchable->GetVolume() ) {
00302     return theTouchable->GetVolume(num_levels-3)->GetName();
00303   } else { 
00304     return "OutOfWorld";
00305   }
00306 }
00307 
00308 
00309 
00310 //-------------------------------------------------------------------------
00311 void MaterialBudgetAction::update(const EndOfTrack* trk)
00312 {
00313   //  std::cout << " EndOfTrack " << saveToHistos << std::endl;
00314   const G4Track * aTrack = (*trk)(); // recover G4 pointer if wanted
00315   //  if( aTrack->GetParentID() != 0 ) return;
00316   
00317   //---------- end of track (OutOfWorld)
00318   //-  std::cout << " Data End Track " << std::endl;
00319   theData->dataEndTrack( aTrack );
00320 }
00321 
00322 void MaterialBudgetAction::update(const EndOfEvent* evt)
00323 {
00324   //-  std::cout << " Data End Event " << std::endl;
00325   if (saveToTree) theTree->fillEndTrack();
00326   if (saveToHistos) theHistos->fillEndTrack();
00327   if (saveToTxt) theTxt->fillEndTrack();  
00328 }
00329 
00330 //-------------------------------------------------------------------------
00331 void MaterialBudgetAction::endRun()
00332 {
00333 }
00334 
00335 
00336 //-------------------------------------------------------------------------
00337 bool MaterialBudgetAction::CheckTouchableInSelectedVolumes( const G4VTouchable*  touch ) 
00338 {
00339   std::vector<G4String>::const_iterator ite;
00340   size_t volh = touch->GetHistoryDepth();
00341   for( ite = theVolumeList.begin(); ite != theVolumeList.end(); ite++ ){
00342     //-  std::cout << " CheckTouchableInSelectedVolumes vol " << *ite << std::endl;
00343     for( int ii = volh; ii >= 0; ii-- ){
00344       //-  std::cout << ii << " CheckTouchableInSelectedVolumes parent  " << touch->GetVolume(ii)->GetName() << std::endl;
00345       if( touch->GetVolume(ii)->GetName() == *ite ) return true;
00346     }
00347   }
00348 
00349   return false;
00350 
00351 }
00352 
00353 
00354 //-------------------------------------------------------------------------
00355 bool MaterialBudgetAction::StopAfterProcess( const G4Step* aStep )
00356 {
00357   if( theProcessToStop == "" ) return false;
00358 
00359   if(aStep->GetPostStepPoint()->GetProcessDefinedStep() == NULL) return false;
00360   if( aStep->GetPostStepPoint()->GetProcessDefinedStep()->GetProcessName() == theProcessToStop ) {
00361     std::cout << " MaterialBudgetAction::StopAfterProcess " << aStep->GetPostStepPoint()->GetProcessDefinedStep()->GetProcessName() << std::endl;
00362     return true;
00363   } else {
00364     return false;
00365   }
00366 }