3 Version 1 for MUONTRKda MUON tracking
4 Working version for pedestal
5 Framework for gain computing
9 Rem: AliMUON2DMap stores all channels, even those which are not connected
10 if pedMean == -1, channel not connected to a pad
21 #include <Riostream.h>
26 #include "AliMUONRawStreamTracker.h"
27 #include "AliMUONDspHeader.h"
28 #include "AliMUONBlockHeader.h"
29 #include "AliMUONBusStruct.h"
30 #include "AliMUONDDLTracker.h"
31 #include "AliMUONV2DStore.h"
32 #include "AliMUON2DMap.h"
33 #include "AliMUONCalibParamNF.h"
34 #include "AliMUONObjectPair.h"
35 #include "AliMUONVDataIterator.h"
36 #include "AliMpDDLStore.h"
37 #include "AliMpIntPair.h"
39 #include "AliRawReaderDate.h"
47 #include "TStopwatch.h"
49 #include "TTimeStamp.h"
50 #include "TGraphErrors.h"
53 #include "TPluginManager.h"
57 AliMUONV2DStore* pedestalStore = new AliMUON2DMap(kFALSE);
58 const Int_t kNchannels = 64;
63 TH1F* pedMeanHisto = 0x0;
64 TH1F* pedSigmaHisto = 0x0;
65 Char_t histoFileName[256];
66 TString command("ped");
71 Double_t fitFunc(Double_t *x, Double_t *par)
73 //fit function for gains
79 f = par[0] + par[1]*xx;
81 f= par[0] + par[1]*xx + par[2]*xx*xx;
89 void MakePed(Int_t busPatchId, Int_t manuId, Int_t channelId, Int_t charge)
92 AliMUONVCalibParam* ped =
93 static_cast<AliMUONVCalibParam*>(pedestalStore->Get(busPatchId, manuId));
97 ped = new AliMUONCalibParamNF(2, kNchannels, -1.); // put default wise -1, not connected channel
98 pedestalStore->Set(busPatchId, manuId, ped, kFALSE);
102 ped->SetValueAsFloat(channelId, 0, 0.);
103 ped->SetValueAsFloat(channelId, 1, 0.);
106 Float_t pedMean = ped->ValueAsFloat(channelId, 0) + charge;
107 Float_t pedSigma = ped->ValueAsFloat(channelId, 1) + charge*charge;
109 ped->SetValueAsFloat(channelId, 0, pedMean);
110 ped->SetValueAsFloat(channelId, 1, pedSigma);
115 void MakePedStore(TString flatOutputFile = "")
126 sprintf(histoFileName,"mutrkped-%d.root",runNumber);
127 TFile* histoFile = new TFile(histoFileName,"RECREATE","MUON Tracking pedestals");
131 sprintf(name,"pedmean_allch");
132 sprintf(title,"Pedestal mean all channels");
136 pedMeanHisto = new TH1F(name,title,nx,xmin,xmax);
137 pedMeanHisto->SetDirectory(histoFile);
139 sprintf(name,"pedsigma_allch");
140 sprintf(title,"Pedestal sigma all channels");
144 pedSigmaHisto = new TH1F(name,title,nx,xmin,xmax);
145 pedSigmaHisto->SetDirectory(histoFile);
147 TTree* tree = new TTree("t","Pedestal tree");
148 tree->Branch("bp",&busPatchId,"bp/I");
149 tree->Branch("manu",&manuId,",manu/I");
150 tree->Branch("channel",&channelId,",channel/I");
152 if (!flatOutputFile.IsNull()) {
153 fileout.open(flatOutputFile.Data());
154 fileout<<"//===========================================================================" << endl;
155 fileout<<"// Pedestal file calculated by MUONTRKda"<<endl;
156 fileout<<"//===========================================================================" << endl;
157 fileout<<"// * Run : " << runNumber << endl;
158 fileout<<"// * Date : " << date.AsString("l") <<endl;
159 fileout<<"// * Statictics : " << nEvents << endl;
160 fileout<<"// * # of MANUS : " << nManu << endl;
161 fileout<<"// * # of channels : " << nChannel << endl;
163 fileout<<"//---------------------------------------------------------------------------" << endl;
164 fileout<<"//---------------------------------------------------------------------------" << endl;
165 fileout<<"//format : BUS_PATCH MANU_ID CHANNEL MEAN SIGMA"<<endl;
166 fileout<<"//---------------------------------------------------------------------------" << endl;
170 // iterator over pedestal
171 AliMUONVDataIterator* it = pedestalStore->Iterator();
173 AliMUONObjectPair* p;
175 while ( ( p = dynamic_cast<AliMUONObjectPair*>(it->Next() ) ) )
177 AliMUONVCalibParam* ped = dynamic_cast<AliMUONVCalibParam*>(p->Value());
178 AliMpIntPair* pair = dynamic_cast<AliMpIntPair*>(p->Key());
179 busPatchId = pair->GetFirst();
180 manuId = pair->GetSecond();
182 for (channelId = 0; channelId < ped->Size() ; ++channelId) {
184 pedMean = ped->ValueAsFloat(channelId, 0);
186 if (pedMean > 0) { // connected channels
188 ped->SetValueAsFloat(channelId, 0, pedMean/(Float_t)nEvents);
190 pedMean = ped->ValueAsFloat(channelId, 0);
191 pedSigma = ped->ValueAsFloat(channelId, 1);
193 ped->SetValueAsFloat(channelId, 1, TMath::Sqrt(TMath::Abs(pedSigma/(Float_t)nEvents - pedMean*pedMean)));
195 pedMean = ped->ValueAsFloat(channelId, 0) + 0.5 ;
196 pedSigma = ped->ValueAsFloat(channelId, 1);
199 if (!flatOutputFile.IsNull()) {
200 fileout << "\t" << busPatchId << "\t" << manuId <<"\t"<< channelId << "\t"
201 << pedMean <<"\t"<< pedSigma << endl;
204 pedMeanHisto->Fill(pedMean);
205 pedSigmaHisto->Fill(pedSigma);
211 delete p; // cos create a new object for each iteration
215 if (!flatOutputFile.IsNull())
224 // not usable need an update of DATE ->5.28, but it compiles
225 // uncomment when DATE version ok.
226 // setenv DATE_FES_PATH
227 // setenv DATE_RUN_NUMBER
228 // setenv DATE_ROLE_NAME
229 // setenv DATE_DETECTOR_CODE
231 // if (!flatOutputFile.IsNull()) {
233 // flatOutputFile.Prepend("./");
235 // status = daqDA_FES_storeFile(flatOutputFile.Data(),"MUONTRKda_results");
237 // printf("Failed to export file : %d\n",status);
245 void MakePedStoreForGain()
247 // store pedestal map in root file
249 Int_t injCharge = 200;
253 // compute and store pedestals
256 // store in root file
257 sprintf(histoFileName,"mutrkgain.root");
259 TString mode("UPDATE");
261 if (command.Contains("cre")) {
264 TFile* histoFile = new TFile(histoFileName, mode.Data(), "MUON Tracking Gains");
266 // second argument should be the injected charge, taken from config crocus file
267 // put also info about run number could be usefull
268 AliMpIntPair* pair = new AliMpIntPair(runNumber, injCharge);
270 if (mode.CompareTo("UPDATE") == 0) {
271 tree = (TTree*)histoFile->Get("t");
272 tree->SetBranchAddress("run",&pair);
273 tree->SetBranchAddress("ped",&pedestalStore);
276 tree = new TTree("t","Pedestal tree");
277 tree->Branch("run", "AliMpIntPair",&pair);
278 tree->Branch("ped", "AliMUON2DMap",&pedestalStore);
279 tree->SetBranchAddress("run",&pair);
280 tree->SetBranchAddress("ped",&pedestalStore);
285 tree->Write("t", TObject::kOverwrite); // overwrite the tree
292 void MakeGainStore(TString flatOutputFile)
295 // open file mutrkgain.root
296 // read again the pedestal for the calibration runs (9 runs ?)
297 // need the injection charge from config file (to be done)
298 // For each channel make a TGraphErrors (mean, sigma) vs injected charge
299 // Fit with a polynomial fct
300 // store the result in a flat file.
303 Float_t pedSigma[10];
304 Float_t injCharge[10];
305 Float_t injChargeErr[10];
310 if (!flatOutputFile.IsNull()) {
311 fileout.open(flatOutputFile.Data());
312 fileout<<"//===========================================================================" << endl;
313 fileout<<"// Pedestal file calculated by MUONTRKda"<<endl;
314 fileout<<"//===========================================================================" << endl;
315 fileout<<"// * Run : " << runNumber << endl;
316 fileout<<"// * Date : " << date.AsString("l") <<endl;
317 fileout<<"// * Statictics : " << nEvents << endl;
318 fileout<<"// * # of MANUS : " << nManu << endl;
319 fileout<<"// * # of channels : " << nChannel << endl;
321 fileout<<"//---------------------------------------------------------------------------" << endl;
322 fileout<<"//---------------------------------------------------------------------------" << endl;
323 fileout<<"//format : BUS_PATCH MANU_ID CHANNEL GAIN0 GAIN1 GAIN2 XLIM CHI2"<<endl;
324 fileout<<"//---------------------------------------------------------------------------" << endl;
329 sprintf(histoFileName,"mutrkgain.root");
330 TFile* histoFile = new TFile(histoFileName);
332 AliMUON2DMap* map[10];
333 AliMUONVCalibParam* ped[10];
335 AliMpIntPair* run[10];
337 //read back from root file
338 TTree* tree = (TTree*)histoFile->Get("t");
339 Int_t nEntries = tree->GetEntries();
342 for (Int_t i = 0; i < nEntries; ++i) {
345 tree->SetBranchAddress("ped",&map[i]);
346 tree->SetBranchAddress("run",&run[i]);
351 TF1* func = new TF1("func",fitFunc, 0., 2500., 4);
352 // TF1* func = new TF1("func","pol1");
354 // iterates over the first pedestal run
355 AliMUONVDataIterator* it = map[0]->Iterator();
357 AliMUONObjectPair* p;
359 while ( ( p = dynamic_cast<AliMUONObjectPair*>(it->Next() ) ) )
361 ped[0] = dynamic_cast<AliMUONVCalibParam*>(p->Value());
362 pair = dynamic_cast<AliMpIntPair*>(p->Key());
364 Int_t busPatchId = pair->GetFirst();
365 Int_t manuId = pair->GetSecond();
367 // read back pedestal from the other runs for the given (bupatch, manu)
368 for (Int_t i = 1; i < nEntries; ++i) {
369 ped[i] = static_cast<AliMUONVCalibParam*>(map[i]->Get(busPatchId, manuId));
372 // compute for each channel the gain parameters
373 for ( Int_t channelId = 0; channelId < ped[0]->Size() ; ++channelId ) {
376 for (Int_t i = 0; i < nEntries; ++i) {
378 if (!ped[i]) continue; //shouldn't happen.
379 pedMean[i] = ped[i]->ValueAsFloat(channelId, 0);
380 pedSigma[i] = ped[i]->ValueAsFloat(channelId, 1);
381 injCharge[i] = (Float_t)run[i]->GetSecond();
382 injChargeErr[i] = 1.;
384 if (pedMean[i] < 0) continue; // not connected
386 if (pedSigma[i] <= 0) pedSigma[i] = 1.; // should not happen.
393 TGraph *gain = new TGraphErrors(n, pedMean, pedSigma, injCharge, injChargeErr);
394 //should set some initial parameters
395 func->SetParameter(0,-300); // a0
396 func->SetParameter(1, 1.); // a1
397 func->SetParameter(2, 0.00001);// a2
398 func->SetParameter(3, 1100.); // xlim in ADC
400 gain->Fit("func","q");
401 cout << setw(8) << func->GetParameter(0) <<"\t"<< setw(8) << func->GetParameter(1) << endl;
403 if (!flatOutputFile.IsNull()) {
404 fileout << "\t" << busPatchId << "\t" << manuId <<"\t"<< channelId << "\t"
405 << setw(8) << func->GetParameter(0) <<"\t"<< setw(8) << func->GetParameter(1)
406 << setw(8) << func->GetParameter(2) <<"\t"<< setw(5) << func->GetParameter(3)
407 << "\t" << func->GetChisquare() << endl;
416 // file outputs for gain
417 if (!flatOutputFile.IsNull())
424 //*************************************************************//
427 int main(Int_t argc, Char_t **argv)
430 // needed for streamer application
431 gROOT->GetPluginManager()->AddHandler("TVirtualStreamerInfo",
438 Int_t printLevel = 0; // Global variable defined as extern in the others .cxx files
439 Int_t skipEvents = 0;
440 Int_t maxEvents = 1000000;
442 Int_t threshold = -1;
443 Char_t inputFile[256];
444 TString flatOutputFile;
445 TString crocusOutputFile;
446 TString crocusConfigFile;
448 Int_t totalParityErr = 0;
449 Int_t totalGlitchErr = 0;
453 // decode the input line
454 for (Int_t i = 1; i < argc; i++) // argument 0 is the executable name
459 if (arg[0] != '-') continue;
464 sprintf(inputFile,argv[i]);
468 flatOutputFile = argv[i];
472 crocusOutputFile = argv[i];
476 crocusConfigFile = argv[i];
484 printLevel=atoi(argv[i]);
488 skipEvents=atoi(argv[i]);
492 sscanf(argv[i],"%d",&maxEvents);
496 sscanf(argv[i],"%f",&nSigma);
500 sscanf(argv[i],"%d",&threshold);
504 printf("\n******************* %s usage **********************",argv[0]);
505 printf("\n%s -options, the available options are :",argv[0]);
506 printf("\n-h help (this screen)");
509 printf("\n-f <raw data file> (default = %s)",inputFile);
510 printf("\n-c <Crocus config. file> (default = %s)",crocusConfigFile.Data());
513 printf("\n-a <Flat ASCII file> (default = %s)",flatOutputFile.Data());
514 printf("\n-o <CROUCUS cmd file> (default = %s)",crocusOutputFile.Data());
516 printf("\n Options");
517 printf("\n-d <print level> (default = %d)",printLevel);
518 printf("\n-s <skip events> (default = %d)",skipEvents);
519 printf("\n-n <max events> (default = %d)",maxEvents);
520 printf("\n-p <n sigmas> (default = %f)",nSigma);
521 printf("\n-t <threshold (-1 = no)> (default = %d)",threshold);
522 printf("\n-e <execute ped/gain> (default = %s)",command.Data());
523 printf("\n-e <gain create> make gain & create a new root file");
524 printf("\n-e <gain> make gain & update root file");
525 printf("\n-e <gain compute> make gain & compute gains");
530 printf("%s : bad argument %s (please check %s -h)\n",argv[0],argv[i],argv[0]);
531 argc = 2; exit(-1); // exit if error
535 // set command to lower case
538 // decoding the events
541 Int_t nDateEvents = 0;
546 AliMUONDDLTracker* ddlTracker = 0x0;
547 AliMUONBlockHeader* blkHeader = 0x0;
548 AliMUONDspHeader* dspHeader = 0x0;
549 AliMUONBusStruct* busStruct = 0x0;
558 // once we have a configuration file in db
559 // copy locally a file from daq detector config db
560 // The current detector is identified by detector code in variable
561 // DATE_DETECTOR_CODE. It must be defined.
562 // If environment variable DAQDA_TEST_DIR is defined, files are copied from DAQDA_TEST_DIR
563 // instead of the database. The usual environment variables are not needed.
564 if (!crocusConfigFile.IsNull()) {
565 status = daqDA_DB_getFile("myconfig", crocusConfigFile.Data());
567 printf("Failed to get config file : %d\n",status);
573 status = monitorSetDataSource(inputFile);
575 cerr << "ERROR : monitorSetDataSource status (hex) = " << hex << status
576 << " " << monitorDecodeError(status) << endl;
579 status = monitorDeclareMp("MUON Tracking monitoring");
581 cerr << "ERROR : monitorDeclareMp status (hex) = " << hex << status
582 << " " << monitorDecodeError(status) << endl;
586 cout << "MUONTRKda : Reading data from file " << inputFile <<endl;
590 if (nEvents >= maxEvents) break;
591 if (nEvents && nEvents % 100 == 0)
592 cout<<"Cumulated events " << nEvents << endl;
594 // check shutdown condition
595 if (daqDA_checkShutdown())
598 // Skip Events if needed
600 status = monitorGetEventDynamic(&event);
605 status = monitorGetEventDynamic(&event);
607 cout<<"EOF found"<<endl;
613 // decoding rawdata headers
614 AliRawReader *rawReader = new AliRawReaderDate(event);
616 Int_t eventType = rawReader->GetType();
617 runNumber = rawReader->GetRunNumber();
619 if (eventType != PHYSICS_EVENT)
620 continue; // for the moment
624 // decoding MUON payload
625 AliMUONRawStreamTracker* rawStream = new AliMUONRawStreamTracker(rawReader);
628 while((status = rawStream->NextDDL())) {
630 if (printLevel) printf("\niDDL %d\n", rawStream->GetDDL());
632 ddlTracker = rawStream->GetDDLTracker();
634 // loop over block (CRT) structure
635 Int_t nBlock = ddlTracker->GetBlkHeaderEntries();
636 for(Int_t iBlock = 0; iBlock < nBlock ;iBlock++){
638 blkHeader = ddlTracker->GetBlkHeaderEntry(iBlock);
639 if (printLevel) printf("Block Total length %d\n",blkHeader->GetTotalLength());
641 // loop over DSP (FRT) structure
642 Int_t nDsp = blkHeader->GetDspHeaderEntries();
643 for(Int_t iDsp = 0; iDsp < nDsp ;iDsp++){ //DSP loop
645 dspHeader = blkHeader->GetDspHeaderEntry(iDsp);
646 if (printLevel) printf("Dsp length %d\n",dspHeader->GetTotalLength());
648 // loop over BusPatch structure
649 Int_t nBusPatch = dspHeader->GetBusPatchEntries();
650 for(Int_t iBusPatch = 0; iBusPatch < nBusPatch; iBusPatch++) {
652 busStruct = dspHeader->GetBusPatchEntry(iBusPatch);
654 if (printLevel) printf("busPatchId %d", busStruct->GetBusPatchId());
655 if (printLevel) printf(" BlockId %d", busStruct->GetBlockId());
656 if (printLevel) printf(" DspId %d\n", busStruct->GetDspId());
658 Int_t busPatchId = busStruct->GetBusPatchId();
661 Int_t dataSize = busStruct->GetLength();
662 for (Int_t iData = 0; iData < dataSize; iData++) {
666 Int_t manuId = busStruct->GetManuId(iData);
667 Int_t channelId = busStruct->GetChannelId(iData);
668 Int_t charge = busStruct->GetCharge(iData);
670 if (printLevel) printf("manuId: %d, channelId: %d charge: %d\n", manuId,
673 MakePed(busPatchId, manuId, channelId, charge);
680 totalParityErr += rawStream->GetPayLoad()->GetParityErrors();
681 totalGlitchErr += rawStream->GetPayLoad()->GetGlitchErrors();
689 nEvents -= totalGlitchErr; // skipped event cos of Glitch errors
692 cout << " Total number of parity errors in the Run: " << totalParityErr << endl;
693 cout << " Total number of glitch errors in the Run: " << totalGlitchErr << endl;
695 if (command.CompareTo("ped") == 0)
696 MakePedStore(flatOutputFile);
698 // option gain -> update root file with pedestal results
699 // gain + create -> recreate root file
700 // gain + comp -> update root file and compute gain parameters
702 if (command.Contains("gain"))
703 MakePedStoreForGain();
705 if (command.Contains("comp"))
706 MakeGainStore(flatOutputFile);
709 delete pedestalStore;
713 if (!(crocusConfigFile.IsNull()))
714 cout << "MUONTRKda : CROCUS command file generated : " << crocusOutputFile.Data() << endl;
716 cout << "MUONTRKda : WARNING no CROCUS command file generated" << endl;
718 cout << "MUONTRKda : Flat ASCII file generated : " << flatOutputFile << endl;
719 cout << "MUONTRKda : Histo file generated : " << histoFileName << endl;
720 cout << "MUONTRKda : Nb of DATE events = " << nDateEvents << endl;
721 cout << "MUONTRKda : Nb of events used = " << nEvents << endl;
723 printf("Execution time : R:%7.2fs C:%7.2fs\n", timers.RealTime(), timers.CpuTime());