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 "AliDCSValue.h"
27 #include "AliMUON1DArray.h"
28 #include "AliMUON2DMap.h"
29 #include "AliMUON2DStoreValidator.h"
30 #include "AliMUONCalibParam1I.h"
31 #include "AliMUONCalibParam2F.h"
32 #include "AliMUONConstants.h"
33 #include "AliMUONHVNamer.h"
34 #include "AliMUONObjectPair.h"
35 #include "AliMUONTriggerEfficiencyCells.h"
36 #include "AliMUONTriggerLut.h"
37 #include "AliMUONV2DStore.h"
38 #include "AliMUONVCalibParam.h"
39 #include "AliMUONVCalibParam.h"
40 #include "AliMUONVDataIterator.h"
41 #include "AliMpDEIterator.h"
42 #include "AliMpDEManager.h"
43 #include "AliMpManuList.h"
44 #include "AliMpSegmentation.h"
45 #include "AliMpStationType.h"
46 #include "AliMpVSegmentation.h"
47 #include "Riostream.h"
52 #include "TObjString.h"
54 #include "TStopwatch.h"
60 //_____________________________________________________________________________
61 AliMUONV2DStore* diff(AliMUONV2DStore& store1, AliMUONV2DStore& store2,
62 const char* /* opt */)
64 // creates a store which contains store1-store2
65 // if opt="abs" the difference is absolute one,
66 // if opt="rel" then what is stored is (store1-store2)/store1
68 AliMUONV2DStore* d = static_cast<AliMUONV2DStore*>(store1.Clone());
70 AliMUONVDataIterator* it = d->Iterator();
74 while ( ( p = dynamic_cast<AliMUONObjectPair*>(it->Next() ) ) )
76 AliMpIntPair* dm = dynamic_cast<AliMpIntPair*>(p->Key());
77 //FIXME: this might happen (if a full manu is missing, for instance)
80 AliMUONVCalibParam* param = dynamic_cast<AliMUONVCalibParam*>(p->Value());
81 //FIXMENOT: this should *not* happen
84 AliMUONVCalibParam* param2 = dynamic_cast<AliMUONVCalibParam*>(store2.Get(dm->GetFirst(),dm->GetSecond()));
85 //FIXME: this might happen. Handle it.
88 for ( Int_t i = 0; i < param->Size(); ++i )
90 for ( Int_t j = 0; j < param->Dimension(); ++j )
92 param->SetValueAsFloat(i,j,param->ValueAsFloat(i,j) - param2->ValueAsFloat(i,j));
99 //_____________________________________________________________________________
100 void getBoundaries(const AliMUONV2DStore& store,
101 Float_t& x0min, Float_t& x0max,
102 Float_t& x1min, Float_t& x1max)
109 TList* list = AliMpManuList::ManuList();
113 while ( ( p = (AliMpIntPair*)next() ) )
115 Int_t detElemId = p->GetFirst();
116 Int_t manuId = p->GetSecond();
118 const AliMpVSegmentation* seg =
119 AliMpSegmentation::Instance()->GetMpSegmentationByElectronics(detElemId,manuId);
121 AliMUONVCalibParam* value =
122 dynamic_cast<AliMUONVCalibParam*>(store.Get(detElemId,manuId));
124 if (!value) continue;
126 for ( Int_t manuChannel = 0; manuChannel < value->Size(); ++manuChannel )
128 AliMpPad pad = seg->PadByLocation(AliMpIntPair(manuId,manuChannel),kFALSE);
129 if (!pad.IsValid()) continue;
131 Float_t x0 = value->ValueAsFloat(manuChannel,0);
133 x0min = TMath::Min(x0min,x0);
134 x0max = TMath::Max(x0max,x0);
135 if ( value->Dimension()>1 )
137 Float_t x1 = value->ValueAsFloat(manuChannel,1);
138 x1min = TMath::Min(x1min,x1);
139 x1max = TMath::Max(x1max,x1);
146 //_____________________________________________________________________________
147 void plot(const AliMUONV2DStore& store, const char* name, Int_t nbins)
149 Float_t x0min, x0max, x1min, x1max;
151 getBoundaries(store,x0min,x0max,x1min,x1max);
155 cerr << Form("Something is wrong with boundaries : x0(min,max)=%e,%e",
156 x0min,x0max) << endl;
160 if ( TMath::Abs(x0min-x0max) < 1E-3 )
166 TH1* h0 = new TH1F(Form("%s_0",name),Form("%s_0",name),
173 h1 = new TH1F(Form("%s_1",name),Form("%s_1",name),
177 TList* list = AliMpManuList::ManuList();
181 Int_t nPerStation[7];
183 for ( Int_t i = 0; i < 7; ++i ) nPerStation[i]=0;
185 while ( ( p = (AliMpIntPair*)next() ) )
187 Int_t detElemId = p->GetFirst();
188 Int_t manuId = p->GetSecond();
189 Int_t station = AliMpDEManager::GetChamberId(detElemId);
191 const AliMpVSegmentation* seg =
192 AliMpSegmentation::Instance()->GetMpSegmentationByElectronics(detElemId,manuId);
194 AliMUONVCalibParam* value =
195 dynamic_cast<AliMUONVCalibParam*>(store.Get(detElemId,manuId));
199 for ( Int_t manuChannel = 0; manuChannel < value->Size(); ++manuChannel )
201 AliMpPad pad = seg->PadByLocation(AliMpIntPair(manuId,manuChannel),kFALSE);
202 if (!pad.IsValid()) continue;
205 ++nPerStation[station];
206 Float_t x = value->ValueAsFloat(manuChannel,0);
210 h1->Fill(value->ValueAsFloat(manuChannel,1));
216 cout << "Got a null value for DE=" << detElemId << " manuId="
221 cout << "Number of channels = " << n << endl;
222 for ( Int_t i = 0; i < 7; ++i )
224 cout << "Station " << (i+1) << " " << nPerStation[i] << endl;
230 //_____________________________________________________________________________
231 void testReadStore(const AliMUONV2DStore& store, Int_t n)
233 TList* list = AliMpManuList::ManuList();
237 while ( ( p = (AliMpIntPair*)next() ) )
239 for ( Int_t i = 0; i < n; ++i )
241 store.Get(p->GetFirst(),p->GetSecond());
247 //_____________________________________________________________________________
248 Int_t makeHVStore(TMap& aliasMap, Bool_t defaultValues)
250 AliMUONHVNamer hvNamer;
253 TObjArray* aliases = hvNamer.GenerateAliases();
258 for ( Int_t i = 0; i < aliases->GetEntries(); ++i )
260 TObjString* alias = static_cast<TObjString*>(aliases->At(i));
261 TString& aliasName = alias->String();
262 if ( aliasName.Contains("sw") )
264 // HV Switch (St345 only)
265 TObjArray* valueSet = new TObjArray;
266 valueSet->SetOwner(kTRUE);
268 Bool_t value = kTRUE;
272 Float_t r = random.Uniform();
273 if ( r < 0.007 ) value = kFALSE;
276 for ( UInt_t timeStamp = 0; timeStamp < 60*3; timeStamp += 60 )
278 AliDCSValue* dcsValue = new AliDCSValue(value,timeStamp);
279 valueSet->Add(dcsValue);
281 aliasMap.Add(new TObjString(*alias),valueSet);
286 TObjArray* valueSet = new TObjArray;
287 valueSet->SetOwner(kTRUE);
288 for ( UInt_t timeStamp = 0; timeStamp < 60*15; timeStamp += 120 )
290 Float_t value = 1500;
291 if (!defaultValues) value = random.Gaus(1750,62.5);
292 AliDCSValue* dcsValue = new AliDCSValue(value,timeStamp);
293 valueSet->Add(dcsValue);
295 aliasMap.Add(new TObjString(*alias),valueSet);
302 cout << nChannels << " HV channels and " << nSwitch << " switches" << endl;
304 return nChannels+nSwitch;
307 //_____________________________________________________________________________
308 Int_t makePedestalStore(AliMUONV2DStore& pedestalStore, Bool_t defaultValues)
310 TList* list = AliMpManuList::ManuList();
318 Bool_t replace = kFALSE;
320 const Int_t nChannels(64);
321 const Float_t kPedestalMeanMean(150);
322 const Float_t kPedestalMeanSigma(10);
323 const Float_t kPedestalSigmaMean(1.0);
324 const Float_t kPedestalSigmaSigma(0.2);
326 while ( ( p = (AliMpIntPair*)next() ) )
329 AliMUONVCalibParam* ped = new AliMUONCalibParam2F(nChannels,AliMUONVCalibParam::InvalidFloatValue());
331 Int_t detElemId = p->GetFirst();
333 Int_t manuId = p->GetSecond();
335 const AliMpVSegmentation* seg =
336 AliMpSegmentation::Instance()->GetMpSegmentationByElectronics(detElemId,manuId);
338 for ( Int_t manuChannel = 0; manuChannel < nChannels; ++manuChannel )
340 AliMpPad pad = seg->PadByLocation(AliMpIntPair(manuId,manuChannel),kFALSE);
341 if (!pad.IsValid()) continue;
345 Float_t meanPedestal;
346 Float_t sigmaPedestal;
356 while ( meanPedestal < 0 )
358 meanPedestal = gRandom->Gaus(kPedestalMeanMean,kPedestalMeanSigma);
361 while ( sigmaPedestal < 0 )
363 sigmaPedestal = gRandom->Gaus(kPedestalSigmaMean,kPedestalSigmaSigma);
366 ped->SetValueAsFloat(manuChannel,0,meanPedestal);
367 ped->SetValueAsFloat(manuChannel,1,sigmaPedestal);
370 Bool_t ok = pedestalStore.Set(detElemId,manuId,ped,replace);
373 cout << "Could not set DetElemId=" << detElemId << " manuId="
379 cout << nmanus << " Manus and " << nchannels << " channels." << endl;
384 //_____________________________________________________________________________
385 Int_t makeGainStore(AliMUONV2DStore& gainStore, Bool_t defaultValues)
387 TList* list = AliMpManuList::ManuList();
395 Bool_t replace = kFALSE;
397 const Int_t nChannels(64);
398 const Double_t kSaturation(3000);
399 const Double_t kGainMean(1.0);
400 const Double_t kGainSigma(0.05);
402 while ( ( p = (AliMpIntPair*)next() ) )
405 AliMUONVCalibParam* gain = new AliMUONCalibParam2F(nChannels,AliMUONVCalibParam::InvalidFloatValue());
407 Int_t detElemId = p->GetFirst();
408 Int_t manuId = p->GetSecond();
410 const AliMpVSegmentation* seg =
411 AliMpSegmentation::Instance()->GetMpSegmentationByElectronics(detElemId,manuId);
413 for ( Int_t manuChannel = 0; manuChannel < nChannels; ++manuChannel )
415 AliMpPad pad = seg->PadByLocation(AliMpIntPair(manuId,manuChannel),kFALSE);
416 if (!pad.IsValid()) continue;
421 Float_t saturation(kSaturation);
430 while ( meanGain < 0 )
432 meanGain = gRandom->Gaus(kGainMean,kGainSigma);
435 gain->SetValueAsFloat(manuChannel,0,meanGain);
436 gain->SetValueAsFloat(manuChannel,1,saturation);
439 Bool_t ok = gainStore.Set(detElemId,manuId,gain,replace);
442 cout << "Could not set DetElemId=" << detElemId << " manuId="
448 cout << nmanus << " Manus and " << nchannels << " channels." << endl;
452 //_____________________________________________________________________________
453 void testMakeStores(Int_t readLoop)
455 AliMUONV2DStore* pedestalStore = new AliMUON2DMap;
456 AliMUONV2DStore* gainStore = new AliMUON2DMap;
460 cout << "Creating" << endl;
462 Bool_t defaultValues = kTRUE;
465 makePedestalStore(*pedestalStore,defaultValues);
466 makeGainStore(*gainStore,defaultValues);
469 cout << "Reading..." << endl;
471 testReadStore(*pedestalStore,readLoop);
472 testReadStore(*gainStore,readLoop);
473 cout << timer.CpuTime()/readLoop << " CPUs (mean of " << readLoop
474 <<" samples." << endl;
476 delete pedestalStore;
480 //_____________________________________________________________________________
481 void generateTrigger(const char* cdbpath)
484 // Generate trigger related conditions :
486 // - trigger masks for board (locals, regionals, global)
488 // - trigger efficiency
489 // - trigger switches (to be implemented FIXME)
491 const Int_t nlboards = 234;
492 AliMUONV1DStore* localBoardMasks = new AliMUON1DArray(nlboards+1);
494 // Generate fake mask values for 234 localboards and put that into
495 // one single container (localBoardMasks)
496 for ( Int_t i = 1; i <= nlboards; ++i )
498 AliMUONVCalibParam* localBoard = new AliMUONCalibParam1I(8);
499 for ( Int_t x = 0; x < 2; ++x )
501 for ( Int_t y = 0; y < 4; ++y )
504 localBoard->SetValueAsInt(index,0,0xFFFF);
507 localBoardMasks->Set(i,localBoard,kFALSE);
510 // Generate values for regional boards
511 const Int_t nrboards = 16;
512 AliMUONV1DStore* regionalBoardMasks = new AliMUON1DArray(16);
514 for ( Int_t i = 0; i < nrboards; ++i )
516 AliMUONVCalibParam* regionalBoard = new AliMUONCalibParam1I(16);
517 for ( Int_t j = 0; j < 16; ++j )
519 regionalBoard->SetValueAsInt(j,0,0x3F);
521 regionalBoardMasks->Set(i,regionalBoard,kFALSE);
524 // Generate values for global board
525 AliMUONVCalibParam* globalBoardMasks = new AliMUONCalibParam1I(16);
527 for ( Int_t j = 0; j < 16; ++j )
529 globalBoardMasks->SetValueAsInt(j,0,0xFFF);
532 AliMUONTriggerLut lut;
533 lut.ReadFromFile("$(ALICE_ROOT)/MUON/data/lutAptLpt1Hpt1p7.root");
535 AliMUONTriggerEfficiencyCells cells("$ALICE_ROOT/MUON/data/efficiencyCells.dat");
537 //--------------------------------------------
538 // Store the resulting containers into the CDB
539 Int_t ever = 99999999;
541 AliCDBId id("MUON/Calib/LocalTriggerBoardMasks",0,ever);
545 md.SetAliRootVersion(gROOT->GetVersion());
546 md.SetComment("Test with default values");
547 md.SetResponsible("Rachid Guernane");
549 AliCDBManager* man = AliCDBManager::Instance();
550 man->SetDefaultStorage(cdbpath);
551 man->Put(localBoardMasks,id,&md);
553 id.SetPath("MUON/Calib/RegionalTriggerBoardMasks");
555 man->Put(regionalBoardMasks,id,(AliCDBMetaData*)md.Clone());
557 id.SetPath("MUON/Calib/GlobalTriggerBoardMasks");
559 man->Put(globalBoardMasks,id,(AliCDBMetaData*)md.Clone());
561 id.SetPath("MUON/Calib/TriggerLut");
563 man->Put(&lut,id,(AliCDBMetaData*)md.Clone());
565 id.SetPath("MUON/Calib/TriggerEfficiency");
566 md.SetResponsible("Diego Stocco");
568 man->Put(&cells,id,(AliCDBMetaData*)md.Clone());
570 delete localBoardMasks;
571 delete regionalBoardMasks;
572 delete globalBoardMasks;
576 //_____________________________________________________________________________
577 void writeToCDB(const char* cdbpath, const char* calibpath, TObject* object,
578 Int_t startRun, Int_t endRun, Bool_t defaultValues)
580 AliCDBId id(calibpath,startRun,endRun);
583 md.SetAliRootVersion(gROOT->GetVersion());
586 md.SetComment("Test with default values");
590 md.SetComment("Test with random values");
592 md.SetResponsible("Laurent Aphecetche");
594 AliCDBManager* man = AliCDBManager::Instance();
595 man->SetDefaultStorage(cdbpath);
596 man->Put(object,id,&md);
599 //_____________________________________________________________________________
600 void writeHV(const char* cdbpath, Bool_t defaultValues,
601 Int_t startRun, Int_t endRun)
603 /// generate HV values (either cste = 1500 V) if defaultValues=true or random
604 /// if defaultValues=false, see makeHVStore) and
605 /// store them into CDB located at cdbpath, with a validity period
606 /// ranging from startRun to endRun
608 TMap* hvStore = new TMap;
609 Int_t ngenerated = makeHVStore(*hvStore,defaultValues);
610 cout << "Ngenerated = " << ngenerated << endl;
612 writeToCDB(cdbpath,"MUON/Calib/HV",hvStore,startRun,endRun,defaultValues);
617 //_____________________________________________________________________________
618 void writePedestals(const char* cdbpath, Bool_t defaultValues,
619 Int_t startRun, Int_t endRun)
621 /// generate pedestal values (either 0 if defaultValues=true or random
622 /// if defaultValues=false, see makePedestalStore) and
623 /// store them into CDB located at cdbpath, with a validity period
624 /// ranging from startRun to endRun
626 AliMUONV2DStore* pedestalStore = new AliMUON2DMap;
627 Int_t ngenerated = makePedestalStore(*pedestalStore,defaultValues);
628 cout << "Ngenerated = " << ngenerated << endl;
630 writeToCDB(cdbpath,"MUON/Calib/Pedestals",pedestalStore,startRun,endRun,defaultValues);
631 delete pedestalStore;
634 //_____________________________________________________________________________
635 void writeGains(const char* cdbpath, Bool_t defaultValues,
636 Int_t startRun, Int_t endRun)
638 /// generate gain values (either 1 if defaultValues=true or random
639 /// if defaultValues=false, see makePedestalStore) and
640 /// store them into CDB located at cdbpath, with a validity period
641 /// ranging from startRun to endRun
643 AliMUONV2DStore* gainStore = new AliMUON2DMap;
644 Int_t ngenerated = makeGainStore(*gainStore,defaultValues);
645 cout << "Ngenerated = " << ngenerated << endl;
647 writeToCDB(cdbpath,"MUON/Calib/Gains",gainStore,startRun,endRun,defaultValues);
651 //_____________________________________________________________________________
652 Bool_t check1I(const AliMUONVCalibParam& calib, Int_t channel)
656 return ( calib.ValueAsInt(channel) == 0);
659 //_____________________________________________________________________________
660 void validate1I(const AliMUONV2DStore& store)
662 AliMUON2DStoreValidator validator;
664 TObjArray* a = validator.Validate(store,check1I);
670 validator.Report(lines);
675 //_____________________________________________________________________________
676 void validate2F(const AliMUONV2DStore& store)
678 AliMUON2DStoreValidator validator;
680 TObjArray* a = validator.Validate(store,AliMUONCalibParam2F::InvalidFloatValue());
686 validator.Report(lines);
691 //_____________________________________________________________________________
692 void dump(const TArrayI& a, const char* what)
694 cout << what << " " << a.GetSize() << " manus" << endl;
695 for ( Int_t i = 0; i < a.GetSize(); ++i )
697 cout << Form(" %5d ",a[i]);
702 //_____________________________________________________________________________
714 Int_t detElemId = it.CurrentDEId();
715 AliMp::StationType stationType = AliMpDEManager::GetStationType(detElemId);
716 if ( stationType != AliMp::kStationTrigger )
718 const AliMpVSegmentation* seg0 = AliMpSegmentation::Instance()->GetMpSegmentation(detElemId,AliMp::kCath0);
719 const AliMpVSegmentation* seg1 = AliMpSegmentation::Instance()->GetMpSegmentation(detElemId,AliMp::kCath1);
722 seg0->GetAllElectronicCardIDs(a0);
723 seg1->GetAllElectronicCardIDs(a1);
725 cout << Form("DE %5d B %4d NB %4d Total %5d",detElemId,
726 a0.GetSize(),a1.GetSize(),a0.GetSize()+a1.GetSize())
732 if ( detElemId == 500 )
741 cout << Form("B %5d NB %5d Total %5d",b,nb,b+nb) << endl;
744 //_____________________________________________________________________________
745 void count(const AliMUONV2DStore& store)
747 AliMUONVDataIterator* it = store.Iterator();
748 AliMUONObjectPair* pair;
749 std::map<int,std::pair<int,int> > demap;
751 while ( ( pair = static_cast<AliMUONObjectPair*>(it->Next()) ) )
753 AliMpIntPair* ip = static_cast<AliMpIntPair*>(pair->First());
755 Int_t detElemId = ip->GetFirst();
757 Int_t manuId = ip->GetSecond();
759 const AliMpVSegmentation* seg =
760 AliMpSegmentation::Instance()->GetMpSegmentationByElectronics(detElemId,manuId);
762 if ( seg->PlaneType() == AliMp::kNonBendingPlane )
764 demap[detElemId].second++;
768 demap[detElemId].first++;
772 std::map<int,std::pair<int,int> >::const_iterator mit;
777 for ( mit = demap.begin(); mit != demap.end(); ++mit )
779 cout << Form("DE %5d B %4d NB %4d Total %5d",mit->first,
780 mit->second.first,mit->second.second,
781 mit->second.first+mit->second.second) << endl;
782 b += mit->second.first;
783 nb += mit->second.second;
786 cout << Form("B %5d NB %5d Total %5d",b,nb,b+nb) << endl;