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 "AliMUONVStore.h"
32 #include "AliMUON2DMap.h"
33 #include "AliMUONCalibParamNF.h"
34 #include "AliMpDDLStore.h"
35 #include "AliMpIntPair.h"
36 #include "AliMpConstants.h"
37 #include "AliRawReaderDate.h"
45 #include "TStopwatch.h"
47 #include "TTimeStamp.h"
48 #include "TGraphErrors.h"
51 #include "TPluginManager.h"
55 AliMUONVStore* pedestalStore = new AliMUON2DMap(kFALSE);
56 const Int_t kNchannels = AliMpConstants::ManuNofChannels();
61 TH1F* pedMeanHisto = 0x0;
62 TH1F* pedSigmaHisto = 0x0;
63 Char_t histoFileName[256];
64 TString command("ped");
69 Double_t fitFunc(Double_t *x, Double_t *par)
71 //fit function for gains
77 f = par[0] + par[1]*xx;
79 f= par[0] + par[1]*xx + par[2]*xx*xx;
87 void MakePed(Int_t busPatchId, Int_t manuId, Int_t channelId, Int_t charge)
90 AliMUONVCalibParam* ped =
91 static_cast<AliMUONVCalibParam*>(pedestalStore->Get(busPatchId, manuId));
95 ped = new AliMUONCalibParamNF(2, kNchannels, -1.); // put default wise -1, not connected channel
96 pedestalStore->Set(busPatchId, manuId, ped, kFALSE);
100 ped->SetValueAsFloat(channelId, 0, 0.);
101 ped->SetValueAsFloat(channelId, 1, 0.);
104 Float_t pedMean = ped->ValueAsFloat(channelId, 0) + charge;
105 Float_t pedSigma = ped->ValueAsFloat(channelId, 1) + charge*charge;
107 ped->SetValueAsFloat(channelId, 0, pedMean);
108 ped->SetValueAsFloat(channelId, 1, pedSigma);
113 void MakePedStore(TString flatOutputFile = "")
124 sprintf(histoFileName,"mutrkped-%d.root",runNumber);
125 TFile* histoFile = new TFile(histoFileName,"RECREATE","MUON Tracking pedestals");
129 sprintf(name,"pedmean_allch");
130 sprintf(title,"Pedestal mean all channels");
134 pedMeanHisto = new TH1F(name,title,nx,xmin,xmax);
135 pedMeanHisto->SetDirectory(histoFile);
137 sprintf(name,"pedsigma_allch");
138 sprintf(title,"Pedestal sigma all channels");
142 pedSigmaHisto = new TH1F(name,title,nx,xmin,xmax);
143 pedSigmaHisto->SetDirectory(histoFile);
145 TTree* tree = new TTree("t","Pedestal tree");
146 tree->Branch("bp",&busPatchId,"bp/I");
147 tree->Branch("manu",&manuId,",manu/I");
148 tree->Branch("channel",&channelId,",channel/I");
150 if (!flatOutputFile.IsNull()) {
151 fileout.open(flatOutputFile.Data());
152 fileout<<"//===========================================================================" << endl;
153 fileout<<"// Pedestal file calculated by MUONTRKda"<<endl;
154 fileout<<"//===========================================================================" << endl;
155 fileout<<"// * Run : " << runNumber << endl;
156 fileout<<"// * Date : " << date.AsString("l") <<endl;
157 fileout<<"// * Statictics : " << nEvents << endl;
158 fileout<<"// * # of MANUS : " << nManu << endl;
159 fileout<<"// * # of channels : " << nChannel << endl;
161 fileout<<"//---------------------------------------------------------------------------" << endl;
162 fileout<<"//---------------------------------------------------------------------------" << endl;
163 fileout<<"//format : BUS_PATCH MANU_ID CHANNEL MEAN SIGMA"<<endl;
164 fileout<<"//---------------------------------------------------------------------------" << endl;
168 // iterator over pedestal
169 TIter next(pedestalStore);
170 AliMUONVCalibParam* ped;
172 while ( ( ped = dynamic_cast<AliMUONVCalibParam*>(next() ) ) )
174 busPatchId = ped->ID0();
177 for (channelId = 0; channelId < ped->Size() ; ++channelId) {
179 pedMean = ped->ValueAsFloat(channelId, 0);
181 if (pedMean > 0) { // connected channels
183 ped->SetValueAsFloat(channelId, 0, pedMean/(Float_t)nEvents);
185 pedMean = ped->ValueAsFloat(channelId, 0);
186 pedSigma = ped->ValueAsFloat(channelId, 1);
188 ped->SetValueAsFloat(channelId, 1, TMath::Sqrt(TMath::Abs(pedSigma/(Float_t)nEvents - pedMean*pedMean)));
190 pedMean = ped->ValueAsFloat(channelId, 0) + 0.5 ;
191 pedSigma = ped->ValueAsFloat(channelId, 1);
194 if (!flatOutputFile.IsNull()) {
195 fileout << "\t" << busPatchId << "\t" << manuId <<"\t"<< channelId << "\t"
196 << pedMean <<"\t"<< pedSigma << endl;
199 pedMeanHisto->Fill(pedMean);
200 pedSigmaHisto->Fill(pedSigma);
208 if (!flatOutputFile.IsNull())
217 // not usable need an update of DATE ->5.28, but it compiles
218 // uncomment when DATE version ok.
219 // setenv DATE_FES_PATH
220 // setenv DATE_RUN_NUMBER
221 // setenv DATE_ROLE_NAME
222 // setenv DATE_DETECTOR_CODE
224 // if (!flatOutputFile.IsNull()) {
226 // flatOutputFile.Prepend("./");
228 // status = daqDA_FES_storeFile(flatOutputFile.Data(),"MUONTRKda_results");
230 // printf("Failed to export file : %d\n",status);
238 void MakePedStoreForGain()
240 // store pedestal map in root file
242 Int_t injCharge = 200;
246 // compute and store pedestals
249 // store in root file
250 sprintf(histoFileName,"mutrkgain.root");
252 TString mode("UPDATE");
254 if (command.Contains("cre")) {
257 TFile* histoFile = new TFile(histoFileName, mode.Data(), "MUON Tracking Gains");
259 // second argument should be the injected charge, taken from config crocus file
260 // put also info about run number could be usefull
261 AliMpIntPair* pair = new AliMpIntPair(runNumber, injCharge);
263 if (mode.CompareTo("UPDATE") == 0) {
264 tree = (TTree*)histoFile->Get("t");
265 tree->SetBranchAddress("run",&pair);
266 tree->SetBranchAddress("ped",&pedestalStore);
269 tree = new TTree("t","Pedestal tree");
270 tree->Branch("run", "AliMpIntPair",&pair);
271 tree->Branch("ped", "AliMUON2DMap",&pedestalStore);
272 tree->SetBranchAddress("run",&pair);
273 tree->SetBranchAddress("ped",&pedestalStore);
278 tree->Write("t", TObject::kOverwrite); // overwrite the tree
285 void MakeGainStore(TString flatOutputFile)
288 // open file mutrkgain.root
289 // read again the pedestal for the calibration runs (9 runs ?)
290 // need the injection charge from config file (to be done)
291 // For each channel make a TGraphErrors (mean, sigma) vs injected charge
292 // Fit with a polynomial fct
293 // store the result in a flat file.
296 Float_t pedSigma[10];
297 Float_t injCharge[10];
298 Float_t injChargeErr[10];
303 if (!flatOutputFile.IsNull()) {
304 fileout.open(flatOutputFile.Data());
305 fileout<<"//===========================================================================" << endl;
306 fileout<<"// Pedestal file calculated by MUONTRKda"<<endl;
307 fileout<<"//===========================================================================" << endl;
308 fileout<<"// * Run : " << runNumber << endl;
309 fileout<<"// * Date : " << date.AsString("l") <<endl;
310 fileout<<"// * Statictics : " << nEvents << endl;
311 fileout<<"// * # of MANUS : " << nManu << endl;
312 fileout<<"// * # of channels : " << nChannel << endl;
314 fileout<<"//---------------------------------------------------------------------------" << endl;
315 fileout<<"//---------------------------------------------------------------------------" << endl;
316 fileout<<"//format : BUS_PATCH MANU_ID CHANNEL GAIN0 GAIN1 GAIN2 XLIM CHI2"<<endl;
317 fileout<<"//---------------------------------------------------------------------------" << endl;
322 sprintf(histoFileName,"mutrkgain.root");
323 TFile* histoFile = new TFile(histoFileName);
325 AliMUON2DMap* map[10];
326 AliMUONVCalibParam* ped[10];
328 AliMpIntPair* run[10];
330 //read back from root file
331 TTree* tree = (TTree*)histoFile->Get("t");
332 Int_t nEntries = tree->GetEntries();
335 for (Int_t i = 0; i < nEntries; ++i) {
338 tree->SetBranchAddress("ped",&map[i]);
339 tree->SetBranchAddress("run",&run[i]);
344 TF1* func = new TF1("func",fitFunc, 0., 2500., 4);
345 // TF1* func = new TF1("func","pol1");
347 // iterates over the first pedestal run
349 AliMUONVCalibParam* p;
351 while ( ( p = dynamic_cast<AliMUONVCalibParam*>(next() ) ) )
355 Int_t busPatchId = p->Id(0);
356 Int_t manuId = p->Id(1);
358 // read back pedestal from the other runs for the given (bupatch, manu)
359 for (Int_t i = 1; i < nEntries; ++i) {
360 ped[i] = static_cast<AliMUONVCalibParam*>(map[i]->Get(busPatchId, manuId));
363 // compute for each channel the gain parameters
364 for ( Int_t channelId = 0; channelId < ped[0]->Size() ; ++channelId ) {
367 for (Int_t i = 0; i < nEntries; ++i) {
369 if (!ped[i]) continue; //shouldn't happen.
370 pedMean[i] = ped[i]->ValueAsFloat(channelId, 0);
371 pedSigma[i] = ped[i]->ValueAsFloat(channelId, 1);
372 injCharge[i] = (Float_t)run[i]->GetSecond();
373 injChargeErr[i] = 1.;
375 if (pedMean[i] < 0) continue; // not connected
377 if (pedSigma[i] <= 0) pedSigma[i] = 1.; // should not happen.
384 TGraph *gain = new TGraphErrors(n, pedMean, injCharge, pedSigma, injChargeErr);
385 //should set some initial parameters
386 func->SetParameter(0,-300); // a0
387 func->SetParameter(1, 1.); // a1
388 func->SetParameter(2, 0.00001);// a2
389 func->SetParameter(3, 1100.); // xlim in ADC
391 gain->Fit("func","q");
392 cout << setw(8) << func->GetParameter(0) <<"\t"<< setw(8) << func->GetParameter(1) << endl;
394 if (!flatOutputFile.IsNull()) {
395 fileout << "\t" << busPatchId << "\t" << manuId <<"\t"<< channelId << "\t"
396 << setw(8) << func->GetParameter(0) <<"\t"<< setw(8) << func->GetParameter(1)
397 << setw(8) << func->GetParameter(2) <<"\t"<< setw(5) << func->GetParameter(3)
398 << "\t" << func->GetChisquare() << endl;
406 // file outputs for gain
407 if (!flatOutputFile.IsNull())
414 //*************************************************************//
417 int main(Int_t argc, Char_t **argv)
420 // needed for streamer application
421 gROOT->GetPluginManager()->AddHandler("TVirtualStreamerInfo",
428 Int_t printLevel = 0; // Global variable defined as extern in the others .cxx files
429 Int_t skipEvents = 0;
430 Int_t maxEvents = 1000000;
432 Int_t threshold = -1;
433 Char_t inputFile[256];
434 TString flatOutputFile;
435 TString crocusOutputFile;
436 TString crocusConfigFile;
438 Int_t totalParityErr = 0;
439 Int_t totalGlitchErr = 0;
443 // decode the input line
444 for (Int_t i = 1; i < argc; i++) // argument 0 is the executable name
449 if (arg[0] != '-') continue;
454 sprintf(inputFile,argv[i]);
458 flatOutputFile = argv[i];
462 crocusOutputFile = argv[i];
466 crocusConfigFile = argv[i];
474 printLevel=atoi(argv[i]);
478 skipEvents=atoi(argv[i]);
482 sscanf(argv[i],"%d",&maxEvents);
486 sscanf(argv[i],"%f",&nSigma);
490 sscanf(argv[i],"%d",&threshold);
494 printf("\n******************* %s usage **********************",argv[0]);
495 printf("\n%s -options, the available options are :",argv[0]);
496 printf("\n-h help (this screen)");
499 printf("\n-f <raw data file> (default = %s)",inputFile);
500 printf("\n-c <Crocus config. file> (default = %s)",crocusConfigFile.Data());
503 printf("\n-a <Flat ASCII file> (default = %s)",flatOutputFile.Data());
504 printf("\n-o <CROUCUS cmd file> (default = %s)",crocusOutputFile.Data());
506 printf("\n Options");
507 printf("\n-d <print level> (default = %d)",printLevel);
508 printf("\n-s <skip events> (default = %d)",skipEvents);
509 printf("\n-n <max events> (default = %d)",maxEvents);
510 printf("\n-p <n sigmas> (default = %f)",nSigma);
511 printf("\n-t <threshold (-1 = no)> (default = %d)",threshold);
512 printf("\n-e <execute ped/gain> (default = %s)",command.Data());
513 printf("\n-e <gain create> make gain & create a new root file");
514 printf("\n-e <gain> make gain & update root file");
515 printf("\n-e <gain compute> make gain & compute gains");
520 printf("%s : bad argument %s (please check %s -h)\n",argv[0],argv[i],argv[0]);
521 argc = 2; exit(-1); // exit if error
525 // set command to lower case
528 // decoding the events
531 Int_t nDateEvents = 0;
536 AliMUONDDLTracker* ddlTracker = 0x0;
537 AliMUONBlockHeader* blkHeader = 0x0;
538 AliMUONDspHeader* dspHeader = 0x0;
539 AliMUONBusStruct* busStruct = 0x0;
548 // once we have a configuration file in db
549 // copy locally a file from daq detector config db
550 // The current detector is identified by detector code in variable
551 // DATE_DETECTOR_CODE. It must be defined.
552 // If environment variable DAQDA_TEST_DIR is defined, files are copied from DAQDA_TEST_DIR
553 // instead of the database. The usual environment variables are not needed.
554 if (!crocusConfigFile.IsNull()) {
555 status = daqDA_DB_getFile("myconfig", crocusConfigFile.Data());
557 printf("Failed to get config file : %d\n",status);
563 status = monitorSetDataSource(inputFile);
565 cerr << "ERROR : monitorSetDataSource status (hex) = " << hex << status
566 << " " << monitorDecodeError(status) << endl;
569 status = monitorDeclareMp("MUON Tracking monitoring");
571 cerr << "ERROR : monitorDeclareMp status (hex) = " << hex << status
572 << " " << monitorDecodeError(status) << endl;
576 cout << "MUONTRKda : Reading data from file " << inputFile <<endl;
580 if (nEvents >= maxEvents) break;
581 if (nEvents && nEvents % 100 == 0)
582 cout<<"Cumulated events " << nEvents << endl;
584 // check shutdown condition
585 if (daqDA_checkShutdown())
588 // Skip Events if needed
590 status = monitorGetEventDynamic(&event);
595 status = monitorGetEventDynamic(&event);
597 cout<<"EOF found"<<endl;
603 // decoding rawdata headers
604 AliRawReader *rawReader = new AliRawReaderDate(event);
606 Int_t eventType = rawReader->GetType();
607 runNumber = rawReader->GetRunNumber();
609 if (eventType != PHYSICS_EVENT)
610 continue; // for the moment
614 // decoding MUON payload
615 AliMUONRawStreamTracker* rawStream = new AliMUONRawStreamTracker(rawReader);
618 while((status = rawStream->NextDDL())) {
620 if (printLevel) printf("\niDDL %d\n", rawStream->GetDDL());
622 ddlTracker = rawStream->GetDDLTracker();
624 // loop over block (CRT) structure
625 Int_t nBlock = ddlTracker->GetBlkHeaderEntries();
626 for(Int_t iBlock = 0; iBlock < nBlock ;iBlock++){
628 blkHeader = ddlTracker->GetBlkHeaderEntry(iBlock);
629 if (printLevel) printf("Block Total length %d\n",blkHeader->GetTotalLength());
631 // loop over DSP (FRT) structure
632 Int_t nDsp = blkHeader->GetDspHeaderEntries();
633 for(Int_t iDsp = 0; iDsp < nDsp ;iDsp++){ //DSP loop
635 dspHeader = blkHeader->GetDspHeaderEntry(iDsp);
636 if (printLevel) printf("Dsp length %d\n",dspHeader->GetTotalLength());
638 // loop over BusPatch structure
639 Int_t nBusPatch = dspHeader->GetBusPatchEntries();
640 for(Int_t iBusPatch = 0; iBusPatch < nBusPatch; iBusPatch++) {
642 busStruct = dspHeader->GetBusPatchEntry(iBusPatch);
644 if (printLevel) printf("busPatchId %d", busStruct->GetBusPatchId());
645 if (printLevel) printf(" BlockId %d", busStruct->GetBlockId());
646 if (printLevel) printf(" DspId %d\n", busStruct->GetDspId());
648 Int_t busPatchId = busStruct->GetBusPatchId();
651 Int_t dataSize = busStruct->GetLength();
652 for (Int_t iData = 0; iData < dataSize; iData++) {
656 Int_t manuId = busStruct->GetManuId(iData);
657 Int_t channelId = busStruct->GetChannelId(iData);
658 Int_t charge = busStruct->GetCharge(iData);
660 if (printLevel) printf("manuId: %d, channelId: %d charge: %d\n", manuId,
663 MakePed(busPatchId, manuId, channelId, charge);
670 totalParityErr += rawStream->GetPayLoad()->GetParityErrors();
671 totalGlitchErr += rawStream->GetPayLoad()->GetGlitchErrors();
679 nEvents -= totalGlitchErr; // skipped event cos of Glitch errors
682 cout << " Total number of parity errors in the Run: " << totalParityErr << endl;
683 cout << " Total number of glitch errors in the Run: " << totalGlitchErr << endl;
685 if (command.CompareTo("ped") == 0)
686 MakePedStore(flatOutputFile);
688 // option gain -> update root file with pedestal results
689 // gain + create -> recreate root file
690 // gain + comp -> update root file and compute gain parameters
692 if (command.Contains("gain"))
693 MakePedStoreForGain();
695 if (command.Contains("comp"))
696 MakeGainStore(flatOutputFile);
699 delete pedestalStore;
703 if (!(crocusConfigFile.IsNull()))
704 cout << "MUONTRKda : CROCUS command file generated : " << crocusOutputFile.Data() << endl;
706 cout << "MUONTRKda : WARNING no CROCUS command file generated" << endl;
708 cout << "MUONTRKda : Flat ASCII file generated : " << flatOutputFile << endl;
709 cout << "MUONTRKda : Histo file generated : " << histoFileName << endl;
710 cout << "MUONTRKda : Nb of DATE events = " << nDateEvents << endl;
711 cout << "MUONTRKda : Nb of events used = " << nEvents << endl;
713 printf("Execution time : R:%7.2fs C:%7.2fs\n", timers.RealTime(), timers.CpuTime());