CMS 3D CMS Logo

BscTest.cc
Go to the documentation of this file.
1 // system include files
2 #include <cmath>
3 #include <iostream>
4 #include <iomanip>
5 #include <map>
6 #include <string>
7 #include <vector>
8 
9 //
16 
27 
28 // to retreive hits
32 
33 // G4 stuff
34 #include "G4SDManager.hh"
35 #include "G4Step.hh"
36 #include "G4Track.hh"
37 #include "G4VProcess.hh"
38 #include "G4HCofThisEvent.hh"
39 #include "G4UserEventAction.hh"
40 #include "G4TransportationManager.hh"
41 #include "G4ProcessManager.hh"
42 #include "G4VTouchable.hh"
43 
44 #include <CLHEP/Vector/ThreeVector.h>
45 #include <CLHEP/Vector/LorentzVector.h>
46 #include <CLHEP/Random/Randomize.h>
47 #include <CLHEP/Units/GlobalSystemOfUnits.h>
48 #include <CLHEP/Units/GlobalPhysicalConstants.h>
49 
50 // ----------------------------------------------------------------
51 // Includes needed for Root ntupling
52 //
53 #include "TROOT.h"
54 #include "TFile.h"
55 #include "TH1.h"
56 #include "TH2.h"
57 #include "TProfile.h"
58 #include "TNtuple.h"
59 #include "TRandom.h"
60 #include "TLorentzVector.h"
61 #include "TUnixSystem.h"
62 #include "TSystem.h"
63 #include "TMath.h"
64 #include "TF1.h"
65 #include "TObjArray.h"
66 #include "TObjString.h"
67 #include "TNamed.h"
68 
69 //#define EDM_ML_DEBUG
70 
71 class BscAnalysisHistManager : public TNamed {
72 public:
73  BscAnalysisHistManager(const TString& managername);
74  ~BscAnalysisHistManager() override;
75 
76  TH1F* getHisto(Int_t Number);
77  TH1F* getHisto(const TObjString& histname);
78 
79  TH2F* getHisto2(Int_t Number);
80  TH2F* getHisto2(const TObjString& histname);
81 
82  void writeToFile(const TString& fOutputFile, const TString& fRecreateFile);
83 
84 private:
85  void bookHistos();
86  void storeWeights();
87  void histInit(const char* name, const char* title, Int_t nbinsx, Axis_t xlow, Axis_t xup);
88  void histInit(
89  const char* name, const char* title, Int_t nbinsx, Axis_t xlow, Axis_t xup, Int_t nbinsy, Axis_t ylow, Axis_t yup);
90 
91  const char* fTypeTitle;
92  TObjArray* fHistArray;
93  TObjArray* fHistNamesArray;
94 };
95 
96 class BscTest : public SimWatcher,
97  public Observer<const BeginOfJob*>,
98  public Observer<const BeginOfRun*>,
99  public Observer<const EndOfRun*>,
100  public Observer<const BeginOfEvent*>,
101  public Observer<const BeginOfTrack*>,
102  public Observer<const G4Step*>,
103  public Observer<const EndOfTrack*>,
104  public Observer<const EndOfEvent*> {
105 public:
106  BscTest(const edm::ParameterSet& p);
107  ~BscTest() override;
108 
109 private:
110  // observer classes
111  void update(const BeginOfJob* run) override;
112  void update(const BeginOfRun* run) override;
113  void update(const EndOfRun* run) override;
114  void update(const BeginOfEvent* evt) override;
115  void update(const BeginOfTrack* trk) override;
116  void update(const G4Step* step) override;
117  void update(const EndOfTrack* trk) override;
118  void update(const EndOfEvent* evt) override;
119 
120  //UHB_Analysis* UserNtuples;
122 
123  int iev;
124  int itrk;
125  G4double entot0, tracklength0;
126 
127  // Utilities to get detector levels during a step
128 
129  int detLevels(const G4VTouchable*) const;
130  G4String detName(const G4VTouchable*, int, int) const;
131  void detectorLevel(const G4VTouchable*, int&, int*, G4String*) const;
132 
133  double rinCalo, zinCalo;
136 
137  // sumEnerDeposit - all deposited energy on all steps ; sumStepl - length in steel !!!
139  // numofpart - # particles produced along primary track
141  // last point of the primary track
142  G4ThreeVector lastpo;
143 
144  // z:
145  double z1, z2, z3, z4;
146 
147 private:
148  Float_t bsceventarray[1];
149  TNtuple* bsceventntuple;
152 
153  BscAnalysisHistManager* TheHistManager; //Histogram Manager of the analysis
154  std::string fDataLabel; // Data type label
155  std::string fOutputFile; // The output file name
156  std::string fRecreateFile; // Recreate the file flag, default="RECREATE"
157 };
158 
160 
161 //================================================================
163  //constructor
164  edm::ParameterSet m_Anal = p.getParameter<edm::ParameterSet>("BscTest");
165  verbosity = m_Anal.getParameter<int>("Verbosity");
166  //verbosity = 1;
167 
168  fDataLabel = m_Anal.getParameter<std::string>("FDataLabel");
169  fOutputFile = m_Anal.getParameter<std::string>("FOutputFile");
170  fRecreateFile = m_Anal.getParameter<std::string>("FRecreateFile");
171 
172  if (verbosity > 0) {
173  edm::LogVerbatim("BscTest") << "============================================================================";
174  edm::LogVerbatim("BscTest") << "BscTestconstructor :: Initialized as observer";
175  }
176  // Initialization:
177 
179  bsceventntuple = new TNtuple("NTbscevent", "NTbscevent", "evt");
180  whichevent = 0;
182 
183  if (verbosity > 0) {
184  edm::LogVerbatim("BscTest") << "BscTest constructor :: Initialized BscAnalysisHistManager";
185  }
186 }
187 
189  // delete UserNtuples;
190  delete theBscNumberingScheme;
191 
192  TFile bscOutputFile("newntbsc.root", "RECREATE");
193  edm::LogVerbatim("BscTest") << "Bsc output root file has been created";
194  bsceventntuple->Write();
195  edm::LogVerbatim("BscTest") << ", written";
196  bscOutputFile.Close();
197  edm::LogVerbatim("BscTest") << ", closed";
198  delete bsceventntuple;
199  edm::LogVerbatim("BscTest") << ", and deleted";
200 
201  //------->while end
202 
203  // Write histograms to file
205  if (verbosity > 0) {
206  edm::LogVerbatim("BscTest") << std::endl << "BscTest Destructor --------> End of BscTest : ";
207  }
208 
209  edm::LogVerbatim("BscTest") << "BscTest: End of process";
210 }
211 
212 //================================================================
213 // Histoes:
214 //-----------------------------------------------------------------------------
215 
217  // The Constructor
218 
219  fTypeTitle = managername;
220  fHistArray = new TObjArray(); // Array to store histos
221  fHistNamesArray = new TObjArray(); // Array to store histos's names
222 
223  bookHistos();
224 
225  fHistArray->Compress(); // Removes empty space
226  fHistNamesArray->Compress();
227 }
228 //-----------------------------------------------------------------------------
229 
231  // The Destructor
232 
233  if (fHistArray) {
234  fHistArray->Delete();
235  delete fHistArray;
236  }
237 
238  if (fHistNamesArray) {
239  fHistNamesArray->Delete();
240  delete fHistNamesArray;
241  }
242 }
243 //-----------------------------------------------------------------------------
244 
246  // at Start: (mm)
247  histInit("TrackPhi", "Primary Phi", 100, 0., 360.);
248  histInit("TrackTheta", "Primary Theta", 100, 0., 180.);
249  histInit("TrackP", "Track XY position Z+ ", 80, -80., 80., 80, -80., 80.);
250  histInit("TrackM", "Track XY position Z-", 80, -80., 80., 80, -80., 80.);
251  histInit("DetIDs", "Track DetId - vs +", 16, -0.5, 15.5, 16, 15.5, 31.5);
252 }
253 
254 //-----------------------------------------------------------------------------
255 
256 void BscAnalysisHistManager::writeToFile(const TString& fOutputFile, const TString& fRecreateFile) {
257  //Write to file = fOutputFile
258 
259  edm::LogVerbatim("BscTest") << "================================================================";
260  edm::LogVerbatim("BscTest") << " Write this Analysis to File " << fOutputFile;
261  edm::LogVerbatim("BscTest") << "================================================================";
262 
263  TFile* file = new TFile(fOutputFile, fRecreateFile);
264 
265  fHistArray->Write();
266  file->Close();
267 }
268 //-----------------------------------------------------------------------------
269 
270 void BscAnalysisHistManager::histInit(const char* name, const char* title, Int_t nbinsx, Axis_t xlow, Axis_t xup) {
271  // Add histograms and histograms names to the array
272 
273  char* newtitle = new char[strlen(title) + strlen(fTypeTitle) + 5];
274  strcpy(newtitle, title);
275  strcat(newtitle, " (");
276  strcat(newtitle, fTypeTitle);
277  strcat(newtitle, ") ");
278  fHistArray->AddLast((new TH1F(name, newtitle, nbinsx, xlow, xup)));
279  fHistNamesArray->AddLast(new TObjString(name));
280 }
281 //-----------------------------------------------------------------------------
282 
284  const char* name, const char* title, Int_t nbinsx, Axis_t xlow, Axis_t xup, Int_t nbinsy, Axis_t ylow, Axis_t yup) {
285  // Add histograms and histograms names to the array
286 
287  char* newtitle = new char[strlen(title) + strlen(fTypeTitle) + 5];
288  strcpy(newtitle, title);
289  strcat(newtitle, " (");
290  strcat(newtitle, fTypeTitle);
291  strcat(newtitle, ") ");
292  fHistArray->AddLast((new TH2F(name, newtitle, nbinsx, xlow, xup, nbinsy, ylow, yup)));
293  fHistNamesArray->AddLast(new TObjString(name));
294 }
295 //-----------------------------------------------------------------------------
296 
298  // Get a histogram from the array with index = Number
299 
300  if (Number <= fHistArray->GetLast() && fHistArray->At(Number) != (TObject*)nullptr) {
301  return (TH1F*)(fHistArray->At(Number));
302 
303  } else {
304  edm::LogVerbatim("BscTest") << "!!!!!!!!!!!!!!!!!!getHisto!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!";
305  edm::LogVerbatim("BscTest") << " WARNING ERROR - HIST ID INCORRECT (TOO HIGH) - " << Number;
306  edm::LogVerbatim("BscTest") << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!";
307 
308  return (TH1F*)(fHistArray->At(0));
309  }
310 }
311 //-----------------------------------------------------------------------------
312 
314  // Get a histogram from the array with index = Number
315 
316  if (Number <= fHistArray->GetLast() && fHistArray->At(Number) != (TObject*)nullptr) {
317  return (TH2F*)(fHistArray->At(Number));
318 
319  } else {
320  edm::LogVerbatim("BscTest") << "!!!!!!!!!!!!!!!!getHisto2!!!!!!!!!!!!!!!!!!!!!!!!!!!";
321  edm::LogVerbatim("BscTest") << " WARNING ERROR - HIST ID INCORRECT (TOO HIGH) - " << Number;
322  edm::LogVerbatim("BscTest") << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!";
323 
324  return (TH2F*)(fHistArray->At(0));
325  }
326 }
327 //-----------------------------------------------------------------------------
328 
329 TH1F* BscAnalysisHistManager::getHisto(const TObjString& histname) {
330  // Get a histogram from the array with name = histname
331 
332  Int_t index = fHistNamesArray->IndexOf(&histname);
333  return getHisto(index);
334 }
335 //-----------------------------------------------------------------------------
336 
337 TH2F* BscAnalysisHistManager::getHisto2(const TObjString& histname) {
338  // Get a histogram from the array with name = histname
339 
340  Int_t index = fHistNamesArray->IndexOf(&histname);
341  return getHisto2(index);
342 }
343 //-----------------------------------------------------------------------------
344 
346  // Add structure to each histogram to store the weights
347 
348  for (int i = 0; i < fHistArray->GetEntries(); i++) {
349  ((TH1F*)(fHistArray->At(i)))->Sumw2();
350  }
351 }
352 
353 //==================================================================== per JOB
354 void BscTest::update(const BeginOfJob* job) {
355  //job
356  edm::LogVerbatim("BscTest") << "BscTest:beggining of job";
357  ;
358 }
359 
360 //==================================================================== per RUN
362  //run
363 
364  edm::LogVerbatim("BscTest") << std::endl << "BscTest:: Begining of Run";
365 }
366 
367 void BscTest::update(const EndOfRun* run) { ; }
368 
369 //=================================================================== per EVENT
370 void BscTest::update(const BeginOfEvent* evt) {
371  iev = (*evt)()->GetEventID();
372  if (verbosity > 0) {
373  edm::LogVerbatim("BscTest") << "BscTest:update Event number = " << iev;
374  }
375  whichevent++;
376 }
377 
378 //=================================================================== per Track
379 void BscTest::update(const BeginOfTrack* trk) {
380  itrk = (*trk)()->GetTrackID();
381  if (verbosity > 1) {
382  edm::LogVerbatim("BscTest") << "BscTest:update BeginOfTrack number = " << itrk;
383  }
384  if (itrk == 1) {
385  sumEnerDeposit = 0.;
386  numofpart = 0;
387  sumStepl = 0.;
388  sumStepc = 0.;
389  tracklength0 = 0.;
390  }
391 }
392 
393 //=================================================================== per EndOfTrack
394 void BscTest::update(const EndOfTrack* trk) {
395  itrk = (*trk)()->GetTrackID();
396  if (verbosity > 1) {
397  edm::LogVerbatim("BscTest") << "BscTest:update EndOfTrack number = " << itrk;
398  }
399  if (itrk == 1) {
400  G4double tracklength = (*trk)()->GetTrackLength(); // Accumulated track length
401 
402  TheHistManager->getHisto("SumEDep")->Fill(sumEnerDeposit);
403  TheHistManager->getHisto("TrackL")->Fill(tracklength);
404 
405  // direction !!!
406  G4ThreeVector vert_mom = (*trk)()->GetVertexMomentumDirection();
407  G4ThreeVector vert_pos = (*trk)()->GetVertexPosition(); // vertex ,where this track was created
408 
409  // last step information
410  const G4Step* aStep = (*trk)()->GetStep();
411  G4StepPoint* preStepPoint = aStep->GetPreStepPoint();
412  lastpo = preStepPoint->GetPosition();
413  }
414 }
415 
416 // ====================================================
417 
418 //=================================================================== each STEP
419 void BscTest::update(const G4Step* aStep) {
420  // ==========================================================================
421 
422  if (verbosity > 2) {
423  G4int stepnumber = aStep->GetTrack()->GetCurrentStepNumber();
424  edm::LogVerbatim("BscTest") << "BscTest:update Step number = " << stepnumber;
425  }
426  // track on aStep: !
427  G4Track* theTrack = aStep->GetTrack();
428  TrackInformation* trkInfo = dynamic_cast<TrackInformation*>(theTrack->GetUserInformation());
429  if (trkInfo == nullptr) {
430  edm::LogVerbatim("BscTest") << "BscTest on aStep: No trk info !!!! abort ";
431  }
432  G4int id = theTrack->GetTrackID();
433  G4String particleType = theTrack->GetDefinition()->GetParticleName(); // !!!
434  G4int parentID = theTrack->GetParentID(); // !!!
435  G4TrackStatus trackstatus = theTrack->GetTrackStatus(); // !!!
436  G4double tracklength = theTrack->GetTrackLength(); // Accumulated track length
437  G4ThreeVector trackmom = theTrack->GetMomentum();
438  G4double entot = theTrack->GetTotalEnergy(); // !!! deposited on step
439  G4int curstepnumber = theTrack->GetCurrentStepNumber();
440  G4double stepl = aStep->GetStepLength();
441  G4double EnerDeposit = aStep->GetTotalEnergyDeposit();
442  G4StepPoint* preStepPoint = aStep->GetPreStepPoint();
443  const G4ThreeVector& preposition = preStepPoint->GetPosition();
444  G4ThreeVector prelocalpoint = theTrack->GetTouchable()->GetHistory()->GetTopTransform().TransformPoint(preposition);
445  G4VPhysicalVolume* currentPV = preStepPoint->GetPhysicalVolume();
446  const G4String& prename = currentPV->GetName();
447 
448  const G4VTouchable* pre_touch = preStepPoint->GetTouchable();
449  int pre_levels = detLevels(pre_touch);
450  G4String name1[20];
451  int copyno1[20];
452  for (int i = 0; i < 20; ++i) {
453  name1[i] = "";
454  copyno1[i] = 0;
455  }
456  if (pre_levels > 0) {
457  detectorLevel(pre_touch, pre_levels, copyno1, name1);
458  }
459 
460  if (id == 1) {
461  // on 1-st step:
462  if (curstepnumber == 1) {
463  entot0 = entot;
464  //UserNtuples->fillg519(entot0,1.);
465  }
466 
467  // on every step:
468 
469  // for Copper:
470  if (prename == "SBST") {
471  sumStepc += stepl;
472  // =========
473  }
474  // for ststeel:
475  // if(prename == "SBSTs") {
476  if (prename == "SBSTs") {
477  sumStepl += stepl;
478  // =========
479  }
480  // =========
481  // =========
482 
483  // exclude last track point if it is in SD (MI was started their)
484  if (trackstatus != 2) {
485  // for SD: Si Det.: SISTATION:SIPLANE:(SIDETL+BOUNDDET +SIDETR + CERAMDET)
486  if (prename == "SIDETL" || prename == "SIDETR") {
487  if (prename == "SIDETL") {
488  //UserNtuples->fillg569(EnerDeposit,1.);
489  }
490  if (prename == "SIDETR") {
491  //UserNtuples->fillg570(EnerDeposit,1.);
492  }
493 
494  G4String posname = aStep->GetPostStepPoint()->GetPhysicalVolume()->GetName();
495  if ((prename == "SIDETL" && posname != "SIDETL") || (prename == "SIDETR" && posname != "SIDETR")) {
496  if (name1[2] == "SISTATION") {
497  //UserNtuples->fillg539(copyno1[2],1.);
498  }
499  if (name1[3] == "SIPLANE") {
500  //UserNtuples->fillg540(copyno1[3],1.);
501  }
502 
503  if (prename == "SIDETL") {
504  //UserNtuples->fillg541(EnerDeposit,1.);
505  //UserNtuples->fillg561(numbcont,1.);
506  if (copyno1[2] < 2) {
507  //UserNtuples->fillg571(dx,1.);
508  } else if (copyno1[2] < 3) {
509  //UserNtuples->fillg563(dx,1.);
510  if (copyno1[3] < 2) {
511  } else if (copyno1[3] < 3) {
512  //UserNtuples->fillg572(dx,1.);
513  } else if (copyno1[3] < 4) {
514  //UserNtuples->fillg573(dx,1.);
515  } else if (copyno1[3] < 5) {
516  //UserNtuples->fillg574(dx,1.);
517  } else if (copyno1[3] < 6) {
518  //UserNtuples->fillg575(dx,1.);
519  } else if (copyno1[3] < 7) {
520  //UserNtuples->fillg576(dx,1.);
521  } else if (copyno1[3] < 8) {
522  //UserNtuples->fillg577(dx,1.);
523  } else if (copyno1[3] < 9) {
524  //UserNtuples->fillg578(dx,1.);
525  } else if (copyno1[3] < 10) {
526  //UserNtuples->fillg579(dx,1.);
527  }
528  } else if (copyno1[2] < 4) {
529  //UserNtuples->fillg565(dx,1.);
530  } else if (copyno1[2] < 5) {
531  //UserNtuples->fillg567(dx,1.);
532  }
533  }
534  if (prename == "SIDETR") {
535  //UserNtuples->fillg542(EnerDeposit,1.);
536  //UserNtuples->fillg562(numbcont,1.);
537  if (copyno1[2] < 2) {
538  //UserNtuples->fillg581(dy,1.);
539  } else if (copyno1[2] < 3) {
540  //UserNtuples->fillg564(dy,1.);
541  if (copyno1[3] < 2) {
542  } else if (copyno1[3] < 3) {
543  //UserNtuples->fillg582(dy,1.);
544  } else if (copyno1[3] < 4) {
545  //UserNtuples->fillg583(dy,1.);
546  } else if (copyno1[3] < 5) {
547  //UserNtuples->fillg584(dy,1.);
548  } else if (copyno1[3] < 6) {
549  //UserNtuples->fillg585(dy,1.);
550  } else if (copyno1[3] < 7) {
551  //UserNtuples->fillg586(dy,1.);
552  } else if (copyno1[3] < 8) {
553  //UserNtuples->fillg587(dy,1.);
554  } else if (copyno1[3] < 9) {
555  //UserNtuples->fillg588(dy,1.);
556  } else if (copyno1[3] < 10) {
557  //UserNtuples->fillg589(dy,1.);
558  }
559  } else if (copyno1[2] < 4) {
560  //UserNtuples->fillg566(dy,1.);
561  } else if (copyno1[2] < 5) {
562  //UserNtuples->fillg568(dy,1.);
563  }
564  }
565  }
566  }
567  // end of prenames SIDETL // SIDETR
568  }
569  // end of trackstatus != 2
570 
571  sumEnerDeposit += EnerDeposit;
572  if (trackstatus == 2) {
573  // primary track length
574  // //UserNtuples->fillg508(tracklength,1.);
575  tracklength0 = tracklength;
576  }
577  }
578  // end of primary track !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
579 
580  if (parentID == 1 && curstepnumber == 1) {
581  // particles deposit their energy along primary track
582  numofpart += 1;
583  if (prename == "SBST") {
584  //UserNtuples->filld225(vert_pos.y(),vert_pos.z(),1.);
585  }
586  if (prename == "SBSTs") {
587  //UserNtuples->filld226(vert_pos.y(),vert_pos.z(),1.);
588  }
589  }
590 }
591 // ==========================================================================
592 // ==========================================================================
593 int BscTest::detLevels(const G4VTouchable* touch) const {
594  //Return number of levels
595  if (touch)
596  return ((touch->GetHistoryDepth()) + 1);
597  else
598  return 0;
599 }
600 // ==========================================================================
601 
602 G4String BscTest::detName(const G4VTouchable* touch, int level, int currentlevel) const {
603  //Go down to current level
604  if (level > 0 && level >= currentlevel) {
605  int ii = level - currentlevel;
606  return touch->GetVolume(ii)->GetName();
607  } else {
608  return "NotFound";
609  }
610 }
611 
612 void BscTest::detectorLevel(const G4VTouchable* touch, int& level, int* copyno, G4String* name) const {
613  //Get name and copy numbers
614  if (level > 0) {
615  for (int ii = 0; ii < level; ii++) {
616  int i = level - ii - 1;
617  G4VPhysicalVolume* pv = touch->GetVolume(i);
618  if (pv != nullptr)
619  name[ii] = pv->GetName();
620  else
621  name[ii] = "Unknown";
622  copyno[ii] = touch->GetReplicaNumber(i);
623  }
624  }
625 }
626 // ==========================================================================
627 
628 //=================================================================== End Of Event
629 void BscTest::update(const EndOfEvent* evt) {
630  // ==========================================================================
631 
632  if (verbosity > 1) {
633  iev = (*evt)()->GetEventID();
634  edm::LogVerbatim("BscTest") << "BscTest:update EndOfEvent = " << iev;
635  }
636  // Fill-in ntuple
638 
639  //
640  int trackID = 0;
641  G4PrimaryParticle* thePrim = nullptr;
642 
643  // prim.vertex:
644  G4int nvertex = (*evt)()->GetNumberOfPrimaryVertex();
645  if (nvertex != 1)
646  edm::LogVerbatim("BscTest") << "BscTest: My warning: NumberOfPrimaryVertex != 1 --> = " << nvertex;
647 
648  for (int i = 0; i < nvertex; i++) {
649  G4PrimaryVertex* avertex = (*evt)()->GetPrimaryVertex(i);
650  if (avertex == nullptr) {
651  edm::LogVerbatim("BscTest") << "BscTest End Of Event ERR: pointer to vertex = 0";
652  continue;
653  }
654  G4int npart = avertex->GetNumberOfParticle();
655  if (npart != 1)
656  edm::LogVerbatim("BscTest") << "BscTest: My warning: NumberOfPrimaryPart != 1 --> = " << npart;
657  if (npart == 0)
658  edm::LogVerbatim("BscTest") << "BscTest End Of Event ERR: no NumberOfParticle";
659 
660  if (thePrim == nullptr)
661  thePrim = avertex->GetPrimary(trackID);
662 
663  if (thePrim != nullptr) {
664  // primary vertex:
665  G4double vx = 0., vy = 0., vz = 0.;
666  vx = avertex->GetX0();
667  vy = avertex->GetY0();
668  vz = avertex->GetZ0();
669  //UserNtuples->fillh01(vx);
670  //UserNtuples->fillh02(vy);
671  //UserNtuples->fillh03(vz);
672  TheHistManager->getHisto("VtxX")->Fill(vx);
673  TheHistManager->getHisto("VtxY")->Fill(vy);
674  TheHistManager->getHisto("VtxZ")->Fill(vz);
675  }
676  }
677  // prim.vertex loop end
678 
679  //=========================== thePrim != 0 ================================================================================
680  if (thePrim != nullptr) {
681  //
682  // number of secondary particles deposited their energy along primary track
683  //UserNtuples->fillg518(numofpart,1.);
684  if (lastpo.z() < z4 && lastpo.perp() < 100.) {
685  //UserNtuples->fillg536(numofpart,1.);
686  }
687  //
688 
689  // direction !!!
690  G4ThreeVector mom = thePrim->GetMomentum();
691 
692  double phi = atan2(mom.y(), mom.x());
693  if (phi < 0.)
694  phi += twopi;
695  double phigrad = phi * 180. / pi;
696 
697  double th = mom.theta();
698  double eta = -log(tan(th / 2));
699  TheHistManager->getHisto("PrimaryEta")->Fill(eta);
700  TheHistManager->getHisto("PrimaryPhigrad")->Fill(phigrad);
701  TheHistManager->getHisto("PrimaryTh")->Fill(th * 180. / pi);
702 
703  TheHistManager->getHisto("PrimaryLastpoZ")->Fill(lastpo.z());
704  if (lastpo.z() < z4) {
705  TheHistManager->getHisto("PrimaryLastpoX")->Fill(lastpo.x());
706  TheHistManager->getHisto("PrimaryLastpoY")->Fill(lastpo.y());
707  }
708  if (numofpart > 4) {
709  TheHistManager->getHisto("XLastpoNumofpart")->Fill(lastpo.x());
710  TheHistManager->getHisto("YLastpoNumofpart")->Fill(lastpo.y());
711  }
712 
713  // ==========================================================================
714 
715  // hit map for Bsc
716  // ==================================
717 
718  std::map<int, float, std::less<int> > themap;
719  std::map<int, float, std::less<int> > themap1;
720 
721  std::map<int, float, std::less<int> > themapxy;
722  std::map<int, float, std::less<int> > themapz;
723  // access to the G4 hit collections: -----> this work OK:
724 #ifdef EDM_ML_DEBUG
725  edm::LogVerbatim("BscTest") << "1";
726 #endif
727  G4HCofThisEvent* allHC = (*evt)()->GetHCofThisEvent();
728 #ifdef EDM_ML_DEBUG
729  edm::LogVerbatim("BscTest") << "2";
730 #endif
731  if (verbosity > 0) {
732  edm::LogVerbatim("BscTest") << "BscTest: accessed all HC";
733  ;
734  }
735  int CAFIid = G4SDManager::GetSDMpointer()->GetCollectionID("BSCHits");
736 
737  BscG4HitCollection* theCAFI = (BscG4HitCollection*)allHC->GetHC(CAFIid);
738  if (verbosity > 0) {
739  edm::LogVerbatim("BscTest") << "BscTest: theCAFI->entries = " << theCAFI->entries();
740  }
741  int varia; // = 0 -all; =1 - MI; =2 - noMI
742  //varia = 0;
743  if (lastpo.z() < z4) {
744  varia = 1;
745  } else {
746  varia = 2;
747  } // no MI end:
748  int nhits = theCAFI->entries();
749  for (int j = 0; j < nhits; j++) {
750  BscG4Hit* aHit = (*theCAFI)[j];
751  const CLHEP::Hep3Vector& hitPoint = aHit->getEntry();
752  double zz = hitPoint.z();
753  TheHistManager->getHisto("zHits")->Fill(zz);
754  if (tracklength0 > 8300.)
755  TheHistManager->getHisto("zHitsTrLoLe")->Fill(zz);
756  }
757 
758  if (varia == 2) {
759  //int nhit11 = 0, nhit12 = 0, nhit13 = 0;
760  double totallosenergy = 0.;
761  for (int j = 0; j < nhits; j++) {
762  BscG4Hit* aHit = (*theCAFI)[j];
763 
764  const CLHEP::Hep3Vector& hitEntryLocalPoint = aHit->getEntryLocalP();
765  const CLHEP::Hep3Vector& hitExitLocalPoint = aHit->getExitLocalP();
766  const CLHEP::Hep3Vector& hitPoint = aHit->getEntry();
767  //int trackIDhit = aHit->getTrackID();
768  unsigned int unitID = aHit->getUnitID();
769  double losenergy = aHit->getEnergyLoss();
770 
771  double zz = hitPoint.z();
772 
773  TheHistManager->getHisto("zHitsnoMI")->Fill(zz);
774 
775  if (verbosity > 2) {
776  edm::LogVerbatim("BscTest") << "BscTest:zHits = " << zz;
777  }
778 
779  themap[unitID] += losenergy;
780  totallosenergy += losenergy;
781 
782  int zside;
783  //int sector;
785  zside = (unitID & 32) >> 5;
786  //sector = (unitID & 7);
787 
788  //
789  //=======================================
790  G4ThreeVector middle = (hitExitLocalPoint + hitEntryLocalPoint) / 2.;
791  themapz[unitID] = hitPoint.z() + middle.z();
792  //=======================================
793  // Y
794  if (zside == 1) {
795  //UserNtuples->fillg24(losenergy,1.);
796  if (losenergy > 0.00003) {
797  themap1[unitID] += 1.;
798  }
799  }
800  //X
801  else if (zside == 2) {
802  //UserNtuples->fillg25(losenergy,1.);
803  if (losenergy > 0.00005) {
804  themap1[unitID] += 1.;
805  }
806  }
807  // }
808  //
809  /*
810  if (sector == 1) {
811  nhit11 += 1;
812  //UserNtuples->fillg33(rr,1.);
813  //UserNtuples->fillg11(yy,1.);
814  }
815  if (sector == 2) {
816  nhit12 += 1;
817  //UserNtuples->fillg34(rr,1.);
818  //UserNtuples->fillg86(yy,1.);
819  }
820  if (sector == 3) {
821  nhit13 += 1;
822  //UserNtuples->fillg35(rr,1.);
823  //UserNtuples->fillg87(yy,1.);
824  }
825 
826  if (lastpo.z() < z4 && lastpo.perp() < 120.) {
827  // MIonly:
828  //UserNtuples->fillg16(lastpo.z(),1.);
829  //UserNtuples->fillg18(zz,1.);
830  // Station I
831  if (zz < z2) {
832  //UserNtuples->fillg54(dx,1.);
833  //UserNtuples->fillg55(dy,1.);
834  }
835  // Station II
836  if (zz < z3 && zz > z2) {
837  //UserNtuples->fillg50(dx,1.);
838  //UserNtuples->fillg51(dy,1.);
839  }
840  // Station III
841  if (zz < z4 && zz > z3) {
842  //UserNtuples->fillg64(dx,1.);
843  //UserNtuples->fillg65(dy,1.);
844  //UserNtuples->filld209(xx,yy,1.);
845  }
846  } else {
847  // no MIonly:
848  //UserNtuples->fillg17(lastpo.z(),1.);
849  //UserNtuples->fillg19(zz,1.);
850  //UserNtuples->fillg74(incidentEnergyHit,1.);
851  //UserNtuples->fillg75(float(trackIDhit),1.);
852  // Station I
853  if (zz < z2) {
854  //UserNtuples->fillg56(dx,1.);
855  //UserNtuples->fillg57(dy,1.);
856  //UserNtuples->fillg20(numofpart,1.);
857  //UserNtuples->fillg21(sumEnerDeposit,1.);
858  if (zside == 1) {
859  //UserNtuples->fillg26(losenergy,1.);
860  }
861  if (zside == 2) {
862  //UserNtuples->fillg76(losenergy,1.);
863  }
864  if (trackIDhit == 1) {
865  //UserNtuples->fillg70(dx,1.);
866  //UserNtuples->fillg71(incidentEnergyHit,1.);
867  //UserNtuples->fillg79(losenergy,1.);
868  } else {
869  //UserNtuples->fillg82(dx,1.);
870  }
871  }
872  // Station II
873  if (zz < z3 && zz > z2) {
874  //UserNtuples->fillg52(dx,1.);
875  //UserNtuples->fillg53(dy,1.);
876  //UserNtuples->fillg22(numofpart,1.);
877  //UserNtuples->fillg23(sumEnerDeposit,1.);
878  //UserNtuples->fillg80(incidentEnergyHit,1.);
879  //UserNtuples->fillg81(float(trackIDhit),1.);
880  if (zside == 1) {
881  //UserNtuples->fillg27(losenergy,1.);
882  }
883  if (zside == 2) {
884  //UserNtuples->fillg77(losenergy,1.);
885  }
886  if (trackIDhit == 1) {
887  //UserNtuples->fillg72(dx,1.);
888  //UserNtuples->fillg73(incidentEnergyHit,1.);
889  } else {
890  //UserNtuples->fillg83(dx,1.);
891  }
892  }
893  // Station III
894  if (zz < z4 && zz > z3) {
895  if (zside == 1) {
896  //UserNtuples->fillg28(losenergy,1.);
897  }
898  if (zside == 2) {
899  //UserNtuples->fillg78(losenergy,1.);
900  }
901  }
902  }
903  */
904  } // MIonly or noMIonly ENDED
905  if (totallosenergy == 0.0) {
906  edm::LogVerbatim("BscTest") << "BscTest: number of hits = " << theCAFI->entries();
907  for (int j = 0; j < nhits; j++) {
908  BscG4Hit* aHit = (*theCAFI)[j];
909  double losenergy = aHit->getEnergyLoss();
910  edm::LogVerbatim("BscTest") << " j hits = " << j << "losenergy = " << losenergy;
911  }
912  }
913  // FIBRE Hit collected analysis
914  /*
915  double totalEnergy = 0.;
916  int nhitsX = 0, nhitsY = 0, nsumhit = 0;
917  for (int sector = 1; sector < 4; sector++) {
918  int nhitsecX = 0, nhitsecY = 0;
919  for (int zmodule = 1; zmodule < 11; zmodule++) {
920  for (int zside = 1; zside < 3; zside++) {
921  int det = 1; // nhit = 0;
922  // int sScale = 20;
923  int index = BscNumberingScheme::packBscIndex(det, zside, sector);
924  double theTotalEnergy = themap[index];
925  // X planes
926  if (zside < 2) {
927  //UserNtuples->fillg47(theTotalEnergy,1.);
928  if (theTotalEnergy > 0.00003) {
929  nhitsX += 1;
930  // nhitsecX += themap1[index];
931  // nhit=1;
932  }
933  }
934  // Y planes
935  else {
936  //UserNtuples->fillg49(theTotalEnergy,1.);
937  if (theTotalEnergy > 0.00005) {
938  nhitsY += 1;
939  // nhitsecY += themap1[index];
940  // nhit=1;
941  }
942  }
943 
944  totalEnergy += themap[index];
945  } // for
946  } // for
947  //UserNtuples->fillg39(nhitsecY,1.);
948  if (nhitsecX > 10 || nhitsecY > 10) {
949  nsumhit += 1;
950  //UserNtuples->fillp213(float(sector),float(1.),1.);
951  } else { //UserNtuples->fillp213(float(sector),float(0.),1.);
952  }
953  } // for
954 
955  if (nsumhit >= 2) { //UserNtuples->fillp212(vy,float(1.),1.);
956  } else { //UserNtuples->fillp212(vy,float(0.),1.);
957  }
958  */
959  } // MI or no MI or all - end
960  } // primary end
961 
962  if (verbosity > 0) {
963  edm::LogVerbatim("BscTest") << "BscTest: END OF Event " << (*evt)()->GetEventID();
964  }
965 }
966 
969 
Log< level::Info, true > LogVerbatim
int numofpart
Definition: BscTest.cc:140
double z2
Definition: BscTest.cc:145
#define DEFINE_SIMWATCHER(type)
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
double z1
Definition: BscTest.cc:145
int detLevels(const G4VTouchable *) const
Definition: BscTest.cc:593
uint32_t getUnitID() const
Definition: BscG4Hit.h:56
std::string fOutputFile
Definition: BscTest.cc:155
TH1F * getHisto(Int_t Number)
Definition: BscTest.cc:297
double npart
Definition: HydjetWrapper.h:46
int zside(DetId const &)
G4ThreeVector lastpo
Definition: BscTest.cc:142
TObjArray * fHistNamesArray
Definition: BscTest.cc:93
int iev
Definition: BscTest.cc:123
TObjArray * fHistArray
Definition: BscTest.cc:92
G4double sumStepc
Definition: BscTest.cc:138
std::string fRecreateFile
Definition: BscTest.cc:156
const Double_t pi
G4String detName(const G4VTouchable *, int, int) const
Definition: BscTest.cc:602
double rinCalo
Definition: BscTest.cc:133
int verbosity
Definition: BscTest.cc:135
double z4
Definition: BscTest.cc:145
const char * fTypeTitle
Definition: BscTest.cc:91
int whichevent
Definition: BscTest.cc:151
void histInit(const char *name, const char *title, Int_t nbinsx, Axis_t xlow, Axis_t xup)
Definition: BscTest.cc:270
void detectorLevel(const G4VTouchable *, int &, int *, G4String *) const
Definition: BscTest.cc:612
std::string fDataLabel
Definition: BscTest.cc:154
G4double entot0
Definition: BscTest.cc:125
BscAnalysisHistManager * TheHistManager
Definition: BscTest.cc:153
double z3
Definition: BscTest.cc:145
Tan< T >::type tan(const T &t)
Definition: Tan.h:22
void update(const BeginOfJob *run) override
This routine will be called when the appropriate signal arrives.
Definition: BscTest.cc:354
BscTest(const edm::ParameterSet &p)
Definition: BscTest.cc:162
static void unpackBscIndex(const unsigned int &idx)
double zinCalo
Definition: BscTest.cc:133
BscNumberingScheme * theBscNumberingScheme
Definition: BscTest.cc:121
ii
Definition: cuy.py:589
TH2F * getHisto2(Int_t Number)
Definition: BscTest.cc:313
G4double sumEnerDeposit
Definition: BscTest.cc:138
G4double sumStepl
Definition: BscTest.cc:138
TNtuple * bsceventntuple
Definition: BscTest.cc:149
~BscTest() override
Definition: BscTest.cc:188
int lastTrackID
Definition: BscTest.cc:134
float getEnergyLoss() const
Definition: BscG4Hit.h:69
const G4ThreeVector & getEntry() const
Definition: BscG4Hit.h:29
int itrk
Definition: BscTest.cc:124
const G4ThreeVector & getExitLocalP() const
Definition: BscG4Hit.h:35
Float_t bsceventarray[1]
Definition: BscTest.cc:148
step
Definition: StallMonitor.cc:98
TFile bscOutputFile
Definition: BscTest.cc:150
G4THitsCollection< BscG4Hit > BscG4HitCollection
ntbsc_elements
Definition: BscTest.cc:159
BscAnalysisHistManager(const TString &managername)
Definition: BscTest.cc:216
const G4ThreeVector & getEntryLocalP() const
Definition: BscG4Hit.h:32
void writeToFile(const TString &fOutputFile, const TString &fRecreateFile)
Definition: BscTest.cc:256
G4double tracklength0
Definition: BscTest.cc:125
~BscAnalysisHistManager() override
Definition: BscTest.cc:230