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

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