source: trunk/MagicSoft/Mars/mtemp/mucm/classes/MDataSetIter.cc@ 6135

Last change on this file since 6135 was 4693, checked in by marcos, 20 years ago
*** empty log message ***
File size: 17.9 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): Marcos Lopez, 4/2004 <mailto:marcos@gae.ucm.es>
19!
20! Copyright: MAGIC Software Development, 2000-2004
21!
22!
23\* ======================================================================== */
24
25/////////////////////////////////////////////////////////////////////////////
26//
27// MDataSetIter
28//
29// This class serves to divide all the data taking during a given night
30// (i.e, all the root files of a given directory),
31// into different data sets of files to be analyzed together. A data set is a
32// set of Data runs, along which their closer Ped and Cal runs.
33//
34// Usage:
35// ------
36// - To specify the directory/ies where the data are use:
37// AddDirectory()
38//
39// - To specify only those files for a given source name (all the files with
40// a name different from the ones in the source-name-list will be ignored)
41// use:
42// SelectSourceName()
43// You can select several src names.
44// By default, if any explict source name is specified, all are valid.
45//
46// - To retrieve the next data set (Ped, Cal, and Data runs) use:
47// NextDataSet()
48// GetPefDataRuns()
49// GetCalRuns()
50// GetDatRuns()
51//
52// Two problems:
53// -Sources not desired : ZetaTauri
54// - Not P/C runs before data runs
55//
56//
57/////////////////////////////////////////////////////////////////////////////
58#include "MDataSetIter.h"
59
60#include "MLog.h"
61#include "MLogManip.h"
62
63#include "MRunIter.h"
64
65#include "TObjArray.h"
66
67#include <TSystem.h>
68
69
70ClassImp(MDataSetIter);
71
72using namespace std;
73
74
75
76// ----------------------------------------------------------------------------
77//
78// Default constructor.
79//
80MDataSetIter::MDataSetIter()
81 : fLastProcessedRun(0), fLog(&gLog), fDefCalRun(0)
82{
83 fPedRuns = new MRunIter;
84 fCalRuns = new MRunIter;
85 fDataRuns = new MRunIter;
86}
87
88
89// ----------------------------------------------------------------------------
90//
91// Check wheather the source name is inside the list of selected source names.
92// If not any source name has been selected, by default, any source name is
93// valid.
94// Return:
95// kTRUE: this run has to be selected
96// kFALSE: this run has to be skipped
97//
98Int_t MDataSetIter::CheckSourceName(TString& src)
99{
100
101 // If any source has been especified, all sorce name are accepted
102 if(fSrcList.GetLast()==-1)
103 return kTRUE;
104
105
106 //
107 // For skipping Ped and Cal runs with CL (aftet June 2004)
108 //
109 if(src.Contains("CL") || src.Contains("ContL"))
110 {
111 *fLog << warn << "Skipping CL run [" << src << "]" << endl;
112 return kFALSE;
113 }
114
115 //
116 // For dealing with the new calibration files nomenclature
117 // (after 23-3-2004) of the type "CrabNebula-5blue", and for off data
118 // like OffMrk421-1, OffMrk421-3 etc, we assume that any source name
119 // can be made up of two parts separated by a dash: the source name and
120 // a suffix indicated for instance the calibration colot of kind of off
121 // data. This suffix is then ignored for checking the source name, ie.,
122 // any suffix is valid !
123 //
124 if(src.Contains("-"))
125 {
126 *fLog << warn << "For source [" << src << "] ignoring suffix ["
127 << src( src.First("-")+1, src.Length() ) << "]" << endl;
128 src.Remove(src.First("-"));
129 }
130
131
132 //
133 // Loop
134 //
135 TIter next(&fSrcList);
136
137 TString ValidSrc;
138 TNamed* n;
139 while ( (n=(TNamed*)next()) )
140 {
141 ValidSrc = n->GetName();
142
143 //if (src==ValidSrc) // For dealing with new calib files as Mrk421blue5
144 if (src.Contains(ValidSrc))
145 return kTRUE;
146 }
147
148 *fLog << warn << "Source [" << src << "] is not in the list of selected source names." << endl;
149
150 return kFALSE;
151}
152
153
154// ---------------------------------------------------------------------------
155//
156// Scan a file name.
157//
158void MDataSetIter::ScanFileName(const TString& file, TString& name, TString& path, TString& date, TString& src, Int_t* run, char* type)
159{
160 const Int_t slash = file.Last('/');
161
162 // Get File name and Path
163 path = file(0,slash+1);
164 name = file(slash+1,file.Length());
165
166 // Get date
167 date = name(0,8);
168
169 // Get run number
170 const TString runstr = name(9,5);
171 *run = atoi(runstr.Data());
172
173 // Get run type
174 *type = name[15];
175
176 // Get source name
177 src = name(17,name.Last('_')-17);
178}
179
180
181// ---------------------------------------------------------------------------
182//
183// Add all the files inside a given directory to the fFileList.
184//
185void MDataSetIter::AddToFileList(MDirIter& dir)
186{
187
188 TString name;
189 while (!(name=dir.Next()).IsNull())
190 {
191 fFileList.Add((TObject*)(new TNamed((const char*)name, "")));
192 }
193
194 //
195 // Sort File List by name
196 //
197 fFileList.Sort();
198}
199
200
201// ----------------------------------------------------------------------------
202//
203// Overload MDirIter::AddDirectory
204//
205// Each time a new directory is added, all the files inside that directory
206// are added to the fFileList.
207//
208// Returns the number of directories added
209//
210Int_t MDataSetIter::AddDirectory(const char *dir, const char *filter, Int_t recursive)
211{
212 const Int_t rc = MDirIter::AddDirectory(dir, filter, recursive);
213 if (rc==0)
214 return 0;
215
216 //
217 // Add the files inside that directory to the fFileList. In order to
218 // add only the files inside the new directory added, we create
219 // and MDirIter with only that directory. Otherwise, it we call
220 // directly this.Next() inside FillFileList the new files and the
221 // previously existing one will be again adde to the fFileList.
222 //
223 MDirIter next(dir,filter,recursive);
224 AddToFileList(next);
225
226 return kTRUE;
227}
228
229
230// ----------------------------------------------------------------------------
231//
232// Add a source name to the list of selected source names.
233//
234void MDataSetIter::SelectSourceName(const char *src)
235{
236 fSrcList.Add((TObject*)(new TNamed(src, src)));
237}
238
239
240// ----------------------------------------------------------------------------
241//
242// First check that previous run was not empty, and then that it contains
243// runs for the current source (fSrcName)
244//
245Int_t MDataSetIter::IsPreviousRunUsable(MRunIter& oldRuns)
246{
247 if( oldRuns.GetNumRuns() == 0)
248 {
249 *fLog << warn << "Doesn't exist previous set." << endl;
250 return kFALSE;
251 }
252
253 //
254 // Go back to ther first entry
255 //
256 oldRuns.Reset();
257
258 //
259 // Check that the previous runs were for the same source name
260 //
261 TString name, path, date, src;
262 Int_t run;
263 char type;
264
265 ScanFileName(oldRuns.Next(), name, path, date, src, &run, &type);
266
267 // if( src != fSrcName)
268// {
269// *fLog << err << "Previous runs were for a diffrent source...exit." << endl;
270// return kFALSE;
271// }
272
273 //
274 // For dealing with calibration color we just ask than the srcname
275 // contains the FirstSrcName
276 //
277 if( !src.Contains(fSrcName) )
278 {
279 *fLog << warn << "Previous runs were for a diffrent source [" << src << "] vs [" << fSrcName << "] ...exit." << endl;
280 return kFALSE;
281 }
282
283
284 return kTRUE;
285}
286
287
288// ---------------------------------------------------------------------------
289//
290// Assumes that a data set has the following structure:
291// P/C,P/C,P/C, ...
292// D,D,D, ...
293//
294// After finishing the loop, it checks that were found at least one Ped, one
295// Cal and one Data runs.
296//
297// Start from the last processed run, looking for Ped, Cal and Dat runs. As
298// soon a Data run is found, it start to look only for Data runs, finishing
299// the loop when a run of different type of Data run is found.
300//
301// Process all the runs which runnumber is later to the last processed run
302// (the first time this function is call fLastProcessedRun is 0, so it start
303// processing from the beginning)
304//
305// NOTE: This FAILS if the directory doesn't start with at lead one P and one
306// C runs
307//
308Int_t MDataSetIter::NextDataSet()
309{
310
311 if(fFileList.GetLast()==-1)
312 return kFALSE;
313
314
315 //
316 // Save temporaly prvious data set;
317 //
318 MRunIter* oldDataRuns = 0;
319 MRunIter* oldPedRuns = 0;
320 MRunIter* oldCalRuns = 0;
321
322
323 if(fDataRuns)
324 oldDataRuns = (MRunIter*)fDataRuns->Clone();
325 if(fPedRuns)
326 oldPedRuns = (MRunIter*)fPedRuns->Clone();
327 if(fCalRuns)
328 oldCalRuns = (MRunIter*)fCalRuns->Clone();
329
330
331
332 //
333 // Retrieve next data set P,C and D runs
334 //
335 Loop("dataset");
336
337
338 //
339 // If no data runs were found, is because we were already at the end of
340 // the directory. Then Return kFALSE
341 //
342 if ( fDataRuns->GetNumRuns()==0 )
343 {
344 *fLog << warn << "No more Data runs left." << endl;
345 return kFALSE;
346 }
347
348
349 //
350 // If no new P runs found, use the previous one, if they were for the
351 // same source
352 //
353 if ( fPedRuns->GetNumRuns()==0 )
354 {
355
356 *fLog << warn << "No Ped runs were found before data runs." << endl;
357
358
359 //
360 // Trying to Use previous pedestal, if there were
361 //
362 if ( IsPreviousRunUsable(*oldPedRuns) )
363 {
364 *fLog << warn << "Using Previous Ped runs." << endl;
365 fPedRuns->Delete();
366 fPedRuns = (MRunIter*)oldPedRuns->Clone();
367 }
368 else
369 {
370 *fLog << warn << "Previous Ped runs exists but for a different source." << endl;
371
372
373 //
374 // Found next P after D runs
375 //
376 *fLog << warn << "Looking for new Ped runs after last data run..." << endl;
377
378 oldDataRuns = (MRunIter*)fDataRuns->Clone();
379 oldCalRuns = (MRunIter*)fCalRuns->Clone();
380
381 Loop("P",fSrcName);
382
383 // fDataRuns and fCalRuns has been overwriteen when calling Loop
384 fDataRuns->Delete();
385 fDataRuns = (MRunIter*)oldDataRuns->Clone();
386
387 fCalRuns->Delete();
388 fCalRuns = (MRunIter*)oldCalRuns->Clone();
389
390
391 //
392 // Last check
393 //
394 if ( fPedRuns->GetNumRuns() == 0 )
395 {
396 *fLog << err << "ERROR: Unable to found Ped runs for this source [" << fSrcName << "] ... exit." << endl;
397 return kFALSE;
398 }
399 }
400
401 }
402
403
404 //
405 // If no new C runs found, use the previous one, if they were for the
406 // same source
407 //
408 if ( fCalRuns->GetNumRuns()==0 )
409 {
410 *fLog << warn << "No Cal runs were found before data runs." << endl;
411
412 //
413 // Trying to Use previous pedestal, if there were
414 //
415 if ( IsPreviousRunUsable(*oldCalRuns) )
416 {
417 *fLog << warn << "Using Previous Cal runs." << endl;
418 fCalRuns->Delete();
419 fCalRuns = (MRunIter*)oldCalRuns->Clone();
420 }
421 else
422 {
423
424 //
425 // Found next P after D runs
426 //
427 *fLog << warn << "Looking for new Cal runs after last data run..." << endl;
428
429
430 oldDataRuns = (MRunIter*)fDataRuns->Clone();
431 oldPedRuns = (MRunIter*)fPedRuns->Clone();
432
433 Loop("C",fSrcName);
434
435
436 // fDataRuns and fPedRuns has been overwriteen when calling Loop
437 fDataRuns->Delete();
438 fDataRuns = (MRunIter*)oldDataRuns->Clone();
439
440 fPedRuns->Delete();
441 fPedRuns = (MRunIter*)oldPedRuns->Clone();
442
443
444
445 //
446 // Last check
447 //
448 if ( fCalRuns->GetNumRuns() == 0 )
449 {
450 *fLog << err << "ERROR: Unable to found Cal runs for this source [" << fSrcName << "] ... exit." << endl;
451
452 //
453 // Using a default Cal run
454 //
455 if( fDefCalRun != 0)
456 {
457 *fLog << warn << "WARN: Using a default calibration run: " << fDefCalRunPath << fDefCalRun << endl;
458 fCalRuns->AddRun(fDefCalRun,fDefCalRunPath.Data());
459 }
460 else
461 return kFALSE;
462 }
463 }
464 }
465
466
467
468
469 //
470 // Print the runs of this data set
471 //
472 Print();
473
474
475 oldDataRuns->Delete();
476 oldPedRuns->Delete();
477 oldCalRuns->Delete();
478
479
480 return kTRUE;
481}
482
483
484
485// ---------------------------------------------------------------------------
486//
487// Look for a set of consecutive runs of a given type (P/C/D), starting from
488// the last processed data run. The runs found are stored in
489// f[Data/Ped/Cal]Runs.
490//
491// If option="P" look for a set of consecutive P runs. The same for C and D
492// runs.
493// If options="dataset", look for set of consecutive D runs, but also retrieve
494// all the P and C runs between the last processed run, and the first data
495// run.
496//
497// Method:
498// -------
499// Loop over all the files, and forechs does:
500// First, performs 2 tests
501// - run number: must be larger than the last processed data run
502// - source name: must be one of the selected source names
503// then
504// - add this run, but before, check that it correspond to the same
505// src name than the previously addded runs.
506//
507Int_t MDataSetIter::Loop(TString option, TString LockSrcName)
508{
509
510 //
511 // Delete previous data runs
512 //
513 fDataRuns->Delete();
514 fDataRuns = new MRunIter();
515
516 fPedRuns->Delete();
517 fPedRuns = new MRunIter();
518
519 fCalRuns->Delete();
520 fCalRuns = new MRunIter();
521
522
523 //
524 TString FirstSrcName = ""; // SrcName of the first run found
525 Bool_t RunFound = kFALSE;
526 Bool_t DataRunFound = kFALSE;
527 Bool_t CalRunFound = kFALSE;
528 Bool_t PedRunFound = kFALSE;
529
530
531 //
532 // Check option
533 //
534 if ( option.CompareTo("dataset",TString::kIgnoreCase) &&
535 option.CompareTo("P",TString::kIgnoreCase) &&
536 option.CompareTo("C",TString::kIgnoreCase) &&
537 option.CompareTo("D",TString::kIgnoreCase) )
538 {
539 *fLog << err << "ERROR: option [" << option << "] invalid...exit" << endl;
540 return kFALSE;
541 }
542
543 char SelectedType = !option.CompareTo("dataset",TString::kIgnoreCase) ? 'D' : option[0];
544
545
546
547
548 //
549 // Loop over all the files in the directory
550 //
551 TIter next(&fFileList);;
552 TNamed* n;
553 while ( (n=(TNamed*)next()) )
554 {
555 TString name, path, date, src;
556 Int_t run;
557 char type;
558
559 ScanFileName(n->GetName(), name, path, date, src, &run, &type);
560
561
562 //
563 // Skip until finding the following run after the fLastDataRun
564 //
565 if( run <= fLastProcessedRun )
566 continue;
567
568 //
569 // Check that the source name is among one of the selected source names
570 //
571 if( LockSrcName != "")
572 {
573 if( src != LockSrcName )
574 continue;
575 }
576 else
577 {
578 if( !CheckSourceName(src) )
579 continue;
580 }
581
582 //
583 // If a Data run has already be found, the next files has to be only
584 // of type Data, if not finish.
585 //
586 if( !option.CompareTo("dataset",TString::kIgnoreCase) && DataRunFound==kTRUE && type!=SelectedType )
587 break;
588
589 else if( !option.CompareTo("P",TString::kIgnoreCase) && PedRunFound==kTRUE && type!=SelectedType )
590 break;
591
592 else if( !option.CompareTo("C",TString::kIgnoreCase) && CalRunFound==kTRUE && type!=SelectedType )
593 break;
594
595 else if( !option.CompareTo("D",TString::kIgnoreCase) && DataRunFound==kTRUE && type!=SelectedType)
596 break;
597
598
599
600 //
601 // For Checking that this run has the same name that the first one of
602 // this set
603 //
604 if (RunFound==kFALSE)
605 {
606 RunFound = kTRUE;
607 FirstSrcName = src;
608 }
609
610 if( src != FirstSrcName)
611 {
612 if(type!='C')
613 {
614 *fLog << err << "ERROR: Source Name differs inside data set ("
615 << src << " vs. " << FirstSrcName << ") ...exit." << endl;
616 return kFALSE;
617 }
618 else
619 {
620 *fLog << warn << "Warning: Cal Source Name differs inside data set (" << src << " vs. " << FirstSrcName << ")." << endl;
621 }
622 }
623
624
625 //
626 // Add this run to its corresponding MRunIter
627 //
628 switch (type)
629 {
630 case 'D':
631 if( !option.CompareTo("dataset",TString::kIgnoreCase) ||
632 !option.CompareTo("D",TString::kIgnoreCase) )
633 {
634 *fLog << "Adding Data run: " << run << " [" << src << "]" << endl;
635
636 fDataRuns->AddRun(run,path.Data());
637
638 fSrcName = src;
639 fDate = date;
640
641 DataRunFound = kTRUE;
642
643 fLastProcessedRun = run;
644
645 }
646 break;
647
648 case 'P':
649 if( !option.CompareTo("dataset",TString::kIgnoreCase) ||
650 !option.CompareTo("P",TString::kIgnoreCase) )
651 {
652 *fLog << "Adding Ped run: "<< run << " [" << src << "]" << endl;
653
654 PedRunFound = kTRUE;
655
656 fPedRuns->AddRun(run,path.Data());
657 }
658 break;
659
660 case 'C':
661 if( !option.CompareTo("dataset",TString::kIgnoreCase) ||
662 !option.CompareTo("C",TString::kIgnoreCase) )
663 {
664
665 if(CalRunFound==kFALSE)
666 {
667 *fLog << "Adding Cal run: "<< run << " [" << src << "]" << endl;
668 CalRunFound = kTRUE;
669
670 fCalRuns->AddRun(run,path.Data());
671 }
672 else
673 *fLog << "SKIPPING Cal run: "<< run << " [" << src << "]" << endl;
674
675
676 }
677 break;
678 }
679
680
681 } // End loop
682
683
684 return kTRUE;
685}
686
687
688
689
690
691// ---------------------------------------------------------------------------
692//
693// By default print the P,C and D runs of the current data set. With the
694// option "all" print only all the files in the FileList.
695//
696void MDataSetIter::Print(const Option_t *option) const
697{
698
699 //
700 // Print all the files in the FileList.
701 //
702 TString s(option);
703 if (s.Contains("all", TString::kIgnoreCase))
704 {
705 fFileList.Print();
706 return;
707 }
708
709
710 //
711 // Reset
712 //
713 fPedRuns->Reset();
714 fCalRuns->Reset();
715 fDataRuns->Reset();
716
717
718 //
719 // Print the runs of this data set
720 //
721 TString file;
722
723 *fLog << all << endl << " pedestal runs: " << endl;
724 *fLog << "---------------" << endl;
725 while( !(file=fPedRuns->Next()).IsNull() )
726 *fLog << file << endl;
727
728
729 *fLog << endl << " calibration runs: " << endl;
730 *fLog << "------------------" << endl;
731 while( !(file=fCalRuns->Next()).IsNull() )
732 *fLog << file << endl;
733
734
735 *fLog << endl << " data runs: " << endl;
736 *fLog << "-----------" << endl;
737 while( !(file=fDataRuns->Next()).IsNull() )
738 *fLog << file << endl;
739
740
741 *fLog << endl;
742
743
744 //
745 // Reset
746 //
747 fPedRuns->Reset();
748 fCalRuns->Reset();
749 fDataRuns->Reset();
750
751}
752
753
Note: See TracBrowser for help on using the repository browser.