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

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