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

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