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 "AliMUONCalibParamND.h"
34 #include "AliMpIntPair.h"
35 #include "AliMpConstants.h"
36 #include "AliRawReaderDate.h"
43 #include "TStopwatch.h"
45 #include "TTimeStamp.h"
46 #include "TGraphErrors.h"
49 #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->FindObject(busPatchId, manuId));
95 ped = new AliMUONCalibParamND(2, kNchannels,busPatchId, manuId, -1.); // put default wise -1, not connected channel
96 pedestalStore->Add(ped);
100 ped->SetValueAsDouble(channelId, 0, 0.);
101 ped->SetValueAsDouble(channelId, 1, 0.);
104 Double_t pedMean = ped->ValueAsDouble(channelId, 0) + charge;
105 Double_t pedSigma = ped->ValueAsDouble(channelId, 1) + charge*charge;
107 ped->SetValueAsDouble(channelId, 0, pedMean);
108 ped->SetValueAsDouble(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->CreateIterator());
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->ValueAsDouble(channelId, 0);
181 if (pedMean > 0) { // connected channels
183 ped->SetValueAsDouble(channelId, 0, pedMean/(Double_t)nEvents);
185 pedMean = ped->ValueAsDouble(channelId, 0);
186 pedSigma = ped->ValueAsDouble(channelId, 1);
188 ped->SetValueAsDouble(channelId, 1, TMath::Sqrt(TMath::Abs(pedSigma/(Double_t)nEvents - pedMean*pedMean)));
190 pedMean = ped->ValueAsDouble(channelId, 0) + 0.5 ;
191 pedSigma = ped->ValueAsDouble(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.
295 Double_t pedMean[10];
296 Double_t pedSigma[10];
297 Double_t injCharge[10];
298 Double_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];
327 // AliMpIntPair* pair;
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., NFITPARAMS);
345 // TF1* func = new TF1("func","pol1");
347 // iterates over the first pedestal run
348 TIter next(map[0]->CreateIterator());
349 AliMUONVCalibParam* p;
351 while ( ( p = dynamic_cast<AliMUONVCalibParam*>(next() ) ) )
355 Int_t busPatchId = p->ID0();
356 Int_t manuId = p->ID1();
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]->FindObject(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]->ValueAsDouble(channelId, 0);
371 pedSigma[i] = ped[i]->ValueAsDouble(channelId, 1);
372 injCharge[i] = (Double_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.
381 if (n > NFITPARAMS) {
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",
427 TFitter *minuitFit = new TFitter(NFITPARAMS);
428 TVirtualFitter::SetFitter(minuitFit);
430 Int_t printLevel = 0; // Global variable defined as extern in the others .cxx files
431 Int_t skipEvents = 0;
432 Int_t maxEvents = 1000000;
434 Int_t threshold = -1;
435 Char_t inputFile[256];
436 TString flatOutputFile;
437 TString crocusOutputFile;
438 TString crocusConfigFile;
442 // decode the input line
443 for (Int_t i = 1; i < argc; i++) // argument 0 is the executable name
448 if (arg[0] != '-') continue;
453 sprintf(inputFile,argv[i]);
457 flatOutputFile = argv[i];
461 crocusOutputFile = argv[i];
465 crocusConfigFile = argv[i];
473 printLevel=atoi(argv[i]);
477 skipEvents=atoi(argv[i]);
481 sscanf(argv[i],"%d",&maxEvents);
485 sscanf(argv[i],"%lf",&nSigma);
489 sscanf(argv[i],"%d",&threshold);
493 printf("\n******************* %s usage **********************",argv[0]);
494 printf("\n%s -options, the available options are :",argv[0]);
495 printf("\n-h help (this screen)");
498 printf("\n-f <raw data file> (default = %s)",inputFile);
499 printf("\n-c <Crocus config. file> (default = %s)",crocusConfigFile.Data());
502 printf("\n-a <Flat ASCII file> (default = %s)",flatOutputFile.Data());
503 printf("\n-o <CROUCUS cmd file> (default = %s)",crocusOutputFile.Data());
505 printf("\n Options");
506 printf("\n-d <print level> (default = %d)",printLevel);
507 printf("\n-s <skip events> (default = %d)",skipEvents);
508 printf("\n-n <max events> (default = %d)",maxEvents);
509 printf("\n-p <n sigmas> (default = %f)",nSigma);
510 printf("\n-t <threshold (-1 = no)> (default = %d)",threshold);
511 printf("\n-e <execute ped/gain> (default = %s)",command.Data());
512 printf("\n-e <gain create> make gain & create a new root file");
513 printf("\n-e <gain> make gain & update root file");
514 printf("\n-e <gain compute> make gain & compute gains");
519 printf("%s : bad argument %s (please check %s -h)\n",argv[0],argv[i],argv[0]);
520 argc = 2; exit(-1); // exit if error
524 // set command to lower case
527 // decoding the events
530 Int_t nDateEvents = 0;
541 // once we have a configuration file in db
542 // copy locally a file from daq detector config db
543 // The current detector is identified by detector code in variable
544 // DATE_DETECTOR_CODE. It must be defined.
545 // If environment variable DAQDA_TEST_DIR is defined, files are copied from DAQDA_TEST_DIR
546 // instead of the database. The usual environment variables are not needed.
547 if (!crocusConfigFile.IsNull()) {
548 status = daqDA_DB_getFile("myconfig", crocusConfigFile.Data());
550 printf("Failed to get config file : %d\n",status);
556 status = monitorSetDataSource(inputFile);
558 cerr << "ERROR : monitorSetDataSource status (hex) = " << hex << status
559 << " " << monitorDecodeError(status) << endl;
562 status = monitorDeclareMp("MUON Tracking monitoring");
564 cerr << "ERROR : monitorDeclareMp status (hex) = " << hex << status
565 << " " << monitorDecodeError(status) << endl;
569 cout << "MUONTRKda : Reading data from file " << inputFile <<endl;
578 if (nEvents >= maxEvents) break;
579 if (nEvents && nEvents % 100 == 0)
580 cout<<"Cumulated events " << nEvents << endl;
582 // check shutdown condition
583 if (daqDA_checkShutdown())
586 // Skip Events if needed
588 status = monitorGetEventDynamic(&event);
593 status = monitorGetEventDynamic(&event);
595 cout<<"EOF found"<<endl;
601 // decoding rawdata headers
602 AliRawReader *rawReader = new AliRawReaderDate(event);
604 Int_t eventType = rawReader->GetType();
605 runNumber = rawReader->GetRunNumber();
607 if (eventType != PHYSICS_EVENT)
608 continue; // for the moment
612 // decoding MUON payload
613 AliMUONRawStreamTracker* rawStream = new AliMUONRawStreamTracker(rawReader);
617 while( (status = rawStream->Next(busPatchId, manuId, channelId, charge)) ) {
622 if (printLevel) printf("manuId: %d, channelId: %d charge: %d\n", manuId,
625 MakePed(busPatchId, (Int_t)manuId, (Int_t)channelId, (Int_t)charge);
635 if (command.CompareTo("ped") == 0)
636 MakePedStore(flatOutputFile);
638 // option gain -> update root file with pedestal results
639 // gain + create -> recreate root file
640 // gain + comp -> update root file and compute gain parameters
642 if (command.Contains("gain"))
643 MakePedStoreForGain();
645 if (command.Contains("comp"))
646 MakeGainStore(flatOutputFile);
649 delete pedestalStore;
652 TVirtualFitter::SetFitter(0);
656 if (!(crocusConfigFile.IsNull()))
657 cout << "MUONTRKda : CROCUS command file generated : " << crocusOutputFile.Data() << endl;
659 cout << "MUONTRKda : WARNING no CROCUS command file generated" << endl;
661 cout << "MUONTRKda : Flat ASCII file generated : " << flatOutputFile << endl;
662 cout << "MUONTRKda : Histo file generated : " << histoFileName << endl;
663 cout << "MUONTRKda : Nb of DATE events = " << nDateEvents << endl;
664 cout << "MUONTRKda : Nb of events used = " << nEvents << endl;
666 printf("Execution time : R:%7.2fs C:%7.2fs\n", timers.RealTime(), timers.CpuTime());