-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_mc_single.C
executable file
·275 lines (223 loc) · 8.92 KB
/
run_mc_single.C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/**
* Geant4 Simulation Macro
*
* - Geant4 settings can be found in SpiRITROOT/gconfig directory.
* By default, QGSP_BERT_HP_EMY physics list is used.
*
* - Geometry and material files can be found in SpiRITROOT/geometry
* directory. Make sure you run geomSpiRIT.C to create geometry
* file. Materials and Geant4 cuts are defined in media.geo.
*
* - Default Field map is constant field. If you want to use field map
* file, 1) put SamuraiMap_0.5T.dat in SpiRIT/input directory and
* 2) set varialble 'useFieldMapFile' kTRUE. This setting may
* cause longer simulation time.
*
* - By default, STEventGenGenerator is used for event generator.
* You may use other classes, but you will have to modify this code
* and it is not recommanded. The format of event generator file
* is described in STEventGenGenerator.hh.
*
* - How To Run
* In bash,
* > root 'run_mc.C("name", "event", kFALSE)'
* You do not need to open this file to change variables.
*
* - Varialbles
* @ name : Name of simulation. Just for indication.
* @ event : Name of event file name.
* @ event : kTRUE to use constant field, with 0.5T.
* kFALSE to use field map file 'SamuraiMap_0.5T.dat'.
*/
#include <sys/stat.h>
#include <sys/types.h>
/*************************************************************
* Functions needed to create directory if it is non-existance*
* Only call recursive_mkdir. There's no need to call rek_mkdir *
**************************************************************/
void rek_mkdir(char* path);
void recursive_mkdir(const char* t_path);
//************End of Directory creation function****************
void AddIons(FairRunSim *fRun, TString event);
void run_mc_single
(
TString name = "singlemc_neuland_test", // XXX
Int_t nEvent = 100,
TString inputDir = "",
TString outputDir = "data/",
Bool_t useFieldMapFile = kTRUE,
Int_t nSplit=0
)
{
TString pwd = "/home/ejungwoo/spiritroot/neuland_macros/configures/";
TString generatorName = pwd + name + ".egen"; // XXX
//////////////////////////////////////////////////////////
// //
// In general, the below parts need not be touched. //
// //
//////////////////////////////////////////////////////////
// -----------------------------------------------------------------
// Set environment
TString workDir = gSystem -> Getenv("VMCWORKDIR");
TString geomDir = workDir + "/geometry";
TString g4ConfDir = workDir + "/gconfig_neuland"; // XXX
TString dataDir = workDir + "/neuland_macros/data/";
if(outputDir.IsNull())
outputDir = dataDir;
TString version; {
TString spiritroot = TString(gSystem -> Getenv("VMCWORKDIR"))+"/";
TString vfname = spiritroot + "VERSION.compiled";
std::ifstream vfile(vfname);
vfile >> version;
vfile.close();
}
name = name + "." + version;
// -----------------------------------------------------------------
// Set file names
TString outputFile = outputDir + name + ".mc.root";
TString outParFile = outputDir + name + ".params.root";
TString loggerFile = outputDir + "log_" + name + ".mc.txt";
// ----------------------------------------------------------------
// Create directories for all the output if they are non-existent
recursive_mkdir(outputFile.Data());
recursive_mkdir(outParFile.Data());
recursive_mkdir(loggerFile.Data());
// -----------------------------------------------------------------
// MC initialization
gRandom -> SetSeed(0);
gSystem -> Setenv("GEOMPATH", geomDir.Data());
gSystem -> Setenv("CONFIG_DIR", g4ConfDir.Data());
// -----------------------------------------------------------------
// Set FairRunSim
FairRunSim* fRun = new FairRunSim();
fRun -> SetName("TGeant4");
fRun -> SetOutputFile(outputFile);
fRun -> SetGenerateRunInfo(kFALSE);
fRun -> SetMaterials("media.geo");
// AddIons(fRun, event);
// -----------------------------------------------------------------
// Logger
FairLogger *fLogger = FairLogger::GetLogger();
fLogger -> SetLogFileName(loggerFile);
fLogger -> SetLogToScreen(kTRUE);
fLogger -> SetLogToFile(kTRUE);
fLogger -> SetLogVerbosityLevel("LOW");
// -----------------------------------------------------------------
// Geometry
FairModule* fCave= new FairCave("CAVE");
fCave -> SetGeometryFileName("cave_vacuum_wneuland.geo"); // XXX
fRun -> AddModule(fCave);
FairDetector* fSpiRIT = new STDetector("STDetector", kTRUE);
fSpiRIT -> SetGeometryFileName("geomSpiRIT_nl.root"); // XXX
fRun -> AddModule(fSpiRIT);
/*
auto fEvent = new STSingleTrackGenerator();
fEvent->ReadConfig(generatorName.Data());
fEvent->SetVertexBegin(nSplit);
//fEvent->SetCocktailEvent(300.);
//fEvent->SetUniformRandomDirection(true);
//fEvent->SetParticleList({2212, 211, -211, 1000010020, 1000010030});
fEvent->SetNEvents(100);
cout << fEvent->GetNEvents() << endl;
*/
auto fEvent = new STEventGenGenerator(generatorName.Data());
fEvent->SetPrimaryVertex(0.,-21.33,-.89);
FairPrimaryGenerator* fGenerator = new FairPrimaryGenerator();
fGenerator -> AddGenerator(fEvent);
fRun -> SetGenerator(fGenerator);
// -----------------------------------------------------------------
// Field
if (useFieldMapFile) {
STFieldMap *fField = new STFieldMap("samurai_field_map","A");
fField -> SetPosition(0., -20.43, 58.);
fRun -> SetField(fField);
}
else {
FairConstField *fField = new FairConstField();
fField -> SetField(0., 5., 0.);
fField -> SetFieldRegion(-150, 150, -150, 150, -150, 150);
fRun -> SetField(fField);
}
// -----------------------------------------------------------------
// Set data base
FairParRootFileIo* fMCPar = new FairParRootFileIo(kTRUE);
fMCPar -> open(outParFile.Data());
FairRuntimeDb* fDb = fRun -> GetRuntimeDb();
fDb -> setOutput(fMCPar);
fDb -> saveOutput();
fDb -> print();
// -----------------------------------------------------------------
// Run initialization
fRun -> SetStoreTraj(kTRUE);
fRun -> Init();
FairTrajFilter* trajFilter = FairTrajFilter::Instance();
trajFilter -> SetStorePrimaries(kTRUE);
trajFilter -> SetStoreSecondaries(kTRUE);
// -----------------------------------------------------------------
// Run
fRun -> Run( nEvent==-1 ? fEvent->GetNEvents() : nEvent );
// -----------------------------------------------------------------
// Summary
cout << endl << endl;
cout << "Macro finished succesfully." << endl << endl;
cout << "- Output file : " << outputFile << endl << endl;
gApplication -> Terminate();
}
void AddIons(FairRunSim *fRun, TString event)
{
TString symbol[50] = {"H", "He", "Li", "Be", "B", "C", "N", "O", "F", "Ne",
"Na", "Mg", "Al", "Si", "P", "S", "Cl", "Ar", "K", "Ca",
"Sc", "Ti", "V", "Cr", "Mn", "Fe", "Co", "Ni", "Cu", "Zn",
"Ga", "Ge", "As", "Se", "Br", "Kr", "Rb", "Sr", "Y", "Zr",
"Nb", "Mo", "Tc", "Ru", "Rh", "Pd", "Ag", "Cd", "In", "Sn"};
TString input_dir = gSystem -> Getenv("VMCWORKDIR");
TString fGenFileName = input_dir + "/input/" + event;
LOG(INFO) << "Opening EventGen file for adding ions: " << fGenFileName <<FairLogger::endl;
ifstream fGenFile(fGenFileName.Data());
if(!fGenFile.is_open())
LOG(FATAL) << "Cannont open EventGen file: " << fGenFileName << FairLogger::endl;
vector<Int_t> ions;
Int_t nEvents, pdg, eventID, nTracks;
Double_t tmp, b;
fGenFile >> nEvents;
for (Int_t iEvent = 0; iEvent < nEvents; iEvent++) {
fGenFile >> eventID >> nTracks >> b;
for(Int_t iTrack = 0; iTrack < nTracks; iTrack++) {
fGenFile >> pdg >> tmp >> tmp >> tmp;
if (pdg > 3000)
ions.push_back(pdg);
}
}
fGenFile.close();
std::sort(ions.begin(), ions.end());
ions.resize(std::distance(ions.begin(), std::unique(ions.begin(), ions.end())));
for (Int_t iIon = 0; iIon < ions.size(); iIon++) {
auto z = (ions.at(iIon)%10000000)/10000;
auto a = (ions.at(iIon)%10000)/10;
fRun -> AddNewIon(new FairIon(Form("%d", a) + symbol[z - 1], z, a, z));
}
}
void rek_mkdir(char* path)
{
char *sep = strrchr(path, '/' );
if(sep != NULL) {
*sep = 0;
rek_mkdir(path);
*sep = '/';
}
if( mkdir(path,0755) && errno != EEXIST )
printf("error while trying to create '%s'\n%m\n",path );
}
void recursive_mkdir(const char* t_path)
{
char* path = new char[strlen(t_path) + 1];
strcpy(path, t_path);
char *sep = strrchr(path, '/' );
if(sep ) {
char *path0 = strdup(path);
path0[ sep - path ] = 0;
rek_mkdir(path0);
free(path0);
}
delete[] path;
}