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

Last change on this file since 7333 was 7220, 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 *fLog << warn << "WARNING - Run #" << run << " alraedy in list... skipped." << endl;
181 else
182 {
183 // set new entry
184 data.Set(n+1);
185 data[n] = run;
186 }
187
188 runs.Remove(0, runs.First(num)+num.Length());
189 }
190}
191
192UInt_t MSequence::SetupRuns(MDirIter &iter, const TArrayI &arr, FileType_t type, const char *path) const
193{
194 TString d(path);
195
196 // Setup path
197 if (d.IsNull())
198 {
199 d = GetStandardPath();
200 switch (type)
201 {
202 case kRawDat:
203 case kRawPed:
204 case kRawCal:
205 case kRawAll:
206 d += "rawfiles/";
207 d += fNight.GetStringFmt("%Y/%m/%d");
208 break;
209 case kRootDat:
210 case kRootPed:
211 case kRootCal:
212 case kRootAll:
213 d += "merpp/";
214 d += fNight.GetStringFmt("%Y/%m/%d");
215 break;
216 case kCalibrated:
217 d += Form("callisto/%04d/%08d/", fSequence/10000, fSequence);
218 break;
219 case kImages:
220 d += Form("star/%04d/%08d/", fSequence/10000, fSequence);
221 break;
222 }
223 }
224 else
225 gSystem->ExpandPathName(d);
226
227 for (int i=0; i<arr.GetSize(); i++)
228 {
229 // R. DeLosReyes and T. Bretz
230 // Changes to read the DAQ numbering format. Changes takes place
231 // between runs 35487 and 00035488 (2004_08_30)
232 const char *fmt = arr[i]>35487 ? "%08d_%s_*_E" : "%05d_%s_*_E";
233
234 TString n;
235 char *id="_";
236 switch (type)
237 {
238 case kRawDat:
239 case kRootDat:
240 id = "D";
241 break;
242 case kRawPed:
243 case kRootPed:
244 id = "P";
245 break;
246 case kRawCal:
247 case kRootCal:
248 id = "C";
249 break;
250 case kRawAll:
251 case kRootAll:
252 id = "[PCD]";
253 break;
254 case kCalibrated:
255 id = "Y";
256 break;
257 case kImages:
258 id = "I";
259 break;
260 }
261
262 // Create file name
263 n = fNight.GetStringFmt("%Y%m%d_");
264 n += Form(fmt, arr[i], id);
265
266 switch (type)
267 {
268 case kRawDat:
269 case kRawPed:
270 case kRawCal:
271 case kRawAll:
272 n += ".raw";
273 break;
274 default:
275 n += ".root";
276 }
277
278 // Add Path/File to TIter
279 iter.AddDirectory(d, n, 0);
280 }
281
282 return iter.GetNumEntries();
283}
284
285// --------------------------------------------------------------------------
286//
287// Read the file fname as setup file for the sequence.
288//
289void MSequence::GetFileNames(TEnv &env, const TArrayI &arr)
290{
291 /*
292 for (int i=0; i<arr.GetSize(); i++)
293 {
294 // Get run number
295 const Int_t num = arr[i];
296
297 // Check if name already set
298 if (fFileNames.GetValue(num))
299 continue;
300
301 TString *str = new TString(env.GetValue(Form("%d", num), ""));
302 fFileNames.Add(num, (Long_t)str);
303 }
304 */
305}
306
307// --------------------------------------------------------------------------
308//
309// Get a file name corresponding to the run-number num, returns 0 if n/a
310//
311const char *MSequence::GetFileName(UInt_t num)
312{
313 return 0;
314 /*
315 TString *str = (TString*)fFileNames.GetValue(num);
316 return str ? str->Data() : 0;*/
317}
318
319MSequence::LightCondition_t MSequence::ReadLightCondition(TEnv &env) const
320{
321 TString str = env.GetValue("LightCondition", "n/a");
322 if (!str.CompareTo("n/a", TString::kIgnoreCase))
323 return kNA;
324 if (!str.CompareTo("NoMoon", TString::kIgnoreCase))
325 return kNoMoon;
326 if (!str.CompareTo("Twilight", TString::kIgnoreCase))
327 return kTwilight;
328 if (!str.CompareTo("Moon", TString::kIgnoreCase))
329 return kMoon;
330
331 gLog << warn << "MSequence: LightCondition-tag not n/a, nomoon, twilight or moon." << endl;
332 return kNA;
333}
334
335// --------------------------------------------------------------------------
336//
337// Read the file fname as setup file for the sequence.
338//
339MSequence::MSequence(const char *fname)
340{
341 fName = fname;
342
343 const char *expname = gSystem->ExpandPathName(fname);
344
345 fTitle = Form("Sequence contained in file %s", expname);
346
347 TEnv env(expname);
348 delete [] expname;
349
350 TString str;
351
352 fSequence = env.GetValue("Sequence", -1);
353 fLastRun = env.GetValue("LastRun", -1);
354 fNumEvents = env.GetValue("NumEvents", -1);
355 fPeriod = env.GetValue("Period", -1);
356
357 fLightCondition = ReadLightCondition(env);
358
359 str = env.GetValue("Start", "");
360 fStart.SetSqlDateTime(str);
361 str = env.GetValue("Night", "");
362 str += " 00:00:00";
363 fNight.SetSqlDateTime(str);
364
365 fProject = env.GetValue("Project", "");
366 fSource = env.GetValue("Source", "");
367 fTriggerTable = env.GetValue("TriggerTable", "");
368 fHvSettings = env.GetValue("HvSettings", "");
369
370 str = env.GetValue("Runs", "");
371 Split(str, fRuns);
372 str = env.GetValue("CalRuns", "");
373 Split(str, fCalRuns);
374 str = env.GetValue("PedRuns", "");
375 Split(str, fPedRuns);
376 str = env.GetValue("DatRuns", "");
377 Split(str, fDatRuns);
378
379 GetFileNames(env, fRuns);
380 GetFileNames(env, fCalRuns);
381 GetFileNames(env, fPedRuns);
382 GetFileNames(env, fDatRuns);
383}
384
385// --------------------------------------------------------------------------
386//
387// Print the contents of the sequence
388//
389void MSequence::Print(Option_t *o) const
390{
391 gLog << all;
392 if (!IsValid())
393 {
394 gLog << "Sequence: " << fName << " <invalid>" << endl;
395 return;
396 }
397 gLog << "Sequence: " << fSequence << endl;
398 gLog << "Period: " << fPeriod << endl;
399 gLog << "Night: " << fNight << endl << endl;
400 gLog << "LightCondition: ";
401 switch (fLightCondition)
402 {
403 case kNA: gLog << "n/a" << endl; break;
404 case kNoMoon: gLog << "NoMoon" << endl; break;
405 case kTwilight: gLog << "Twilight" << endl; break;
406 case kMoon: gLog << "Moon" << endl; break;
407 }
408 gLog << "Start: " << fStart << endl;
409 gLog << "LastRun: " << fLastRun << endl;
410 gLog << "NumEvents: " << fNumEvents << endl;
411 gLog << "Project: " << fProject << endl;
412 gLog << "Source: " << fSource << endl;
413 gLog << "TriggerTable: " << fTriggerTable << endl;
414 gLog << "HvSettings: " << fHvSettings << endl << endl;
415 gLog << "Runs:";
416 for (int i=0; i<fRuns.GetSize(); i++)
417 gLog << " " << fRuns[i];
418 gLog << endl;
419 gLog << "CalRuns:";
420 for (int i=0; i<fCalRuns.GetSize(); i++)
421 gLog << " " << fCalRuns[i];
422 gLog << endl;
423 gLog << "PedRuns:";
424 for (int i=0; i<fPedRuns.GetSize(); i++)
425 gLog << " " << fPedRuns[i];
426 gLog << endl;
427 gLog << "DatRuns:";
428 for (int i=0; i<fDatRuns.GetSize(); i++)
429 gLog << " " << fDatRuns[i];
430 gLog << endl;
431}
432
433// --------------------------------------------------------------------------
434//
435// Add all ped runs from the sequence to MDirIter.
436// If path==0 the standard path of the data-center is assumed.
437// If you have the runs locally use path="."
438// Using raw=kTRUE you get correspodning raw-files setup.
439// Return the number of files added.
440UInt_t MSequence::SetupPedRuns(MDirIter &iter, const char *path, Bool_t raw) const
441{
442 return SetupRuns(iter, fPedRuns, raw?kRawPed:kRootPed, path);
443}
444
445// --------------------------------------------------------------------------
446//
447// Add all data runs from the sequence to MDirIter.
448// If path==0 the standard path of the data-center is assumed.
449// If you have the runs locally use path="."
450// Using raw=kTRUE you get correspodning raw-files setup.
451// Return the number of files added.
452//
453UInt_t MSequence::SetupDatRuns(MDirIter &iter, const char *path, Bool_t raw) const
454{
455 return SetupRuns(iter, fDatRuns, raw?kRawDat:kRootDat, path);
456}
457
458// --------------------------------------------------------------------------
459//
460// Add all runs from the sequence to MDirIter.
461// If path==0 the standard path of the data-center is assumed.
462// If you have the runs locally use path="."
463// Using raw=kTRUE you get correspodning raw-files setup.
464// Return the number of files added.
465//
466UInt_t MSequence::SetupAllRuns(MDirIter &iter, const char *path, Bool_t raw) const
467{
468 return SetupRuns(iter, fRuns, raw?kRawAll:kRootAll, path);
469}
470
471// --------------------------------------------------------------------------
472//
473// Add all calibration runs from the sequence to MDirIter.
474// If path==0 the standard path of the data-center is assumed.
475// If you have the runs locally use path="."
476// Using raw=kTRUE you get correspodning raw-files setup.
477// Return the number of files added.
478//
479UInt_t MSequence::SetupCalRuns(MDirIter &iter, const char *path, Bool_t raw) const
480{
481 return SetupRuns(iter, fCalRuns, raw?kRawCal:kRootCal, path);
482}
483
484// --------------------------------------------------------------------------
485//
486// Add all data runs from the sequence to MDirIter.
487// If path==0 the standard path of the data-center is assumed.
488// If you have the runs locally use path="."
489// Using raw=kTRUE you get correspodning raw-files setup.
490// Return the number of files added.
491//
492UInt_t MSequence::SetupDatRuns(MDirIter &iter, FileType_t type, const char *path) const
493{
494 return SetupRuns(iter, fDatRuns, type, path);
495}
496
497// --------------------------------------------------------------------------
498//
499// If you want to add runs manually, use this function.
500//
501UInt_t MSequence::AddRuns(UInt_t first, UInt_t last, TArrayI *runs)
502{
503 if (last<first)
504 {
505 *fLog << warn << "MSequence::AddRuns - WARNING: Last runnumber " << last;
506 *fLog << " smaller than first " << first << "... ignored." << endl;
507 return 0;
508 }
509 if (!IsValid())
510 {
511 *fLog << inf << "Setting Sequence number to #" << first << endl;
512 fSequence = first;
513 }
514
515 const UInt_t nall = fRuns.GetSize();
516 const UInt_t nrun = runs ? runs->GetSize() : 0;
517 const UInt_t add = last-first+1;
518
519 fRuns.Set(nall+add);
520 if (runs)
521 runs->Set(nrun+add);
522
523 for (UInt_t i=0; i<add; i++)
524 {
525 fRuns[nall+i] = first+i;
526 if (runs)
527 (*runs)[nrun+i] = first+i;
528 }
529 return add;
530}
531
532// --------------------------------------------------------------------------
533//
534// If you want to change or set the night manually.
535// The Format is
536// SetNight("yyyy-mm-dd");
537//
538void MSequence::SetNight(const char *txt)
539{
540 TString night(txt);
541 night += " 00:00:00";
542 fNight.SetSqlDateTime(night);
543
544 fPeriod = MAstro::GetMagicPeriod(fNight.GetMjd());
545}
Note: See TracBrowser for help on using the repository browser.