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 **************************************************************************/
21 /// \file CreateWeightedRejectList.C
23 /// \brief Macro test for the creation of a RejectList object (for simulations) in the OCDB, for the MUON Tracke
27 /// root[0] .L CreateWeightedRejectList.C+
28 /// root[1] CreateWeightedRejectList("runlist.txt","local://$HOME/myLocalOCDB");
30 /// where runlist.txt has 2 integers per line = "run nevents"
31 /// where nevents is the number of events where there's (for instance) a CMUS1B trigger
34 /// Assuming the file coming from the Export feature of the logbook is runlist.logbook.txt =>
36 /// 115521;PHYSICS_1;1357;0.9
37 /// 115516;PHYSICS_1;944;0.9
39 /// The awk command below will output the needed format for this macro :
41 /// awk '{split ($0,a,";"); print a[1] " " a[3];}' runlist.logbook.txt =>
46 /// \author Matthieu Lenhardt, Subatech
49 #include "AliCDBEntry.h"
50 #include "AliCDBManager.h"
52 #include "AliDCSValue.h"
53 #include "AliMUONCDB.h"
54 #include "AliMUONCalibParamNI.h"
55 #include "AliMUONCalibrationData.h"
56 #include "AliMUONPadStatusMaker.h"
57 #include "AliMUONPadStatusMapMaker.h"
58 #include "AliMUONRecoParam.h"
59 #include "AliMUONRejectList.h"
60 #include "AliMUONRejectList.h"
61 #include "AliMUONTrackerData.h"
62 #include "AliMUONVCalibParam.h"
63 #include "AliMpBusPatch.h"
65 #include "AliMpConstants.h"
66 #include "AliMpDCSNamer.h"
67 #include "AliMpDDLStore.h"
68 #include "AliMpDEIterator.h"
69 #include "AliMpDEManager.h"
70 #include "AliMpDetElement.h"
71 #include "AliSysInfo.h"
73 #include <Riostream.h>
77 #include <TObjArray.h>
85 RunInfo(Int_t run, Long64_t nevents) : fNumber(run), fNevents(nevents) { }
87 Int_t Number() const { return fNumber; }
89 Long64_t Nevents() const { return fNevents; }
91 void SetNevents(Long64_t n) { fNevents = n; }
98 std::ostream& operator<<(std::ostream& out, const RunInfo& run)
100 out << Form("RUN %09d NEVENTS %10d",run.Number(),run.Nevents());
104 AliMUONRejectList* CheckDE_BP_ManuPedestals(Int_t rejectMask, AliMUONPadStatusMaker& status);
106 Int_t AddEventsSingleRun(Int_t index,
108 AliMUONRejectList& rejectedEvents);
110 std::vector<RunInfo> runs;
112 //______________________________________________________________________________
113 bool CreateWeightedRejectList(const char* runlistfile="runlist.txt",
114 const char* rejectListPath="local://$HOME/OCDB")
116 /// Create a weighted RejectList for the runs included in the run list
117 /// The cuts are the same that the one applied in the RecoParam used to create the ESDs.
119 /// \param runlistfile : an ASCII file where each line is a pair "run nevents"
120 /// \param rejectListPath : set this to the folder where the RejectList will be created (must have right access to it, of course)
122 TGrid::Connect("alien://");
124 ifstream in(runlistfile);
127 while ( in >> run >> nevents )
129 runs.push_back(RunInfo(run,nevents));
132 for ( std::vector<RunInfo>::size_type i = 0; i < runs.size(); ++i )
134 cout << runs[i] << endl;
137 if (runs.empty()) return false;
139 Int_t firstRun = runs[0].Number();
140 Int_t lastRun = runs[runs.size()-1].Number();
142 const char* rawOCDB = "raw://";
144 AliCDBManager* manager = AliCDBManager::Instance();
146 manager->SetCacheFlag(kFALSE);
148 manager->SetDefaultStorage(rawOCDB);
150 manager->SetSpecificStorage("MUON/Calib/RejectList", rejectListPath);
152 AliMUONRejectList weightedRejectList;
153 AliMUONRejectList rejectedEvents;
155 Long64_t nEventsTotal = 0;
157 for (std::vector<RunInfo>::size_type ii = 0; ii < runs.size(); ++ii)
159 Int_t runNumber = runs[ii].Number();
160 AliSysInfo::Instance()->AddStamp(Form("RUN%d",runNumber));
161 nEventsTotal += AddEventsSingleRun(ii, runNumber, rejectedEvents);
164 AliMpDEIterator DEiter;
166 for (DEiter.First(); !DEiter.IsDone(); DEiter.Next())
167 if (DEiter.CurrentDEId() < 1100)
169 Int_t DEid = DEiter.CurrentDEId();
171 Double_t nBadEventsDE = rejectedEvents.DetectionElementProbability(DEid);
172 Double_t nEventsTotalDE = static_cast<Double_t>(nEventsTotal);
173 Float_t probaDE = 0.0;
174 if (nEventsTotalDE != 0.0)
175 probaDE = nBadEventsDE / nEventsTotalDE;
176 weightedRejectList.SetDetectionElementProbability(DEid, probaDE);
178 Int_t nBusPatches = DEiter.CurrentDE()->GetNofBusPatches();
179 for (Int_t ii = 0; ii < nBusPatches; ii++)
181 Int_t BPid = DEiter.CurrentDE()->GetBusPatchId(ii);
183 Double_t nBadEventsBP = rejectedEvents.BusPatchProbability(BPid);
184 Double_t nEventsTotalBP = nEventsTotalDE - nBadEventsDE;
185 Float_t probaBP = 0.0;
186 if (nEventsTotalBP != 0.0)
187 probaBP = nBadEventsBP / nEventsTotalBP;
188 weightedRejectList.SetBusPatchProbability(BPid, probaBP);
190 Int_t nManus = AliMpDDLStore::Instance(kFALSE)->GetBusPatch(BPid, kFALSE)->GetNofManus();
191 for (Int_t jj = 0; jj < nManus; jj++)
193 Int_t manuId = AliMpDDLStore::Instance(kFALSE)->GetBusPatch(BPid, kFALSE)->GetManuId(jj);
195 Double_t nBadEventsManu = rejectedEvents.ManuProbability(DEid, manuId);
196 Double_t nEventsTotalManu = nEventsTotalBP - nBadEventsBP;
197 Float_t probaManu = 0.0;
198 if (nEventsTotalManu != 0.0)
199 probaManu = nBadEventsManu / nEventsTotalManu;
200 weightedRejectList.SetManuProbability(DEid, manuId, probaManu);
202 for (Int_t channel = 0; channel < AliMpConstants::ManuNofChannels(); channel++)
204 Double_t nBadEventsChannel = rejectedEvents.ChannelProbability(DEid, manuId, channel);
205 Double_t nEventsTotalChannel = nEventsTotalManu - nBadEventsManu;
206 Float_t probaChannel = 0.0;
207 if (nEventsTotalChannel != 0.0)
209 probaChannel = nBadEventsChannel / nEventsTotalChannel;
212 weightedRejectList.SetChannelProbability(DEid, manuId, channel, probaChannel);
219 AliMUONCDB::WriteToCDB(&weightedRejectList, "MUON/Calib/RejectList",
221 "Weighted reject List for MCH, for simulations only",
222 "Matthieu Lenhardt");
227 //____________________________________________________________________________________________
228 AliMUONRejectList* CheckDE_BP_ManuPedestals(Int_t rejectMask, AliMUONPadStatusMaker& status)
230 AliMUONRejectList* nBadChannels = new AliMUONRejectList;
232 AliMpDEIterator DEiter;
233 for (DEiter.First(); !DEiter.IsDone(); DEiter.Next())
234 if (DEiter.CurrentDEId() < 1100)
236 Int_t DEid = DEiter.CurrentDEId();
237 Int_t nBusPatches = DEiter.CurrentDE()->GetNofBusPatches();
238 for (Int_t ii = 0; ii < nBusPatches; ii++)
240 Int_t BPid = DEiter.CurrentDE()->GetBusPatchId(ii);
242 Int_t nManus = AliMpDDLStore::Instance(kFALSE)->GetBusPatch(BPid, kFALSE)->GetNofManus();
243 for (Int_t jj = 0; jj < nManus; jj++)
245 Int_t manuId = AliMpDDLStore::Instance(kFALSE)->GetBusPatch(BPid, kFALSE)->GetManuId(jj);
247 Int_t nChannels = DEiter.CurrentDE()->NofChannelsInManu(manuId);
248 for (Int_t channel = 0; channel < nChannels; channel++)
250 Int_t padStatus = status.PadStatus(DEid, manuId, channel);
251 if ((rejectMask == 0 && padStatus != 0) || (padStatus & rejectMask) != 0)
253 nBadChannels->SetDetectionElementProbability(DEid, nBadChannels->DetectionElementProbability(DEid) + 1.0);
254 nBadChannels->SetBusPatchProbability(BPid, nBadChannels->BusPatchProbability(BPid) + 1.0);
255 nBadChannels->SetManuProbability(DEid, manuId, nBadChannels->ManuProbability(DEid, manuId) + 1.0);
256 nBadChannels->SetChannelProbability(DEid, manuId, channel, 1.0);
267 //____________________________________________________________________________________________
268 Int_t AddEventsSingleRun(Int_t index, Int_t runNumber, AliMUONRejectList& rejectedEvents)
270 AliCDBManager::Instance()->SetRun(runNumber);
274 Double_t maxBadChannels = 80.0;
276 AliMUONRecoParam *recoParam = AliMUONCDB::LoadRecoParam();
277 AliMUONCalibrationData calibrationData(runNumber);
278 AliMUONPadStatusMaker status(calibrationData);
280 status.SetLimits(*recoParam);
282 Long64_t nEvents = runs[index].Nevents();
284 Int_t rejectMask = recoParam->PadGoodnessMask();
286 // Functions to compute the number of bad channels at the DE, BP and Manu level
287 AliMUONRejectList* nBadChannels = CheckDE_BP_ManuPedestals(rejectMask, status);
289 AliMpDEIterator DEiter;
290 for (DEiter.First(); !DEiter.IsDone(); DEiter.Next())
291 if (DEiter.CurrentDEId() < 1100)
293 Int_t DEid = DEiter.CurrentDEId();
294 Int_t nBusPatches = DEiter.CurrentDE()->GetNofBusPatches();
296 Double_t nChannelsDE = static_cast<Double_t>(AliMpDDLStore::Instance(kFALSE)->GetDetElement(DEid)->NofChannels());
297 Double_t nBadChannelsDE = nBadChannels->DetectionElementProbability(DEid);
298 Double_t ratioBadChannelsDE = 0.0;
299 if (nChannelsDE != 0.0)
300 ratioBadChannelsDE = 100.0 * nBadChannelsDE / nChannelsDE;
303 Bool_t goodDE = kTRUE;
304 if (ratioBadChannelsDE >= maxBadChannels)
306 Double_t newRejectedEventsDE = rejectedEvents.DetectionElementProbability(DEid) + static_cast<Double_t>(nEvents);
307 rejectedEvents.SetDetectionElementProbability(DEid, newRejectedEventsDE);
312 for (Int_t ii = 0; ii < nBusPatches; ii++)
314 Int_t BPid = DEiter.CurrentDE()->GetBusPatchId(ii);
315 Int_t nManus = AliMpDDLStore::Instance(kFALSE)->GetBusPatch(BPid, kFALSE)->GetNofManus();
317 Double_t nChannelsBP = static_cast<Double_t>(nManus * AliMpConstants::ManuNofChannels());
318 Double_t nBadChannelsBP = nBadChannels->BusPatchProbability(BPid);
319 Double_t ratioBadChannelsBP = 100.0 * nBadChannelsBP / nChannelsBP;
321 Bool_t goodBP = kTRUE;
324 if (ratioBadChannelsBP >= maxBadChannels)
326 Double_t newRejectedEventsBP = rejectedEvents.BusPatchProbability(BPid) + static_cast<Double_t>(nEvents);
327 rejectedEvents.SetBusPatchProbability(BPid, newRejectedEventsBP);
332 for (Int_t jj = 0; jj < nManus; jj++)
334 Int_t manuId = AliMpDDLStore::Instance(kFALSE)->GetBusPatch(BPid, kFALSE)->GetManuId(jj);
336 Double_t nChannelsManu = DEiter.CurrentDE()->NofChannelsInManu(manuId);;
337 Double_t nBadChannelsManu = nBadChannels->ManuProbability(DEid, manuId);
338 Double_t ratioBadChannelsManu = 100.0 * nBadChannelsManu / nChannelsManu;
340 Bool_t goodManu = kTRUE;
342 if (ratioBadChannelsManu >= maxBadChannels)
344 Double_t newRejectedEventsManu = rejectedEvents.ManuProbability(DEid, manuId) + static_cast<Double_t>(nEvents);
345 rejectedEvents.SetManuProbability(DEid, manuId, newRejectedEventsManu);
350 Int_t nChannels = DEiter.CurrentDE()->NofChannelsInManu(manuId);
353 for (Int_t channel = 0; channel < nChannels; channel++)
354 if (nBadChannels->ChannelProbability(DEid, manuId, channel) > 0.0)
356 Double_t newRejectedEventsChannel = rejectedEvents.ChannelProbability(DEid, manuId, channel) + static_cast<Double_t>(nEvents);
357 rejectedEvents.SetChannelProbability(DEid, manuId, channel, newRejectedEventsChannel);
358 } // end of Channels loop
359 } // end of MANUs loop
368 //______________________________________________________________________________
371 // The code below might be used someday to retrieve the number of events
372 // with at least one muon directly from a runlist, instead of requiring
373 // as input both the run numbers and the number of events.
375 // This approach though is not without its problems :
377 // - even if we are using tags, as the tags are chunk-based (for the moment at
378 // least), it's pretty slow to loop on all the chunks (it is becoming usual
379 // to get runs with some 600 chunks or so...)
380 // - if part of a run is not reconstructed (for whatever reason) with the pass
381 // we use to compute the number of events with at least one muon, we might
382 // underestimate its weight in the final rejectlist for the next passes (
383 // if for the next passes the full run is correctly reconstructed for instance)
384 // - a muon in the tag = either a trigger track or a tracker track, i.e. we don't
385 // know for sure it's a good track...
387 // Given all those reasons, we for the moment use a dumb approach :
389 // - go to alice-logbook.cern.ch . Select the runs you want and the trigger class
390 // you want (most probably CMUS1B), and export the list as (run,nevents)
391 // - run this macro with this (run,nevents) list
393 //______________________________________________________________________________
398 #include "AliDetectorTagCuts.h"
399 #include "AliEventTagCuts.h"
400 #include "AliLHCTagCuts.h"
401 #include "AliRunTagCuts.h"
402 #include "AliTagAnalysis.h"
403 #include "TGridResult.h"
405 //______________________________________________________________________________
406 bool CreateXMLCollectionFromRunList(const char* collectionName,
411 /// From a list of run, create a collection with only events containing at least one muon
412 /// \param collectionName : output name of the collection (without extension, which will be .xml)
413 /// \param runlist : text file containing one integer per line = one run number per line
414 /// \param type : files to consider, either ESD or AD
415 /// \param passNumber : 1 or 2 most probably (to distinguish which reco pass is used)
417 if (!gGrid) TGrid::Connect("alien://");
426 if ( stype != "ESD" && stype != "AOD" )
428 cout << "Only ESD or AOD type supported" << endl;
432 ifstream in(gSystem->ExpandPathName(runlist));
436 AliTagAnalysis tagAnalysis("ESD");
438 while ( in >> runNumber )
440 TGridResult *res = gGrid->Query("/alice/data",
441 Form("%09d/%ss/pass%d/*%d*/Run%d.Event*.ESD.tag.root",
442 runNumber,stype.Data(),passNumber,runNumber,runNumber));
443 Int_t nFiles = res->GetEntries();
449 for (Int_t i = 0; i < nFiles; ++i)
451 TString filename = res->GetKey(i, "turl");
452 if(filename == "") continue;
453 tagAnalysis.AddTagsFile(filename.Data(),kFALSE);
459 cout << ntagfiles << " tag files added" << endl;
461 AliRunTagCuts runCuts;
462 AliEventTagCuts eventCuts;
463 AliLHCTagCuts lhcCuts;
464 AliDetectorTagCuts detCuts;
466 eventCuts.SetNMuonRange(1,99999);
468 return tagAnalysis.CreateXMLCollection(collectionName,&runCuts,&lhcCuts,&detCuts,&eventCuts);
472 //______________________________________________________________________________
473 TChain* CreateChainFromXMLCollection(const char* collectionName, const char* type)
475 /// Create a chain from an XML collection file.
477 if (!gGrid) TGrid::Connect("alien://");
486 if ( stype != "ESD" && stype != "AOD" )
488 cout << "Only ESD or AOD type supported" << endl;
492 AliTagAnalysis tagAnalysis(stype.Data());
494 return tagAnalysis.CreateChainFromCollection(collectionName,stype=="ESD" ? "esdTree":"aodTree");