source: trunk/MagicSoft/Mars/mjobs/MSequence.cc@ 7189

Last change on this file since 7189 was 7179, checked in by tbretz, 19 years ago
*** empty log message ***
File size: 15.2 KB
Line 
1/* ======================================================================== *\
2!
3! *
4! * This file is part of MARS, the MAGIC Analysis and Reconstruction
5! * Software. It is distributed to you in the hope that it can be a useful
6! * and timesaving tool in analysing Data of imaging Cerenkov telescopes.
7! * It is distributed WITHOUT ANY WARRANTY.
8! *
9! * Permission to use, copy, modify and distribute this software and its
10! * documentation for any purpose is hereby granted without fee,
11! * provided that the above copyright notice appear in all copies and
12! * that both that copyright notice and this permission notice appear
13! * in supporting documentation. It is provided "as is" without express
14! * or implied warranty.
15! *
16!
17!
18! Author(s): Thomas Bretz, 8/2004 <mailto:tbretz@astro.uni-wuerzburg.de>
19!
20! Copyright: MAGIC Software Development, 2004
21!
22!
23\* ======================================================================== */
24
25/////////////////////////////////////////////////////////////////////////////
26//
27// MSequence
28//
29// This class describes a sequence. For sequences see:
30// http://magic.astro.uni-wuerzburg.de/mars/db/queryseq.html
31//
32// A sequence is a collection of runs which should be used together.
33// Any run can be contained only once.
34//
35// Here is an example how a file describing a sequence could look like:
36//
37// ===========================================================================
38//
39// sequence.txt
40// ------------
41//
42// # Sequence number (identifier)
43// Sequence: 31015
44// # Observation Period (used to get the path-names)
45// Period: 18
46// # Date of sunrise of the observation night
47// Night: 2004-06-24
48//
49// # Start time of the sequence (first data run)
50// Start: 2004-06-24 03:12:42
51// # Run number of last data run in sequence
52// LastRun: 31032
53// # Project name of data-runs of sequence
54// Project: 3EG2033+41
55// # Source name of all runs of sequence
56// Source: 3EG2033+41
57// # Trigger table of data-runs of sequence
58// TriggerTable: L1_4NN:L2_DEFAULT
59// # HV Setting table of data-runs of sequence
60// HvSettings: HVSettings_FF36q
61// # Total number of data-events in sequence
62// NumEvents: 250914
63//
64// # List of all runs of this sequence
65// Runs: 31015 31016 31017 31018 31019 31020 31021 31022 31023 31024 31025 31026 31027 31028 31029 31030 31031 31032
66//
67// # List of all calibration runs of this sequence
68// CalRuns: 31015 31016 31017
69// # List of pedestal runs belonging to the calibration runs of this sequence
70// PedRuns: 31018
71// # List of all data runs belonging to this sequence
72// DatRuns: 31019 31020 31022 31023 31024 31025 31027 31028 31030 31032
73//
74// # List of run types of all runs
75// 31015: C
76// 31016: C
77// 31017: C
78// 31018: P
79// 31019: D
80// 31020: D
81// 31021: P
82// 31022: D
83// 31023: D
84// 31024: D
85// 31025: D
86// 31026: P
87// 31027: D
88// 31028: D
89// 31029: P
90// 31030: D
91// 31031: P
92// 31032: D
93//
94// ===========================================================================
95//
96// For special cases you can also setup a sequence directly from a macro,
97// for example:
98//
99// MDirIter pediter, datiter, caliter;
100//
101// MSequence seq;
102// seq.SetNight("2004-07-06");
103// seq.AddPedRuns(31751);
104// seq.AddCalRuns(31752);
105// seq.AddDatRuns(31753, 31764);
106// seq.SetupPedRuns(pediter);
107// seq.SetupCalRuns(caliter);
108// seq.SetupDatRuns(datiter);
109//
110// or
111//
112// MDirIter iter;
113//
114// MSequence seq;
115// seq.SetNight("2004-07-06");
116// seq.AddRuns(31753, 31764);
117// seq.SetupRuns(iter);
118// seq.SetupPedRuns(iter, "/mypath", "[DPC]");
119//
120/////////////////////////////////////////////////////////////////////////////
121#include "MSequence.h"
122
123#include <stdlib.h>
124
125#include <TEnv.h>
126#include <TRegexp.h>
127#include <TSystem.h> // TSystem::ExpandPath
128
129#include "MLog.h"
130#include "MLogManip.h"
131
132#include "MAstro.h"
133#include "MString.h"
134#include "MDirIter.h"
135
136ClassImp(MSequence);
137
138using namespace std;
139
140MSequence::~MSequence()
141{
142 /*
143 TExMapIter iter(&fFileNames);
144
145 Long_t key, val;
146
147 while (iter.Next(key, val))
148 delete (TString*)val;
149 */
150}
151
152
153// --------------------------------------------------------------------------
154//
155// Copy the run numbers from the TString runs into the TArrayI data.
156// Runs which are twice in the list are only added once. In this case
157// a warning is emitted.
158//
159void MSequence::Split(TString &runs, TArrayI &data) const
160{
161 const TRegexp regexp("[0-9]+");
162
163 data.Set(0);
164 runs = runs.Strip(TString::kTrailing);
165
166 while (!runs.IsNull())
167 {
168 TString num = runs(regexp);
169
170 const Int_t run = atoi(num.Data());
171 const Int_t n = data.GetSize();
172
173 // skip already existing entries
174 int i;
175 for (i=0; i<n; i++)
176 if (data[i] == run)
177 break;
178
179 if (i<n)
180 {
181 *fLog << warn << "WARNING - Run #" << run << " alraedy in list... skipped." << endl;
182 continue;
183 }
184
185 // set new entry
186 data.Set(n+1);
187 data[n] = run;
188
189 runs.Remove(0, runs.First(num)+num.Length());
190 }
191}
192
193UInt_t MSequence::SetupRuns(MDirIter &iter, const TArrayI &arr, FileType_t type, const char *path) const
194{
195 TString d(path);
196
197 // Setup path
198 if (d.IsNull())
199 {
200 d = GetStandardPath();
201 switch (type)
202 {
203 case kRawDat:
204 case kRawPed:
205 case kRawCal:
206 case kRawAll:
207 d += "rawfiles/";
208 d += fNight.GetStringFmt("%Y/%m/%d");
209 break;
210 case kRootDat:
211 case kRootPed:
212 case kRootCal:
213 case kRootAll:
214 d += "merpp/";
215 d += fNight.GetStringFmt("%Y/%m/%d");
216 break;
217 case kCalibrated:
218 d += Form("callisto/%04d/%08d/", fSequence/10000, fSequence);
219 break;
220 case kImages:
221 d += Form("star/%04d/%08d/", fSequence/10000, fSequence);
222 break;
223 }
224 }
225 else
226 gSystem->ExpandPathName(d);
227
228 for (int i=0; i<arr.GetSize(); i++)
229 {
230 // R. DeLosReyes and T. Bretz
231 // Changes to read the DAQ numbering format. Changes takes place
232 // between runs 35487 and 00035488 (2004_08_30)
233 const char *fmt = arr[i]>35487 ? "%08d_%s_*_E" : "%05d_%s_*_E";
234
235 TString n;
236 char *id="_";
237 switch (type)
238 {
239 case kRawDat:
240 case kRootDat:
241 id = "D";
242 break;
243 case kRawPed:
244 case kRootPed:
245 id = "P";
246 break;
247 case kRawCal:
248 case kRootCal:
249 id = "C";
250 break;
251 case kRawAll:
252 case kRootAll:
253 id = "[PCD]";
254 break;
255 case kCalibrated:
256 id = "Y";
257 break;
258 case kImages:
259 id = "I";
260 break;
261 }
262
263 // Create file name
264 n = fNight.GetStringFmt("%Y%m%d_");
265 n += Form(fmt, arr[i], id);
266
267 switch (type)
268 {
269 case kRawDat:
270 case kRawPed:
271 case kRawCal:
272 case kRawAll:
273 n += ".raw";
274 break;
275 default:
276 n += ".root";
277 }
278
279 // Add Path/File to TIter
280 iter.AddDirectory(d, n, 0);
281 }
282
283 return iter.GetNumEntries();
284}
285
286// --------------------------------------------------------------------------
287//
288// Read the file fname as setup file for the sequence.
289//
290void MSequence::GetFileNames(TEnv &env, const TArrayI &arr)
291{
292 /*
293 for (int i=0; i<arr.GetSize(); i++)
294 {
295 // Get run number
296 const Int_t num = arr[i];
297
298 // Check if name already set
299 if (fFileNames.GetValue(num))
300 continue;
301
302 TString *str = new TString(env.GetValue(Form("%d", num), ""));
303 fFileNames.Add(num, (Long_t)str);
304 }
305 */
306}
307
308// --------------------------------------------------------------------------
309//
310// Get a file name corresponding to the run-number num, returns 0 if n/a
311//
312const char *MSequence::GetFileName(UInt_t num)
313{
314 return 0;
315 /*
316 TString *str = (TString*)fFileNames.GetValue(num);
317 return str ? str->Data() : 0;*/
318}
319
320MSequence::LightCondition_t MSequence::ReadLightCondition(TEnv &env) const
321{
322 TString str = env.GetValue("LightCondition", "n/a");
323 if (!str.CompareTo("n/a", TString::kIgnoreCase))
324 return kNA;
325 if (!str.CompareTo("NoMoon", TString::kIgnoreCase))
326 return kNoMoon;
327 if (!str.CompareTo("Twilight", TString::kIgnoreCase))
328 return kTwilight;
329 if (!str.CompareTo("Moon", TString::kIgnoreCase))
330 return kMoon;
331
332 gLog << warn << "MSequence: LightCondition-tag not n/a, nomoon, twilight or moon." << endl;
333 return kNA;
334}
335
336// --------------------------------------------------------------------------
337//
338// Read the file fname as setup file for the sequence.
339//
340MSequence::MSequence(const char *fname)
341{
342 fName = fname;
343
344 const char *expname = gSystem->ExpandPathName(fname);
345
346 fTitle = Form("Sequence contained in file %s", expname);
347
348 TEnv env(expname);
349 delete [] expname;
350
351 TString str;
352
353 fSequence = env.GetValue("Sequence", -1);
354 fLastRun = env.GetValue("LastRun", -1);
355 fNumEvents = env.GetValue("NumEvents", -1);
356 fPeriod = env.GetValue("Period", -1);
357
358 fLightCondition = ReadLightCondition(env);
359
360 str = env.GetValue("Start", "");
361 fStart.SetSqlDateTime(str);
362 str = env.GetValue("Night", "");
363 str += " 00:00:00";
364 fNight.SetSqlDateTime(str);
365
366 fProject = env.GetValue("Project", "");
367 fSource = env.GetValue("Source", "");
368 fTriggerTable = env.GetValue("TriggerTable", "");
369 fHvSettings = env.GetValue("HvSettings", "");
370
371 str = env.GetValue("Runs", "");
372 Split(str, fRuns);
373 str = env.GetValue("CalRuns", "");
374 Split(str, fCalRuns);
375 str = env.GetValue("PedRuns", "");
376 Split(str, fPedRuns);
377 str = env.GetValue("DatRuns", "");
378 Split(str, fDatRuns);
379
380 GetFileNames(env, fRuns);
381 GetFileNames(env, fCalRuns);
382 GetFileNames(env, fPedRuns);
383 GetFileNames(env, fDatRuns);
384}
385
386// --------------------------------------------------------------------------
387//
388// Print the contents of the sequence
389//
390void MSequence::Print(Option_t *o) const
391{
392 gLog << all;
393 if (!IsValid())
394 {
395 gLog << "Sequence: " << fName << " <invalid>" << endl;
396 return;
397 }
398 gLog << "Sequence: " << fSequence << endl;
399 gLog << "Period: " << fPeriod << endl;
400 gLog << "Night: " << fNight << endl << endl;
401 gLog << "LightCondition: ";
402 switch (fLightCondition)
403 {
404 case kNA: gLog << "n/a" << endl; break;
405 case kNoMoon: gLog << "NoMoon" << endl; break;
406 case kTwilight: gLog << "Twilight" << endl; break;
407 case kMoon: gLog << "Moon" << endl; break;
408 }
409 gLog << "Start: " << fStart << endl;
410 gLog << "LastRun: " << fLastRun << endl;
411 gLog << "NumEvents: " << fNumEvents << endl;
412 gLog << "Project: " << fProject << endl;
413 gLog << "Source: " << fSource << endl;
414 gLog << "TriggerTable: " << fTriggerTable << endl;
415 gLog << "HvSettings: " << fHvSettings << endl << endl;
416 gLog << "Runs:";
417 for (int i=0; i<fRuns.GetSize(); i++)
418 gLog << " " << fRuns[i];
419 gLog << endl;
420 gLog << "CalRuns:";
421 for (int i=0; i<fCalRuns.GetSize(); i++)
422 gLog << " " << fCalRuns[i];
423 gLog << endl;
424 gLog << "PedRuns:";
425 for (int i=0; i<fPedRuns.GetSize(); i++)
426 gLog << " " << fPedRuns[i];
427 gLog << endl;
428 gLog << "DatRuns:";
429 for (int i=0; i<fDatRuns.GetSize(); i++)
430 gLog << " " << fDatRuns[i];
431 gLog << endl;
432}
433
434// --------------------------------------------------------------------------
435//
436// Add all ped runs from the sequence to MDirIter.
437// If path==0 the standard path of the data-center is assumed.
438// If you have the runs locally use path="."
439// Using raw=kTRUE you get correspodning raw-files setup.
440// Return the number of files added.
441UInt_t MSequence::SetupPedRuns(MDirIter &iter, const char *path, Bool_t raw) const
442{
443 return SetupRuns(iter, fPedRuns, raw?kRawPed:kRootPed, path);
444}
445
446// --------------------------------------------------------------------------
447//
448// Add all data runs from the sequence to MDirIter.
449// If path==0 the standard path of the data-center is assumed.
450// If you have the runs locally use path="."
451// Using raw=kTRUE you get correspodning raw-files setup.
452// Return the number of files added.
453//
454UInt_t MSequence::SetupDatRuns(MDirIter &iter, const char *path, Bool_t raw) const
455{
456 return SetupRuns(iter, fDatRuns, raw?kRawDat:kRootDat, path);
457}
458
459// --------------------------------------------------------------------------
460//
461// Add all runs from the sequence to MDirIter.
462// If path==0 the standard path of the data-center is assumed.
463// If you have the runs locally use path="."
464// Using raw=kTRUE you get correspodning raw-files setup.
465// Return the number of files added.
466//
467UInt_t MSequence::SetupAllRuns(MDirIter &iter, const char *path, Bool_t raw) const
468{
469 return SetupRuns(iter, fRuns, raw?kRawAll:kRootAll, path);
470}
471
472// --------------------------------------------------------------------------
473//
474// Add all calibration runs from the sequence to MDirIter.
475// If path==0 the standard path of the data-center is assumed.
476// If you have the runs locally use path="."
477// Using raw=kTRUE you get correspodning raw-files setup.
478// Return the number of files added.
479//
480UInt_t MSequence::SetupCalRuns(MDirIter &iter, const char *path, Bool_t raw) const
481{
482 return SetupRuns(iter, fCalRuns, raw?kRawCal:kRootCal, path);
483}
484
485// --------------------------------------------------------------------------
486//
487// Add all data runs from the sequence to MDirIter.
488// If path==0 the standard path of the data-center is assumed.
489// If you have the runs locally use path="."
490// Using raw=kTRUE you get correspodning raw-files setup.
491// Return the number of files added.
492//
493UInt_t MSequence::SetupDatRuns(MDirIter &iter, FileType_t type, const char *path) const
494{
495 return SetupRuns(iter, fDatRuns, type, path);
496}
497
498// --------------------------------------------------------------------------
499//
500// If you want to add runs manually, use this function.
501//
502UInt_t MSequence::AddRuns(UInt_t first, UInt_t last, TArrayI *runs)
503{
504 if (last<first)
505 {
506 *fLog << warn << "MSequence::AddRuns - WARNING: Last runnumber " << last;
507 *fLog << " smaller than first " << first << "... ignored." << endl;
508 return 0;
509 }
510 if (!IsValid())
511 {
512 *fLog << inf << "Setting Sequence number to #" << first << endl;
513 fSequence = first;
514 }
515
516 const UInt_t nall = fRuns.GetSize();
517 const UInt_t nrun = runs ? runs->GetSize() : 0;
518 const UInt_t add = last-first+1;
519
520 fRuns.Set(nall+add);
521 if (runs)
522 runs->Set(nrun+add);
523
524 for (UInt_t i=0; i<add; i++)
525 {
526 fRuns[nall+i] = first+i;
527 if (runs)
528 (*runs)[nrun+i] = first+i;
529 }
530 return add;
531}
532
533// --------------------------------------------------------------------------
534//
535// If you want to change or set the night manually.
536// The Format is
537// SetNight("yyyy-mm-dd");
538//
539void MSequence::SetNight(const char *txt)
540{
541 TString night(txt);
542 night += " 00:00:00";
543 fNight.SetSqlDateTime(night);
544
545 fPeriod = MAstro::GetMagicPeriod(fNight.GetMjd());
546}
Note: See TracBrowser for help on using the repository browser.