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

Last change on this file since 8221 was 8221, checked in by tbretz, 18 years ago
*** empty log message ***
File size: 16.6 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 "MJob.h"
133#include "MAstro.h"
134#include "MString.h"
135#include "MDirIter.h"
136
137ClassImp(MSequence);
138
139using namespace std;
140
141MSequence::~MSequence()
142{
143 /*
144 TExMapIter iter(&fFileNames);
145
146 Long_t key, val;
147
148 while (iter.Next(key, val))
149 delete (TString*)val;
150 */
151}
152
153
154// --------------------------------------------------------------------------
155//
156// Copy the run numbers from the TString runs into the TArrayI data.
157// Runs which are twice in the list are only added once. In this case
158// a warning is emitted.
159//
160void MSequence::Split(TString &runs, TArrayI &data) const
161{
162 const TRegexp regexp("[0-9]+");
163
164 data.Set(0);
165 runs = runs.Strip(TString::kTrailing);
166
167 while (!runs.IsNull())
168 {
169 const TString num = runs(regexp);
170
171 if (num.IsNull())
172 {
173 *fLog << warn << "WARNING - Run is NaN (not a number): " << runs << endl;
174 break;
175 }
176
177 const Int_t run = atoi(num.Data());
178 const Int_t n = data.GetSize();
179
180 // skip already existing entries
181 int i;
182 for (i=0; i<n; i++)
183 if (data[i] == run)
184 break;
185
186 if (i<n)
187 *fLog << warn << "WARNING - Run #" << run << " already in list... skipped." << endl;
188 else
189 {
190 // set new entry
191 data.Set(n+1);
192 data[n] = run;
193 }
194
195 runs.Remove(0, runs.First(num)+num.Length());
196 }
197
198 MJob::SortArray(data);
199}
200
201UInt_t MSequence::SetupRuns(MDirIter &iter, const TArrayI &arr, FileType_t type, const char *path) const
202{
203 TString d(path);
204
205 const Bool_t def = d.IsNull();
206
207 // Setup path
208 if (def)
209 {
210 d = GetStandardPath();
211 switch (type)
212 {
213 case kRawDat:
214 case kRawPed:
215 case kRawCal:
216 case kRawAll:
217 d += "rawfiles/";
218 d += fNight.GetStringFmt("%Y/%m/%d");
219 break;
220 case kRootDat:
221 case kRootPed:
222 case kRootCal:
223 case kRootAll:
224 d += "merpp/";
225 d += fNight.GetStringFmt("%Y/%m/%d");
226 break;
227 case kCalibrated:
228 d += Form("callisto/%04d/%08d/", fSequence/10000, fSequence);
229 break;
230 case kImages:
231 d += Form("star/%04d/%08d/", fSequence/10000, fSequence);
232 break;
233 }
234 }
235 else
236 gSystem->ExpandPathName(d);
237
238 for (int i=0; i<arr.GetSize(); i++)
239 {
240 // R. DeLosReyes and T. Bretz
241 // Changes to read the DAQ numbering format. Changes takes place
242 // between runs 35487 and 00035488 (2004_08_30)
243 const char *fmt = arr[i]>35487 ? "%08d_%s_*_E" : "%05d_%s_*_E";
244
245 TString n;
246 const char *id="_";
247 switch (type)
248 {
249 case kRawDat:
250 case kRootDat:
251 id = "D";
252 break;
253 case kRawPed:
254 case kRootPed:
255 id = "P";
256 break;
257 case kRawCal:
258 case kRootCal:
259 id = "C";
260 break;
261 case kRawAll:
262 case kRootAll:
263 id = "[PCD]";
264 break;
265 case kCalibrated:
266 id = "Y";
267 break;
268 case kImages:
269 id = "I";
270 break;
271 }
272
273 // Create file name
274 n = fNight.GetStringFmt("%Y%m%d_");
275 n += Form(fmt, arr[i], id);
276
277 switch (type)
278 {
279 case kRawDat:
280 case kRawPed:
281 case kRawCal:
282 case kRawAll:
283 n += ".raw.?g?z?";
284 break;
285 default:
286 n += ".root";
287 }
288
289 // Add Path/File to TIter
290 iter.AddDirectory(d, n, 0);
291 }
292
293 const Int_t n0 = iter.GetNumEntries();
294 const Int_t n1 = arr.GetSize();
295 if (n0==0)
296 {
297 *fLog << err;
298 *fLog < "ERROR - No input files for sequence #" << GetSequence() << endl;
299 *fLog << " read from " << GetName() << endl;
300 *fLog << " found in" << (def?" default-path ":" ") << d << endl;
301 return 0;
302 }
303
304 if (n0==n1)
305 return n0;
306
307 *fLog << err;
308 *fLog << "ERROR - " << n0 << " input files for sequence #" << GetSequence() << " found in" << endl;
309 *fLog << " " << (def?" default-path ":" ") << d << endl;
310 *fLog << " but " << n1 << " files were defined in sequence file" << endl;
311 *fLog << " " << GetName() << endl;
312 if (fLog->GetDebugLevel()<=4)
313 return 0;
314
315 *fLog << dbg << "Files which are searched for this sequence:" << endl;
316 iter.Print();
317 return 0;
318}
319
320// --------------------------------------------------------------------------
321//
322// Read the file fname as setup file for the sequence.
323//
324void MSequence::GetFileNames(TEnv &env, const TArrayI &arr)
325{
326 /*
327 for (int i=0; i<arr.GetSize(); i++)
328 {
329 // Get run number
330 const Int_t num = arr[i];
331
332 // Check if name already set
333 if (fFileNames.GetValue(num))
334 continue;
335
336 TString *str = new TString(env.GetValue(Form("%d", num), ""));
337 fFileNames.Add(num, (Long_t)str);
338 }
339 */
340}
341
342// --------------------------------------------------------------------------
343//
344// Get a file name corresponding to the run-number num, returns 0 if n/a
345//
346const char *MSequence::GetFileName(UInt_t num)
347{
348 return 0;
349 /*
350 TString *str = (TString*)fFileNames.GetValue(num);
351 return str ? str->Data() : 0;*/
352}
353
354MSequence::LightCondition_t MSequence::ReadLightCondition(TEnv &env) const
355{
356 TString str = env.GetValue("LightCondition", "n/a");
357 if (!str.CompareTo("n/a", TString::kIgnoreCase))
358 return kNA;
359 if (!str.CompareTo("NoMoon", TString::kIgnoreCase))
360 return kNoMoon;
361 if (!str.CompareTo("Twilight", TString::kIgnoreCase))
362 return kTwilight;
363 if (!str.CompareTo("Moon", TString::kIgnoreCase))
364 return kMoon;
365 if (!str.CompareTo("Day", TString::kIgnoreCase))
366 return kDay;
367
368 gLog << warn << "MSequence: LightCondition-tag not n/a, nomoon, twilight, moon or day." << endl;
369 return kNA;
370}
371
372// --------------------------------------------------------------------------
373//
374// Read the file fname as setup file for the sequence.
375//
376MSequence::MSequence(const char *fname)
377{
378 fName = fname;
379
380 const char *expname = gSystem->ExpandPathName(fname);
381
382 fTitle = Form("Sequence contained in file %s", expname);
383
384 const Bool_t access = !gSystem->AccessPathName(expname, kFileExists);
385 if (!access)
386 gLog << err << "ERROR - Dataset file " << expname << " not accessible!" << endl;
387
388 TEnv env(expname);
389 delete [] expname;
390
391 TString str;
392
393 fSequence = env.GetValue("Sequence", -1);
394 fLastRun = env.GetValue("LastRun", -1);
395 fNumEvents = env.GetValue("NumEvents", -1);
396 fPeriod = env.GetValue("Period", -1);
397
398 fLightCondition = ReadLightCondition(env);
399
400 str = env.GetValue("Start", "");
401 fStart.SetSqlDateTime(str);
402 str = env.GetValue("Night", "");
403 str += " 00:00:00";
404 fNight.SetSqlDateTime(str);
405
406 fProject = env.GetValue("Project", "");
407 fSource = env.GetValue("Source", "");
408 fTriggerTable = env.GetValue("TriggerTable", "");
409 fHvSettings = env.GetValue("HvSettings", "");
410
411 str = env.GetValue("Runs", "");
412 Split(str, fRuns);
413 str = env.GetValue("CalRuns", "");
414 Split(str, fCalRuns);
415 str = env.GetValue("PedRuns", "");
416 Split(str, fPedRuns);
417 str = env.GetValue("DatRuns", "");
418 Split(str, fDatRuns);
419
420 GetFileNames(env, fRuns);
421 GetFileNames(env, fCalRuns);
422 GetFileNames(env, fPedRuns);
423 GetFileNames(env, fDatRuns);
424}
425
426// --------------------------------------------------------------------------
427//
428// Print the contents of the sequence
429//
430void MSequence::Print(Option_t *o) const
431{
432 gLog << all;
433 if (!IsValid())
434 {
435 gLog << "Sequence: " << fName << " <invalid>" << endl;
436 return;
437 }
438 gLog << "Sequence: " << fSequence << endl;
439 gLog << "Period: " << fPeriod << endl;
440 gLog << "Night: " << fNight << endl << endl;
441 gLog << "LightCondition: ";
442 switch (fLightCondition)
443 {
444 case kNA: gLog << "n/a" << endl; break;
445 case kNoMoon: gLog << "NoMoon" << endl; break;
446 case kTwilight: gLog << "Twilight" << endl; break;
447 case kMoon: gLog << "Moon" << endl; break;
448 case kDay: gLog << "Day" << endl; break;
449 }
450 gLog << "Start: " << fStart << endl;
451 gLog << "LastRun: " << fLastRun << endl;
452 gLog << "NumEvents: " << fNumEvents << endl;
453 gLog << "Project: " << fProject << endl;
454 gLog << "Source: " << fSource << endl;
455 gLog << "TriggerTable: " << fTriggerTable << endl;
456 gLog << "HvSettings: " << fHvSettings << endl << endl;
457 gLog << "Runs:";
458 for (int i=0; i<fRuns.GetSize(); i++)
459 gLog << " " << fRuns[i];
460 gLog << endl;
461 gLog << "CalRuns:";
462 for (int i=0; i<fCalRuns.GetSize(); i++)
463 gLog << " " << fCalRuns[i];
464 gLog << endl;
465 gLog << "PedRuns:";
466 for (int i=0; i<fPedRuns.GetSize(); i++)
467 gLog << " " << fPedRuns[i];
468 gLog << endl;
469 gLog << "DatRuns:";
470 for (int i=0; i<fDatRuns.GetSize(); i++)
471 gLog << " " << fDatRuns[i];
472 gLog << endl;
473}
474
475// --------------------------------------------------------------------------
476//
477// Add all ped runs from the sequence to MDirIter.
478// If path==0 the standard path of the data-center is assumed.
479// If you have the runs locally use path="."
480// Using raw=kTRUE you get correspodning raw-files setup.
481// Return the number of files added.
482UInt_t MSequence::SetupPedRuns(MDirIter &iter, const char *path, Bool_t raw) const
483{
484 return SetupRuns(iter, fPedRuns, raw?kRawPed:kRootPed, path);
485}
486
487// --------------------------------------------------------------------------
488//
489// Add all data runs from the sequence to MDirIter.
490// If path==0 the standard path of the data-center is assumed.
491// If you have the runs locally use path="."
492// Using raw=kTRUE you get correspodning raw-files setup.
493// Return the number of files added.
494//
495UInt_t MSequence::SetupDatRuns(MDirIter &iter, const char *path, Bool_t raw) const
496{
497 return SetupRuns(iter, fDatRuns, raw?kRawDat:kRootDat, path);
498}
499
500// --------------------------------------------------------------------------
501//
502// Add all runs from the sequence to MDirIter.
503// If path==0 the standard path of the data-center is assumed.
504// If you have the runs locally use path="."
505// Using raw=kTRUE you get correspodning raw-files setup.
506// Return the number of files added.
507//
508UInt_t MSequence::SetupAllRuns(MDirIter &iter, const char *path, Bool_t raw) const
509{
510 return SetupRuns(iter, fRuns, raw?kRawAll:kRootAll, path);
511}
512
513// --------------------------------------------------------------------------
514//
515// Add all calibration runs from the sequence to MDirIter.
516// If path==0 the standard path of the data-center is assumed.
517// If you have the runs locally use path="."
518// Using raw=kTRUE you get correspodning raw-files setup.
519// Return the number of files added.
520//
521UInt_t MSequence::SetupCalRuns(MDirIter &iter, const char *path, Bool_t raw) const
522{
523 return SetupRuns(iter, fCalRuns, raw?kRawCal:kRootCal, path);
524}
525
526// --------------------------------------------------------------------------
527//
528// Add all data runs from the sequence to MDirIter.
529// If path==0 the standard path of the data-center is assumed.
530// If you have the runs locally use path="."
531// Using raw=kTRUE you get correspodning raw-files setup.
532// Return the number of files added.
533//
534UInt_t MSequence::SetupDatRuns(MDirIter &iter, FileType_t type, const char *path) const
535{
536 return SetupRuns(iter, fDatRuns, type, path);
537}
538
539// --------------------------------------------------------------------------
540//
541// If you want to add runs manually, use this function.
542//
543UInt_t MSequence::AddRuns(UInt_t first, UInt_t last, TArrayI *runs)
544{
545 if (last<first)
546 {
547 *fLog << warn << "MSequence::AddRuns - WARNING: Last runnumber " << last;
548 *fLog << " smaller than first " << first << "... ignored." << endl;
549 return 0;
550 }
551 if (!IsValid())
552 {
553 *fLog << inf << "Setting Sequence number to #" << first << endl;
554 fSequence = first;
555 }
556
557 const UInt_t nall = fRuns.GetSize();
558 const UInt_t nrun = runs ? runs->GetSize() : 0;
559 const UInt_t add = last-first+1;
560
561 fRuns.Set(nall+add);
562 if (runs)
563 runs->Set(nrun+add);
564
565 for (UInt_t i=0; i<add; i++)
566 {
567 fRuns[nall+i] = first+i;
568 if (runs)
569 (*runs)[nrun+i] = first+i;
570 }
571 return add;
572}
573
574// --------------------------------------------------------------------------
575//
576// If you want to change or set the night manually.
577// The Format is
578// SetNight("yyyy-mm-dd");
579//
580void MSequence::SetNight(const char *txt)
581{
582 TString night(txt);
583 night += " 00:00:00";
584 fNight.SetSqlDateTime(night);
585
586 fPeriod = MAstro::GetMagicPeriod(fNight.GetMjd());
587}
Note: See TracBrowser for help on using the repository browser.