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

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