BDSIM
BDSIM is a Geant4 extension toolkit for simulation of particle transport in accelerator beamlines.
Loading...
Searching...
No Matches
EventAnalysisOrbit.cc
1/*
2Beam Delivery Simulation (BDSIM) Copyright (C) Royal Holloway,
3University of London 2001 - 2023.
4
5This file is part of BDSIM.
6
7BDSIM is free software: you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published
9by the Free Software Foundation version 3 of the License.
10
11BDSIM is distributed in the hope that it will be useful, but
12WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with BDSIM. If not, see <http://www.gnu.org/licenses/>.
18*/
19#include "EventAnalysisOrbit.hh"
20#include "RBDSException.hh"
21#include "SamplerAnalysis.hh"
22
23#include <iomanip>
24#include <iostream>
25#include <string>
26#include <vector>
27
28#include "TChain.h"
29#include "TFile.h"
30#include "TString.h"
31#include "TTree.h"
32
33ClassImp(EventAnalysisOrbit)
34
37{;}
38
40 TChain* chainIn,
41 bool perEntryAnalysis,
42 bool processSamplersIn,
43 bool debugIn,
44 bool printOutIn,
45 double printModuloFraction,
46 bool emittanceOnTheFlyIn,
47 long int eventStartIn,
48 long int eventEndIn):
49 EventAnalysis(eventIn, chainIn, perEntryAnalysis, processSamplersIn,
50 debugIn, printOutIn, printModuloFraction, emittanceOnTheFlyIn,
51 eventStartIn, eventEndIn)
52{;}
53
55{
56 ss.clear();
57 x.clear();
58 xp.clear();
59 y.clear();
60 yp.clear();
61 elementName.clear();
62}
63
65{
66 if (index > entries-1)
67 {
68 std::string errString = "Orbit index: " + std::to_string(index) +
69 " greater than number of events: " + std::to_string(entries) + " in file minus one";
70 throw RBDSException(errString);
71 }
72
73 int nSamplers = (int)samplerAnalyses.size();
74
75 std::cout << "Getting orbit " << index << std::endl;
76 chain->GetEntry(index);
77 std::cout << "Loaded" << std::endl;
78
79 int counter = 0;
80 for (auto s : samplerAnalyses)
81 {
82 std::cout << "\rSampler #" << std::setw(6) << counter << " of " << nSamplers;
83 std::cout.flush();
84 if (s->s->n > 0)
85 {// valid entry on that sampler
86 ss.push_back(s->s->S);
87 x.push_back(s->s->x[0]);
88 xp.push_back(s->s->xp[0]);
89 y.push_back(s->s->y[0]);
90 yp.push_back(s->s->yp[0]);
91 elementName.push_back(s->s->samplerName);
92 }
93 counter++;
94 }
95 std::cout << std::endl;
96}
97
99{
100 f->cd("/");
101 TTree* orbitTree = new TTree("Orbit", "Orbit");
102
103 double dss;
104 double dx;
105 double dxp;
106 double dy;
107 double dyp;
108 std::string delementName;
109
110 orbitTree->Branch("s", &dss, "s/D");
111 orbitTree->Branch("x", &dx, "x/D");
112 orbitTree->Branch("xp", &dxp, "xp/D");
113 orbitTree->Branch("y", &dy, "y/D");
114 orbitTree->Branch("yp", &dyp, "yp/D");
115 orbitTree->Branch("elementName", &delementName);
116
117 for (int i = 0; i < (int)ss.size(); ++i)
118 {
119 dss = ss.at(i);
120 dx = x.at(i);
121 dxp = xp.at(i);
122 dy = y.at(i);
123 dyp = yp.at(i);
124 delementName = elementName.at(i);
125 orbitTree->Fill();
126 }
127
128 orbitTree->Write();
129 f->Close();
130}
long int entries
Number of entries in the chain.
Definition: Analysis.hh:99
Simple analysis to pull out first hit in each sampler.
std::vector< std::string > elementName
Temporary storage for orbit.
std::vector< double > y
Temporary storage for orbit.
std::vector< double > ss
Temporary storage for orbit.
std::vector< double > xp
Temporary storage for orbit.
void Clear()
Empty the member vectors of their data.
void ExtractOrbit(int index)
Extract an orbit from the data.
std::vector< double > x
Temporary storage for orbit.
std::vector< double > yp
Temporary storage for orbit.
void WriteOrbit(TFile *f)
Write orbit to a ROOT file.
Event level analysis.
std::vector< SamplerAnalysis * > samplerAnalyses
Holder for sampler analysis objects.
Event loader.
Definition: Event.hh:50
General exception with possible name of object and message.