source: trunk/Mars/ceres.cc@ 14866

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