1 /**************************************************************************
2 * Copyright(c) 2004, ALICE Experiment at CERN, All rights reserved. *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
7 * Permission to use, copy, modify and distribute this software and its *
8 * documentation strictly for non-commercial purposes is hereby granted *
9 * without fee, provided that the above copyright notice appears in all *
10 * copies and that both the copyright notice and this permission notice *
11 * appear in the supporting documentation. The authors make no claims *
12 * about the suitability of this software for any purpose. It is *
13 * provided "as is" without express or implied warranty. *
14 **************************************************************************/
16 /** @file AliFMDBaseDA.cxx
17 @author Hans Hjersing Dalsgaard <canute@nbi.dk>
18 @date Wed Mar 26 11:30:45 2008
19 @brief Base class for detector algorithms.
22 // This is the implementation of the (virtual) base class for the FMD
23 // detector algorithms(DA). It implements the creation of the relevant
24 // containers and handles the loop over the raw data. The derived
25 // classes can control the parameters and action to be taken making
26 // this the base class for the Pedestal, Gain and Physics DA.
29 #include "AliFMDBaseDA.h"
30 #include "AliRawReader.h"
31 #include "AliFMDDigit.h"
32 #include "AliFMDParameters.h"
33 #include "AliFMDRawReader.h"
34 #include "AliFMDCalibSampleRate.h"
35 #include "AliFMDCalibStripRange.h"
37 #include "AliRawEventHeaderBase.h"
38 #include "AliFMDDigit.h"
39 #include <TClonesArray.h>
44 #include <TStopwatch.h>
46 #include <TPluginManager.h>
50 //_____________________________________________________________________
51 ClassImp(AliFMDBaseDA)
53 ; // Do not delete - to let Emacs for mat the code
56 //_____________________________________________________________________
58 AliFMDBaseDA::GetStripPath(UShort_t det,
67 // det Detector number
68 // ring Ring identifier
71 // full If true, return full path
75 return Form("%s%sFMD%d%c[%02d,%03d]",
76 (full ? GetSectorPath(det, ring, sec, full) : ""),
77 (full ? "/" : ""), det, ring, sec, str);
79 //_____________________________________________________________________
81 AliFMDBaseDA::GetSectorPath(UShort_t det,
89 // det Detector number
90 // ring Ring identifier
93 // full If true, return full path
97 return Form("%s%sFMD%d%c[%02d]",
98 (full ? GetRingPath(det, ring, full) : ""),
99 (full ? "/" : ""), det, ring, sec);
101 //_____________________________________________________________________
103 AliFMDBaseDA::GetRingPath(UShort_t det,
107 // Get the strip path
110 // det Detector number
111 // ring Ring identifier
114 // full If true, return full path
118 return Form("%s%sFMD%d%c",
119 (full ? GetDetectorPath(det, full) : ""),
120 (full ? "/" : ""), det, ring);
122 //_____________________________________________________________________
124 AliFMDBaseDA::GetDetectorPath(UShort_t det,
127 // Get the strip path
130 // det Detector number
131 // ring Ring identifier
134 // full If true, return full path
138 return Form("%s%sFMD%d",
139 (full ? fDiagnosticsFilename.Data() : ""),
140 (full ? ":/" : ""), det);
143 //_____________________________________________________________________
144 AliFMDBaseDA::AliFMDBaseDA() :
146 fDiagnosticsFilename("diagnosticsHistograms.root"),
149 fSaveHistograms(kFALSE),
150 fMakeSummaries(kFALSE),
161 for(Int_t i = 0; i< 3;i++) {
162 fSeenDetectors[i] = false;
163 fNEventsPerDetector[i] = 0;
165 fDetectorArray.SetOwner();
166 Rotate("conditions.csv", 3);
167 fConditionsFile.open("conditions.csv");
169 //_____________________________________________________________________
170 AliFMDBaseDA::AliFMDBaseDA(const AliFMDBaseDA & baseDA) :
172 fDiagnosticsFilename(baseDA.fDiagnosticsFilename.Data()),
175 fSaveHistograms(baseDA.fSaveHistograms),
176 fMakeSummaries(baseDA.fMakeSummaries),
177 fDetectorArray(baseDA.fDetectorArray),
178 fPulseSize(baseDA.fPulseSize),
179 fPulseLength(baseDA.fPulseLength),
180 fRequiredEvents(baseDA.fRequiredEvents),
181 fCurrentEvent(baseDA.fCurrentEvent),
182 fRunno(baseDA.fRunno),
187 for(Int_t i = 0; i< 3;i++) {
188 fSeenDetectors[i] = baseDA.fSeenDetectors[0];
189 fNEventsPerDetector[i] = baseDA.fNEventsPerDetector[i];
192 fDetectorArray.SetOwner();
196 //_____________________________________________________________________
197 AliFMDBaseDA::~AliFMDBaseDA()
202 //_____________________________________________________________________
203 Bool_t AliFMDBaseDA::HaveEnough(Int_t nEvents) const
205 // if (!fAll) return nEvents > GetRequiredEvents();
206 if (nEvents <= 1) return false;
208 Bool_t ret = true; // Assume we have it
209 for (Int_t i = 0; i < 3; i++) {
210 if (!fSeenDetectors[i]) continue;
211 if (Int_t(fNEventsPerDetector[i]) <= GetRequiredEvents()) ret = false;
215 //_____________________________________________________________________
216 UShort_t AliFMDBaseDA::GetProgress(Int_t nEvents) const
219 // return UShort_t((nEvents *100)/ GetRequiredEvents());
221 if (nEvents <= 1) return 0;
225 for (Int_t i = 0; i < 3; i++) {
226 if (!fSeenDetectors[i]) continue;
227 got += fNEventsPerDetector[i];
228 total += GetRequiredEvents();
230 return UShort_t(total > 0 ? (got * 100.) / total : 0);
233 //_____________________________________________________________________
234 void AliFMDBaseDA::Run(AliRawReader* reader)
238 // if (fSaveHistograms)
239 // diagFile = TFile::Open(fDiagnosticsFilename.Data(),"RECREATE");
242 fRunno = reader->GetRunNumber();
244 AliFMDRawReader* fmdReader = new AliFMDRawReader(reader,0);
245 TClonesArray* digitArray = new TClonesArray("AliFMDDigit",0);
247 Bool_t sodread = kFALSE;
249 for(Int_t i=0;i<3;i++) {
250 if (!reader->NextEvent()) {
251 // Read Start-of-Run / Start-of-Files event
252 AliWarning(Form("Failed to read the %d%s event",
253 i+1, (i == 0 ? "st" : (i == 1 ? "nd" : "rd"))));
257 UInt_t eventType = reader->GetType();
258 if(eventType == AliRawEventHeaderBase::kStartOfData ||
259 eventType == AliRawEventHeaderBase::kFormatError) {
261 WriteConditionsData(fmdReader);
268 InitContainer(diagFile);
269 if (AliLog::GetDebugLevel("FMD","") >= 3) {
274 AliWarning("No SOD event detected!");
276 int lastProgress = 0;
278 for(Int_t i = 0; i< 3;i++) fNEventsPerDetector[i] = 0;
280 for(Int_t n = 1; !HaveEnough(n); n++) {
281 AliInfoF("Get the next event %d", n);
282 if(!reader->NextEvent()) { n--; continue; }
283 UInt_t eventType = reader->GetType();
284 AliInfoF("Event type is %d", eventType);
285 if(eventType != AliRawEventHeaderBase::kPhysicsEvent) { n--; continue; }
289 fmdReader->ReadAdcs(digitArray);
291 Bool_t seen[] = { false, false, false };
292 for(Int_t i = 0; i<digitArray->GetEntriesFast();i++) {
293 AliFMDDigit* digit = static_cast<AliFMDDigit*>(digitArray->At(i));
294 UShort_t det = digit->Detector();
295 fSeenDetectors[det-1] = true;
298 // Only fill if we do not have enough for this detector
299 if (Int_t(fNEventsPerDetector[det-1]) < GetRequiredEvents())
303 for(Int_t i = 0; i< 3;i++)
304 if (seen[i]) (fNEventsPerDetector[i])++;
308 Int_t nReq = GetRequiredEvents();
309 AliInfoF("%9d: %6d/%6d %6d/%6d %6d/%6d", n,
310 fNEventsPerDetector[0], nReq,
311 fNEventsPerDetector[1], nReq,
312 fNEventsPerDetector[2], nReq);
314 int progress = GetProgress(n);
315 if (progress <= lastProgress) continue;
316 lastProgress = progress;
317 std::cout << "Progress: " << lastProgress << " / 100 " << std::endl;
319 if (AliLog::GetDebugLevel("FMD","") >= 0) {
324 AliInfoF("Looped over %d events (%d,%d,%d)",GetCurrentEvent(),
325 fNEventsPerDetector[0],
326 fNEventsPerDetector[1],
327 fNEventsPerDetector[2]);
330 for(UShort_t det=1;det<=3;det++) {
331 if (!fSeenDetectors[det-1]) continue;
332 std::cout << "FMD" << det << std::endl;
333 UShort_t firstRing = (det == 1 ? 1 : 0);
334 for (UShort_t ir = firstRing; ir < 2; ir++) {
335 Char_t ring = (ir == 0 ? 'O' : 'I');
336 UShort_t nsec = (ir == 0 ? 40 : 20);
337 UShort_t nstr = (ir == 0 ? 256 : 512);
339 if (fMakeSummaries) MakeSummary(det, ring);
341 std::cout << " Ring " << ring << ": " << std::flush;
342 for(UShort_t sec =0; sec < nsec; sec++) {
343 for(UShort_t strip = 0; strip < nstr; strip++) {
344 Analyse(det,ring,sec,strip);
346 std::cout << '.' << std::flush;
348 // if(fSaveHistograms)
349 // diagFile->Flush();
350 std::cout << "done" << std::endl;
354 if(fOutputFile.is_open()) {
355 fOutputFile.write("# EOF\n",6);
361 if(fSaveHistograms ) {
362 diagFile = TFile::Open(fDiagnosticsFilename.Data(),"RECREATE");
363 fDetectorArray.Write("FMD", TObject::kSingleKey);
365 AliInfo("Closing diagnostics file - please wait ...");
366 // diagFile->Write();
372 //_____________________________________________________________________
374 void AliFMDBaseDA::InitContainer(TDirectory* diagFile)
376 //Prepare container for diagnostics
377 TDirectory* savDir = gDirectory;
379 for(UShort_t det=1;det<=3;det++) {
380 TObjArray* detArray = new TObjArray(det == 1 ? 1 : 2);
381 detArray->SetOwner();
382 detArray->SetName(GetDetectorPath(det, false));
383 fDetectorArray.AddAtAndExpand(detArray,det);
385 TDirectory* detDir = 0;
388 detDir = diagFile->mkdir(GetDetectorPath(det, kFALSE));
391 UShort_t FirstRing = (det == 1 ? 1 : 0);
392 for (UShort_t ir = FirstRing; ir < 2; ir++) {
393 Char_t ring = (ir == 0 ? 'O' : 'I');
394 UShort_t nsec = (ir == 0 ? 40 : 20);
395 UShort_t nstr = (ir == 0 ? 256 : 512);
396 TObjArray* ringArray = new TObjArray(nsec);
397 ringArray->SetOwner();
398 ringArray->SetName(GetRingPath(det, ring, false));
399 detArray->AddAtAndExpand(ringArray,ir);
402 TDirectory* ringDir = 0;
405 ringDir = detDir->mkdir(GetRingPath(det,ring, kFALSE));
409 for(UShort_t sec =0; sec < nsec; sec++) {
410 TObjArray* sectorArray = new TObjArray(nstr);
411 sectorArray->SetOwner();
412 sectorArray->SetName(GetSectorPath(det, ring, sec, false));
413 ringArray->AddAtAndExpand(sectorArray,sec);
416 TDirectory* secDir = 0;
419 secDir = ringDir->mkdir(GetSectorPath(det, ring, sec, kFALSE));
422 for(UShort_t strip = 0; strip < nstr; strip++) {
425 secDir->mkdir(GetStripPath(det, ring, sec, strip, kFALSE));
427 TObjArray* stripArray = new TObjArray(0);
428 stripArray->SetOwner(true);
429 stripArray->SetName(GetStripPath(det, ring, sec, strip, false));
430 sectorArray->AddAtAndExpand(stripArray, strip);
431 AddChannelContainer(stripArray, det, ring, sec, strip);
433 AddSectorSummary(sectorArray, det, ring, sec, nstr);
440 //_____________________________________________________________________
441 void AliFMDBaseDA::WriteConditionsData(AliFMDRawReader* fmdReader)
443 //Write the conditions data to file
444 AliFMDParameters* pars = AliFMDParameters::Instance();
445 fConditionsFile.write(Form("# %s \n",pars->GetConditionsShuttleID()),14);
447 fConditionsFile << "# This file created from run number " << fRunno
448 << " at " << now.AsString() << std::endl;
450 AliFMDCalibSampleRate* sampleRate = new AliFMDCalibSampleRate();
451 AliFMDCalibStripRange* stripRange = new AliFMDCalibStripRange();
453 fmdReader->ReadSODevent(sampleRate,stripRange,fPulseSize,fPulseLength,
456 sampleRate->WriteToFile(fConditionsFile, fSeenDetectors);
457 stripRange->WriteToFile(fConditionsFile, fSeenDetectors);
464 fConditionsFile.write("# Gain Events \n",15);
466 for(UShort_t det=1; det<=3;det++) {
467 if (!fSeenDetectors[det-1]) {
470 UShort_t firstring = (det == 1 ? 1 : 0);
471 for(UShort_t iring = firstring; iring <=1;iring++) {
472 Char_t ring = (iring == 1 ? 'I' : 'O');
473 for(UShort_t board =0 ; board <=1; board++) {
475 Int_t idx = GetHalfringIndex(det,ring,board);
477 fConditionsFile << det << ','
480 << fPulseLength.At(idx) << "\n";
486 fConditionsFile.write("# Gain Pulse \n",14);
488 for(UShort_t det=1; det<=3;det++) {
489 if (!fSeenDetectors[det-1]) {
492 UShort_t firstring = (det == 1 ? 1 : 0);
493 for(UShort_t iring = firstring; iring <=1;iring++) {
494 Char_t ring = (iring == 1 ? 'I' : 'O');
495 for(UShort_t board =0 ; board <=1; board++) {
497 Int_t idx = GetHalfringIndex(det,ring,board);
499 fConditionsFile << det << ','
502 << fPulseSize.At(idx) << "\n";
507 // sampleRate->WriteToFile(std::cout, fSeenDetectors);
508 // stripRange->WriteToFile(std::cout, fSeenDetectors);
510 if(fConditionsFile.is_open()) {
512 fConditionsFile.write("# EOF\n",6);
513 fConditionsFile.close();
518 //_____________________________________________________________________
519 Int_t AliFMDBaseDA::GetHalfringIndex(UShort_t det, Char_t ring,
520 UShort_t board) const
522 // Get the index corresponding to a half-ring
525 // det Detector number
526 // ring Ring identifier
527 // board Board number
530 // Internal index of the board
531 UShort_t iring = (ring == 'I' ? 1 : 0);
533 Int_t index = (((det-1) << 2) | (iring << 1) | (board << 0));
538 //_____________________________________________________________________
539 void AliFMDBaseDA::Rotate(const char* base, int max) const
542 // Rotate a set of files. base is the basic name of the files.
543 // If the file base.max exists it is removed.
544 // If the file base.n exists (where n < max) it is renamed to
546 // If the file base exists, it is renamed to base.1
549 // base Base name of the files
550 // max Maximum number to keep (minus one for the current).
552 // Note: TSystem::AccessPathName returns false if the condition is
555 // Check if we have base.max, and if so, remove it.
556 TString testName(Form("%s.%d", base, max));
557 if (!gSystem->AccessPathName(testName.Data()))
558 gSystem->Unlink(testName.Data());
560 // Loop down from max-1 to 1 and move files
561 for (int i = max-1; i >= 1; i--) {
562 testName = Form("%s.%d", base, i);
563 if (!gSystem->AccessPathName(testName.Data())) {
564 TString newName(Form("%s.%d", base, i+1));
565 gSystem->Rename(testName.Data(), newName.Data());
569 // If we have the file base, rename it to base.1
570 testName = Form("%s", base);
571 if (!gSystem->AccessPathName(testName.Data())){
572 TString newName(Form("%s.%d", base, 1));
573 gSystem->Rename(testName.Data(), newName.Data());
577 //_____________________________________________________________________
579 AliFMDBaseDA::MakeSummaryHistogram(const char* prefix, const char* title,
580 UShort_t d, Char_t r)
583 // Utility function for defining summary histograms
587 // ring Ring identifier
588 // prefix Histogram prefix
589 // title Histogram title
591 Int_t nX = ((d == 1 || r == 'I' || r == 'i') ? 20 : 40);
592 Int_t nY = ((d == 1 || r == 'I' || r == 'i') ? 512 : 256);
594 TH2* ret = new TH2F(Form("%sFMD%d%c", prefix, d, r),
595 Form("%s for FMD%d%c", title, d, r),
596 nX, -0.5, nX-0.5, nY, -0.5, nY-0.5);
597 ret->SetXTitle("Sector #");
598 ret->SetYTitle("Strip #");
599 ret->SetDirectory(0);
600 // if (!fSummaries) fSummaries = new TObjArray;
605 //_____________________________________________________________________
607 AliFMDBaseDA::GetDetectorArray(UShort_t det)
609 if (det < 1 || det > 3) {
610 AliErrorF("Detector index %d out of bounds", det);
613 return static_cast<TObjArray*>(fDetectorArray.At(det));
615 //_____________________________________________________________________
617 AliFMDBaseDA::GetRingArray(UShort_t det, Char_t ring)
619 Int_t idx = (ring == 'O' || ring == 'o' ? 0 : 1);
620 TObjArray* array = GetDetectorArray(det);
621 if (!array) return 0;
622 array = static_cast<TObjArray*>(array->At(idx));
623 if (!array) AliErrorF("No ring array for FMD%d%c (%d)", det, ring, idx);
626 //_____________________________________________________________________
628 AliFMDBaseDA::GetSectorArray(UShort_t det, Char_t ring, UShort_t sector)
630 TObjArray* array = GetRingArray(det,ring);
631 if (!array) return 0;
632 array = static_cast<TObjArray*>(array->At(sector));
633 if (!array) AliErrorF("No sector array for FMD%d%c[%02d]", det, ring, sector);
636 //_____________________________________________________________________
638 AliFMDBaseDA::GetStripArray(UShort_t det, Char_t ring,
639 UShort_t sector, UShort_t strip)
641 TObjArray* array = GetSectorArray(det,ring,sector);
642 if (!array) return 0;
643 array = static_cast<TObjArray*>(array->At(strip));
644 if (!array) AliErrorF("No strip array for FMD%d%c[%02d,%03d]",
645 det, ring, sector, strip);
649 //=====================================================================
650 AliFMDBaseDA::Runner::Runner()
657 //_____________________________________________________________________
659 AliFMDBaseDA::Runner::AddHandlers()
661 gROOT->GetPluginManager()->AddHandler("TVirtualStreamerInfo",
666 gROOT->GetPluginManager()->AddHandler("ROOT::Math::Minimizer", "Minuit",
669 "TMinuitMinimizer(const char *)");
670 gROOT->GetPluginManager()->AddHandler("ROOT::Math::Minimizer",
672 "ROOT::Math::GSLMinimizer",
674 "GSLMinimizer(const char *)");
675 gROOT->GetPluginManager()->AddHandler("ROOT::Math::Minimizer",
677 "ROOT::Math::GSLNLSMinimizer",
678 "MathMore", "GSLNLSMinimizer(int)");
679 gROOT->GetPluginManager()->AddHandler("ROOT::Math::Minimizer",
681 "ROOT::Math::GSLSimAnMinimizer",
683 "GSLSimAnMinimizer(int)");
684 gROOT->GetPluginManager()->AddHandler("ROOT::Math::Minimizer",
688 "TLinearMinimizer(const char *)");
689 gROOT->GetPluginManager()->AddHandler("ROOT::Math::Minimizer",
693 "TFumiliMinimizer(int)");
695 //_____________________________________________________________________
697 AliFMDBaseDA::Runner::ShowUsage(std::ostream& o, const char* progname)
699 o << "Usage: " << progname << " SOURCE [OPTIONS]\n\n"
701 << " -h,--help Show this help\n"
702 << " -d,--diagnostics[=FILE] Write diagnostics to file\n"
703 << " -D,--debug=LEVEL Set the debug level\n"
704 << " -A,--all Try to get data from all detectors\n\n"
705 << "SOURCE is one of\n"
706 << " * FILE.raw Raw data file\n"
707 << " * FILE.root ROOT'ified raw data file\n"
708 << " * collection://FILE.root Event list in a ROOT file\n"
709 << " * collection://FILE File containing list of ROOT files\n"
710 << " * ^FMD Monitor source\n"
711 << "There are other options too. Check the AliRawReader docs\n"
715 //_____________________________________________________________________
717 Bool_t ExtractValue(const TString& arg, TString& val)
720 Int_t eq = arg.Index("=");
721 if (eq == kNPOS) return false;
723 val = arg(eq+1, arg.Length()-eq-1);
728 //_____________________________________________________________________
730 AliFMDBaseDA::Runner::Init(int argc, char** argv)
734 // --- Process the command line ------------------------------------
736 Int_t debugLevel = 0;
739 for (int i = 1; i < argc; i++) {
740 TString arg(argv[i]);
741 Bool_t badOption = false;
743 if (arg[0] == '-') { // It's an option
744 if (arg[1] == '-') { // It's a long option
746 if (arg.EqualTo("--help")) help = true;
747 else if (arg.BeginsWith("--debug")) {
748 if (ExtractValue(arg, val))
749 debugLevel = val.Atoi();
751 else if (arg.BeginsWith("--diagnostics")) {
753 if (ExtractValue(arg, val))
756 else if (arg.EqualTo("--all")) fAll = true;
757 else badOption = true;
759 else { // Short option
760 TString next(i < argc-1 ? argv[i+1] : "");
762 case 'h': help = true; break;
763 case 'd': fDiag = true;
764 if (!next.IsNull() && next[0] != '-') {
770 if (!next.IsNull() && next[0] != '-') {
771 debugLevel = next.Atoi();
775 case 'A': fAll = true ; break ;
776 default: badOption = true;
780 std::cerr << argv[0] << ": Unknown option " << argv[i]
785 else { // source or compatibility debug level
786 if (source.IsNull()) source = arg;
787 else debugLevel = arg.Atoi();
791 // --- Check if help was requested ---------------------------------
793 ShowUsage(std::cout, argv[0]);
797 // --- Check if we have a source -----------------------------------
798 if (source.IsNull()) {
799 std::cerr << "No source given" << std::endl;
803 // --- Initialize various things -----------------------------------
804 AliFMDParameters::Instance()->Init(kFALSE,0);
806 //This will only work for FDR 1 data. When newer data becomes
807 //available the ! must be removed!
809 AliFMDParameters::Instance()->UseCompleteHeader(old);
811 AliLog::EnableDebug(debugLevel > 0);
812 AliLog::SetModuleDebugLevel("FMD", debugLevel);
814 // --- Make our reader ---------------------------------------------
815 fReader = AliRawReader::Create(source);
817 std::cerr << "Failed to make raw reader for source \"" << source
818 << "\"" << std::endl;
824 //_____________________________________________________________________
826 AliFMDBaseDA::Runner::RunNumber() const
828 if (!fReader) return -1;
829 return fReader->GetRunNumber();
832 //_____________________________________________________________________
834 AliFMDBaseDA::Runner::Exec(AliFMDBaseDA& da)
839 da.SetSaveDiagnostics(fDiag || !fDiagFile.IsNull());
841 if (!fDiagFile.IsNull()) da.SetDiagnosticsFilename(fDiagFile);
850 amore::da::AmoreDA myAmore(amore::da::AmoreDA::kSender);
852 for (UShort_t det = 1; det <= 3; det++) {
853 if (!da.HasSeenDetector(det)) continue;
854 TObject* runNo = new TObject;
855 runNo->SetUniqueID(fReader->GetRunNumber());
856 myAmore.Send(Form("gainRunNoFMD%d", det), runNo);
859 TIter next(&da.GetSummaries());
861 while ((obj = next()))
862 myAmore.Send(obj->GetName(), obj);
865 catch (exception& e) {
866 cerr << "Failed to make AMORE instance: " << e.what() << endl;
876 //_____________________________________________________________________
878 //_____________________________________________________________________