1 /**************************************************************************
2 * Copyright(c) 1998-1999, 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 **************************************************************************/
18 /// By Laurent Aphecetche
20 #if !defined(__CINT__) || defined(__MAKECINT__)
24 #include "AliCDBEntry.h"
25 #include "AliCDBManager.h"
26 #include "AliMUON1DArray.h"
27 #include "AliMUON2DMap.h"
28 #include "AliMUONCalibParam1I.h"
29 #include "AliMUONCalibParam2F.h"
30 #include "AliMUONConstants.h"
31 #include "AliMUONObjectPair.h"
32 #include "AliMUONTriggerEfficiencyCells.h"
33 #include "AliMUONTriggerLut.h"
34 #include "AliMUONV2DStore.h"
35 #include "AliMUONVCalibParam.h"
36 #include "AliMUONVDataIterator.h"
37 #include "AliMpDEIterator.h"
38 #include "AliMpDEManager.h"
39 #include "AliMpManuList.h"
40 #include "AliMpSegmentation.h"
41 #include "AliMpVSegmentation.h"
42 #include "AliMpStationType.h"
43 #include "Riostream.h"
47 #include "TStopwatch.h"
51 //_____________________________________________________________________________
52 Int_t countChannels(const AliMpVSegmentation& seg)
56 for ( Int_t ix = 0; ix < seg.MaxPadIndexX(); ++ix )
58 for ( Int_t iy = 0; iy < seg.MaxPadIndexY(); ++iy )
60 if ( seg.HasPad(AliMpIntPair(ix,iy)) ) ++n;
66 //_____________________________________________________________________________
74 for ( Int_t station = 0; station < AliMUONConstants::NCh(); ++station )
80 Int_t de = it.CurrentDE();
81 for ( Int_t cathode = 0; cathode < 2; ++cathode )
83 const AliMpVSegmentation* seg
84 = AliMpSegmentation::Instance()->GetMpSegmentation(de,cathode);
85 n += countChannels(*seg);
89 cout << "Station " << station << " has " << n << " channels" << endl;
90 if ( station < AliMUONConstants::NTrackingCh() )
100 cout << "Tracker channels = " << ntracker << endl;
101 cout << "Trigger channels = " << ntrigger << endl;
102 cout << "Total channels =" << ntotal << endl;
105 //_____________________________________________________________________________
106 AliMUONV2DStore* read2D(const char* calibType, Int_t runNumber)
108 AliCDBManager* man = AliCDBManager::Instance();
109 man->SetDefaultStorage(CDBPath);
111 AliCDBEntry* entry = man->Get(calibType,runNumber);
115 return (AliMUONV2DStore*)entry->GetObject();
120 //_____________________________________________________________________________
121 AliMUONV1DStore* read1D(const char* calibType, Int_t runNumber)
123 AliCDBManager* man = AliCDBManager::Instance();
124 man->SetDefaultStorage(CDBPath);
126 AliCDBEntry* entry = man->Get(calibType,runNumber);
130 return (AliMUONV1DStore*)entry->GetObject();
135 //_____________________________________________________________________________
136 void checkCDB(const char* calibType)
138 TString c(calibType);
141 if ( c == "MUON/Calib/DeadChannels" )
146 AliMUONV2DStore* store = read2D(calibType);
149 TList* list = AliMpManuList::ManuList();
153 while ( ( p = (AliMpIntPair*)next() ) )
155 Int_t detElemId = p->GetFirst();
156 Int_t manuId = p->GetSecond();
158 AliMUONVCalibParam* value =
159 dynamic_cast<AliMUONVCalibParam*>(store->Get(detElemId,manuId));
163 for ( Int_t manuChannel = 0; manuChannel < value->Size(); ++manuChannel )
165 Float_t testValue = value->ValueAsFloat(manuChannel,0);
166 if ( testValue && testValue != refValue )
168 cout << "Got a strange value for DE=" << detElemId << " manuId="
169 << manuId << " manuChannel=" << manuChannel << " was expecting "
170 << refValue << " and I got " << testValue << endl;
176 cout << "Got a null value for DE=" << detElemId << " manuId="
185 //_____________________________________________________________________________
186 //void testDump(AliMUONV2DStore& store, int n)
188 // AliMUONObjectPair* p;
192 // for ( Int_t i = 0; i < n; ++i )
194 // AliMUONVDataIterator* it = store.Iterator();
196 // while ( ( p = dynamic_cast<AliMUONObjectPair*>(it->Next() ) ) )
198 // AliMpIntPair* dm = dynamic_cast<AliMpIntPair*>(p->Key());
201 // Int_t a(dm->GetFirst()+dm->GetSecond());
204 // AliMUONVCalibParam* param = dynamic_cast<AliMUONVCalibParam*>(p->Value());
211 // cout << c << endl;
214 //_____________________________________________________________________________
215 AliMUONV2DStore* diff(AliMUONV2DStore& store1, AliMUONV2DStore& store2,
216 const char* /* opt */)
218 // creates a store which contains store1-store2
219 // if opt="abs" the difference is absolute one,
220 // if opt="rel" then what is stored is (store1-store2)/store1
222 AliMUONV2DStore* d = static_cast<AliMUONV2DStore*>(store1.Clone());
224 AliMUONVDataIterator* it = d->Iterator();
226 AliMUONObjectPair* p;
228 while ( ( p = dynamic_cast<AliMUONObjectPair*>(it->Next() ) ) )
230 AliMpIntPair* dm = dynamic_cast<AliMpIntPair*>(p->Key());
231 //FIXME: this might happen (if a full manu is missing, for instance)
234 AliMUONVCalibParam* param = dynamic_cast<AliMUONVCalibParam*>(p->Value());
235 //FIXMENOT: this should *not* happen
238 AliMUONVCalibParam* param2 = dynamic_cast<AliMUONVCalibParam*>(store2.Get(dm->GetFirst(),dm->GetSecond()));
239 //FIXME: this might happen. Handle it.
242 for ( Int_t i = 0; i < param->Size(); ++i )
244 for ( Int_t j = 0; j < param->Dimension(); ++j )
246 param->SetValueAsFloat(i,j,param->ValueAsFloat(i,j) - param2->ValueAsFloat(i,j));
253 //_____________________________________________________________________________
254 void getBoundaries(const AliMUONV2DStore& store,
255 Float_t& x0min, Float_t& x0max,
256 Float_t& x1min, Float_t& x1max)
263 TList* list = AliMpManuList::ManuList();
267 while ( ( p = (AliMpIntPair*)next() ) )
269 Int_t detElemId = p->GetFirst();
270 Int_t manuId = p->GetSecond();
272 const AliMpVSegmentation* seg =
273 AliMpSegmentation::Instance()->GetMpSegmentationByElectronics(detElemId,manuId);
275 AliMUONVCalibParam* value =
276 dynamic_cast<AliMUONVCalibParam*>(store.Get(detElemId,manuId));
278 if (!value) continue;
280 for ( Int_t manuChannel = 0; manuChannel < value->Size(); ++manuChannel )
282 AliMpPad pad = seg->PadByLocation(AliMpIntPair(manuId,manuChannel),kFALSE);
283 if (!pad.IsValid()) continue;
285 Float_t x0 = value->ValueAsFloat(manuChannel,0);
287 x0min = TMath::Min(x0min,x0);
288 x0max = TMath::Max(x0max,x0);
289 if ( value->Dimension()>1 )
291 Float_t x1 = value->ValueAsFloat(manuChannel,1);
292 x1min = TMath::Min(x1min,x1);
293 x1max = TMath::Max(x1max,x1);
300 //_____________________________________________________________________________
301 void plot(const AliMUONV2DStore& store, const char* name, Int_t nbins)
303 Float_t x0min, x0max, x1min, x1max;
305 getBoundaries(store,x0min,x0max,x1min,x1max);
309 cerr << Form("Something is wrong with boundaries : x0(min,max)=%e,%e",
310 x0min,x0max) << endl;
314 if ( TMath::Abs(x0min-x0max) < 1E-3 )
320 TH1* h0 = new TH1F(Form("%s_0",name),Form("%s_0",name),
327 h1 = new TH1F(Form("%s_1",name),Form("%s_1",name),
331 TList* list = AliMpManuList::ManuList();
335 Int_t nPerStation[7];
337 for ( Int_t i = 0; i < 7; ++i ) nPerStation[i]=0;
339 while ( ( p = (AliMpIntPair*)next() ) )
341 Int_t detElemId = p->GetFirst();
342 Int_t manuId = p->GetSecond();
343 Int_t station = AliMpDEManager::GetChamberId(detElemId);
345 const AliMpVSegmentation* seg =
346 AliMpSegmentation::Instance()->GetMpSegmentationByElectronics(detElemId,manuId);
348 AliMUONVCalibParam* value =
349 dynamic_cast<AliMUONVCalibParam*>(store.Get(detElemId,manuId));
353 for ( Int_t manuChannel = 0; manuChannel < value->Size(); ++manuChannel )
355 AliMpPad pad = seg->PadByLocation(AliMpIntPair(manuId,manuChannel),kFALSE);
356 if (!pad.IsValid()) continue;
359 ++nPerStation[station];
360 Float_t x = value->ValueAsFloat(manuChannel,0);
364 h1->Fill(value->ValueAsFloat(manuChannel,1));
370 cout << "Got a null value for DE=" << detElemId << " manuId="
375 cout << "Number of channels = " << n << endl;
376 for ( Int_t i = 0; i < 7; ++i )
378 cout << "Station " << (i+1) << " " << nPerStation[i] << endl;
384 //_____________________________________________________________________________
385 void plotCDB(const char* calibType, Int_t runNumber)
387 AliMUONV2DStore* store = read2D(calibType,runNumber);
390 TString c(calibType);
391 c.ReplaceAll("/","_");
393 plot(*store,c.Data());
398 //_____________________________________________________________________________
399 void testReadStore(const AliMUONV2DStore& store, Int_t n)
401 TList* list = AliMpManuList::ManuList();
405 while ( ( p = (AliMpIntPair*)next() ) )
407 for ( Int_t i = 0; i < n; ++i )
409 store.Get(p->GetFirst(),p->GetSecond());
415 //_____________________________________________________________________________
416 Int_t makePedestalStore(AliMUONV2DStore& pedestalStore, Bool_t defaultValues)
418 TList* list = AliMpManuList::ManuList();
426 Bool_t replace = kFALSE;
428 const Int_t nChannels(64);
429 const Float_t kPedestalMeanMean(150);
430 const Float_t kPedestalMeanSigma(10);
431 const Float_t kPedestalSigmaMean(1.0);
432 const Float_t kPedestalSigmaSigma(0.2);
434 while ( ( p = (AliMpIntPair*)next() ) )
437 AliMUONVCalibParam* ped = new AliMUONCalibParam2F(nChannels,AliMUONVCalibParam::InvalidFloatValue());
439 Int_t detElemId = p->GetFirst();
441 Int_t manuId = p->GetSecond();
443 const AliMpVSegmentation* seg =
444 AliMpSegmentation::Instance()->GetMpSegmentationByElectronics(detElemId,manuId);
446 for ( Int_t manuChannel = 0; manuChannel < nChannels; ++manuChannel )
448 AliMpPad pad = seg->PadByLocation(AliMpIntPair(manuId,manuChannel),kFALSE);
449 if (!pad.IsValid()) continue;
453 Float_t meanPedestal;
454 Float_t sigmaPedestal;
464 while ( meanPedestal < 0 )
466 meanPedestal = gRandom->Gaus(kPedestalMeanMean,kPedestalMeanSigma);
469 while ( sigmaPedestal < 0 )
471 sigmaPedestal = gRandom->Gaus(kPedestalSigmaMean,kPedestalSigmaSigma);
474 ped->SetValueAsFloat(manuChannel,0,meanPedestal);
475 ped->SetValueAsFloat(manuChannel,1,sigmaPedestal);
478 Bool_t ok = pedestalStore.Set(detElemId,manuId,ped,replace);
481 cout << "Could not set DetElemId=" << detElemId << " manuId="
487 cout << nmanus << " Manus and " << nchannels << " channels." << endl;
492 //_____________________________________________________________________________
493 Int_t makeGainStore(AliMUONV2DStore& gainStore, Bool_t defaultValues)
495 TList* list = AliMpManuList::ManuList();
503 Bool_t replace = kFALSE;
505 const Int_t nChannels(64);
506 const Double_t kSaturation(3000);
507 const Double_t kGainMean(1.0);
508 const Double_t kGainSigma(0.05);
510 while ( ( p = (AliMpIntPair*)next() ) )
513 AliMUONVCalibParam* gain = new AliMUONCalibParam2F(nChannels,AliMUONVCalibParam::InvalidFloatValue());
515 Int_t detElemId = p->GetFirst();
516 Int_t manuId = p->GetSecond();
518 const AliMpVSegmentation* seg =
519 AliMpSegmentation::Instance()->GetMpSegmentationByElectronics(detElemId,manuId);
521 for ( Int_t manuChannel = 0; manuChannel < nChannels; ++manuChannel )
523 AliMpPad pad = seg->PadByLocation(AliMpIntPair(manuId,manuChannel),kFALSE);
524 if (!pad.IsValid()) continue;
529 Float_t saturation(kSaturation);
538 while ( meanGain < 0 )
540 meanGain = gRandom->Gaus(kGainMean,kGainSigma);
543 gain->SetValueAsFloat(manuChannel,0,meanGain);
544 gain->SetValueAsFloat(manuChannel,1,saturation);
547 Bool_t ok = gainStore.Set(detElemId,manuId,gain,replace);
550 cout << "Could not set DetElemId=" << detElemId << " manuId="
556 cout << nmanus << " Manus and " << nchannels << " channels." << endl;
560 //_____________________________________________________________________________
561 Int_t makeDeadStore(AliMUONV2DStore& deadStore, Bool_t defaultValues)
563 TList* list = AliMpManuList::ManuList();
571 Bool_t replace = kFALSE;
573 const Int_t nChannels(64);
574 const Double_t deadProba = 1.0; // 1%
576 while ( ( p = (AliMpIntPair*)next() ) )
579 AliMUONVCalibParam* dead = new AliMUONCalibParam1I(nChannels,-9999);
581 Int_t detElemId = p->GetFirst();
582 Int_t manuId = p->GetSecond();
584 const AliMpVSegmentation* seg =
585 AliMpSegmentation::Instance()->GetMpSegmentationByElectronics(detElemId,manuId);
587 for ( Int_t manuChannel = 0; manuChannel < nChannels; ++manuChannel )
589 AliMpPad pad = seg->PadByLocation(AliMpIntPair(manuId,manuChannel),kFALSE);
590 if (!pad.IsValid()) continue;
596 // probability that this channel is dead ~ 1%
597 if ( gRandom->Uniform(100.0) < deadProba )
599 Int_t reason = 5; // that value could be used to distinguish
600 // why the channel is dead or how it was flagged bad (online,
601 // offline, by chance...). 5 is of course a fake number.
602 dead->SetValueAsInt(manuChannel,0,reason);
606 Bool_t ok = deadStore.Set(detElemId,manuId,dead,replace);
609 cout << "Could not set DetElemId=" << detElemId << " manuId="
615 cout << nmanus << " Manus and " << nchannels << " channels." << endl;
619 //_____________________________________________________________________________
620 void testMakeStores(Int_t readLoop)
622 AliMUONV2DStore* pedestalStore = new AliMUON2DMap;
623 AliMUONV2DStore* gainStore = new AliMUON2DMap;
624 AliMUONV2DStore* deadStore = new AliMUON2DMap;
628 cout << "Creating" << endl;
630 Bool_t defaultValues = kTRUE;
633 makePedestalStore(*pedestalStore,defaultValues);
634 makeGainStore(*gainStore,defaultValues);
635 makeDeadStore(*deadStore,defaultValues);
638 cout << "Reading..." << endl;
640 testReadStore(*pedestalStore,readLoop);
641 testReadStore(*gainStore,readLoop);
642 testReadStore(*deadStore,readLoop);
643 cout << timer.CpuTime()/readLoop << " CPUs (mean of " << readLoop
644 <<" samples." << endl;
646 delete pedestalStore;
651 //_____________________________________________________________________________
652 void generateTrigger(const char* cdbpath)
655 // Generate trigger related conditions :
657 // - trigger masks for board (locals, regionals, global)
659 // - trigger efficiency
660 // - trigger switches (to be implemented FIXME)
662 const Int_t nlboards = 234;
663 AliMUONV1DStore* localBoardMasks = new AliMUON1DArray(nlboards+1);
665 // Generate fake mask values for 234 localboards and put that into
666 // one single container (localBoardMasks)
667 for ( Int_t i = 1; i <= nlboards; ++i )
669 AliMUONVCalibParam* localBoard = new AliMUONCalibParam1I(8);
670 for ( Int_t x = 0; x < 2; ++x )
672 for ( Int_t y = 0; y < 4; ++y )
675 localBoard->SetValueAsInt(index,0,0xFFFF);
678 localBoardMasks->Set(i,localBoard,kFALSE);
681 // Generate values for regional boards
682 const Int_t nrboards = 16;
683 AliMUONV1DStore* regionalBoardMasks = new AliMUON1DArray(16);
685 for ( Int_t i = 0; i < nrboards; ++i )
687 AliMUONVCalibParam* regionalBoard = new AliMUONCalibParam1I(16);
688 for ( Int_t j = 0; j < 16; ++j )
690 regionalBoard->SetValueAsInt(j,0,0x3F);
692 regionalBoardMasks->Set(i,regionalBoard,kFALSE);
695 // Generate values for global board
696 AliMUONVCalibParam* globalBoardMasks = new AliMUONCalibParam1I(16);
698 for ( Int_t j = 0; j < 16; ++j )
700 globalBoardMasks->SetValueAsInt(j,0,0xFFF);
703 AliMUONTriggerLut lut;
704 lut.ReadFromFile("$(ALICE_ROOT)/MUON/data/lutAptLpt1Hpt1p7.root");
706 AliMUONTriggerEfficiencyCells cells("$ALICE_ROOT/MUON/data/efficiencyCells.dat");
708 //--------------------------------------------
709 // Store the resulting containers into the CDB
710 Int_t ever = 99999999;
712 AliCDBId id("MUON/Calib/LocalTriggerBoardMasks",0,ever);
716 md.SetAliRootVersion(gROOT->GetVersion());
717 md.SetComment("Test with default values");
718 md.SetResponsible("Rachid Guernane");
720 AliCDBManager* man = AliCDBManager::Instance();
721 man->SetDefaultStorage(cdbpath);
722 man->Put(localBoardMasks,id,&md);
724 id.SetPath("MUON/Calib/RegionalTriggerBoardMasks");
726 man->Put(regionalBoardMasks,id,(AliCDBMetaData*)md.Clone());
728 id.SetPath("MUON/Calib/GlobalTriggerBoardMasks");
730 man->Put(globalBoardMasks,id,(AliCDBMetaData*)md.Clone());
732 id.SetPath("MUON/Calib/TriggerLut");
734 man->Put(&lut,id,(AliCDBMetaData*)md.Clone());
736 id.SetPath("MUON/Calib/TriggerEfficiency");
737 md.SetResponsible("Diego Stocco");
739 man->Put(&cells,id,(AliCDBMetaData*)md.Clone());
741 delete localBoardMasks;
742 delete regionalBoardMasks;
743 delete globalBoardMasks;
747 //_____________________________________________________________________________
748 void writeToCDB(const char* cdbpath, const char* calibpath, TObject* object,
749 Int_t startRun, Int_t endRun, Bool_t defaultValues)
751 AliCDBId id(calibpath,startRun,endRun);
754 md.SetAliRootVersion(gROOT->GetVersion());
757 md.SetComment("Test with default values");
761 md.SetComment("Test with random values");
763 md.SetResponsible("Laurent Aphecetche");
765 AliCDBManager* man = AliCDBManager::Instance();
766 man->SetDefaultStorage(cdbpath);
767 man->Put(object,id,&md);
770 //_____________________________________________________________________________
771 void writePedestals(const char* cdbpath, Bool_t defaultValues,
772 Int_t startRun, Int_t endRun)
774 AliMUONV2DStore* pedestalStore = new AliMUON2DMap;
775 Int_t ngenerated = makePedestalStore(*pedestalStore,defaultValues);
776 cout << "Ngenerated = " << ngenerated << endl;
778 writeToCDB(cdbpath,"MUON/Calib/Pedestals",pedestalStore,startRun,endRun,defaultValues);
781 //_____________________________________________________________________________
782 void writeGains(const char* cdbpath, Bool_t defaultValues,
783 Int_t startRun, Int_t endRun)
785 AliMUONV2DStore* gainStore = new AliMUON2DMap;
786 Int_t ngenerated = makeGainStore(*gainStore,defaultValues);
787 cout << "Ngenerated = " << ngenerated << endl;
789 writeToCDB(cdbpath,"MUON/Calib/Gains",gainStore,startRun,endRun,defaultValues);
792 //_____________________________________________________________________________
793 void writeDeadChannels(const char* cdbpath, Bool_t defaultValues,
794 Int_t startRun, Int_t endRun)
796 AliMUONV2DStore* deadStore = new AliMUON2DMap;
797 Int_t ngenerated = makeDeadStore(*deadStore,defaultValues);
798 cout << "Ngenerated = " << ngenerated << endl;
800 writeToCDB(cdbpath,"MUON/Calib/DeadChannels",deadStore,startRun,endRun,defaultValues);