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

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