source: trunk/Mars/ceres.cc@ 19672

Last change on this file since 19672 was 19627, checked in by tbretz, 5 years ago
Implemented the possiblity to read a file list instead of a sequence file (using the --list) option and specifying a file with the .txt ending
File size: 14.6 KB
Line 
1#include <TClass.h>
2#include <TSystem.h>
3#include <TApplication.h>
4#include <TObjectTable.h>
5
6#include <TVector2.h>
7
8#include "MArray.h"
9
10#include "MLog.h"
11#include "MLogManip.h"
12
13#include "MStatusDisplay.h"
14
15#include "MEnv.h"
16#include "MArgs.h"
17#include "MDirIter.h"
18
19#include "MJSimulation.h"
20
21
22using namespace std;
23
24static void StartUpMessage()
25{
26 gLog << all << endl;
27
28 // 1 2 3 4 5
29 // 12345678901234567890123456789012345678901234567890
30 gLog << "====================================================" << endl;
31 gLog << " Ceres - MARS V" << MARSVER << endl;
32 gLog << " MARS - Camera Electronics and REflector Simulation" << endl;
33 gLog << " Compiled with ROOT v" << ROOT_RELEASE << " on <" << __DATE__ << ">" << endl;
34 gLog << "====================================================" << endl;
35 gLog << endl;
36}
37
38static void Usage()
39{
40 gLog << all << endl;
41 gLog << "Sorry the usage is:" << endl;
42 gLog << " ceres [options] [inputfiles|sequence.txt]" << endl << endl;
43 gLog << " inputfiles MMCS (CORSIKA) binary (cherenkov) files, wildcards allowed." << endl;
44 gLog << " sequence.txt A sequence file." << endl;
45 gLog << " Root Options:" << endl;
46 gLog << " -b Batch mode (no graphical output to screen)" << endl<<endl;
47 gLog << " Options:" << endl;
48 gLog.Usage();
49 gLog << " --debug-env=0 Disable debugging setting resources <default>" << endl;
50 gLog << " --debug-env[=1] Display untouched resources after program execution" << endl;
51 gLog << " --debug-env=2 Display untouched resources after eventloop setup" << endl;
52 gLog << " --debug-env=3 Debug setting resources from resource file and" << endl;
53 gLog << " command line" << endl;
54 gLog << " --debug-mem Debug memory usage" << endl << endl;
55 gLog << " --rc=Name:option Set or overwrite a resource of the resource file." << endl;
56 gLog << " (Note, that this option can be used multiple times)" << endl;
57 gLog << endl;
58 gLog << " Output options:" << endl;
59 gLog << " -q Quit when job is finished" << endl;
60 gLog << " -f Force overwrite of existing files" << endl;
61 gLog << " -ff Force reading of file even if problems occur" << endl;
62 gLog << " --out=path Path to write the all results to [def=local path]" << endl;
63 gLog << " --ind=path Input path of Corsika files if sequence used" << endl;
64 gLog << " [def=standard path in datacenter]" << endl;
65 gLog << " --outf=filename Output filename. Combines all input files into" << endl;
66 gLog << " one output file. (Note that the output file will" << endl;
67 gLog << " contain only the headers corresponding to the" << endl;
68 gLog << " first input file)" << endl;
69 gLog << " --dev-null Suppress output of files (for test purpose)" << endl;
70 gLog << " --print-seq Print Sequence information [sequence only]" << endl;
71 gLog << " --print-files Print Files taken from Sequence" << endl;
72 gLog << " --print-found Print Files found from Sequence" << endl;
73 gLog << " --config=ceres.rc Resource file [default=reflector.rc]" << endl;
74 gLog << endl;
75 gLog << " --mode=pedestal Execution mode. Produce either pedestals," << endl;
76 gLog << " --mode=calibration calibration data (no input files required) or" << endl;
77 gLog << " --mode=data process data files [default]" << endl << endl;
78 gLog << " --run-number=# Optionally set the run number to simulate" << endl;
79 gLog << " --fits Write FITS output files instead of root files." << endl;
80 gLog << " --list The specified .txt file contains a file list" << endl;
81 gLog << " (see MDirIter::ReadFile)." << endl << endl;
82 gLog << endl;
83// gLog << " -f: force reading of runheader" << endl;
84 gLog << " --version, -V Show startup message with version number" << endl;
85 gLog << " -?, -h, --help This help" << endl << endl;
86 gLog << "Background:" << endl;
87 gLog << " Ceres, formal designation 1 Ceres, is the smallest identified dwarf planet in" << endl;
88 gLog << " the Solar System and the only one in the asteroid belt. It was discovered on" << endl;
89 gLog << " January 1, 1801, by Giuseppe Piazzi, and is named after the Roman goddess" << endl;
90 gLog << " Ceres, the goddess of growing plants, the harvest, and of motherly love." << endl;
91 gLog << " With a diameter of about 950km, Ceres is by far the largest and most massive" << endl;
92 gLog << " body in the asteroid belt, and contains a third of the belt's total mass." << endl;
93 gLog << " Recent observations have revealed that it is spherical, unlike the irregular" << endl;
94 gLog << " shapes of smaller bodies with lower gravity. The surface of Ceres is probably" << endl;
95 gLog << " made of a mixture of water ice and various hydrated minerals like carbonates" << endl;
96 gLog << " and clays. Ceres appears to be differentiated into a rocky core and ice mantle." << endl;
97 gLog << " It may harbour an ocean of liquid water underneath its surface, which makes it" << endl;
98 gLog << " a potential target in the search for extraterrestrial life." << endl;
99 gLog << " Ceres' apparent magnitude ranges from 6.7 to 9.3, hence at its brightest is" << endl;
100 gLog << " still too dim to be seen with the naked eye. On Sept. 27, 2007, NASA launched" << endl;
101 gLog << " the Dawn space probe to explore Vesta and Ceres." << endl << endl;
102 gLog << "Example:" << endl;
103 gLog << " ceres -f --out=outpath/ cer000001 cer000002" << endl;
104 gLog << endl;
105}
106
107static void PrintFiles(const MArgs &arg, Bool_t allopt)
108{
109 const char *prep = allopt ? "Found" : "Scheduled";
110
111 gLog << all;
112 gLog.Separator(Form("%s Data Files", prep));
113 for (int i=0; i<arg.GetNumArguments(); i++)
114 if (!allopt || gSystem->AccessPathName(arg.GetArgumentStr(i), kFileExists)==0)
115 gLog << arg.GetArgumentStr(i) << endl;
116 gLog << endl;
117}
118
119
120static void PrintFiles(const MSequence &seq, const TString &kInpathD, Bool_t allopt)
121{
122 const char *prep = allopt ? "Found" : "Scheduled";
123
124 MDirIter Next;
125 seq.GetRuns(Next, MSequence::kCorsika, kInpathD);
126
127 gLog << all;
128 gLog.Separator(Form("%s Data Files", prep));
129 Next.Print(allopt?"all":"");
130 gLog << endl;
131}
132
133int main(int argc, char **argv)
134{
135 if (!MARS::CheckRootVer())
136 return 0xff;
137
138 MLog::RedirectErrorHandler(MLog::kColor);
139
140 //
141 // Evaluate arguments
142 //
143 MArgs arg(argc, argv);
144 gLog.Setup(arg);
145
146 StartUpMessage();
147
148 if (arg.HasOnly("-V") || arg.HasOnly("--version"))
149 return 0;
150
151 if (arg.HasOnly("-?") || arg.HasOnly("-h") || arg.HasOnly("--help"))
152 {
153 Usage();
154 return 2;
155 }
156
157 const Bool_t kBatch = arg.HasOnlyAndRemove("-b");
158 //const Int_t kCompLvl = arg.GetIntAndRemove("--comp=", 1);
159 const Bool_t kForce = arg.HasOnlyAndRemove("-ff");
160 const Bool_t kDebugMem = arg.HasOnlyAndRemove("--debug-mem");
161 const Bool_t kNullOut = arg.HasOnlyAndRemove("--dev-null");
162 Int_t kDebugEnv = arg.HasOnlyAndRemove("--debug-env") ? 1 : 0;
163 kDebugEnv = arg.GetIntAndRemove("--debug-env=", kDebugEnv);
164
165 const Bool_t kPrintSeq = arg.HasOnlyAndRemove("--print-seq");
166 const Bool_t kPrintFiles = arg.HasOnlyAndRemove("--print-files");
167 const Bool_t kPrintFound = arg.HasOnlyAndRemove("--print-found");
168
169 const Bool_t kQuit = arg.HasOnlyAndRemove("-q");
170 const Bool_t kOverwrite = arg.HasOnlyAndRemove("-f");
171
172 const TString kConfig = arg.GetStringAndRemove("--config=", "ceres.rc");
173 const TString kInpath = arg.GetStringAndRemove("--ind=", "");
174 const TString kOutpath = arg.GetStringAndRemove("--out=", ".");
175 const TString kOutfile = arg.GetStringAndRemove("--outf=", "");
176
177 const Int_t kRunNumber = arg.GetIntAndRemove("--run-number=", -1);
178
179 const TString kOpMode = arg.GetStringAndRemove("--mode=", "data");
180 const Bool_t kFitsFile = arg.HasOnlyAndRemove("--fits");
181 const Bool_t kFileList = arg.HasOnlyAndRemove("--list");
182
183 Int_t opmode = -1;
184 if (TString("data").BeginsWith(kOpMode, TString::kIgnoreCase))
185 opmode = MJSimulation::kModeData;
186 if (TString("pointrun").BeginsWith(kOpMode, TString::kIgnoreCase))
187 opmode = MJSimulation::kModePointRun;
188 if (TString("pedestal").BeginsWith(kOpMode, TString::kIgnoreCase))
189 opmode = MJSimulation::kModePed;
190 if (TString("calibration").BeginsWith(kOpMode, TString::kIgnoreCase))
191 opmode = MJSimulation::kModeCal;
192
193 if (opmode<0)
194 {
195 gLog << err << "ERROR - Wrong mode specified..." << endl;
196 Usage();
197 return 2;
198 }
199
200 //
201 // check for the right usage of the program (number of arguments)
202 if (arg.GetNumArguments()<1 && opmode==MJSimulation::kModeData)
203 {
204 gLog << warn << "WARNING - Wrong number of arguments..." << endl;
205 Usage();
206 return 2;
207 }
208
209 //
210 // for compatibility with the first version of ceres
211 //
212 if (arg.GetNumArguments()==1 && opmode==MJSimulation::kModeData)
213 {
214 if (arg.GetArgumentStr(0)=="pedestal")
215 {
216 opmode = MJSimulation::kModePed;
217 arg.RemoveArgument(0);
218 }
219 if (arg.GetArgumentStr(0)=="calibration")
220 {
221 opmode = MJSimulation::kModeCal;
222 arg.RemoveArgument(0);
223 }
224 }
225
226 if (arg.GetNumArguments()>0 && opmode!=MJSimulation::kModeData)
227 {
228 gLog << warn << "WARNING - No arguments allowed in this mode..." << endl;
229 Usage();
230 return 2;
231 }
232
233 //
234 // Now we access/read the resource file. This will remove all
235 // --rc= from the list of arguments.
236 //
237 MEnv env(kConfig, "ceres.rc");
238 if (!env.IsValid())
239 {
240 gLog << err << "ERROR - Reading resource file " << kConfig << "." << endl;
241 return 0xfe;
242 }
243
244 // And move the resource options from the command line to the MEnv
245 if (!env.TakeEnv(arg, kDebugEnv>2))
246 return 0xfd;
247
248 //
249 // check for the right usage of the program (number of options)
250 //
251 if (arg.GetNumOptions()>0)
252 {
253 gLog << warn << "WARNING - Unknown commandline options..." << endl;
254 arg.Print("options");
255 gLog << endl;
256 return 2;
257 }
258
259 //
260 // Setup sequence file and check for its existance
261 //
262 TString kSequence = arg.GetNumArguments()==1 ? arg.GetArgumentStr(0) : "";
263
264 //
265 // Check if the first argument (if any) is a sequence file or not
266 //
267 if (!kSequence.EndsWith(".txt"))
268 kSequence = "";
269
270 //
271 // Something special for datacenter access
272 //
273 if (!kSequence.IsNull() && !MSequence::InflateSeq(kSequence, kTRUE))
274 return 2;
275
276 //
277 // Setup sequence and check its validity
278 //
279 MSequence seq(kFileList ? "" : kSequence, kInpath);
280 if (!kSequence.IsNull() && !kFileList)
281 {
282 if (kPrintSeq)
283 {
284 gLog << all;
285 gLog.Separator(kSequence);
286 seq.Print();
287 gLog << endl;
288 }
289 if (seq.IsValid() && !seq.IsMonteCarlo())
290 {
291 gLog << err << "Sequence is not a Monte Carlo Sequence." << endl << endl;
292 return 2;
293 }
294 if (!seq.IsValid())
295 {
296 gLog << err << "Sequence read but not valid!" << endl << endl;
297 return 2;
298 }
299
300 //
301 // Process print options
302 //
303 if (kPrintFiles)
304 PrintFiles(seq, kInpath, kFALSE);
305 if (kPrintFound)
306 PrintFiles(seq, kInpath, kTRUE);
307 }
308 else
309 {
310 if (kFileList)
311 {
312 arg.RemoveArgument(0);
313
314 MDirIter Next;
315 Next.ReadList(kSequence);
316
317 TString name;
318 while (!(name=Next()).IsNull())
319 arg.AddEntry(name);
320 }
321
322 if (kPrintFiles)
323 PrintFiles(arg, kFALSE);
324 if (kPrintFound)
325 PrintFiles(arg, kTRUE);
326 }
327
328 //
329 // Initialize root
330 //
331 TVector2::Class()->IgnoreTObjectStreamer();
332 MArray::Class()->IgnoreTObjectStreamer();
333 MParContainer::Class()->IgnoreTObjectStreamer();
334
335 TApplication app("ceres", &argc, argv);
336 if ((!gROOT->IsBatch() && !gClient) || (gROOT->IsBatch() && !kBatch))
337 {
338 gLog << err << "Bombing... maybe your DISPLAY variable is not set correctly!" << endl;
339 return 1;
340 }
341
342 //
343 // Update frequency by default = 1Hz
344 //
345 MStatusDisplay *d = new MStatusDisplay;
346
347 // From now on each 'Exit' means: Terminate the application
348 d->SetBit(MStatusDisplay::kExitLoopOnExit);
349 d->SetTitle(seq.IsValid() ? Form("-- Ceres: %s --", kSequence.Data()) : "-- Ceres --");
350
351 if (kDebugMem)
352 TObject::SetObjectStat(kTRUE);
353
354 //
355 // Do star in a block (debug mem)
356 //
357 {
358 MJSimulation job(seq.IsValid() ? Form("Ceres #%d", seq.GetSequence()) : "Ceres");
359 //job.SetSequence(seq);
360 job.SetEnv(&env);
361 job.SetEnvDebug(kDebugEnv);
362 job.SetDisplay(d);;
363 job.SetOverwrite(kOverwrite);
364 job.SetPathOut(kOutpath);
365 job.SetFileOut(kOutfile);
366 job.SetNullOut(kNullOut);
367 job.SetForceMode(kForce);
368 job.SetWriteFitsFile(kFitsFile);
369 job.SetMode(opmode);
370 job.SetCommandLine(MArgs::GetCommandLine(argc, argv));
371 job.SetRunNumber(kRunNumber);
372
373 // job.SetPathIn(kInpath); // not yet needed
374
375 if (!job.Process(arg, seq))
376 {
377 gLog << err << "Calculation of ceres failed." << endl << endl;
378 return 2;
379 }
380
381 if (kDebugEnv>0)
382 env.PrintUntouched();
383
384 if (!job.GetDisplay())
385 {
386 gLog << warn << "Display closed by user... execution aborted." << endl << endl;
387 return 1;
388 }
389 }
390
391 if (kBatch || kQuit)
392 delete d;
393 else
394 {
395 // From now on each 'Close' means: Terminate the application
396 d->SetBit(MStatusDisplay::kExitLoopOnClose);
397
398 // Wait until the user decides to exit the application
399 app.Run(kFALSE);
400 }
401
402 if (TObject::GetObjectStat())
403 {
404 TObject::SetObjectStat(kFALSE);
405 gObjectTable->Print();
406 }
407
408 return 0;
409}
Note: See TracBrowser for help on using the repository browser.