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 Long64_t nEvents = runs[index].Nevents();
282 Int_t rejectMask = recoParam->PadGoodnessMask();
284 // Functions to compute the number of bad channels at the DE, BP and Manu level
285 AliMUONRejectList* nBadChannels = CheckDE_BP_ManuPedestals(rejectMask, status);
287 AliMpDEIterator DEiter;
288 for (DEiter.First(); !DEiter.IsDone(); DEiter.Next())
289 if (DEiter.CurrentDEId() < 1100)
291 Int_t DEid = DEiter.CurrentDEId();
292 Int_t nBusPatches = DEiter.CurrentDE()->GetNofBusPatches();
294 Double_t nChannelsDE = static_cast<Double_t>(AliMpDDLStore::Instance(kFALSE)->GetDetElement(DEid)->NofChannels());
295 Double_t nBadChannelsDE = nBadChannels->DetectionElementProbability(DEid);
296 Double_t ratioBadChannelsDE = 0.0;
297 if (nChannelsDE != 0.0)
298 ratioBadChannelsDE = 100.0 * nBadChannelsDE / nChannelsDE;
301 Bool_t goodDE = kTRUE;
302 if (ratioBadChannelsDE >= maxBadChannels)
304 Double_t newRejectedEventsDE = rejectedEvents.DetectionElementProbability(DEid) + static_cast<Double_t>(nEvents);
305 rejectedEvents.SetDetectionElementProbability(DEid, newRejectedEventsDE);
310 for (Int_t ii = 0; ii < nBusPatches; ii++)
312 Int_t BPid = DEiter.CurrentDE()->GetBusPatchId(ii);
313 Int_t nManus = AliMpDDLStore::Instance(kFALSE)->GetBusPatch(BPid, kFALSE)->GetNofManus();
315 Double_t nChannelsBP = static_cast<Double_t>(nManus * AliMpConstants::ManuNofChannels());
316 Double_t nBadChannelsBP = nBadChannels->BusPatchProbability(BPid);
317 Double_t ratioBadChannelsBP = 100.0 * nBadChannelsBP / nChannelsBP;
319 Bool_t goodBP = kTRUE;
322 if (ratioBadChannelsBP >= maxBadChannels)
324 Double_t newRejectedEventsBP = rejectedEvents.BusPatchProbability(BPid) + static_cast<Double_t>(nEvents);
325 rejectedEvents.SetBusPatchProbability(BPid, newRejectedEventsBP);
330 for (Int_t jj = 0; jj < nManus; jj++)
332 Int_t manuId = AliMpDDLStore::Instance(kFALSE)->GetBusPatch(BPid, kFALSE)->GetManuId(jj);
334 Double_t nChannelsManu = DEiter.CurrentDE()->NofChannelsInManu(manuId);;
335 Double_t nBadChannelsManu = nBadChannels->ManuProbability(DEid, manuId);
336 Double_t ratioBadChannelsManu = 100.0 * nBadChannelsManu / nChannelsManu;
338 Bool_t goodManu = kTRUE;
340 if (ratioBadChannelsManu >= maxBadChannels)
342 Double_t newRejectedEventsManu = rejectedEvents.ManuProbability(DEid, manuId) + static_cast<Double_t>(nEvents);
343 rejectedEvents.SetManuProbability(DEid, manuId, newRejectedEventsManu);
348 Int_t nChannels = DEiter.CurrentDE()->NofChannelsInManu(manuId);
351 for (Int_t channel = 0; channel < nChannels; channel++)
352 if (nBadChannels->ChannelProbability(DEid, manuId, channel) > 0.0)
354 Double_t newRejectedEventsChannel = rejectedEvents.ChannelProbability(DEid, manuId, channel) + static_cast<Double_t>(nEvents);
355 rejectedEvents.SetChannelProbability(DEid, manuId, channel, newRejectedEventsChannel);
356 } // end of Channels loop
357 } // end of MANUs loop
366 //______________________________________________________________________________
369 // The code below might be used someday to retrieve the number of events
370 // with at least one muon directly from a runlist, instead of requiring
371 // as input both the run numbers and the number of events.
373 // This approach though is not without its problems :
375 // - even if we are using tags, as the tags are chunk-based (for the moment at
376 // least), it's pretty slow to loop on all the chunks (it is becoming usual
377 // to get runs with some 600 chunks or so...)
378 // - if part of a run is not reconstructed (for whatever reason) with the pass
379 // we use to compute the number of events with at least one muon, we might
380 // underestimate its weight in the final rejectlist for the next passes (
381 // if for the next passes the full run is correctly reconstructed for instance)
382 // - a muon in the tag = either a trigger track or a tracker track, i.e. we don't
383 // know for sure it's a good track...
385 // Given all those reasons, we for the moment use a dumb approach :
387 // - go to alice-logbook.cern.ch . Select the runs you want and the trigger class
388 // you want (most probably CMUS1B), and export the list as (run,nevents)
389 // - run this macro with this (run,nevents) list
391 //______________________________________________________________________________
396 #include "AliDetectorTagCuts.h"
397 #include "AliEventTagCuts.h"
398 #include "AliLHCTagCuts.h"
399 #include "AliRunTagCuts.h"
400 #include "AliTagAnalysis.h"
401 #include "TGridResult.h"
403 //______________________________________________________________________________
404 bool CreateXMLCollectionFromRunList(const char* collectionName,
409 /// From a list of run, create a collection with only events containing at least one muon
410 /// \param collectionName : output name of the collection (without extension, which will be .xml)
411 /// \param runlist : text file containing one integer per line = one run number per line
412 /// \param type : files to consider, either ESD or AD
413 /// \param passNumber : 1 or 2 most probably (to distinguish which reco pass is used)
415 if (!gGrid) TGrid::Connect("alien://");
424 if ( stype != "ESD" && stype != "AOD" )
426 cout << "Only ESD or AOD type supported" << endl;
430 ifstream in(gSystem->ExpandPathName(runlist));
434 AliTagAnalysis tagAnalysis("ESD");
436 while ( in >> runNumber )
438 TGridResult *res = gGrid->Query("/alice/data",
439 Form("%09d/%ss/pass%d/*%d*/Run%d.Event*.ESD.tag.root",
440 runNumber,stype.Data(),passNumber,runNumber,runNumber));
441 Int_t nFiles = res->GetEntries();
447 for (Int_t i = 0; i < nFiles; ++i)
449 TString filename = res->GetKey(i, "turl");
450 if(filename == "") continue;
451 tagAnalysis.AddTagsFile(filename.Data(),kFALSE);
457 cout << ntagfiles << " tag files added" << endl;
459 AliRunTagCuts runCuts;
460 AliEventTagCuts eventCuts;
461 AliLHCTagCuts lhcCuts;
462 AliDetectorTagCuts detCuts;
464 eventCuts.SetNMuonRange(1,99999);
466 return tagAnalysis.CreateXMLCollection(collectionName,&runCuts,&lhcCuts,&detCuts,&eventCuts);
470 //______________________________________________________________________________
471 TChain* CreateChainFromXMLCollection(const char* collectionName, const char* type)
473 /// Create a chain from an XML collection file.
475 if (!gGrid) TGrid::Connect("alien://");
484 if ( stype != "ESD" && stype != "AOD" )
486 cout << "Only ESD or AOD type supported" << endl;
490 AliTagAnalysis tagAnalysis(stype.Data());
492 return tagAnalysis.CreateChainFromCollection(collectionName,stype=="ESD" ? "esdTree":"aodTree");