CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_10_patch2/src/SimG4CMS/FP420/plugins/FP420Test.cc

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 //
00003 
00004 // system include files
00005 #include <iostream>
00006 #include <iomanip>
00007 #include <cmath>
00008 #include<vector>
00009 //
00010 #include "SimG4Core/Notification/interface/BeginOfEvent.h"
00011 #include "SimG4Core/Notification/interface/EndOfEvent.h"
00012 #include "SimG4Core/Notification/interface/TrackWithHistory.h"
00013 #include "SimG4Core/Notification/interface/TrackInformation.h"
00014  
00015 #include "FWCore/Framework/interface/Event.h"
00016 #include "FWCore/Framework/interface/EventSetup.h"
00017 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00018 // to retreive hits
00019 #include "SimG4CMS/FP420/interface//FP420NumberingScheme.h"
00020 //#include "SimG4CMS/Calo/interface/CaloG4Hit.h"
00021 //#include "SimG4CMS/Calo/interface/CaloG4HitCollection.h"
00022 #include "SimG4CMS/FP420/interface//FP420G4HitCollection.h"
00023 //#include "SimG4CMS/FP420/interface/FP420G4Hit.h"
00024 #include "SimG4CMS/FP420/interface/FP420Test.h"
00025 
00026 //#include "Utilities/GenUtil/interface/CMSexception.h"
00027 //#include "Utilities/UI/interface/SimpleConfigurable.h"
00028 
00029 // G4 stuff
00030 #include "G4SDManager.hh"
00031 #include "G4Step.hh"
00032 #include "G4Track.hh"
00033 #include "G4VProcess.hh"
00034 #include "G4HCofThisEvent.hh"
00035 #include "G4UserEventAction.hh"
00036 #include "G4TransportationManager.hh"
00037 #include "G4ProcessManager.hh"
00038 //#include "G4EventManager.hh"
00039 
00040 #include "CLHEP/Units/GlobalSystemOfUnits.h"
00041 #include "CLHEP/Units/GlobalPhysicalConstants.h"
00042 #include <stdio.h>
00043 //#include <gsl/gsl_fit.h>
00044 
00045 
00046 
00047 //================================================================
00048 // Root stuff
00049 
00050 // Include the standard header <cassert> to effectively include
00051 // the standard header <assert.h> within the std namespace.
00052 #include <cassert>
00053 
00054 using namespace edm;
00055 using namespace std;
00056 //================================================================
00057 
00058 //UserVerbosity FP420Test::std::cout("FP420Test","info","FP420Test");
00059 
00060 enum ntfp420_elements {
00061   ntfp420_evt
00062 };
00063 //================================================================
00064 FP420Test::FP420Test(const edm::ParameterSet &p){
00065   //constructor
00066   edm::ParameterSet m_Anal = p.getParameter<edm::ParameterSet>("FP420Test");
00067     verbosity    = m_Anal.getParameter<int>("Verbosity");
00068   //verbosity    = 1;
00069 
00070     fDataLabel  = m_Anal.getParameter<std::string>("FDataLabel");
00071     fOutputFile = m_Anal.getParameter<std::string>("FOutputFile");
00072     fRecreateFile = m_Anal.getParameter<std::string>("FRecreateFile");
00073    
00074   if (verbosity > 0) {
00075    std::cout<<"============================================================================"<<std::endl;
00076    std::cout << "FP420Testconstructor :: Initialized as observer"<< std::endl;
00077   }
00078   // Initialization:
00079 
00080   theFP420NumberingScheme = new FP420NumberingScheme();
00081   pn0 = 6;
00082   sn0 = 3;
00083   rn00 = 7;
00084 
00085   z420          = 420000.0; // mm
00086   zD2           = 4000.0;  // mm
00087   zD3           = 8000.0;  // mm
00088   //
00089   zBlade = 5.00;
00090   gapBlade = 1.6;
00091   double gapSupplane = 1.6;
00092   ZSiPlane=2*zBlade+gapBlade+gapSupplane;
00093   
00094   double ZKapton = 0.1;
00095   ZSiStep=ZSiPlane+ZKapton;
00096   
00097   double ZBoundDet = 0.020;
00098   double ZSiElectr = 0.250;
00099   double ZCeramDet = 0.500;
00100   //
00101   ZSiDet = 0.250;
00102   ZGapLDet= zBlade/2-(ZSiDet+ZSiElectr+ZBoundDet+ZCeramDet/2);
00103   //
00104   //  ZSiStation = 5*(2*(5.+1.6)+0.1)+2*6.+1.0 =  79.5  
00105   double ZSiStation = (pn0-1)*(2*(zBlade+gapBlade)+ZKapton)+2*6.+0.0;   // =  78.5  
00106   // 11.=e1, 12.=e2 in zzzrectangle.xml
00107   double eee1=11.;
00108   double eee2=12.;
00109   
00110   zinibeg = (eee1-eee2)/2.;
00111   
00112   z1 = zinibeg + (ZSiStation+10.)/2 + z420; // z1 - right after 1st Station
00113   z2 = z1+zD2;                       //z2 - right after middle Station
00114   z3 = z1+zD3;                       //z3 - right after last   Station
00115   z4 = z1+2*zD3;
00116   //==================================
00117 
00118  fp420eventntuple = new TNtuple("NTfp420event","NTfp420event","evt");
00119 
00120   whichevent = 0;
00121 
00122   //   fDataLabel      = "defaultData";
00123   //       fOutputFile     = "TheAnlysis.root";
00124   //       fRecreateFile   = "RECREATE";
00125 
00126         TheHistManager = new Fp420AnalysisHistManager(fDataLabel);
00127 
00128   if (verbosity > 0) {
00129    std::cout << "FP420Test constructor :: Initialized Fp420AnalysisHistManager"<< std::endl;
00130   }
00131 }
00132 
00133 
00134 
00135 FP420Test::~FP420Test() {
00136   //  delete UserNtuples;
00137   delete theFP420NumberingScheme;
00138 
00139   TFile fp420OutputFile("newntfp420.root","RECREATE");
00140   std::cout << "FP420 output root file has been created";
00141   fp420eventntuple->Write();
00142   std::cout << ", written";
00143   fp420OutputFile.Close();
00144   std::cout << ", closed";
00145   delete fp420eventntuple;
00146   std::cout << ", and deleted" << std::endl;
00147 
00148         //------->while end
00149 
00150         // Write histograms to file
00151         TheHistManager->WriteToFile(fOutputFile,fRecreateFile);
00152   if (verbosity > 0) {
00153     std::cout << std::endl << "FP420Test Destructor  -------->  End of FP420Test : "
00154       << std::cout << std::endl; 
00155   }
00156 
00157   std::cout<<"FP420Test: End of process"<<std::endl;
00158 
00159 
00160 
00161 }
00162 
00163 //================================================================
00164 // Histoes:
00165 //-----------------------------------------------------------------------------
00166 
00167 Fp420AnalysisHistManager::Fp420AnalysisHistManager(TString managername)
00168 {
00169         // The Constructor
00170 
00171         fTypeTitle=managername;
00172         fHistArray = new TObjArray();      // Array to store histos
00173         fHistNamesArray = new TObjArray(); // Array to store histos's names
00174 
00175         TH1::AddDirectory(kFALSE);
00176         BookHistos();
00177 
00178         fHistArray->Compress();            // Removes empty space
00179         fHistNamesArray->Compress();
00180 
00181 //      StoreWeights();                    // Store the weights
00182 
00183 }
00184 //-----------------------------------------------------------------------------
00185 
00186 Fp420AnalysisHistManager::~Fp420AnalysisHistManager()
00187 {
00188         // The Destructor
00189 
00190         if(fHistArray){
00191                 fHistArray->Delete();
00192                 delete fHistArray;
00193         }
00194 
00195         if(fHistNamesArray){
00196                 fHistNamesArray->Delete();
00197                 delete fHistNamesArray;
00198         }
00199 }
00200 //-----------------------------------------------------------------------------
00201 
00202 void Fp420AnalysisHistManager::BookHistos()
00203 {
00204   // at Start: (mm)
00205     HistInit("PrimaryEta", "Primary Eta",   100,   9., 12. );
00206     HistInit("PrimaryPhigrad", "Primary Phigrad",   100,   0.,360. );
00207     HistInit("PrimaryTh", "Primary Th",   100,   0.,180. );
00208     HistInit("PrimaryLastpoZ", "Primary Lastpo Z",   100, -200.,430000. );
00209     HistInit("PrimaryLastpoX", "Primary Lastpo X Z<z4",   100, -30., 30. );
00210     HistInit("PrimaryLastpoY", "Primary Lastpo Y Z<z4",   100, -30., 30. );
00211     HistInit("XLastpoNumofpart", "Primary Lastpo X n>10",   100, -30., 30. );
00212     HistInit("YLastpoNumofpart", "Primary Lastpo Y n>10",   100, -30., 30. );
00213     HistInit("VtxX", "Vtx X",   100, -50., 50. );
00214     HistInit("VtxY", "Vtx Y",   100, -50., 50. );
00215     HistInit("VtxZ", "Vtx Z",   100, -200.,430000. );
00216         // Book the histograms and add them to the array
00217     HistInit("SumEDep", "This is sum Energy deposited",   100,   -1,   199.);
00218     HistInit("TrackL", "This is TrackL",   100,   0.,   12000.);
00219     HistInit("zHits", "z Hits all events",                100, 400000.,430000. );
00220     HistInit("zHitsnoMI", "z Hits no MI",                 100, 400000.,430000. );
00221     HistInit("zHitsTrLoLe", "z Hits TrLength bigger 8300",100, 400000.,430000. );
00222     HistInit("NumberOfHits", "NumberOfHits",100, 0.,300. );
00223 }
00224 
00225 //-----------------------------------------------------------------------------
00226 
00227 void Fp420AnalysisHistManager::WriteToFile(TString fOutputFile,TString fRecreateFile)
00228 {
00229 
00230         //Write to file = fOutputFile
00231 
00232         std::cout <<"================================================================"<<std::endl;
00233         std::cout <<" Write this Analysis to File "<<fOutputFile<<std::endl;
00234         std::cout <<"================================================================"<<std::endl;
00235 
00236         TFile* file = new TFile(fOutputFile, fRecreateFile);
00237 
00238         fHistArray->Write();
00239         file->Close();
00240 }
00241 //-----------------------------------------------------------------------------
00242 
00243 void Fp420AnalysisHistManager::HistInit(const char* name, const char* title, Int_t nbinsx, Axis_t xlow, Axis_t xup)
00244 {
00245         // Add histograms and histograms names to the array
00246 
00247         char* newtitle = new char[strlen(title)+strlen(fTypeTitle)+5];
00248         strcpy(newtitle,title);
00249         strcat(newtitle," (");
00250         strcat(newtitle,fTypeTitle);
00251         strcat(newtitle,") ");
00252         fHistArray->AddLast((new TH1F(name, newtitle, nbinsx, xlow, xup)));
00253         fHistNamesArray->AddLast(new TObjString(name));
00254 
00255 }
00256 //-----------------------------------------------------------------------------
00257 
00258 void Fp420AnalysisHistManager::HistInit(const char* name, const char* title, Int_t nbinsx, Axis_t xlow, Axis_t xup, Int_t nbinsy, Axis_t ylow, Axis_t yup)
00259 {
00260         // Add histograms and histograms names to the array
00261 
00262         char* newtitle = new char[strlen(title)+strlen(fTypeTitle)+5];
00263         strcpy(newtitle,title);
00264         strcat(newtitle," (");
00265         strcat(newtitle,fTypeTitle);
00266         strcat(newtitle,") ");
00267         fHistArray->AddLast((new TH2F(name, newtitle, nbinsx, xlow, xup, nbinsy, ylow, yup)));
00268         fHistNamesArray->AddLast(new TObjString(name));
00269 
00270 }
00271 //-----------------------------------------------------------------------------
00272 
00273 TH1F* Fp420AnalysisHistManager::GetHisto(Int_t Number)
00274 {
00275         // Get a histogram from the array with index = Number
00276 
00277         if (Number <= fHistArray->GetLast()  && fHistArray->At(Number) != (TObject*)0){
00278 
00279                 return (TH1F*)(fHistArray->At(Number));
00280 
00281         }else{
00282 
00283                 std::cout << "!!!!!!!!!!!!!!!!!!GetHisto!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl;
00284                 std::cout << " WARNING ERROR - HIST ID INCORRECT (TOO HIGH) - " << Number << std::endl;
00285                 std::cout << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl;
00286 
00287                 return (TH1F*)(fHistArray->At(0));
00288         }
00289 }
00290 //-----------------------------------------------------------------------------
00291 
00292 TH2F* Fp420AnalysisHistManager::GetHisto2(Int_t Number)
00293 {
00294         // Get a histogram from the array with index = Number
00295 
00296         if (Number <= fHistArray->GetLast()  && fHistArray->At(Number) != (TObject*)0){
00297 
00298                 return (TH2F*)(fHistArray->At(Number));
00299 
00300         }else{
00301 
00302                 std::cout << "!!!!!!!!!!!!!!!!GetHisto2!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl;
00303                 std::cout << " WARNING ERROR - HIST ID INCORRECT (TOO HIGH) - " << Number << std::endl;
00304                 std::cout << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl;
00305 
00306                 return (TH2F*)(fHistArray->At(0));
00307         }
00308 }
00309 //-----------------------------------------------------------------------------
00310 
00311 TH1F* Fp420AnalysisHistManager::GetHisto(const TObjString histname)
00312 {
00313         // Get a histogram from the array with name = histname
00314 
00315         Int_t index = fHistNamesArray->IndexOf(&histname);
00316         return GetHisto(index);
00317 }
00318 //-----------------------------------------------------------------------------
00319 
00320 TH2F* Fp420AnalysisHistManager::GetHisto2(const TObjString histname)
00321 {
00322         // Get a histogram from the array with name = histname
00323 
00324         Int_t index = fHistNamesArray->IndexOf(&histname);
00325         return GetHisto2(index);
00326 }
00327 //-----------------------------------------------------------------------------
00328 
00329 void Fp420AnalysisHistManager::StoreWeights()
00330 {
00331         // Add structure to each histogram to store the weights
00332 
00333         for(int i = 0; i < fHistArray->GetEntries(); i++){
00334                 ((TH1F*)(fHistArray->At(i)))->Sumw2();
00335         }
00336 }
00337 
00338 // Histoes end :
00339 
00340 //================================================================
00341 
00342 /*
00343 ObserveBeginOfRun::ObserveBeginOfRun() {  }
00344 
00345 void ObserveBeginOfRun::update(const BeginOfRun *) {
00346     std::cout <<" BeginOfRun " << std::endl;
00347   // const G4Run * r = (*run)(); 
00348   // recover G4 pointer if wanted
00349   // user monitoring code... 
00350 }
00351 
00352 ObserveEndOfRun::ObserveEndOfRun() {  }
00353 
00354 void ObserveEndOfRun::update(const EndOfRun *) {
00355   // const G4Run * r = (*run)();
00356   // recover G4 pointer if wanted
00357   // user monitoring code...
00358 }
00359 
00360 ObserveBeginOfEvent::ObserveBeginOfEvent() {  }
00361 
00362 void ObserveBeginOfEvent::update(const BeginOfEvent * evt) {
00363     std::cout <<" BeginOfEvent " << std::endl;
00364  // const G4Event * e = (*evt)();
00365  // recover G4 pointer if wanted
00366   // user monitoring code...
00367 }
00368 
00369 ObserveEndOfEvent::ObserveEndOfEvent() {  }
00370 
00371 void ObserveEndOfEvent::update(const EndOfEvent *) {
00372   // const G4Event * e = (*evt)(); 
00373   // recover G4 pointer if wanted
00374   // user monitoring code... 
00375 
00376 //    std::cout << "ObserveEndOfEvent:  start   " << std::endl;
00377 
00378 }
00379 
00380 ObserveBeginOfTrack::ObserveBeginOfTrack() {  }
00381 
00382 void ObserveBeginOfTrack::update(const BeginOfTrack * trk) {
00383   // const G4Track * t = (*trk)(); 
00384   // recover G4 pointer if wanted
00385   // user monitoring code...
00386 }
00387 
00388 ObserveEndOfTrack::ObserveEndOfTrack() {  }
00389 
00390 void ObserveEndOfTrack::update(const EndOfTrack *) {
00391   // const G4Track * t = (*trk)(); 
00392   // recover G4 pointer if wanted
00393   // user monitoring code... 
00394 }
00395 
00396 ObserveStep::ObserveStep() {  }
00397 
00398 void ObserveStep::update(const G4Step *) {
00399   // user monitoring code... 
00400 }
00401 */
00402 // using several observers
00403 
00404 //==================================================================== per JOB
00405 void FP420Test::update(const BeginOfJob * job) {
00406   //job
00407   std::cout<<"FP420Test:beggining of job"<<std::endl;;
00408 }
00409 
00410 
00411 //==================================================================== per RUN
00412 void FP420Test::update(const BeginOfRun * run) {
00413   //run
00414 
00415  std::cout << std::endl << "FP420Test:: Begining of Run"<< std::endl; 
00416 }
00417 
00418 
00419 void FP420Test::update(const EndOfRun * run) {;}
00420 
00421 
00422 
00423 //=================================================================== per EVENT
00424 void FP420Test::update(const BeginOfEvent * evt) {
00425   iev = (*evt)()->GetEventID();
00426   if (verbosity > 0) {
00427     std::cout <<"FP420Test:update Event number = " << iev << std::endl;
00428   }
00429   whichevent++;
00430 }
00431 
00432 //=================================================================== per Track
00433 void FP420Test::update(const BeginOfTrack * trk) {
00434   itrk = (*trk)()->GetTrackID();
00435   if (verbosity > 1) {
00436     std::cout <<"FP420Test:update BeginOfTrack number = " << itrk << std::endl;
00437   }
00438   if(itrk == 1) {
00439      SumEnerDeposit = 0.;
00440      numofpart = 0;
00441      SumStepl = 0.;
00442      SumStepc = 0.;
00443      tracklength0 = 0.;
00444   }
00445 }
00446 
00447 
00448 
00449 //=================================================================== per EndOfTrack
00450 void FP420Test::update(const EndOfTrack * trk) {
00451   itrk = (*trk)()->GetTrackID();
00452   if (verbosity > 1) {
00453     std::cout <<"FP420Test:update EndOfTrack number = " << itrk << std::endl;
00454   }
00455   if(itrk == 1) {
00456   G4double tracklength  = (*trk)()->GetTrackLength();    // Accumulated track length
00457 
00458        TheHistManager->GetHisto("SumEDep")->Fill(SumEnerDeposit);
00459        TheHistManager->GetHisto("TrackL")->Fill(tracklength);
00460 
00461       // direction !!!
00462   G4ThreeVector   vert_mom  = (*trk)()->GetVertexMomentumDirection();
00463   G4ThreeVector   vert_pos  = (*trk)()->GetVertexPosition(); // vertex ,where this track was created
00464   
00465 //    float eta = 0.5 * log( (1.+vert_mom.z()) / (1.-vert_mom.z()) );
00466     float phi = atan2(vert_mom.y(),vert_mom.x());
00467     if (phi < 0.) phi += twopi;
00468 //    float phigrad = phi*180./pi;
00469 
00470 //      float XV = vert_pos.x(); // mm
00471 //      float YV = vert_pos.y(); // mm
00472       //UserNtuples->fillg543(phigrad,1.);
00473       //UserNtuples->fillp203(phigrad,SumStepl,1.);
00474       //UserNtuples->fillg544(XV,1.);
00475       //UserNtuples->fillp201(XV,SumStepl,1.);
00476       //UserNtuples->fillg545(YV,1.);
00477       //UserNtuples->fillp202(YV,SumStepl,1.);
00478 
00479     //UserNtuples->fillg524(eta,1.);
00480   
00481       //UserNtuples->fillg534(SumStepl,1.);
00482       //UserNtuples->fillg535(eta,SumStepl);
00483       //UserNtuples->fillp207(eta,SumStepl,1.);
00484       //UserNtuples->filld217(eta,SumStepl,1.);
00485       //UserNtuples->filld220(phigrad,SumStepl,1.);
00486       //UserNtuples->filld221(XV,SumStepl,1.);
00487       //UserNtuples->filld222(YV,SumStepl,1.);
00488       //UserNtuples->fillg537(SumStepc,1.);
00489       //UserNtuples->fillg84(SumStepl,1.);
00490 
00491 // MI = (multiple interactions):
00492        if(tracklength < z4) {
00493 //        //UserNtuples->fillp208(eta,tracklength,1.);
00494         //UserNtuples->filld218(eta,tracklength,1.);
00495         //UserNtuples->fillg538(SumStepc,1.);
00496         //UserNtuples->fillg85(SumStepl,1.);
00497        }
00498 
00499         // last step information
00500         const G4Step* aStep = (*trk)()->GetStep();
00501         //   G4int csn = (*trk)()->GetCurrentStepNumber();
00502         //   G4double sl = (*trk)()->GetStepLength();
00503          // preStep
00504          G4StepPoint*      preStepPoint = aStep->GetPreStepPoint(); 
00505          lastpo   = preStepPoint->GetPosition();        
00506 
00507          // Analysis:
00508          if(lastpo.z()<z1 && lastpo.perp()< 100.) {
00509              //UserNtuples->fillg525(eta,1.);
00510              //UserNtuples->fillg546(XV,1.);
00511              //UserNtuples->fillg551(YV,1.);
00512              //UserNtuples->fillg556(phigrad,1.);
00513          }
00514          if((lastpo.z()>z1 && lastpo.z()<z2) && lastpo.perp()< 100.) {
00515              //UserNtuples->fillg526(eta,1.);
00516              //UserNtuples->fillg547(XV,1.);
00517              //UserNtuples->fillg552(YV,1.);
00518              //UserNtuples->fillg557(phigrad,1.);
00519          }
00520          if(lastpo.z()<z2 && lastpo.perp()< 100.) {
00521              //UserNtuples->fillg527(eta,1.);
00522              //UserNtuples->fillg548(XV,1.);
00523              //UserNtuples->fillg553(YV,1.);
00524               //UserNtuples->fillg558(phigrad,1.);
00525          //UserNtuples->fillg521(lastpo.x(),1.);
00526          //UserNtuples->fillg522(lastpo.y(),1.);
00527          //UserNtuples->fillg523(lastpo.z(),1.);
00528         }
00529          if(lastpo.z()<z3 && lastpo.perp()< 100.) {
00530              //UserNtuples->fillg528(eta,1.);
00531              //UserNtuples->fillg549(XV,1.);
00532              //UserNtuples->fillg554(YV,1.);
00533              //UserNtuples->fillg559(phigrad,1.);
00534          }
00535          if(lastpo.z()<z4 && lastpo.perp()< 100.) {
00536              //UserNtuples->fillg529(eta,1.);
00537              //UserNtuples->fillg550(XV,1.);
00538              //UserNtuples->fillg555(YV,1.);
00539              //UserNtuples->fillg560(phigrad,1.);
00540          //UserNtuples->fillg531(lastpo.x(),1.);
00541          //UserNtuples->fillg532(lastpo.y(),1.);
00542          //UserNtuples->fillg533(lastpo.z(),1.);
00543          }
00544 
00545          /*
00546       float th_tr     = lastpo.theta();
00547       float eta_tr    = -log(tan(th_tr/2));
00548       float phi_tr    = lastpo.phi();
00549       if (phi_tr < 0.) phi_tr += twopi;
00550 */
00551 
00552 
00553   }
00554 
00555 }
00556 
00557 // ====================================================
00558 
00559 //=================================================================== each STEP
00560 void FP420Test::update(const G4Step * aStep) {
00561 // ==========================================================================
00562   
00563   if (verbosity > 2) {
00564     G4int stepnumber  = aStep->GetTrack()->GetCurrentStepNumber();
00565     std::cout <<"FP420Test:update Step number = " << stepnumber << std::endl;
00566   }
00567   // track on aStep:                                                                                         !
00568   G4Track*     theTrack     = aStep->GetTrack();   
00569   TrackInformation* trkInfo = dynamic_cast<TrackInformation*> (theTrack->GetUserInformation());
00570    if (trkInfo == 0) {
00571      std::cout << "FP420Test on aStep: No trk info !!!! abort " << std::endl;
00572      //     throw Genexception("FP420Test:FP420Test on aStep: cannot get trkInfo");
00573    } 
00574   G4int         id             = theTrack->GetTrackID();
00575   G4String       particleType   = theTrack->GetDefinition()->GetParticleName();   //   !!!
00576   G4int         parentID       = theTrack->GetParentID();   //   !!!
00577   G4TrackStatus   trackstatus    = theTrack->GetTrackStatus();   //   !!!
00578   G4double       tracklength    = theTrack->GetTrackLength();    // Accumulated track length
00579   G4ThreeVector   trackmom       = theTrack->GetMomentum();
00580   G4double       entot          = theTrack->GetTotalEnergy();   //   !!! deposited on step
00581   G4int         curstepnumber  = theTrack->GetCurrentStepNumber();
00582   G4ThreeVector   vert_pos       = theTrack->GetVertexPosition(); // vertex ,where this track was created
00583   G4ThreeVector   vert_mom       = theTrack->GetVertexMomentumDirection();
00584   
00585 //   double costheta =vert_mom.z()/sqrt(vert_mom.x()*vert_mom.x()+vert_mom.y()*vert_mom.y()+vert_mom.z()*vert_mom.z());
00586 //   double theta = acos(min(max(costheta,double(-1.)),double(1.)));
00587 //  float eta = -log(tan(theta/2));
00588 //   double phi = -1000.;
00589 //   if (vert_mom.x() != 0) phi = atan2(vert_mom.y(),vert_mom.x()); 
00590 //   if (phi < 0.) phi += twopi;
00591 //   double phigrad = phi*360./twopi;  
00592 
00593 //G4double       trackvel       = theTrack->GetVelocity();
00594 
00595 //std::cout << " trackstatus= " << trackstatus << " entot= " << entot  << std::endl;
00596 
00597 
00598   // step points:                                                                                         !
00599   G4double        stepl         = aStep->GetStepLength();
00600   G4double        EnerDeposit   = aStep->GetTotalEnergyDeposit();
00601   // pointers:                                                                                         !
00602 //G4VPhysicalVolume*  physvol       = theTrack->GetVolume();
00603 //G4VPhysicalVolume*  nextphysvol   = theTrack->GetNextVolume();
00604 //G4Material*       materialtr     = theTrack->GetMaterial();
00605 //G4Material*       nextmaterialtr = theTrack->GetNextMaterial();
00606 
00607   // preStep
00608   G4StepPoint*      preStepPoint = aStep->GetPreStepPoint(); 
00609   G4ThreeVector     preposition   = preStepPoint->GetPosition();        
00610   G4ThreeVector     prelocalpoint = theTrack->GetTouchable()->GetHistory()->
00611                                            GetTopTransform().TransformPoint(preposition);
00612   G4VPhysicalVolume* currentPV     = preStepPoint->GetPhysicalVolume();
00613   G4String         prename       = currentPV->GetName();
00614 
00615 const G4VTouchable*  pre_touch    = preStepPoint->GetTouchable();
00616      int          pre_levels   = detLevels(pre_touch);
00617 
00618 //     G4String      pre_name1    = detName(pre_touch, pre_levels, 1);
00619 //     G4String      pre_name2    = detName(pre_touch, pre_levels, 2);
00620 //     G4String      pre_name3    = detName(pre_touch, pre_levels, 3);
00621         G4String name1[20]; int copyno1[20];
00622       if (pre_levels > 0) {
00623         detectorLevel(pre_touch, pre_levels, copyno1, name1);
00624       }
00625 
00626 //  G4LogicalVolume*   lv            = currentPV->GetLogicalVolume();
00627 //  G4Material*       mat           = lv->GetMaterial();
00628 //  std::string prenameVolume;
00629 //  prenameVolume.assign(prename,0,20);
00630 
00631 //   G4double         prebeta          = preStepPoint->GetBeta();
00632 //   G4double         precharge        = preStepPoint->GetCharge();
00633 //  G4double          prerad           = mat->GetRadlen();
00634 
00635 //  std::cout << " EneryDeposited = " << EnerDeposit << std::endl;
00636 //  std::cout << " prevolume = "      << prename << std::endl;
00638 //  std::cout << " preposition = "    << preposition << std::endl; 
00639      /*
00640   // postStep
00641   G4StepPoint*      postStepPoint  = aStep->GetPostStepPoint();   
00642   G4ThreeVector     posposition    = postStepPoint->GetPosition();      
00643   G4ThreeVector     poslocalpoint  = theTrack->GetTouchable()->GetHistory()->
00644                                            GetTopTransform().TransformPoint(posposition);
00645   G4VPhysicalVolume* poscurrentPV      = postStepPoint->GetPhysicalVolume();
00646   G4String         posname        = poscurrentPV->GetName();
00647 //  G4LogicalVolume*   poslv             = poscurrentPV->GetLogicalVolume();
00648 //  G4Material*       posmat            = poslv->GetMaterial();
00649 //  std::string posnameVolume;
00650 //  posnameVolume.assign(posname,0,20);
00651 
00652 #ifdef ddebug
00653      std::cout << "============posStep: information:============" << std::endl;
00654      std::cout << " posposition = "    << posposition
00655           << " poslocalpoint = "  << poslocalpoint
00656           << " posvolume = "      << posname
00657        //          << " posnameVolume = "  << posnameVolume 
00658           << std::endl;
00659 
00660      std::cout << " ==========================================" << std::endl;
00661 #endif
00662 
00663 
00664 */
00665 
00666 //      //
00667 //      //
00668 
00669       // 24.01.2006:
00670 // tr     :   id    parentID   trackstatus   tracklength   curstepnumber  entot  vert_pos  
00671 // st     :   stepl EnerDeposit 
00672 // prestep:   preposition   prevolume = SBSTm SIDETL SIDETR       name= SISTATION  copy= 1,2,3    name= SIPLANE  copy= 1..5..10
00673 
00674 // gen_track:
00675 //  id=1    parentID=1   trackstatus=0,2   tracklength(accumulated)  curstepnumber   entot  vert_pos 
00676  if ( id == 1 ) {
00677 
00678          // on 1-st step:
00679          if (curstepnumber == 1 ) {
00680            entot0 = entot;
00681            //UserNtuples->fillg519(entot0,1.);
00682 
00683          }
00684 
00685       // on every step:
00686 
00687          // for Copper:
00688          if(prename == "SBST" ) {
00689            SumStepc += stepl;
00690            // =========
00691          }
00692          // for ststeel:
00693 //       if(prename == "SBSTs") {
00694          if(prename == "SBSTs" ) {
00695            SumStepl += stepl;
00696            // =========
00697          }
00698            // =========
00699            // =========
00700 
00701          // exclude last track point if it is in SD (MI was started their)
00702   if (trackstatus != 2 ) {
00703          // for SD:   Si Det.:   SISTATION:SIPLANE:(SIDETL+BOUNDDET        +SIDETR + CERAMDET)
00704     if(prename == "SIDETL" || prename == "SIDETR" ) {
00705            if(prename == "SIDETL") {
00706              //UserNtuples->fillg569(EnerDeposit,1.);
00707            }
00708            if(prename == "SIDETR") {
00709              //UserNtuples->fillg570(EnerDeposit,1.);
00710            }
00711 
00712 //         double numbcont = 10.*(copyno1[2]-1)+copyno1[3];
00713 
00714            // =========
00715 //         double   xx    = preposition.x();
00716 //         double   yy    = preposition.y();
00717 //         double   zz    = preposition.z();
00718            // =========
00719              //UserNtuples->fillg580(theta,1.);
00720              //UserNtuples->fillg07(phigrad,1.);
00721 //           double xPrimAtZhit = vert_pos.x() + (zz-vert_pos.z())*tan(theta)*cos(phi);
00722 //           double yPrimAtZhit = vert_pos.y() + (zz-vert_pos.z())*tan(theta)*sin(phi);
00723            // =========
00724 //         double  dx = xPrimAtZhit - xx;
00725 //         double  dy = yPrimAtZhit - yy;
00726            // =========
00727 
00728 //         //UserNtuples->fillp212(numbcont,dx,1.);
00729 //         //UserNtuples->fillp213(numbcont,dy,1.);
00730            // =========
00731 
00732            // last step of current SD Volume:
00733        G4String posname = aStep->GetPostStepPoint()->GetPhysicalVolume()->GetName();
00734        if((prename == "SIDETL" && posname != "SIDETL") || (prename == "SIDETR" && posname != "SIDETR")) {
00735            if(name1[2] == "SISTATION" ) {
00736              //UserNtuples->fillg539(copyno1[2],1.);
00737            }
00738            if(name1[3] == "SIPLANE" ) {
00739              //UserNtuples->fillg540(copyno1[3],1.);
00740            }
00741 
00742            if(prename == "SIDETL") {
00743              //UserNtuples->fillg541(EnerDeposit,1.);
00744              //UserNtuples->fillg561(numbcont,1.);
00745              if(copyno1[2]<2) {
00746                  //UserNtuples->fillg571(dx,1.);
00747              }
00748              else if(copyno1[2]<3) {
00749                //UserNtuples->fillg563(dx,1.);
00750                if(copyno1[3]<2) {
00751                }
00752                else if(copyno1[3]<3) {
00753                  //UserNtuples->fillg572(dx,1.);
00754                }
00755                else if(copyno1[3]<4) {
00756                  //UserNtuples->fillg573(dx,1.);
00757                }
00758                else if(copyno1[3]<5) {
00759                  //UserNtuples->fillg574(dx,1.);
00760                }
00761                else if(copyno1[3]<6) {
00762                  //UserNtuples->fillg575(dx,1.);
00763                }
00764                else if(copyno1[3]<7) {
00765                  //UserNtuples->fillg576(dx,1.);
00766                }
00767                else if(copyno1[3]<8) {
00768                  //UserNtuples->fillg577(dx,1.);
00769                }
00770                else if(copyno1[3]<9) {
00771                  //UserNtuples->fillg578(dx,1.);
00772                }
00773                else if(copyno1[3]<10) {
00774                  //UserNtuples->fillg579(dx,1.);
00775                }
00776              }
00777              else if(copyno1[2]<4) {
00778                //UserNtuples->fillg565(dx,1.);
00779              }
00780              else if(copyno1[2]<5) {
00781                //UserNtuples->fillg567(dx,1.);
00782              }
00783            }
00784            if(prename == "SIDETR") {
00785              //UserNtuples->fillg542(EnerDeposit,1.);
00786              //UserNtuples->fillg562(numbcont,1.);
00787              if(copyno1[2]<2) {
00788                  //UserNtuples->fillg581(dy,1.);
00789              }
00790              else if(copyno1[2]<3) {
00791                //UserNtuples->fillg564(dy,1.);
00792                if(copyno1[3]<2) {
00793                }
00794                else if(copyno1[3]<3) {
00795                  //UserNtuples->fillg582(dy,1.);
00796                }
00797                else if(copyno1[3]<4) {
00798                  //UserNtuples->fillg583(dy,1.);
00799                }
00800                else if(copyno1[3]<5) {
00801                  //UserNtuples->fillg584(dy,1.);
00802                }
00803                else if(copyno1[3]<6) {
00804                  //UserNtuples->fillg585(dy,1.);
00805                }
00806                else if(copyno1[3]<7) {
00807                  //UserNtuples->fillg586(dy,1.);
00808                }
00809                else if(copyno1[3]<8) {
00810                  //UserNtuples->fillg587(dy,1.);
00811                }
00812                else if(copyno1[3]<9) {
00813                  //UserNtuples->fillg588(dy,1.);
00814                }
00815                else if(copyno1[3]<10) {
00816                  //UserNtuples->fillg589(dy,1.);
00817                }
00818              }
00819              else if(copyno1[2]<4) {
00820                //UserNtuples->fillg566(dy,1.);
00821              }
00822              else if(copyno1[2]<5) {
00823                //UserNtuples->fillg568(dy,1.);
00824              }
00825            }
00826 
00827        }
00828     }
00829     // end of prenames SIDETL // SIDETR
00830   }
00831   // end of trackstatus != 2
00832 
00833 
00834       // deposition of energy on steps along primary track
00835       //UserNtuples->fillg500(EnerDeposit,1.);
00836       // collect sum deposited energy on all steps along primary track
00837       SumEnerDeposit += EnerDeposit;
00838       // position of step for primary track:
00839       //UserNtuples->fillg501(preposition.x(),1.);
00840       //UserNtuples->fillg502(preposition.y(),1.);
00841       //UserNtuples->fillg503(preposition.z(),1.);
00842       //UserNtuples->fillg504(preposition.x(),EnerDeposit);
00843       //UserNtuples->fillg505(preposition.y(),EnerDeposit);
00844       //UserNtuples->fillg506(preposition.z(),EnerDeposit);
00845       // 2D step coordinates weighted by energy deposited on step
00846 //      //UserNtuples->fillp201(preposition.x(),preposition.y(),EnerDeposit);
00847 //      //UserNtuples->fillp202(preposition.x(),preposition.z(),EnerDeposit);
00848 //      //UserNtuples->fillp203(preposition.y(),preposition.z(),EnerDeposit);
00849       //UserNtuples->filld204(preposition.x(),preposition.y(),EnerDeposit);
00850       //UserNtuples->filld205(preposition.x(),preposition.z(),EnerDeposit);
00851       //UserNtuples->filld206(preposition.y(),preposition.z(),EnerDeposit);
00852       //UserNtuples->filld223(preposition.y(),preposition.z(),EnerDeposit);
00853       // last step of primary track
00854      if (trackstatus == 2 ) {
00855       // primary track length 
00856        //      //UserNtuples->fillg508(tracklength,1.);
00857           tracklength0 = tracklength;
00858       // how many steps primary track consist 
00859       //UserNtuples->fillg509(curstepnumber,1.);
00860       // tot. energy of primary track at the end of trajectory(before disappeare)
00861       //UserNtuples->fillg510((entot0-entot),1.);
00862       //UserNtuples->fillg520((entot0-entot),1.);
00863      }
00864  }
00865  // end of primary track !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
00866 
00867 
00868     if (parentID == 1 && curstepnumber == 1) {
00869       // particles deposit their energy along primary track
00870       numofpart += 1;
00871       // energy of radiated particle
00872       //UserNtuples->fillg511(entot,1.);
00873       // coordinates  of radiated particle
00874       //UserNtuples->fillg512(vert_pos.x(),1.);
00875       //UserNtuples->fillg513(vert_pos.y(),1.);
00876       //UserNtuples->fillg514(vert_pos.z(),1.);
00877       //UserNtuples->fillg515(vert_pos.x(),entot);
00878       //UserNtuples->fillg516(vert_pos.y(),entot);
00879       //UserNtuples->fillg517(vert_pos.z(),entot);
00880 
00881       //UserNtuples->filld214(vert_pos.x(),vert_pos.y(),1.);
00882       //UserNtuples->filld215(vert_pos.x(),vert_pos.z(),1.);
00883       //UserNtuples->filld216(vert_pos.y(),vert_pos.z(),1.);
00884       //UserNtuples->filld219(vert_pos.y(),vert_pos.z(),1.);
00885       //UserNtuples->filld224(vert_pos.y(),vert_pos.z(),1.);
00886          if(prename == "SBST" ) {
00887            //UserNtuples->filld225(vert_pos.y(),vert_pos.z(),1.);
00888          }
00889          if(prename == "SBSTs" ) {
00890            //UserNtuples->filld226(vert_pos.y(),vert_pos.z(),1.);
00891          }
00892     }
00893 
00894 
00895 
00896   // ==========================================================================
00897 
00898 }
00899 // ==========================================================================
00900 // ==========================================================================
00901 int FP420Test::detLevels(const G4VTouchable* touch) const {
00902 
00903   //Return number of levels
00904   if (touch) 
00905     return ((touch->GetHistoryDepth())+1);
00906   else
00907     return 0;
00908 }
00909 // ==========================================================================
00910 
00911 G4String FP420Test::detName(const G4VTouchable* touch, int level,
00912                                     int currentlevel) const {
00913 
00914   //Go down to current level
00915   if (level > 0 && level >= currentlevel) {
00916     int ii = level - currentlevel; 
00917     return touch->GetVolume(ii)->GetName();
00918   } else {
00919     return "NotFound";
00920   }
00921 }
00922 
00923 void FP420Test::detectorLevel(const G4VTouchable* touch, int& level,
00924                                       int* copyno, G4String* name) const {
00925 
00926   //Get name and copy numbers
00927   if (level > 0) {
00928     for (int ii = 0; ii < level; ii++) {
00929       int i      = level - ii - 1;
00930       G4VPhysicalVolume* pv = touch->GetVolume(i);
00931       if (pv != 0) 
00932         name[ii] = pv->GetName();
00933       else
00934         name[ii] = "Unknown";
00935       copyno[ii] = touch->GetReplicaNumber(i);
00936     }
00937   }
00938 }
00939 // ==========================================================================
00940 
00941 //===================================================================   End Of Event
00942 void FP420Test::update(const EndOfEvent * evt) {
00943   // ==========================================================================
00944   
00945   if (verbosity > 1) {
00946   iev = (*evt)()->GetEventID();
00947     std::cout <<"FP420Test:update EndOfEvent = " << iev << std::endl;
00948   }
00949   // Fill-in ntuple
00950   fp420eventarray[ntfp420_evt] = (float)whichevent;
00951 
00952   //
00953   int trackID = 0;
00954   G4PrimaryParticle* thePrim=0;
00955 
00956 
00957   // prim.vertex:
00958   G4int nvertex = (*evt)()->GetNumberOfPrimaryVertex();
00959   if (nvertex !=1)
00960     std::cout << "FP420Test: My warning: NumberOfPrimaryVertex != 1  -->  = " << nvertex <<  std::endl;
00961 
00962     for (int i = 0 ; i<nvertex; i++) {
00963       G4PrimaryVertex* avertex = (*evt)()->GetPrimaryVertex(i);
00964       if (avertex == 0)
00965         std::cout << "FP420Test  End Of Event ERR: pointer to vertex = 0"
00966              << std::endl;
00967     G4int npart = avertex->GetNumberOfParticle();
00968     if (npart !=1)
00969       std::cout << "FP420Test: My warning: NumberOfPrimaryPart != 1  -->  = " << npart <<  std::endl;
00970     if (npart ==0)
00971       std::cout << "FP420Test End Of Event ERR: no NumberOfParticle" << std::endl;
00972 
00973     // find just primary track:                                                             track pointer: thePrim
00974     if (thePrim==0) thePrim=avertex->GetPrimary(trackID);
00975 
00976        if (thePrim!=0) {
00977          // primary vertex:
00978          G4double vx=0.,vy=0.,vz=0.;
00979          vx = avertex->GetX0();
00980          vy = avertex->GetY0();
00981          vz = avertex->GetZ0();
00982          //UserNtuples->fillh01(vx);
00983          //UserNtuples->fillh02(vy);
00984          //UserNtuples->fillh03(vz);
00985          TheHistManager->GetHisto("VtxX")->Fill(vx);
00986          TheHistManager->GetHisto("VtxY")->Fill(vy);
00987          TheHistManager->GetHisto("VtxZ")->Fill(vz);
00988        }
00989     }
00990   // prim.vertex loop end
00991 
00992 //=========================== thePrim != 0 ================================================================================
00993     if (thePrim != 0) {
00994 //      inline G4ParticleDefinition * GetG4code() const
00995 //      inline G4PrimaryParticle * GetNext() const
00996 //      inline G4PrimaryParticle * GetDaughter() const
00997 
00998 // prim.vertex
00999       //int ivert = 0 ;
01000       //G4PrimaryVertex* avertex = (*evt)()->GetPrimaryVertex(ivert);
01001       //G4double vx=0.,vy=0.,vz=0.;
01002       //vx = avertex->GetX0();
01003       //vy = avertex->GetY0();
01004       //vz = avertex->GetZ0();
01005 
01006   //
01007       // number of secondary particles deposited their energy along primary track
01008            //UserNtuples->fillg518(numofpart,1.);
01009          if(lastpo.z()<z4 && lastpo.perp()< 100.) {
01010            //UserNtuples->fillg536(numofpart,1.);
01011          }
01012   //
01013 
01014       // direction !!!
01015       G4ThreeVector   mom  = thePrim->GetMomentum();
01016   
01017     double phi = atan2(mom.y(),mom.x());
01018     if (phi < 0.) phi += twopi;
01019     double phigrad = phi*180./pi;
01020 
01021       double th     = mom.theta();
01022       double eta = -log(tan(th/2));
01023 // works OK:
01024 //      double  costheta =mom.z()/sqrt(mom.x()*mom.x()+mom.y()*mom.y()+mom.z()*mom.z());
01025       //      double theta = acos(min(max(costheta,double(-1.)),double(1.)));
01026 
01027       //UserNtuples->fillh04(eta);
01028       //UserNtuples->fillh05(phigrad);
01029       //UserNtuples->fillh06(th);
01030 
01031       //UserNtuples->fillg13(lastpo.x(),1.);
01032       //UserNtuples->fillg14(lastpo.y(),1.);
01033       //UserNtuples->fillg15(lastpo.z(),1.);
01034 
01035          TheHistManager->GetHisto("PrimaryEta")->Fill(eta);
01036          TheHistManager->GetHisto("PrimaryPhigrad")->Fill(phigrad);
01037          TheHistManager->GetHisto("PrimaryTh")->Fill(th*180./pi);
01038 
01039          TheHistManager->GetHisto("PrimaryLastpoZ")->Fill(lastpo.z());
01040          if(lastpo.z() <  z4  ) {
01041            TheHistManager->GetHisto("PrimaryLastpoX")->Fill(lastpo.x());
01042            TheHistManager->GetHisto("PrimaryLastpoY")->Fill(lastpo.y());
01043          }
01044          if(numofpart >  4  ) {
01045            TheHistManager->GetHisto("XLastpoNumofpart")->Fill(lastpo.x());
01046            TheHistManager->GetHisto("YLastpoNumofpart")->Fill(lastpo.y());
01047          }
01048 
01049   // ==========================================================================
01050 
01051   // hit map for FP420
01052   // ==================================
01053 
01054   map<int,float,less<int> > themap;
01055   map<int,float,less<int> > themap1;
01056 
01057   map<int,float,less<int> > themapxy;
01058   map<int,float,less<int> > themapz;
01059   // access to the G4 hit collections:  -----> this work OK:
01060 
01061 //  edm::LogInfo("FP420Test") << "1";
01062   G4HCofThisEvent* allHC = (*evt)()->GetHCofThisEvent();
01063 //  edm::LogInfo("FP420Test") << "2";
01064     if (verbosity > 0) {
01065       std::cout << "FP420Test:  accessed all HC" << std::endl;;
01066     }
01067   int CAFIid = G4SDManager::GetSDMpointer()->GetCollectionID("FP420SI");
01068  // edm::LogInfo("FP420Test") << "3";
01069  // std::cout << " CAFIid = " << CAFIid << std::endl;;
01070 
01071   FP420G4HitCollection* theCAFI = (FP420G4HitCollection*) allHC->GetHC(CAFIid);
01072   //  CaloG4HitCollection* theCAFI = (CaloG4HitCollection*) allHC->GetHC(CAFIid);
01073     if (verbosity > 0) {
01074       //std::cout << "FP420Test: theCAFI = " << theCAFI << std::endl;
01075       std::cout << "FP420Test: theCAFI->entries = " << theCAFI->entries() << std::endl;
01076     }
01077  // edm::LogInfo("FP420Test") << "theCAFI->entries="<< theCAFI->entries();
01078     TheHistManager->GetHisto("NumberOfHits")->Fill(theCAFI->entries());
01079 
01080   // access to the G4 hit collections ----> variant 2: give 0 hits
01081 //  FP420G4HitCollection *   theCAFI;
01082 //  theCAFI = new FP420G4HitCollection();
01083   // ==========================================================================
01084   //   Silicon Hit collection start
01085     //0) if particle goes into flat beam pipe below detector:
01086   int varia ;   // = 0 -all; =1 - MI; =2 - noMI
01087     varia = 0;
01088     //                      Select MI or noMI over all 3 stations
01089     // 1)MI:
01090   //     if particle goes through window into detector:
01091   // lastpoint of track in lateral dir. outside the detector and inside in z
01092   // lastpoint of track in lateral dir. outside the detector and inside in z
01093   // for all except zzzmarta.xml
01094 //    if(  lastpo.z()<z4  ||  abs(lastpo.x())> 5. || lastpo.y()< 10.2 || lastpo.y()>30.2   ) {
01095 // for zzzmarta.xml
01096     //   if(  lastpo.z()<z4  ||  abs(lastpo.x())> 10. || lastpo.y()< 3.2 || lastpo.y()>43.2   ) {
01097     if(  lastpo.z()< z4) {
01098 //  if(  lastpo.z()<z4 && lastpo.perp()< 100. ) {
01099 //  if(lastpo.z()<z4  || lastpo.perp()> 45.) {
01100     //UserNtuples->fillg66(theCAFI->entries(),1.);
01101     varia = 1;
01102   }
01103   else{
01104     // 2)   no MI start in detector:
01105     //UserNtuples->fillg31(numofpart,1.);
01106     //UserNtuples->fillg67(theCAFI->entries(),1.);
01107     varia = 2;
01108   }   // no MI end:
01109 
01110    for (int j=0; j<theCAFI->entries(); j++) {
01111     FP420G4Hit* aHit = (*theCAFI)[j];
01112     G4ThreeVector hitPoint = aHit->getEntry();
01113     double   zz    = hitPoint.z();
01114     TheHistManager->GetHisto("zHits")->Fill(zz);
01115     if(tracklength0>8300.) TheHistManager->GetHisto("zHitsTrLoLe")->Fill(zz);
01116    }
01117    // varia = 0;
01118    //     if( varia == 0) {
01119    if( varia == 2) {
01120 
01121 // .............
01122 // number of hits < 50
01123 //    if(theCAFI->entries() <50) {
01124 //    if(theCAFI->entries() > 0) {
01125 //    if(theCAFI->entries() > -1) {
01126 // .............
01127       int nhit11 = 0, nhit12 = 0, nhit13 = 0 ;
01128    double  totallosenergy= 0.;
01129    for (int j=0; j<theCAFI->entries(); j++) {
01130     FP420G4Hit* aHit = (*theCAFI)[j];
01131 
01132     G4ThreeVector hitEntryLocalPoint = aHit->getEntryLocalP();
01133     G4ThreeVector hitExitLocalPoint = aHit->getExitLocalP();
01134     G4ThreeVector hitPoint = aHit->getEntry();
01135 //    double  elmenergy =  aHit->getEM();
01136 //    double  hadrenergy =  aHit->getHadr();
01137 //    double incidentEnergyHit  = aHit->getIncidentEnergy();
01138     int trackIDhit  = aHit->getTrackID();
01139     unsigned int unitID = aHit->getUnitID();
01140 //    double   timeslice = aHit->getTimeSlice();     
01141 //    int     timesliceID = aHit->getTimeSliceID();     
01142 //    double  depenergy = aHit->getEnergyDeposit();
01143 //    float   pabs = aHit->getPabs();
01144 //    float   tof = aHit->getTof();
01145     double  losenergy = aHit->getEnergyLoss();
01146 //    int   particletype = aHit->getParticleType();
01147 //    float thetaEntry = aHit->getThetaAtEntry();   
01148 //    float phiEntry = aHit->getPhiAtEntry();
01149 //    float xEntry = aHit->getX();
01150 //    float yEntry = aHit->getY();
01151 //    float zEntry = aHit->getZ();
01152 //    int  parentID = aHit->getParentId();
01153 //    float vxget = aHit->getVx();
01154 //    float vyget = aHit->getVy();
01155 //    float vzget = aHit->getVz();
01156 
01157 //    double th_hit    = hitPoint.theta();
01158 //    double eta_hit = -log(tan(th_hit/2));
01159     double phi_hit   = hitPoint.phi();
01160     if (phi_hit < 0.) phi_hit += twopi;
01161 //    double phigrad_hit = phi_hit*180./pi;
01162     //UserNtuples->fillg60(eta_hit,losenergy);
01163     //UserNtuples->fillg61(eta_hit,1.);
01164     //UserNtuples->fillg62(phigrad_hit,losenergy);
01165     //UserNtuples->fillg63(phigrad_hit,1.);
01166 
01167 //    double   xx    = hitPoint.x();
01168 //    double   yy    = hitPoint.y();
01169     double   zz    = hitPoint.z();
01170 
01171     TheHistManager->GetHisto("zHitsnoMI")->Fill(zz);
01172 
01173     if (verbosity > 2) {
01174       std::cout << "FP420Test:zHits = " << zz << std::endl;
01175     }
01176 //       double   rr    = hitPoint.perp();
01177     /*
01178       if(aHit->getTrackID() == 1) {
01179           emu += aHit->getEnergyDeposit();} else {
01180           erest += aHit->getEnergyDeposit();}
01181     */
01182 
01183     //collect lost in Si en.of hits in every plane and put it into themap[]     
01184              //UserNtuples->fillg30(losenergy,1.);
01185     themap[unitID] += losenergy;
01186         totallosenergy += losenergy;
01187 
01188     int det, zside, sector, zmodule;
01189 //    CaloNumberingPacker::unpackCastorIndex(unitID, det, zside, sector, zmodule);
01190     FP420NumberingScheme::unpackFP420Index(unitID, det, zside, sector, zmodule);
01191     int justlayer = theFP420NumberingScheme->FP420NumberingScheme::unpackLayerIndex(rn00, zside);// 1,2
01192     if(justlayer<1||justlayer>2) {
01193       std::cout << "FP420Test:WRONG  justlayer= " << justlayer << std::endl; 
01194     }
01195     // zside=1,2 ; zmodule=1,10 ; sector=1,3
01196     //UserNtuples->fillg44(float(sector),1.);
01197     //UserNtuples->fillg45(float(zmodule),1.);
01198     //UserNtuples->fillg46(float(zside),1.);
01199     //      int sScale = 20;
01200     // intindex is a continues numbering of FP420
01201     //int zScale = 2; unsigned int intindex = sScale*(sector - 1)+zScale*(zmodule - 1)+zside; //intindex=1-30:X,Y,X,Y,X,Y...
01202     // int zScale = 10;   unsigned int intindex = sScale*(sector - 1)+zScale*(zside - 1)+zmodule; //intindex=1-30:XXXXXXXXXX,YYYYYYYYYY,...
01203     //UserNtuples->fillg40(float(intindex),1.);
01204     //UserNtuples->fillg48(float(intindex),losenergy);
01205     //
01206     //=======================================
01207     //   G4ThreeVector middle = (hitExitLocalPoint+hitEntryLocalPoint)/2.;
01208     G4ThreeVector middle = (hitExitLocalPoint-hitEntryLocalPoint)/2.;
01209     themapz[unitID]  = hitPoint.z()+fabs(middle.z());
01210     if (verbosity > 2) {
01211       std::cout << "1111111111111111111111111111111111111111111111111111111111111111111111111 " << std::endl;
01212       std::cout << "FP420Test: det, zside, sector, zmodule = " << det << zside << sector << zmodule << std::endl;
01213       std::cout << "FP420Test: justlayer = " << justlayer << std::endl;
01214       std::cout << "FP420Test: hitExitLocalPoint = " << hitExitLocalPoint << std::endl;
01215       std::cout << "FP420Test: hitEntryLocalPoint = " << hitEntryLocalPoint << std::endl;
01216       std::cout << "FP420Test:  middle= " << middle << std::endl;
01217       std::cout << "FP420Test:  hitPoint.z()-419000.= " << hitPoint.z()-419000. << std::endl;
01218       
01219       std::cout << "FP420Test:zHits-419000. = " << themapz[unitID]-419000. << std::endl;
01220     }
01221     //=======================================
01222     // Y
01223     if(zside==1) {
01224              //UserNtuples->fillg24(losenergy,1.);
01225              if(losenergy > 0.00003) {
01226                themap1[unitID] += 1.;
01227              }
01228            }
01229 //X
01230            if(zside==2){
01231              //UserNtuples->fillg25(losenergy,1.);
01232              if(losenergy > 0.00005) {
01233                themap1[unitID] += 1.;
01234              }
01235            }
01236 //         }
01237 //
01238            if(sector==1) {
01239              nhit11 += 1;
01240              //UserNtuples->fillg33(rr,1.);
01241              //UserNtuples->fillg11(yy,1.);
01242            }
01243            if(sector==2) {
01244              nhit12 += 1;
01245              //UserNtuples->fillg34(rr,1.);
01246              //UserNtuples->fillg86(yy,1.);
01247            }
01248            if(sector==3) {
01249              nhit13 += 1;
01250              //UserNtuples->fillg35(rr,1.);
01251              //UserNtuples->fillg87(yy,1.);
01252            }
01253     //UserNtuples->fillg10(xx,1.);
01254     //UserNtuples->fillg12(zz,1.);
01255     //UserNtuples->fillg32(rr,1.);
01256            
01257 
01258 
01259 
01260 
01261 
01262 
01263     // =========
01264 //    double xPrimAtZhit = vx + (zz-vz)*tan(th)*cos(phi);
01265 //    double yPrimAtZhit = vy + (zz-vz)*tan(th)*sin(phi);
01266 
01267 //       double  dx = xPrimAtZhit - xx;
01268 //       double  dy = yPrimAtZhit - yy;
01269 
01270     //                      Select SD hits
01271 //    if(rr<120.) {
01272     //                      Select MI or noMI over all 3 stations
01273     if(lastpo.z()<z4  && lastpo.perp()< 120.) {
01274     // MIonly:
01275       //UserNtuples->fillg16(lastpo.z(),1.);
01276       //UserNtuples->fillg18(zz,1.);
01277     //                                                                     Station I
01278       if( zz < z2){
01279         //UserNtuples->fillg54(dx,1.);
01280         //UserNtuples->fillg55(dy,1.);
01281       }
01282     //                                                                     Station II
01283       if( zz < z3 && zz > z2){
01284         //UserNtuples->fillg50(dx,1.);
01285         //UserNtuples->fillg51(dy,1.);
01286       }
01287     //                                                                     Station III
01288       if( zz < z4 && zz > z3){
01289         //UserNtuples->fillg64(dx,1.);
01290         //UserNtuples->fillg65(dy,1.);
01291         //UserNtuples->filld209(xx,yy,1.);
01292       }
01293     }
01294     else{
01295       // no MIonly:
01296       //UserNtuples->fillg17(lastpo.z(),1.);
01297       //UserNtuples->fillg19(zz,1.);
01298       //UserNtuples->fillg74(incidentEnergyHit,1.);
01299       //UserNtuples->fillg75(float(trackIDhit),1.);
01300     //                                                                     Station I
01301       if( zz < z2){
01302         //UserNtuples->fillg56(dx,1.);
01303         //UserNtuples->fillg57(dy,1.);
01304         //UserNtuples->fillg20(numofpart,1.);
01305         //UserNtuples->fillg21(SumEnerDeposit,1.);
01306         if(zside==1) {
01307           //UserNtuples->fillg26(losenergy,1.);
01308         }
01309         if(zside==2){
01310           //UserNtuples->fillg76(losenergy,1.);
01311         }
01312         if(trackIDhit == 1){
01313           //UserNtuples->fillg70(dx,1.);
01314           //UserNtuples->fillg71(incidentEnergyHit,1.);
01315           //UserNtuples->fillg79(losenergy,1.);
01316         }
01317         else{
01318           //UserNtuples->fillg82(dx,1.);
01319         }
01320       }
01321     //                                                                     Station II
01322       if( zz < z3 && zz > z2){
01323         //UserNtuples->fillg52(dx,1.);
01324         //UserNtuples->fillg53(dy,1.);
01325         //UserNtuples->fillg22(numofpart,1.);
01326         //UserNtuples->fillg23(SumEnerDeposit,1.);
01327         //UserNtuples->fillg80(incidentEnergyHit,1.);
01328         //UserNtuples->fillg81(float(trackIDhit),1.);
01329         if(zside==1) {
01330           //UserNtuples->fillg27(losenergy,1.);
01331         }
01332         if(zside==2){
01333           //UserNtuples->fillg77(losenergy,1.);
01334         }
01335         if(trackIDhit == 1){
01336           //UserNtuples->fillg72(dx,1.);
01337           //UserNtuples->fillg73(incidentEnergyHit,1.);
01338         }
01339         else{
01340           //UserNtuples->fillg83(dx,1.);
01341         }
01342       }
01343     //                                                                     Station III
01344       if( zz < z4 && zz > z3){
01345         //UserNtuples->fillg68(dx,1.);
01346         //UserNtuples->fillg69(dy,1.);
01347         //UserNtuples->filld210(xx,yy,1.);
01348         //UserNtuples->fillg22(numofpart,1.);
01349         //UserNtuples->fillg23(SumEnerDeposit,1.);
01350         if(zside==1) {
01351           //UserNtuples->fillg28(losenergy,1.);
01352         }
01353         if(zside==2){
01354           //UserNtuples->fillg78(losenergy,1.);
01355         }
01356       }
01357     } // MIonly or noMIonly ENDED
01358 //    }
01359 
01360    //     !!!!!!!!!!!!!
01361 
01362    }  // for loop on all hits ENDED  ENDED  ENDED  !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
01363 
01364    //     !!!!!!!!!!!!!
01365    
01366    //======================================================================================================SUMHIT
01367    //UserNtuples->fillg29(totallosenergy,1.);
01368    //UserNtuples->fillg36(nhit11,1.);
01369    //UserNtuples->fillg37(nhit12,1.);
01370    //UserNtuples->fillg38(nhit13,1.);
01371    //======================================================================================================SUMHIT
01372    //   int rn00=3;//test only with 2 sensors in superlayer, not 4
01373     //    int rn00=rn0;//always
01374    if (verbosity > 2) {
01375      std::cout << "22222222222222222222222222222222222222222222222222222222222222222222222222 " << std::endl;
01376    } 
01377   int det = 1;
01378    int allplacesforsensors=7;
01379    for (int sector=1; sector < sn0; sector++) {
01380      for (int zmodule=1; zmodule<pn0; zmodule++) {
01381        for (int zsideinorder=1; zsideinorder<allplacesforsensors; zsideinorder++) {
01382          int zside = theFP420NumberingScheme->FP420NumberingScheme::realzside(rn00, zsideinorder);//1,3,5,2,4,6
01383          if (verbosity > 2) {
01384            std::cout << "FP420Test:  sector= " << sector << " zmodule= " << zmodule << " zsideinorder= " << zsideinorder << " zside= " << zside << std::endl; 
01385          }       
01386          if(zside != 0) {
01387            int justlayer = theFP420NumberingScheme->FP420NumberingScheme::unpackLayerIndex(rn00, zside);// 1,2
01388            if(justlayer<1||justlayer>2) {
01389              std::cout << "FP420Test:WRONG  justlayer= " << justlayer << std::endl; 
01390            }
01391            int copyinlayer = theFP420NumberingScheme->FP420NumberingScheme::unpackCopyIndex(rn00, zside);// 1,2,3
01392            if(copyinlayer<1||copyinlayer>3) {
01393              std::cout << "FP420Test:WRONG  copyinlayer= " << copyinlayer << std::endl; 
01394            }
01395            int orientation = theFP420NumberingScheme->FP420NumberingScheme::unpackOrientation(rn00, zside);// Front: = 1; Back: = 2
01396            if(orientation<1||orientation>2) {
01397              std::cout << "FP420Test:WRONG  orientation= " << orientation << std::endl; 
01398            }
01399            
01400            // iu is a continues numbering of planes(!)  over two arm FP420 set up
01401            int detfixed=1;// use this treatment for each set up arm, hence no sense to do it defferently for +FP420 and -FP420;
01402            //                                                                    and  ...[ii] massives have prepared in such a way
01403            unsigned int ii=theFP420NumberingScheme->FP420NumberingScheme::packMYIndex(rn00,pn0,sn0,detfixed,justlayer,sector,zmodule)-1;
01404            // ii = 0-19   --> 20 items
01405            if (verbosity > 2) {
01406              std::cout << "FP420Test:  justlayer = " << justlayer << " copyinlayer = " << copyinlayer << " orientation = " << orientation << " ii= " << ii << std::endl; 
01407            }       
01408            double zdiststat = 0.;
01409            if(sn0<4) {
01410              if(sector==2) zdiststat = zD3;
01411            }
01412            else {
01413              if(sector==2) zdiststat = zD2;
01414              if(sector==3) zdiststat = zD3;
01415            }
01416            double kplane = -(pn0-1)/2 - 0.5  +  (zmodule-1); //-3.5 +0...5 = -3.5,-2.5,-1.5,+2.5,+1.5
01417            double zcurrent = zinibeg + z420 + (ZSiStep-ZSiPlane)/2  + kplane*ZSiStep + zdiststat;  
01418            //double zcurrent = zinibeg +(ZSiStep-ZSiPlane)/2  + kplane*ZSiStep + (sector-1)*zUnit;  
01419            if (verbosity > 2) {
01420              std::cout << "FP420Test:  Leftzcurrent-419000. = " << zcurrent-419000. << std::endl; 
01421              std::cout << "FP420Test:  ZGapLDet = " << ZGapLDet << std::endl; 
01422            }       
01423            if(justlayer==1){
01424              if(orientation==1) zcurrent += (ZGapLDet+ZSiDet/2);
01425              if(orientation==2) zcurrent += zBlade-(ZGapLDet+ZSiDet/2);
01426            }
01427            if(justlayer==2){
01428              if(orientation==1) zcurrent += (ZGapLDet+ZSiDet/2)+zBlade+gapBlade;
01429              if(orientation==2) zcurrent += 2*zBlade+gapBlade-(ZGapLDet+ZSiDet/2);
01430            }
01431            //   .
01432            //
01433            if(det == 2) zcurrent = -zcurrent;
01434            //
01435            if (verbosity > 2) {
01436              std::cout << "FP420Test:  zcurrent-419000. = " << zcurrent-419000. << std::endl; 
01437            }
01438            //================================== end of for loops in continuius number iu:
01439          }//if(zside!=0
01440        }   // for superlayer
01441      }   // for zmodule
01442    }   // for sector
01443    
01444 
01445    if (verbosity > 2) {
01446      std::cout << "----------------------------------------------------------------------------- " << std::endl;
01447    }   
01448    
01449 
01450 
01451 //======================================================================================================CHECK
01452   if(totallosenergy == 0.0) {
01453     std::cout << "FP420Test:     number of hits = " << theCAFI->entries()   << std::endl;
01454     for (int j=0; j<theCAFI->entries(); j++) {
01455       FP420G4Hit* aHit = (*theCAFI)[j];
01456       double  losenergy = aHit->getEnergyLoss();
01457       std::cout << " j hits = " << j   << "losenergy = " << losenergy << std::endl;
01458     }
01459   }
01460 //======================================================================================================CHECK
01461   
01462 
01463 
01464 //====================================================================================================== HIT  START
01465 
01466   //   FIBRE Hit collected analysis
01467   double totalEnergy = 0.;
01468   int nhitsX = 0, nhitsY = 0, nsumhit = 0 ;
01469   for (int sector=1; sector<4; sector++) {
01470     int nhitsecX = 0, nhitsecY = 0;
01471     for (int zmodule=1; zmodule<11; zmodule++) {
01472       for (int zside=1; zside<3; zside++) {
01473         int det= 1;
01474 //      int nhit = 0;
01475 //      int sScale = 20;
01476         int index = FP420NumberingScheme::packFP420Index(det, zside, sector, zmodule);
01477         double   theTotalEnergy = themap[index];
01478         //   X planes
01479         if(zside<2){ 
01480           //UserNtuples->fillg47(theTotalEnergy,1.); 
01481           if(theTotalEnergy > 0.00003) {
01482             nhitsX += 1;
01483 //          nhitsecX += themap1[index];
01484 //          nhit=1;
01485           }
01486         }
01487         //   Y planes
01488         else {
01489           //UserNtuples->fillg49(theTotalEnergy,1.);
01490           if(theTotalEnergy > 0.00005) {
01491             nhitsY += 1;
01492 //          nhitsecY += themap1[index];
01493 //          nhit=1;
01494           }
01495         }
01496         // intindex is a continues numbering of FP420
01497 //        int zScale=2;  unsigned int intindex = sScale*(sector - 1)+zScale*(zmodule - 1)+zside;
01498         // int zScale=10;       unsigned int intindex = sScale*(sector - 1)+zScale*(zside - 1)+zmodule;
01499         //UserNtuples->fillg41(float(intindex),theTotalEnergy);
01500         //UserNtuples->fillg42(float(intindex),1.);
01501         //UserNtuples->fillp208(float(intindex),float(nhit),1.);
01502         //UserNtuples->fillp211(float(intindex),float(themap1[index]),1.);
01503         totalEnergy += themap[index];
01504       } // for
01505     } // for
01506           //UserNtuples->fillg39(nhitsecY,1.); 
01507           if(nhitsecX > 10 || nhitsecY > 10) {
01508             nsumhit +=1;
01509             //UserNtuples->fillp213(float(sector),float(1.),1.);
01510           }
01511           else{ //UserNtuples->fillp213(float(sector),float(0.),1.);
01512           }
01513   } // for
01514 //====================================================================================================== HIT  END
01515 
01516 //====================================================================================================== HIT  ALL
01517   //UserNtuples->fillg43(totalEnergy,1.);
01518   //UserNtuples->fillg58(nhitsX,1.); 
01519   //UserNtuples->fillg59(nhitsY,1.); 
01520   //  if( nsumhit !=0 ) { //UserNtuples->fillp212(vy,float(1.),1.);
01521   if( nsumhit >=2 ) { //UserNtuples->fillp212(vy,float(1.),1.);
01522   }
01523   else{   //UserNtuples->fillp212(vy,float(0.),1.);
01524   }
01525 
01526 //====================================================================================================== HIT  ALL
01527 
01528 //====================================================================================================== number of hits
01529 // .............
01530 //    } // number of hits < 50
01531 // .............
01532     }   // MI or no MI or all  - end
01533 
01534 
01535 
01536     }                                                // primary end
01537 //=========================== thePrim != 0  end   ================================================================================
01538   //==================================================================================================================
01539 
01540 
01541 
01542 
01543   // ==========================================================================
01544   if (verbosity > 0) {
01545    std::cout << "FP420Test:  END OF Event " << (*evt)()->GetEventID() << std::endl;
01546   }
01547 
01548 }
01549 
01550 // ==========================================================================